Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 3eb61b9

Browse files
authored
Sns:v2 publish (#13399)
1 parent 02ce6a2 commit 3eb61b9

7 files changed

Lines changed: 759 additions & 64 deletions

File tree

localstack-core/localstack/services/sns/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
MSG_ATTR_NAME_REGEX = re.compile(r"^(?!\.)(?!.*\.$)(?!.*\.\.)[a-zA-Z0-9_\-.]+$")
2929
ATTR_TYPE_REGEX = re.compile(r"^(String|Number|Binary)\..+$")
3030
VALID_MSG_ATTR_NAME_CHARS = set(ascii_letters + digits + "." + "-" + "_")
31+
E164_REGEX = re.compile(r"^\+?[1-9]\d{1,14}$")
3132

3233

3334
GCM_URL = "https://fcm.googleapis.com/fcm/send"
@@ -42,6 +43,7 @@
4243
SNS_CERT_ENDPOINT = "/_aws/sns/SimpleNotificationService-6c6f63616c737461636b69736e696365.pem"
4344

4445
DUMMY_SUBSCRIPTION_PRINCIPAL = "arn:{partition}:iam::{account_id}:user/DummySNSPrincipal"
45-
E164_REGEX = re.compile(r"^\+?[1-9]\d{1,14}$")
4646

4747
VALID_APPLICATION_PLATFORMS = list(get_args(SnsApplicationPlatforms))
48+
49+
MAXIMUM_MESSAGE_LENGTH = 262144

localstack-core/localstack/services/sns/publisher.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
SnsStore,
3131
SnsSubscription,
3232
)
33+
from localstack.services.sns.v2.utils import get_topic_subscriptions
3334
from localstack.utils.aws.arns import (
3435
PARTITION_NAMES,
3536
extract_account_id_from_arn,
@@ -1009,7 +1010,10 @@ def create_sns_message_body(
10091010
# FIFO topics do not add the signature in the message
10101011
if not subscriber.get("TopicArn", "").endswith(".fifo"):
10111012
signature_version = (
1012-
topic_attributes.get("signature_version", "1") if topic_attributes else "1"
1013+
# we allow for both casings, depending on v1 or v2 provider
1014+
topic_attributes.get("signature_version", topic_attributes.get("SignatureVersion", "1"))
1015+
if topic_attributes
1016+
else "1"
10131017
)
10141018
canonical_string = compute_canonical_string(data, message_type)
10151019
signature = get_message_signature(canonical_string, signature_version=signature_version)
@@ -1234,7 +1238,7 @@ def _should_publish(
12341238
)
12351239

12361240
def publish_to_topic(self, ctx: SnsPublishContext, topic_arn: str) -> None:
1237-
subscriptions = ctx.store.get_topic_subscriptions(topic_arn)
1241+
subscriptions = get_topic_subscriptions(ctx.store, topic_arn)
12381242
for subscriber in subscriptions:
12391243
if self._should_publish(ctx.store.subscription_filter_policy, ctx.message, subscriber):
12401244
notifier = self.topic_notifiers[subscriber["Protocol"]]
@@ -1249,7 +1253,7 @@ def publish_to_topic(self, ctx: SnsPublishContext, topic_arn: str) -> None:
12491253
self._submit_notification(notifier, ctx, subscriber)
12501254

12511255
def publish_batch_to_topic(self, ctx: SnsBatchPublishContext, topic_arn: str) -> None:
1252-
subscriptions = ctx.store.get_topic_subscriptions(topic_arn)
1256+
subscriptions = get_topic_subscriptions(ctx.store, topic_arn)
12531257
for subscriber in subscriptions:
12541258
protocol = subscriber["Protocol"]
12551259
notifier = self.batch_topic_notifiers.get(protocol)

localstack-core/localstack/services/sns/v2/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,15 @@ class SnsStore(BaseStore):
181181
# maps endpoint arns to platform endpoints
182182
platform_endpoints: dict[str, PlatformEndpoint] = LocalAttribute(default=dict)
183183

184+
# cache of topic ARN to platform endpoint messages (used primarily for testing)
185+
platform_endpoint_messages: dict[str, list[dict]] = LocalAttribute(default=dict)
186+
184187
# topic/subscription independent default values for sending sms messages
185188
sms_attributes: dict[str, str] = LocalAttribute(default=dict)
186189

190+
# list of sent SMS messages
191+
sms_messages: list[dict] = LocalAttribute(default=list)
192+
187193
TAGS: TaggingService = CrossRegionAttribute(default=TaggingService)
188194

189195

0 commit comments

Comments
 (0)