fix(iam): check SQS requests on the queue their URL reaches, from the generated table (#8943)

SQS was built by hand: ResourceArnBuilder read a request's QueueUrl or
QueueName into a queue ARN, the request resources table held no SQS
operation, and nothing gave a policy condition a queue's tags. botocore
documents every QueueUrl as "The URL of the Amazon SQS queue ...", which
the generator's documentation rule did not read: it took only a name, an
id or an identifier as what a member names, and the service's own name
stood between "the" and the type's word. It now reads a URL as one more
such kind, and a member's lead drops its model's own service name as
botocore's metadata spells it before the word it qualifies, and a
hyphenated word before the type's word qualifies it ("The URL of a
dead-letter queue"), so QueueUrl names the queue; being neither the
queue's name nor its id, SQS finds it (find). Only SQS's operations
change in the table. sqs moves from HAND_BUILT to NAMESPACES and its
ResourceArnBuilder branch goes. SqsStoredResourceArns finds the queue a
URL reaches as the handlers do, in the account the URL names, and
answers a queue's stored tags; TagQueue and UntagQueue give the queue a
new map one call at a time, and its tags field is volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-19 07:16:17 -04:00
1 parent 79646214e7
commit 634073fb83
11 files changed
+340 -80

No files matched your search

@@ -57,9 +57,9 @@ 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); 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); 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
@@ -21,7 +21,6 @@ import io.floci.aws.services.kms.KmsService;
import io.floci.aws.services.s3.S3ControlArnUtils;
import io.floci.aws.services.s3.S3ObjectKeys;
import io.floci.aws.services.s3.S3ResourceArns;
import io.floci.aws.services.sqs.SqsService;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.vertx.ext.web.RoutingContext;
import jakarta.enterprise.context.ApplicationScoped;
@@ -186,7 +185,6 @@ public class ResourceArnBuilder {
: IamActionRegistry.decodedPath(ctx);
return switch (credentialScope) {
case "s3" -> buildS3Resources(operation, path, ctx, region);
case "sqs" -> List.of(buildSqsArn(ctx, region, accountId));
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
@@ -1202,28 +1200,6 @@ public class ResourceArnBuilder {
return AwsArnUtils.isArn(resourceArn) ? resourceArn : "*";
}
// ── SQS ─────────────────────────────────────────────────────────────────────
private String buildSqsArn(ContainerRequestContext ctx, String region, String accountId) {
String queueUrl = firstFormParam(ctx, "QueueUrl");
if (queueUrl == null) {
JsonNode json = readJsonBody(ctx);
if (json != null && json.isObject()) {
if (json.hasNonNull("QueueUrl")) {
queueUrl = json.get("QueueUrl").asText().trim();
} else if (json.hasNonNull("QueueName")) {
String queueName = json.get("QueueName").asText().trim();
if (!queueName.isEmpty()) {
return AwsArnUtils.Arn.of("sqs", region, accountId, queueName).toString();
}
}
}
}
if (queueUrl != null && !queueUrl.isEmpty()) {
return SqsService.queueArn(queueUrl, AwsRegions.partitionFor(region), region, accountId);
}
return AwsArnUtils.Arn.of("sqs", region, accountId, "*").toString();
}
// ── SNS ─────────────────────────────────────────────────────────────────────
private String buildSnsArn(ContainerRequestContext ctx, String region, String accountId) {
String topicArn = firstFormParam(ctx, "TopicArn");
@@ -2105,32 +2105,59 @@ public class SqsService implements Resettable, ResourceProvider {
return results;
}
public void tagQueue(String queueUrl, Map<String, String> tags, String region) {
/**
* Gives the queue 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 tagQueue(String queueUrl, Map<String, String> tags, String region) {
String storageKey = regionKey(region, queueUrl);
Queue queue = queueStore.get(storageKey)
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
"The specified queue does not exist.", 400));
if (tags != null) {
queue.getTags().putAll(tags);
Map<String, String> merged = new LinkedHashMap<>(queue.getTags());
merged.putAll(tags);
queue.setTags(merged);
}
queueStore.put(storageKey, queue);
LOG.infov("Tagged queue: {0}", queueUrl);
}
public void untagQueue(String queueUrl, List<String> tagKeys, String region) {
/** Gives the queue a new map without {@code tagKeys}, one tag call at a time, as {@link #tagQueue}. */
public synchronized void untagQueue(String queueUrl, List<String> tagKeys, String region) {
String storageKey = regionKey(region, queueUrl);
Queue queue = queueStore.get(storageKey)
.orElseThrow(() -> new AwsException("AWS.SimpleQueueService.NonExistentQueue",
"The specified queue does not exist.", 400));
if (tagKeys != null) {
for (String key : tagKeys) {
queue.getTags().remove(key);
}
Map<String, String> remaining = new LinkedHashMap<>(queue.getTags());
tagKeys.forEach(remaining::remove);
queue.setTags(remaining);
}
queueStore.put(storageKey, queue);
LOG.infov("Untagged queue: {0}", queueUrl);
}
/**
* The ARN of the queue {@code queueUrl} reaches from {@code region}, looked up as the handlers look it up: in the
* account the URL's path names, and the queue of that name held in the Region. Empty when there is no such queue.
*/
Optional<String> heldQueueArn(String queueUrl, String region) {
return getQueueByUrl(regionKey(region, queueUrl), queueUrl).map(queue -> AwsArnUtils.Arn.of("sqs", region,
queue.getAccountId() != null ? queue.getAccountId() : regionResolver.getAccountId(),
queue.getQueueName()).toString());
}
/**
* The tags of the queue {@code queueName} held in {@code region} of {@code account}, read in place under the key
* the handlers read; none when there is no such queue.
*/
Map<String, String> queueTagsInAccount(String account, String region, String queueName) {
String storageKey = regionKey(region, "sqs://queue/" + account + "/" + queueName);
return AccountAwareStorageBackend.readInAccount(queueStore, account, storageKey)
.map(Queue::getTags).filter(Objects::nonNull).orElse(Map.of());
}
private static final ObjectMapper POLICY_MAPPER = new ObjectMapper();
public void addPermission(String queueUrl, String label, List<String> awsAccountIds,
@@ -0,0 +1,73 @@
package io.floci.aws.services.sqs;
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.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* IAM's lookups of what SQS holds, for the {@code sqs} namespace: the queue a request names by its URL, which the
* generated request-resource table asks SQS to find, and the tags of a queue.
*/
@ApplicationScoped
public class SqsStoredResourceArns implements StoredResourceArns {
private static final String NAMESPACE = "sqs";
private static final String QUEUE = "queue";
private final SqsService sqs;
private final RegionResolver regionResolver;
@Inject
public SqsStoredResourceArns(SqsService sqs, RegionResolver regionResolver) {
this.sqs = sqs;
this.regionResolver = regionResolver;
}
@Override
public String namespace() {
return NAMESPACE;
}
/**
* The ARN of the queue the one URL in {@code names} reaches from {@code region}, found as the handlers find it: in
* the account the URL's path names and held in the Region. None for another type, several URLs or a URL naming no
* queue.
*/
@Override
public Optional<String> find(String type, List<String> names, String region) {
if (!QUEUE.equals(type) || names.size() != 1) {
return Optional.empty();
}
return sqs.heldQueueArn(names.getFirst(), region);
}
/**
* The tags of the queue whose ARN is {@code arn}, in the request's account, where the tag handlers read: an ARN of
* another account names none, and neither does one other than the ARN {@link #find} gives the queue. The queue is
* read in place by the Region and the name the ARN gives. A tag without
* a value reads as an empty one. Tag calls give the queue 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();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String account = regionResolver.getAccountId();
if (!parsed.accountId().equals(account) || parsed.region().isBlank()
|| !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;
}
}
@@ -16,7 +16,7 @@ public class Queue {
private String queueUrl;
private String accountId;
private Map<String, String> attributes;
private Map<String, String> tags;
private volatile Map<String, String> tags;
private Instant createdTimestamp;
private Instant lastModifiedTimestamp;