From 9a8a35298ebd117fb3e589fa6c0acd5da52fdc38 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:10:14 +0100 Subject: [PATCH] fix(sns): publish to a topic in the account its ARN names (#10088) Publish and PublishBatch looked the topic up in the caller's account, so publishing to another account's topic answered NotFound even though the ARN named an existing topic. The topic is now resolved in the account its ARN names, as topicExists already did, and the fan-out runs as that account so the owner's subscriptions and queues receive the message. --- .../io/floci/aws/services/sns/SnsService.java | 40 ++++++++++++--- ...SnsCrossAccountPublishIntegrationTest.java | 49 +++++++++++++++++++ 2 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/sns/SnsCrossAccountPublishIntegrationTest.java diff --git a/src/main/java/io/floci/aws/services/sns/SnsService.java b/src/main/java/io/floci/aws/services/sns/SnsService.java index 5182a7fbb9..b8bac21e00 100644 --- a/src/main/java/io/floci/aws/services/sns/SnsService.java +++ b/src/main/java/io/floci/aws/services/sns/SnsService.java @@ -88,6 +88,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -728,12 +729,17 @@ public class SnsService implements Resettable, ResourceProvider { } public boolean topicExists(String topicArn, String region) { + return findTopic(topicArn, region).isPresent(); + } + + /** The topic in the account its ARN names, which for a Publish is not necessarily the caller's. */ + private Optional findTopic(String topicArn, String region) { if (topicStore instanceof AccountAwareStorageBackend aware) { String accountId = AwsArnUtils.accountOrDefault(topicArn, regionResolver.getAccountId()); return aware.getForAccountMigratingLegacy(accountId, topicKey(region, topicArn), - topic -> topicArn.equals(topic.getTopicArn())).isPresent(); + topic -> topicArn.equals(topic.getTopicArn())); } - return topicStore.get(topicKey(region, topicArn)).isPresent(); + return topicStore.get(topicKey(region, topicArn)); } public Map getTopicAttributes(String topicArn, String region) { @@ -1245,10 +1251,29 @@ public class SnsService implements Resettable, ResourceProvider { 400); } - String topicStoreKey = topicKey(region, effectiveArn); - Topic topic = topicStore.get(topicStoreKey) + Topic topic = findTopic(effectiveArn, region) .orElseThrow(() -> new AwsException("NotFound", "Topic does not exist.", 404)); + return asTopicOwner(effectiveArn, () -> publishToTopic(topic, effectiveArn, payloadSize, message, subject, + messageStructure, messageAttributes, messageGroupId, messageDeduplicationId, region)); + } + /** + * Runs a publish's fan-out as the account that owns the topic. A Publish may name another + * account's topic, and the delivery that follows is made on the owner's behalf, so every + * account-scoped lookup it makes (subscriptions, queues) must see the owner, not the caller. + */ + private T asTopicOwner(String topicArn, Supplier fanOut) { + String owner = AwsArnUtils.accountOrDefault(topicArn, regionResolver.getAccountId()); + if (owner == null || owner.equals(regionResolver.getAccountId())) { + return fanOut.get(); + } + return RequestScopes.callAs(owner, fanOut); + } + + private String publishToTopic(Topic topic, String effectiveArn, int payloadSize, String message, + String subject, String messageStructure, + Map messageAttributes, + String messageGroupId, String messageDeduplicationId, String region) { requireWithinMaxMessageSize(payloadSize, maxMessageSize(topic)); validateTopicMessageStructure(message, messageStructure); @@ -1709,10 +1734,13 @@ public class SnsService implements Resettable, ResourceProvider { } public BatchPublishResult publishBatch(String topicArn, List> entries, String region) { - String topicStoreKey = topicKey(region, topicArn); - Topic topic = topicStore.get(topicStoreKey) + Topic topic = findTopic(topicArn, region) .orElseThrow(() -> new AwsException("NotFound", "Topic does not exist.", 404)); + return asTopicOwner(topicArn, () -> publishBatchToTopic(topic, topicArn, entries, region)); + } + private BatchPublishResult publishBatchToTopic(Topic topic, String topicArn, List> entries, + String region) { int batchSize = 0; for (Map entry : entries) { String message = (String) entry.get("Message"); diff --git a/src/test/java/io/floci/aws/services/sns/SnsCrossAccountPublishIntegrationTest.java b/src/test/java/io/floci/aws/services/sns/SnsCrossAccountPublishIntegrationTest.java new file mode 100644 index 0000000000..447e9ad957 --- /dev/null +++ b/src/test/java/io/floci/aws/services/sns/SnsCrossAccountPublishIntegrationTest.java @@ -0,0 +1,49 @@ +package io.floci.aws.services.sns; + +import io.floci.aws.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.sns.SnsClient; +import software.amazon.awssdk.services.sns.model.PublishBatchRequestEntry; +import software.amazon.awssdk.services.sqs.SqsClient; +import software.amazon.awssdk.services.sqs.model.Message; +import software.amazon.awssdk.services.sqs.model.QueueAttributeName; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class SnsCrossAccountPublishIntegrationTest { + + private static final String OWNER = "730000000011"; + private static final String CALLER = "730000000012"; + + @Test + void publishAndPublishBatchToAnotherAccountsTopicReachTheOwnersSubscribers() { + try (SnsClient ownerSns = AwsTestClients.create(SnsClient.builder(), OWNER, Region.US_EAST_1); + SqsClient ownerSqs = AwsTestClients.create(SqsClient.builder(), OWNER, Region.US_EAST_1); + SnsClient callerSns = AwsTestClients.create(SnsClient.builder(), CALLER, Region.US_EAST_1)) { + String suffix = UUID.randomUUID().toString().substring(0, 8); + String topicArn = ownerSns.createTopic(r -> r.name("xacct-" + suffix)).topicArn(); + String queueUrl = ownerSqs.createQueue(r -> r.queueName("xacct-" + suffix)).queueUrl(); + String queueArn = ownerSqs.getQueueAttributes(r -> r.queueUrl(queueUrl) + .attributeNames(QueueAttributeName.QUEUE_ARN)).attributes().get(QueueAttributeName.QUEUE_ARN); + ownerSns.subscribe(r -> r.topicArn(topicArn).protocol("sqs").endpoint(queueArn) + .attributes(Map.of("RawMessageDelivery", "true"))); + + callerSns.publish(r -> r.topicArn(topicArn).message("single")); + callerSns.publishBatch(r -> r.topicArn(topicArn).publishBatchRequestEntries( + PublishBatchRequestEntry.builder().id("1").message("batched").build())); + + List bodies = ownerSqs.receiveMessage(r -> r.queueUrl(queueUrl).maxNumberOfMessages(10) + .waitTimeSeconds(2)).messages().stream().map(Message::body).sorted().toList(); + assertEquals(List.of("batched", "single"), bodies); + assertTrue(ownerSns.listTopics().topics().stream().anyMatch(t -> t.topicArn().equals(topicArn))); + } + } +}