mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 02:28:08 -05:00
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.
This commit is contained in:
1 parent
24de0d13ae
commit
9a8a35298e
2 files changed
+83
-6
No files matched your search
@@ -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<Topic> findTopic(String topicArn, String region) {
|
||||
if (topicStore instanceof AccountAwareStorageBackend<Topic> 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<String, String> 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> T asTopicOwner(String topicArn, Supplier<T> 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<String, MessageAttributeValue> 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<Map<String, Object>> 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<Map<String, Object>> entries,
|
||||
String region) {
|
||||
int batchSize = 0;
|
||||
for (Map<String, Object> entry : entries) {
|
||||
String message = (String) entry.get("Message");
|
||||
|
||||
@@ -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<String> 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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user