From c81d9dab468077e0159fb40524ad7a26f3166b16 Mon Sep 17 00:00:00 2001 From: luiseterc Date: Thu, 19 Nov 2026 16:04:10 +0100 Subject: [PATCH] fix(sqs): read resource tags from the queue owner account (#8866) --- docs/services/sqs.md | 9 + .../services/sqs/SqsStoredResourceArns.java | 13 +- .../SqsResourceTagsIntegrationTest.java | 174 ++++++++++++++++++ .../services/iam/ResourceArnBuilderTest.java | 2 +- 4 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 src/test/java/io/floci/aws/core/common/SqsResourceTagsIntegrationTest.java diff --git a/docs/services/sqs.md b/docs/services/sqs.md index 7c9535a490..905dbd36d8 100644 --- a/docs/services/sqs.md +++ b/docs/services/sqs.md @@ -243,3 +243,12 @@ does not begin a new delay or visibility period. Older stored messages without these fields still load as visible messages. Delivery state that an older version did not save cannot be reconstructed. Hybrid mode retains its configured flush window; this does not add synchronous durability to hybrid storage. + +### IAM resource tags + +With IAM enforcement enabled, queue operations use the exact queue's stored tags +for `aws:ResourceTag` conditions. The lookup reads the ARN's account, Region and +partition and observes tag changes immediately, including for cross-account +requests. A matching tag does not replace the queue policy grant required for +cross-account access; another queue's tags cannot satisfy the target queue's +condition. diff --git a/src/main/java/io/floci/aws/services/sqs/SqsStoredResourceArns.java b/src/main/java/io/floci/aws/services/sqs/SqsStoredResourceArns.java index 30c7ffbc6b..c0a33efd6a 100644 --- a/src/main/java/io/floci/aws/services/sqs/SqsStoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/sqs/SqsStoredResourceArns.java @@ -1,7 +1,6 @@ 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; @@ -21,12 +20,10 @@ public class SqsStoredResourceArns implements StoredResourceArns { private static final String QUEUE = "queue"; private final SqsService sqs; - private final RegionResolver regionResolver; @Inject - public SqsStoredResourceArns(SqsService sqs, RegionResolver regionResolver) { + public SqsStoredResourceArns(SqsService sqs) { this.sqs = sqs; - this.regionResolver = regionResolver; } @Override @@ -48,8 +45,8 @@ public class SqsStoredResourceArns implements StoredResourceArns { } /** - * 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 + * The tags of the queue whose ARN is {@code arn}, in the queue's account, including when the caller belongs to + * another account. An ARN other than the one {@link #find} gives the queue names none. 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. */ @@ -59,8 +56,8 @@ public class SqsStoredResourceArns implements StoredResourceArns { return Map.of(); } AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn); - String account = regionResolver.getAccountId(); - if (!parsed.accountId().equals(account) || parsed.region().isBlank() + String account = parsed.accountId(); + if (account.isBlank() || parsed.region().isBlank() || !arn.equals(AwsArnUtils.Arn.of(NAMESPACE, parsed.region(), account, parsed.resource()).toString())) { return Map.of(); } diff --git a/src/test/java/io/floci/aws/core/common/SqsResourceTagsIntegrationTest.java b/src/test/java/io/floci/aws/core/common/SqsResourceTagsIntegrationTest.java new file mode 100644 index 0000000000..32099f0f4b --- /dev/null +++ b/src/test/java/io/floci/aws/core/common/SqsResourceTagsIntegrationTest.java @@ -0,0 +1,174 @@ +package io.floci.aws.core.common; + +import io.floci.aws.testing.AwsTestClients; +import io.floci.aws.testing.EnforcementFixtures; +import io.floci.aws.testing.EnforcementFixtures.UserCredentials; +import io.floci.aws.testing.IamEnforcementProfile; +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; +import software.amazon.awssdk.services.sqs.model.SqsException; + +import java.net.URI; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.UUID; + +import static io.floci.aws.testing.EnforcementFixtures.auth; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class SqsResourceTagsIntegrationTest { + private static final String ACCOUNT = "000000000000"; + @TestHTTPResource("/") + URI endpoint; + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @ParameterizedTest + @ValueSource(strings = {"eu-west-1", "us-gov-west-1", "cn-north-1"}) + void queueAttributesAndDeletionUseCurrentStoredTags(String region) { + String name = "sqs-tags-" + UUID.randomUUID(); + UserCredentials credentials = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), auth(ACCOUNT, "iam"), name); + try (SqsClient admin = admin(ACCOUNT, region); SqsClient client = client(credentials, region)) { + String url = admin.createQueue(r -> r.queueName(name).tags(Map.of("Owner", "fixture"))).queueUrl(); + try { + String arn = admin.getQueueAttributes(r -> r.queueUrl(url).attributeNames(QueueAttributeName.QUEUE_ARN)) + .attributes().get(QueueAttributeName.QUEUE_ARN); + policy(name, """ + {"Effect":"Allow","Action":["sqs:SetQueueAttributes","sqs:GetQueueAttributes", + "sqs:ListQueueTags","sqs:DeleteQueue"],"Resource":"%s", + "Condition":{"StringEquals":{"aws:ResourceTag/Owner":"fixture"}}} + """.formatted(arn)); + client.setQueueAttributes(r -> r.queueUrl(url).attributes(Map.of(QueueAttributeName.VISIBILITY_TIMEOUT, "45"))); + assertEquals("45", client.getQueueAttributes(r -> r.queueUrl(url) + .attributeNames(QueueAttributeName.VISIBILITY_TIMEOUT)).attributes().get(QueueAttributeName.VISIBILITY_TIMEOUT)); + String queuePolicy = """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow", + "Principal":{"AWS":"arn:%s:iam::%s:root"},"Action":"sqs:SendMessage","Resource":"%s"}]} + """.formatted(AwsRegions.partitionFor(region), ACCOUNT, arn); + client.setQueueAttributes(r -> r.queueUrl(url).attributes(Map.of(QueueAttributeName.POLICY, queuePolicy))); + assertEquals(queuePolicy, admin.getQueueAttributes(r -> r.queueUrl(url) + .attributeNames(QueueAttributeName.POLICY)).attributes().get(QueueAttributeName.POLICY)); + assertEquals("fixture", client.listQueueTags(r -> r.queueUrl(url)).tags().get("Owner")); + admin.tagQueue(r -> r.queueUrl(url).tags(Map.of("Owner", "other"))); + denied(() -> client.setQueueAttributes(r -> r.queueUrl(url) + .attributes(Map.of(QueueAttributeName.VISIBILITY_TIMEOUT, "60")))); + denied(() -> client.deleteQueue(r -> r.queueUrl(url))); + assertEquals("45", admin.getQueueAttributes(r -> r.queueUrl(url) + .attributeNames(QueueAttributeName.VISIBILITY_TIMEOUT)).attributes().get(QueueAttributeName.VISIBILITY_TIMEOUT)); + admin.untagQueue(r -> r.queueUrl(url).tagKeys("Owner")); + denied(() -> client.listQueueTags(r -> r.queueUrl(url))); + admin.tagQueue(r -> r.queueUrl(url).tags(Map.of("Owner", "fixture"))); + client.deleteQueue(r -> r.queueUrl(url)); + } finally { + try { + admin.deleteQueue(r -> r.queueUrl(url)); + } catch (SqsException absent) { + assertEquals("AWS.SimpleQueueService.NonExistentQueue", absent.awsErrorDetails().errorCode()); + } + } + } + } + + @Test + void resourceTagsComeFromTheQueuesAccountAndRegion() { + String name = "sqs-coordinates-" + UUID.randomUUID(); + String otherAccount = "111111111111"; + UserCredentials credentials = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), auth(ACCOUNT, "iam"), name); + try (SqsClient admin = admin(ACCOUNT, "eu-west-1"); + SqsClient foreign = admin(otherAccount, "eu-west-1"); + SqsClient elsewhere = admin(ACCOUNT, "us-east-1"); + SqsClient client = client(credentials, "eu-west-1"); + SqsClient remote = client(credentials, "us-east-1")) { + String local = admin.createQueue(r -> r.queueName(name).tags(Map.of("Owner", "fixture"))).queueUrl(); + String other = foreign.createQueue(r -> r.queueName(name).tags(Map.of("Owner", "other"))).queueUrl(); + String regional = elsewhere.createQueue(r -> r.queueName(name).tags(Map.of("Owner", "other"))).queueUrl(); + try { + policy(name, """ + {"Effect":"Allow","Action":["sqs:GetQueueAttributes","sqs:SendMessage"],"Resource":"arn:aws:sqs:*:*:%s", + "Condition":{"StringEquals":{"aws:ResourceTag/Owner":"fixture"}}} + """.formatted(name)); + String grant = """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow", + "Principal":{"AWS":"arn:aws:iam::%s:user/%s"},"Action":"sqs:SendMessage", + "Resource":"arn:aws:sqs:eu-west-1:%s:%s"}]} + """.formatted(ACCOUNT, name, otherAccount, name); + foreign.setQueueAttributes(r -> r.queueUrl(other).attributes(Map.of(QueueAttributeName.POLICY, grant))); + client.getQueueAttributes(r -> r.queueUrl(local).attributeNames(QueueAttributeName.QUEUE_ARN)); + denied(() -> client.sendMessage(r -> r.queueUrl(other).messageBody("tag-boundary"))); + denied(() -> remote.getQueueAttributes(r -> r.queueUrl(regional).attributeNames(QueueAttributeName.QUEUE_ARN))); + foreign.tagQueue(r -> r.queueUrl(other).tags(Map.of("Owner", "fixture"))); + client.sendMessage(r -> r.queueUrl(other).messageBody("tag-boundary")); + foreign.setQueueAttributes(r -> r.queueUrl(other).attributes(Map.of(QueueAttributeName.POLICY, "{}"))); + denied(() -> client.sendMessage(r -> r.queueUrl(other).messageBody("grant-required"))); + foreign.setQueueAttributes(r -> r.queueUrl(other).attributes(Map.of(QueueAttributeName.POLICY, grant))); + policy(name, """ + {"Effect":"Allow","Action":"sqs:SendMessage","Resource":"*"}, + {"Effect":"Deny","Action":"sqs:SendMessage","Resource":"*", + "Condition":{"StringEquals":{"aws:ResourceTag/Owner":"fixture"}}} + """); + denied(() -> client.sendMessage(r -> r.queueUrl(other).messageBody("tag-deny"))); + } finally { + admin.deleteQueue(r -> r.queueUrl(local)); + foreign.deleteQueue(r -> r.queueUrl(other)); + elsewhere.deleteQueue(r -> r.queueUrl(regional)); + } + } + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void caseVariantTagsCannotHideAnExplicitDeny(boolean reverse) { + String name = "sqs-case-" + UUID.randomUUID(); + UserCredentials credentials = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), auth(ACCOUNT, "iam"), name); + Map tags = new LinkedHashMap<>(); + tags.put(reverse ? "Environment" : "environment", reverse ? "prod" : "dev"); + tags.put(reverse ? "environment" : "Environment", reverse ? "dev" : "prod"); + try (SqsClient admin = admin(ACCOUNT, "eu-west-1"); SqsClient client = client(credentials, "eu-west-1")) { + String url = admin.createQueue(r -> r.queueName(name).tags(tags)).queueUrl(); + try { + policy(name, """ + {"Effect":"Allow","Action":"sqs:SetQueueAttributes","Resource":"*"}, + {"Effect":"Deny","Action":"sqs:SetQueueAttributes","Resource":"*", + "Condition":{"ForAnyValue:StringEquals":{"aws:ResourceTag/Environment":"prod"}}} + """); + denied(() -> client.setQueueAttributes(r -> r.queueUrl(url) + .attributes(Map.of(QueueAttributeName.VISIBILITY_TIMEOUT, "45")))); + admin.untagQueue(r -> r.queueUrl(url).tagKeys("Environment")); + client.setQueueAttributes(r -> r.queueUrl(url).attributes(Map.of(QueueAttributeName.VISIBILITY_TIMEOUT, "45"))); + } finally { + admin.deleteQueue(r -> r.queueUrl(url)); + } + } + } + + private SqsClient admin(String account, String region) { + return AwsTestClients.create(SqsClient.builder(), account, Region.of(region), endpoint); + } + + private SqsClient client(UserCredentials credentials, String region) { + return AwsTestClients.create(SqsClient.builder(), credentials.accessKeyId(), credentials.secretAccessKey(), + Region.of(region), endpoint); + } + + private void policy(String user, String statements) { + EnforcementFixtures.putUserPolicy(endpoint.getPort(), auth(ACCOUNT, "iam"), user, "tags", + "{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}"); + } + + private static void denied(Runnable call) { + SqsException error = assertThrows(SqsException.class, call::run); + assertEquals("AccessDeniedException", error.awsErrorDetails().errorCode()); + } +} diff --git a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java index 3cbd7dbe30..71bff61b42 100644 --- a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java @@ -1804,7 +1804,7 @@ class ResourceArnBuilderTest { for (String queue : queues) { sqs.createQueue(queue, Map.of(), "us-east-1"); } - return reading(new SqsStoredResourceArns(sqs, new RegionResolver("us-east-1", ACCOUNT))); + return reading(new SqsStoredResourceArns(sqs)); } /**