mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): check SNS requests on the topics they name, from the generated table (#8971)
SNS was built by hand: ResourceArnBuilder read a request's TopicArn and checked everything else on the account's open topic ARN, the request resources table held no SNS operation, and nothing gave a policy condition a topic's tags. sns moves from HAND_BUILT to NAMESPACES, so the table gives each operation the topics botocore's members name (TopicArn, ResourceArn, TargetArn, and CreateTopic's Name), and its ResourceArnBuilder branch goes. Only SNS's operations change in the table. SnsStoredResourceArns answers a topic's stored tags; TagResource and UntagResource give the topic a new map one call at a time, and its tags field is volatile.
This commit is contained in:
1 parent
96015e7532
commit
aabe2247c5
11 files changed
+207
-45
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -57,9 +58,10 @@ import java.util.Optional;
|
||||
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy; Control Tower, for the tags
|
||||
* of a landing zone or an enabled control; GuardDuty, for the tags of a detector or a Malware Protection plan; Cloud
|
||||
* Map, for the tags of a namespace or a service; Elastic Load Balancing, for the tags of a load balancer, a target
|
||||
* group, a listener, a rule or a Classic Load Balancer; SQS, for the tags of a queue); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
* group, a listener, a rule or a Classic Load Balancer; SQS, for the tags of a queue; SNS, for the tags of a topic);
|
||||
* looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
@@ -121,6 +123,16 @@ public interface StoredResourceArns {
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code tags} as {@link #storedTags} answers them: in their order, a copy the caller may keep, and a tag without
|
||||
* a value as an empty one, as a resource-tag condition key reads it.
|
||||
*/
|
||||
static Map<String, String> withEmptyValues(Map<String, String> tags) {
|
||||
Map<String, String> copy = new LinkedHashMap<>();
|
||||
tags.forEach((key, value) -> copy.put(key, value == null ? "" : value));
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the resource {@code arn} names, one {@link #find} gave, lets every principal of its account have a
|
||||
* service act on it through that service, as the key policy AWS writes for an AWS managed KMS key does: a request
|
||||
|
||||
@@ -185,7 +185,6 @@ public class ResourceArnBuilder {
|
||||
: IamActionRegistry.decodedPath(ctx);
|
||||
return switch (credentialScope) {
|
||||
case "s3" -> buildS3Resources(operation, path, ctx, region);
|
||||
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
|
||||
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
|
||||
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
@@ -1200,20 +1199,6 @@ public class ResourceArnBuilder {
|
||||
return AwsArnUtils.isArn(resourceArn) ? resourceArn : "*";
|
||||
}
|
||||
|
||||
// ── SNS ─────────────────────────────────────────────────────────────────────
|
||||
private String buildSnsArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
String topicArn = firstFormParam(ctx, "TopicArn");
|
||||
if (topicArn == null) {
|
||||
JsonNode json = readJsonBody(ctx);
|
||||
if (json != null && json.isObject() && json.hasNonNull("TopicArn")) {
|
||||
topicArn = json.get("TopicArn").asText().trim();
|
||||
}
|
||||
}
|
||||
return (topicArn != null && !topicArn.isEmpty())
|
||||
? topicArn
|
||||
: AwsArnUtils.Arn.of("sns", region, accountId, "*").toString();
|
||||
}
|
||||
|
||||
// ── DynamoDB ─────────────────────────────────────────────────────────────────
|
||||
private String buildDynamoDbArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
List<String> arns = buildDynamoDbArns(ctx, region, accountId);
|
||||
|
||||
@@ -1680,24 +1680,47 @@ public class SnsService implements Resettable, ResourceProvider {
|
||||
return new BatchPublishResult(successful, failed);
|
||||
}
|
||||
|
||||
public void tagResource(String resourceArn, Map<String, String> tags, String region) {
|
||||
/**
|
||||
* Gives the topic a new map holding its tags and {@code tags}. Tag calls run one at a time, so two of them never
|
||||
* lose one's tags, and a lookup reads one whole set.
|
||||
*/
|
||||
public synchronized void tagResource(String resourceArn, Map<String, String> tags, String region) {
|
||||
String key = topicKey(region, resourceArn);
|
||||
Topic topic = topicStore.get(key)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Resource does not exist.", 404));
|
||||
if (tags != null) topic.getTags().putAll(tags);
|
||||
if (tags != null) {
|
||||
Map<String, String> merged = new LinkedHashMap<>(topic.getTags());
|
||||
merged.putAll(tags);
|
||||
topic.setTags(merged);
|
||||
}
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
|
||||
public void untagResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
/** Gives the topic a new map without {@code tagKeys}, one tag call at a time, as {@link #tagResource}. */
|
||||
public synchronized void untagResource(String resourceArn, List<String> tagKeys, String region) {
|
||||
String key = topicKey(region, resourceArn);
|
||||
Topic topic = topicStore.get(key)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Resource does not exist.", 404));
|
||||
if (tagKeys != null) tagKeys.forEach(topic.getTags()::remove);
|
||||
if (tagKeys != null) {
|
||||
Map<String, String> remaining = new LinkedHashMap<>(topic.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
topic.setTags(remaining);
|
||||
}
|
||||
topicStore.put(key, topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the topic whose ARN is {@code topicArn} in {@code account}, read in place under the key the handlers
|
||||
* read for the ARN's Region; none when there is no such topic.
|
||||
*/
|
||||
Map<String, String> topicTagsInAccount(String account, String topicArn) {
|
||||
return AccountAwareStorageBackend.readInAccount(topicStore, account,
|
||||
topicKey(AwsArnUtils.parse(topicArn).region(), topicArn))
|
||||
.map(Topic::getTags).orElse(Map.of());
|
||||
}
|
||||
|
||||
public Map<String, String> listTagsForResource(String resourceArn, String region) {
|
||||
String key = topicKey(region, resourceArn);
|
||||
Topic topic = topicStore.get(key)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.floci.aws.services.sns;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/** IAM's lookup of the tags SNS holds on a topic, for the {@code sns} namespace. */
|
||||
@ApplicationScoped
|
||||
public class SnsStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String NAMESPACE = "sns";
|
||||
|
||||
private final SnsService sns;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public SnsStoredResourceArns(SnsService sns, RegionResolver regionResolver) {
|
||||
this.sns = sns;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
/** A request names a topic by its ARN, or creates one by its name: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the topic whose ARN is {@code arn}, in the request's account, where the tag handlers read: an ARN of
|
||||
* another account names none. The topic is read in place under the whole ARN, in the ARN's Region. A tag without a
|
||||
* value reads as an empty one. Tag calls give the topic a new map, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, NAMESPACE)) {
|
||||
return Map.of();
|
||||
}
|
||||
String account = regionResolver.getAccountId();
|
||||
if (!AwsArnUtils.parse(arn).accountId().equals(account)) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(sns.topicTagsInAccount(account, arn));
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class Topic {
|
||||
private Map<String, String> attributes = new HashMap<>();
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
|
||||
@JsonProperty("CreatedAt")
|
||||
private Instant createdAt;
|
||||
|
||||
@@ -6,7 +6,6 @@ import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -65,9 +64,6 @@ public class SqsStoredResourceArns implements StoredResourceArns {
|
||||
|| !arn.equals(AwsArnUtils.Arn.of(NAMESPACE, parsed.region(), account, parsed.resource()).toString())) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> copy = new LinkedHashMap<>();
|
||||
sqs.queueTagsInAccount(account, parsed.region(), parsed.resource())
|
||||
.forEach((key, value) -> copy.put(key, value == null ? "" : value));
|
||||
return copy;
|
||||
return StoredResourceArns.withEmptyValues(sqs.queueTagsInAccount(account, parsed.region(), parsed.resource()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user