8000 fix publishing to FIFO topic with TargetArn (#7564) · localstack/localstack@fb4f5f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fb4f5f6

Browse files
authored
fix publishing to FIFO topic with TargetArn (#7564)
1 parent 2b114d8 commit fb4f5f6

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

localstack/services/sns/provider.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,20 @@ def publish(
533533
if len(message) > MAXIMUM_MESSAGE_LENGTH:
534534
raise InvalidParameterException("Invalid parameter: Message too long")
535535

536-
if topic_arn and ".fifo" in topic_arn:
536+
# for compatibility reasons, AWS allows users to use either TargetArn or TopicArn for publishing to a topic
537+
# use any of them for topic validation
538+
topic_or_target_arn = topic_arn or target_arn
539+
540+
if topic_or_target_arn and ".fifo" in topic_or_target_arn:
537541
if not message_group_id:
538542
raise InvalidParameterException(
539543
"Invalid parameter: The MessageGroupId parameter is required for FIFO topics",
540544
)
541545
moto_sns_backend = sns_backends[context.account_id][context.region]
542-
if moto_sns_backend.get_topic(arn=topic_arn).content_based_deduplication == "false":
546+
if (
547+
moto_sns_backend.get_topic(arn=topic_or_target_arn).content_based_deduplication
548+
== "false"
549+
):
543550
if not message_deduplication_id:
544551
raise InvalidParameterException(
545552
"Invalid parameter: The topic should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly",
@@ -581,8 +588,7 @@ def publish(
581588
"Invalid parameter: TargetArn Reason: No endpoint found for the target arn specified"
582589
)
583590
else:
584-
topic = topic_arn or target_arn
585-
if topic not in store.sns_subscriptions:
591+
if topic_or_target_arn not in store.sns_subscriptions:
586592
raise NotFoundException(
587593
"Topic does not exist",
588594
)
@@ -610,7 +616,7 @@ def publish(
610616
# beware if the subscription is FIFO, the order might not be guaranteed.
611617
# 2 quick call to this method in succession might not be executed in order in the executor?
612618
# TODO: test how this behaves in a FIFO context with a lot of threads.
613-
self._publisher.publish_to_topic(publish_ctx, topic_arn or target_arn)
619+
self._publisher.publish_to_topic(publish_ctx, topic_or_target_arn)
614620

615621
return PublishResponse(MessageId=message_ctx.message_id)
616622

tests/integration/test_sns.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,3 +3241,25 @@ def test_publish_to_fifo_topic_to_sqs_queue_no_content_dedup(
32413241
AttributeNames=["All"],
32423242
)
32433243
snapshot.match("messages", response)
3244+
3245+
@pytest.mark.aws_validated
3246+
def test_publish_to_fifo_with_target_arn(self, sns_client, sns_create_topic):
3247+
topic_name = f"topic-{short_uid()}.fifo"
3248+
topic_attributes = {
3249+
"FifoTopic": "true",
3250+
"ContentBasedDeduplication": "true",
3251+
}
3252+
3253+
topic_arn = sns_create_topic(
3254+
Name=topic_name,
3255+
Attributes=topic_attributes,
3256+
)["TopicArn"]
3257+
3258+
message = {"foo": "bar"}
3259+
response = sns_client.publish(
3260+
TargetArn=topic_arn,
3261+
Message=json.dumps({"default": json.dumps(message)}),
3262+
MessageStructure="json",
3263+
MessageGroupId="123",
3264+
)
3265+
assert "MessageId" in response

0 commit comments

Comments
 (0)
0