localstack-core 4.10.1.dev42__py3-none-any.whl → 4.11.2.dev14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. localstack/aws/api/apigateway/__init__.py +42 -0
  2. localstack/aws/api/cloudformation/__init__.py +161 -0
  3. localstack/aws/api/ec2/__init__.py +1165 -12
  4. localstack/aws/api/iam/__init__.py +227 -0
  5. localstack/aws/api/kms/__init__.py +1 -0
  6. localstack/aws/api/lambda_/__init__.py +418 -66
  7. localstack/aws/api/logs/__init__.py +312 -0
  8. localstack/aws/api/opensearch/__init__.py +89 -0
  9. localstack/aws/api/redshift/__init__.py +69 -0
  10. localstack/aws/api/resourcegroupstaggingapi/__init__.py +36 -0
  11. localstack/aws/api/route53/__init__.py +42 -0
  12. localstack/aws/api/route53resolver/__init__.py +1 -0
  13. localstack/aws/api/s3/__init__.py +62 -0
  14. localstack/aws/api/secretsmanager/__init__.py +28 -23
  15. localstack/aws/api/stepfunctions/__init__.py +52 -10
  16. localstack/aws/api/sts/__init__.py +52 -0
  17. localstack/aws/handlers/logging.py +8 -4
  18. localstack/aws/handlers/service.py +11 -2
  19. localstack/aws/protocol/serializer.py +1 -1
  20. localstack/deprecations.py +0 -6
  21. localstack/services/acm/provider.py +4 -0
  22. localstack/services/apigateway/legacy/provider.py +28 -15
  23. localstack/services/cloudformation/engine/template_preparer.py +6 -2
  24. localstack/services/cloudformation/engine/v2/change_set_model_preproc.py +12 -0
  25. localstack/services/cloudwatch/provider.py +10 -3
  26. localstack/services/cloudwatch/provider_v2.py +6 -3
  27. localstack/services/configservice/provider.py +5 -1
  28. localstack/services/dynamodb/provider.py +1 -0
  29. localstack/services/dynamodb/v2/provider.py +1 -0
  30. localstack/services/dynamodbstreams/provider.py +6 -0
  31. localstack/services/dynamodbstreams/v2/provider.py +6 -0
  32. localstack/services/ec2/provider.py +6 -0
  33. localstack/services/es/provider.py +6 -0
  34. localstack/services/events/provider.py +4 -0
  35. localstack/services/events/v1/provider.py +9 -0
  36. localstack/services/firehose/provider.py +5 -0
  37. localstack/services/iam/provider.py +4 -0
  38. localstack/services/kms/models.py +10 -20
  39. localstack/services/kms/provider.py +4 -0
  40. localstack/services/lambda_/api_utils.py +37 -20
  41. localstack/services/lambda_/event_source_mapping/pollers/stream_poller.py +1 -1
  42. localstack/services/lambda_/invocation/assignment.py +4 -1
  43. localstack/services/lambda_/invocation/execution_environment.py +21 -2
  44. localstack/services/lambda_/invocation/lambda_models.py +27 -2
  45. localstack/services/lambda_/invocation/lambda_service.py +51 -3
  46. localstack/services/lambda_/invocation/models.py +9 -1
  47. localstack/services/lambda_/invocation/version_manager.py +18 -3
  48. localstack/services/lambda_/provider.py +239 -95
  49. localstack/services/lambda_/resource_providers/aws_lambda_function.py +33 -1
  50. localstack/services/lambda_/runtimes.py +3 -1
  51. localstack/services/logs/provider.py +9 -0
  52. localstack/services/opensearch/provider.py +53 -3
  53. localstack/services/resource_groups/provider.py +5 -1
  54. localstack/services/resourcegroupstaggingapi/provider.py +6 -1
  55. localstack/services/s3/provider.py +28 -15
  56. localstack/services/s3/utils.py +35 -14
  57. localstack/services/s3control/provider.py +101 -2
  58. localstack/services/s3control/validation.py +50 -0
  59. localstack/services/sns/constants.py +3 -1
  60. localstack/services/sns/publisher.py +15 -6
  61. localstack/services/sns/v2/models.py +6 -0
  62. localstack/services/sns/v2/provider.py +650 -19
  63. localstack/services/sns/v2/utils.py +12 -0
  64. localstack/services/stepfunctions/asl/component/common/path/result_path.py +1 -1
  65. localstack/services/stepfunctions/asl/component/state/state_execution/execute_state.py +0 -1
  66. localstack/services/stepfunctions/asl/component/state/state_execution/state_map/state_map.py +0 -1
  67. localstack/services/stepfunctions/asl/component/state/state_execution/state_task/lambda_eval_utils.py +8 -8
  68. localstack/services/stepfunctions/asl/component/state/state_execution/state_task/{mock_eval_utils.py → local_mock_eval_utils.py} +13 -9
  69. localstack/services/stepfunctions/asl/component/state/state_execution/state_task/service/state_task_service.py +6 -6
  70. localstack/services/stepfunctions/asl/component/state/state_execution/state_task/service/state_task_service_callback.py +1 -1
  71. localstack/services/stepfunctions/asl/component/state/state_fail/state_fail.py +4 -0
  72. localstack/services/stepfunctions/asl/component/test_state/state/base_mock.py +118 -0
  73. localstack/services/stepfunctions/asl/component/test_state/state/common.py +82 -0
  74. localstack/services/stepfunctions/asl/component/test_state/state/execution.py +139 -0
  75. localstack/services/stepfunctions/asl/component/test_state/state/map.py +77 -0
  76. localstack/services/stepfunctions/asl/component/test_state/state/task.py +44 -0
  77. localstack/services/stepfunctions/asl/eval/environment.py +30 -22
  78. localstack/services/stepfunctions/asl/eval/states.py +1 -1
  79. localstack/services/stepfunctions/asl/eval/test_state/environment.py +49 -9
  80. localstack/services/stepfunctions/asl/eval/test_state/program_state.py +22 -0
  81. localstack/services/stepfunctions/asl/jsonata/jsonata.py +5 -1
  82. localstack/services/stepfunctions/asl/parse/preprocessor.py +67 -24
  83. localstack/services/stepfunctions/asl/parse/test_state/asl_parser.py +5 -4
  84. localstack/services/stepfunctions/asl/parse/test_state/preprocessor.py +222 -31
  85. localstack/services/stepfunctions/asl/static_analyser/test_state/test_state_analyser.py +170 -22
  86. localstack/services/stepfunctions/backend/execution.py +6 -6
  87. localstack/services/stepfunctions/backend/execution_worker.py +5 -5
  88. localstack/services/stepfunctions/backend/test_state/execution.py +36 -0
  89. localstack/services/stepfunctions/backend/test_state/execution_worker.py +33 -1
  90. localstack/services/stepfunctions/backend/test_state/test_state_mock.py +127 -0
  91. localstack/services/stepfunctions/local_mocking/__init__.py +9 -0
  92. localstack/services/stepfunctions/{mocking → local_mocking}/mock_config.py +24 -17
  93. localstack/services/stepfunctions/provider.py +78 -27
  94. localstack/services/stepfunctions/test_state/mock_config.py +47 -0
  95. localstack/testing/pytest/fixtures.py +28 -0
  96. localstack/testing/snapshots/transformer_utility.py +5 -0
  97. localstack/utils/analytics/publisher.py +37 -155
  98. localstack/utils/analytics/service_request_aggregator.py +6 -4
  99. localstack/utils/aws/arns.py +7 -0
  100. localstack/utils/batching.py +258 -0
  101. localstack/utils/collections.py +23 -11
  102. localstack/version.py +2 -2
  103. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/METADATA +5 -5
  104. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/RECORD +113 -105
  105. localstack_core-4.11.2.dev14.dist-info/plux.json +1 -0
  106. localstack/services/stepfunctions/mocking/__init__.py +0 -0
  107. localstack/utils/batch_policy.py +0 -124
  108. localstack_core-4.10.1.dev42.dist-info/plux.json +0 -1
  109. /localstack/services/stepfunctions/{mocking → local_mocking}/mock_config_file.py +0 -0
  110. {localstack_core-4.10.1.dev42.data → localstack_core-4.11.2.dev14.data}/scripts/localstack +0 -0
  111. {localstack_core-4.10.1.dev42.data → localstack_core-4.11.2.dev14.data}/scripts/localstack-supervisor +0 -0
  112. {localstack_core-4.10.1.dev42.data → localstack_core-4.11.2.dev14.data}/scripts/localstack.bat +0 -0
  113. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/WHEEL +0 -0
  114. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/entry_points.txt +0 -0
  115. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/licenses/LICENSE.txt +0 -0
  116. {localstack_core-4.10.1.dev42.dist-info → localstack_core-4.11.2.dev14.dist-info}/top_level.txt +0 -0
@@ -1,25 +1,30 @@
1
1
  import contextlib
2
2
  import copy
3
+ import functools
3
4
  import json
4
5
  import logging
5
6
  import re
6
7
 
7
8
  from botocore.utils import InvalidArnException
9
+ from rolo import Request, Router, route
8
10
 
9
11
  from localstack.aws.api import CommonServiceException, RequestContext
10
12
  from localstack.aws.api.sns import (
11
13
  AmazonResourceName,
14
+ BatchEntryIdsNotDistinctException,
12
15
  ConfirmSubscriptionResponse,
13
16
  CreateEndpointResponse,
14
17
  CreatePlatformApplicationResponse,
15
18
  CreateTopicResponse,
16
19
  Endpoint,
20
+ EndpointDisabledException,
17
21
  GetEndpointAttributesResponse,
18
22
  GetPlatformApplicationAttributesResponse,
19
23
  GetSMSAttributesResponse,
20
24
  GetSubscriptionAttributesResponse,
21
25
  GetTopicAttributesResponse,
22
26
  InvalidParameterException,
27
+ InvalidParameterValueException,
23
28
  ListEndpointsByPlatformApplicationResponse,
24
29
  ListPlatformApplicationsResponse,
25
30
  ListString,
@@ -28,8 +33,14 @@ from localstack.aws.api.sns import (
28
33
  ListTagsForResourceResponse,
29
34
  ListTopicsResponse,
30
35
  MapStringToString,
36
+ MessageAttributeMap,
31
37
  NotFoundException,
38
+ PhoneNumber,
32
39
  PlatformApplication,
40
+ PublishBatchRequestEntryList,
41
+ PublishBatchResponse,
42
+ PublishBatchResultEntry,
43
+ PublishResponse,
33
44
  SetSMSAttributesResponse,
34
45
  SnsApi,
35
46
  String,
@@ -39,26 +50,48 @@ from localstack.aws.api.sns import (
39
50
  TagKeyList,
40
51
  TagList,
41
52
  TagResourceResponse,
53
+ TooManyEntriesInBatchRequestException,
42
54
  TopicAttributesMap,
43
55
  UntagResourceResponse,
44
56
  attributeName,
45
57
  attributeValue,
46
58
  authenticateOnUnsubscribe,
47
59
  endpoint,
60
+ message,
61
+ messageStructure,
48
62
  nextToken,
49
63
  protocol,
64
+ subject,
50
65
  subscriptionARN,
51
66
  topicARN,
52
67
  topicName,
53
68
  )
54
- from localstack.services.sns import constants as sns_constants
69
+ from localstack.constants import AWS_REGION_US_EAST_1, DEFAULT_AWS_ACCOUNT_ID
70
+ from localstack.http import Response
71
+ from localstack.services.edge import ROUTER
72
+ from localstack.services.plugins import ServiceLifecycleHook
73
+ from localstack.services.sns.analytics import internal_api_calls
55
74
  from localstack.services.sns.certificate import SNS_SERVER_CERT
56
75
  from localstack.services.sns.constants import (
76
+ ATTR_TYPE_REGEX,
57
77
  DUMMY_SUBSCRIPTION_PRINCIPAL,
78
+ MAXIMUM_MESSAGE_LENGTH,
79
+ MSG_ATTR_NAME_REGEX,
80
+ PLATFORM_ENDPOINT_MSGS_ENDPOINT,
81
+ SMS_MSGS_ENDPOINT,
82
+ SNS_CERT_ENDPOINT,
83
+ SNS_PROTOCOLS,
84
+ SUBSCRIPTION_TOKENS_ENDPOINT,
58
85
  VALID_APPLICATION_PLATFORMS,
86
+ VALID_MSG_ATTR_NAME_CHARS,
87
+ VALID_SUBSCRIPTION_ATTR_NAME,
59
88
  )
60
89
  from localstack.services.sns.filter import FilterPolicyValidator
61
- from localstack.services.sns.publisher import PublishDispatcher, SnsPublishContext
90
+ from localstack.services.sns.publisher import (
91
+ PublishDispatcher,
92
+ SnsBatchPublishContext,
93
+ SnsPublishContext,
94
+ )
62
95
  from localstack.services.sns.v2.models import (
63
96
  SMS_ATTRIBUTE_NAMES,
64
97
  SMS_DEFAULT_SENDER_REGEX,
@@ -79,18 +112,22 @@ from localstack.services.sns.v2.utils import (
79
112
  encode_subscription_token_with_region,
80
113
  get_next_page_token_from_arn,
81
114
  get_region_from_subscription_token,
115
+ get_topic_subscriptions,
82
116
  is_valid_e164_number,
83
117
  parse_and_validate_platform_application_arn,
84
118
  parse_and_validate_topic_arn,
85
119
  validate_subscription_attribute,
86
120
  )
87
121
  from localstack.utils.aws.arns import (
122
+ extract_account_id_from_arn,
123
+ extract_region_from_arn,
88
124
  get_partition,
89
125
  parse_arn,
90
126
  sns_platform_application_arn,
91
127
  sns_topic_arn,
92
128
  )
93
129
  from localstack.utils.collections import PaginatedList, select_from_typed_dict
130
+ from localstack.utils.strings import to_bytes
94
131
 
95
132
  # set up logger
96
133
  LOG = logging.getLogger(__name__)
@@ -99,12 +136,27 @@ SNS_TOPIC_NAME_PATTERN_FIFO = r"^[a-zA-Z0-9_-]{1,256}\.fifo$"
99
136
  SNS_TOPIC_NAME_PATTERN = r"^[a-zA-Z0-9_-]{1,256}$"
100
137
 
101
138
 
102
- class SnsProvider(SnsApi):
139
+ class SnsProvider(SnsApi, ServiceLifecycleHook):
103
140
  def __init__(self) -> None:
104
141
  super().__init__()
105
142
  self._publisher = PublishDispatcher()
106
143
  self._signature_cert_pem: str = SNS_SERVER_CERT
107
144
 
145
+ def on_before_stop(self):
146
+ self._publisher.shutdown()
147
+
148
+ def on_after_init(self):
149
+ # Allow sent platform endpoint messages to be retrieved from the SNS endpoint
150
+ register_sns_api_resource(ROUTER)
151
+ # add the route to serve the certificate used to validate message signatures
152
+ ROUTER.add(self.get_signature_cert_pem_file)
153
+
154
+ @route(SNS_CERT_ENDPOINT, methods=["GET"])
155
+ def get_signature_cert_pem_file(self, request: Request):
156
+ # see http://sns-public-resources.s3.amazonaws.com/SNS_Message_Signing_Release_Note_Jan_25_2011.pdf
157
+ # see https://docs.aws.amazon.com/sns/latest/dg/sns-verify-signature-of-message.html
158
+ return Response(self._signature_cert_pem, 200)
159
+
108
160
  ## Topic Operations
109
161
 
110
162
  def create_topic(
@@ -135,7 +187,6 @@ class SnsProvider(SnsApi):
135
187
  )
136
188
  return CreateTopicResponse(TopicArn=topic_arn)
137
189
 
138
- attributes = attributes or {}
139
190
  if attributes.get("FifoTopic") and attributes["FifoTopic"].lower() == "true":
140
191
  fifo_match = re.match(SNS_TOPIC_NAME_PATTERN_FIFO, name)
141
192
  if not fifo_match:
@@ -219,14 +270,12 @@ class SnsProvider(SnsApi):
219
270
 
220
271
  store = self.get_store(account_id=parsed_topic_arn["account"], region=context.region)
221
272
 
222
- if topic_arn not in store.topics:
223
- raise NotFoundException("Topic does not exist")
224
-
225
- topic_subscriptions = store.topics[topic_arn]["subscriptions"]
273
+ topic = self._get_topic(arn=topic_arn, context=context)
274
+ topic_subscriptions = topic["subscriptions"]
226
275
  if not endpoint:
227
276
  # TODO: check AWS behaviour (because endpoint is optional)
228
277
  raise NotFoundException("Endpoint not specified in subscription")
229
- if protocol not in sns_constants.SNS_PROTOCOLS:
278
+ if protocol not in SNS_PROTOCOLS:
230
279
  raise InvalidParameterException(
231
280
  f"Invalid parameter: Amazon SNS does not support this protocol string: {protocol}"
232
281
  )
@@ -276,7 +325,7 @@ class SnsProvider(SnsApi):
276
325
  if sub.get("Endpoint") == endpoint:
277
326
  if sub_attributes:
278
327
  # validate the subscription attributes aren't different
279
- for attr in sns_constants.VALID_SUBSCRIPTION_ATTR_NAME:
328
+ for attr in VALID_SUBSCRIPTION_ATTR_NAME:
280
329
  # if a new attribute is present and different from an existent one, raise
281
330
  if (new_attr := sub_attributes.get(attr)) and sub.get(attr) != new_attr:
282
331
  raise InvalidParameterException(
@@ -338,8 +387,7 @@ class SnsProvider(SnsApi):
338
387
  message=message_ctx,
339
388
  store=store,
340
389
  request_headers=context.request.headers,
341
- # TODO: add topic attributes once they are ported from moto to LocalStack
342
- # topic_attributes=vars(self._get_topic(topic_arn, context)),
390
+ topic_attributes=topic["attributes"],
343
391
  )
344
392
  self._publisher.publish_to_topic_subscriber(
345
393
  ctx=publish_ctx,
@@ -539,11 +587,10 @@ class SnsProvider(SnsApi):
539
587
  def list_subscriptions_by_topic(
540
588
  self, context: RequestContext, topic_arn: topicARN, next_token: nextToken = None, **kwargs
541
589
  ) -> ListSubscriptionsByTopicResponse:
542
- topic: Topic = self._get_topic(topic_arn, context)
590
+ self._get_topic(topic_arn, context) # for validation purposes only
543
591
  parsed_topic_arn = parse_and_validate_topic_arn(topic_arn)
544
592
  store = self.get_store(parsed_topic_arn["account"], parsed_topic_arn["region"])
545
- sub_arns: list[str] = topic.get("subscriptions", [])
546
- subscriptions = [store.subscriptions[k] for k in sub_arns if k in store.subscriptions]
593
+ subscriptions = get_topic_subscriptions(store, topic_arn)
547
594
 
548
595
  paginated_subscriptions = PaginatedList(subscriptions)
549
596
  page, next_token = paginated_subscriptions.get_page(
@@ -557,6 +604,238 @@ class SnsProvider(SnsApi):
557
604
  response["NextToken"] = next_token
558
605
  return response
559
606
 
607
+ #
608
+ # Publish
609
+ #
610
+
611
+ def publish(
612
+ self,
613
+ context: RequestContext,
614
+ message: message,
615
+ topic_arn: topicARN | None = None,
616
+ target_arn: String | None = None,
617
+ phone_number: PhoneNumber | None = None,
618
+ subject: subject | None = None,
619
+ message_structure: messageStructure | None = None,
620
+ message_attributes: MessageAttributeMap | None = None,
621
+ message_deduplication_id: String | None = None,
622
+ message_group_id: String | None = None,
623
+ **kwargs,
624
+ ) -> PublishResponse:
625
+ if subject == "":
626
+ raise InvalidParameterException("Invalid parameter: Subject")
627
+ if not message or all(not m for m in message):
628
+ raise InvalidParameterException("Invalid parameter: Empty message")
629
+
630
+ # TODO: check for topic + target + phone number at the same time?
631
+ # TODO: more validation on phone, it might be opted out?
632
+ if phone_number and not is_valid_e164_number(phone_number):
633
+ raise InvalidParameterException(
634
+ f"Invalid parameter: PhoneNumber Reason: {phone_number} is not valid to publish to"
635
+ )
636
+
637
+ if message_attributes:
638
+ _validate_message_attributes(message_attributes)
639
+
640
+ if _get_total_publish_size(message, message_attributes) > MAXIMUM_MESSAGE_LENGTH:
641
+ raise InvalidParameterException("Invalid parameter: Message too long")
642
+
643
+ # for compatibility reasons, AWS allows users to use either TargetArn or TopicArn for publishing to a topic
644
+ # use any of them for topic validation
645
+ topic_or_target_arn = topic_arn or target_arn
646
+ topic = None
647
+
648
+ if is_fifo := (topic_or_target_arn and ".fifo" in topic_or_target_arn):
649
+ if not message_group_id:
650
+ raise InvalidParameterException(
651
+ "Invalid parameter: The MessageGroupId parameter is required for FIFO topics",
652
+ )
653
+ topic = self._get_topic(topic_or_target_arn, context)
654
+ if topic["attributes"]["ContentBasedDeduplication"] == "false":
655
+ if not message_deduplication_id:
656
+ raise InvalidParameterException(
657
+ "Invalid parameter: The topic should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly",
658
+ )
659
+ elif message_deduplication_id:
660
+ # this is the first one to raise if both are set while the topic is not fifo
661
+ raise InvalidParameterException(
662
+ "Invalid parameter: MessageDeduplicationId Reason: The request includes MessageDeduplicationId parameter that is not valid for this topic type"
663
+ )
664
+
665
+ is_endpoint_publish = target_arn and ":endpoint/" in target_arn
666
+ if message_structure == "json":
667
+ try:
668
+ message = json.loads(message)
669
+ # Keys in the JSON object that correspond to supported transport protocols must have
670
+ # simple JSON string values.
671
+ # Non-string values will cause the key to be ignored.
672
+ message = {key: field for key, field in message.items() if isinstance(field, str)}
673
+ # TODO: check no default key for direct TargetArn endpoint publish, need credentials
674
+ # see example: https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html
675
+ if "default" not in message and not is_endpoint_publish:
676
+ raise InvalidParameterException(
677
+ "Invalid parameter: Message Structure - No default entry in JSON message body"
678
+ )
679
+ except json.JSONDecodeError:
680
+ raise InvalidParameterException(
681
+ "Invalid parameter: Message Structure - JSON message body failed to parse"
682
+ )
683
+
684
+ if not phone_number:
685
+ # use the account to get the store from the TopicArn (you can only publish in the same region as the topic)
686
+ parsed_arn = parse_and_validate_topic_arn(topic_or_target_arn)
687
+ store = self.get_store(account_id=parsed_arn["account"], region=context.region)
688
+ if is_endpoint_publish:
689
+ if not (platform_endpoint := store.platform_endpoints.get(target_arn)):
690
+ raise InvalidParameterException(
691
+ "Invalid parameter: TargetArn Reason: No endpoint found for the target arn specified"
692
+ )
693
+ elif (
694
+ not platform_endpoint.platform_endpoint["Attributes"]
695
+ .get("Enabled", "false")
696
+ .lower()
697
+ == "true"
698
+ ):
699
+ raise EndpointDisabledException("Endpoint is disabled")
700
+ else:
701
+ topic = self._get_topic(topic_or_target_arn, context)
702
+ else:
703
+ # use the store from the request context
704
+ store = self.get_store(account_id=context.account_id, region=context.region)
705
+
706
+ message_ctx = SnsMessage(
707
+ type=SnsMessageType.Notification,
708
+ message=message,
709
+ message_attributes=message_attributes,
710
+ message_deduplication_id=message_deduplication_id,
711
+ message_group_id=message_group_id,
712
+ message_structure=message_structure,
713
+ subject=subject,
714
+ is_fifo=is_fifo,
715
+ )
716
+ publish_ctx = SnsPublishContext(
717
+ message=message_ctx, store=store, request_headers=context.request.headers
718
+ )
719
+
720
+ if is_endpoint_publish:
721
+ self._publisher.publish_to_application_endpoint(
722
+ ctx=publish_ctx, endpoint_arn=target_arn
723
+ )
724
+ elif phone_number:
725
+ self._publisher.publish_to_phone_number(ctx=publish_ctx, phone_number=phone_number)
726
+ else:
727
+ # beware if the subscription is FIFO, the order might not be guaranteed.
728
+ # 2 quick call to this method in succession might not be executed in order in the executor?
729
+ # TODO: test how this behaves in a FIFO context with a lot of threads.
730
+ publish_ctx.topic_attributes |= topic["attributes"]
731
+ self._publisher.publish_to_topic(publish_ctx, topic_or_target_arn)
732
+
733
+ if is_fifo:
734
+ return PublishResponse(
735
+ MessageId=message_ctx.message_id, SequenceNumber=message_ctx.sequencer_number
736
+ )
737
+
738
+ return PublishResponse(MessageId=message_ctx.message_id)
739
+
740
+ def publish_batch(
741
+ self,
742
+ context: RequestContext,
743
+ topic_arn: topicARN,
744
+ publish_batch_request_entries: PublishBatchRequestEntryList,
745
+ **kwargs,
746
+ ) -> PublishBatchResponse:
747
+ if len(publish_batch_request_entries) > 10:
748
+ raise TooManyEntriesInBatchRequestException(
749
+ "The batch request contains more entries than permissible."
750
+ )
751
+
752
+ parsed_arn = parse_and_validate_topic_arn(topic_arn)
753
+ store = self.get_store(account_id=parsed_arn["account"], region=context.region)
754
+ topic = self._get_topic(topic_arn, context)
755
+ ids = [entry["Id"] for entry in publish_batch_request_entries]
756
+ if len(set(ids)) != len(publish_batch_request_entries):
757
+ raise BatchEntryIdsNotDistinctException(
758
+ "Two or more batch entries in the request have the same Id."
759
+ )
760
+
761
+ response: PublishBatchResponse = {"Successful": [], "Failed": []}
762
+
763
+ # TODO: write AWS validated tests with FilterPolicy and batching
764
+ # TODO: find a scenario where we can fail to send a message synchronously to be able to report it
765
+ # right now, it seems that AWS fails the whole publish if something is wrong in the format of 1 message
766
+
767
+ total_batch_size = 0
768
+ message_contexts = []
769
+ for entry_index, entry in enumerate(publish_batch_request_entries, start=1):
770
+ message_payload = entry.get("Message")
771
+ message_attributes = entry.get("MessageAttributes", {})
772
+ if message_attributes:
773
+ # if a message contains non-valid message attributes, it
774
+ # will fail for the first non-valid message encountered, and raise ParameterValueInvalid
775
+ _validate_message_attributes(message_attributes, position=entry_index)
776
+
777
+ total_batch_size += _get_total_publish_size(message_payload, message_attributes)
778
+
779
+ # TODO: WRITE AWS VALIDATED
780
+ if entry.get("MessageStructure") == "json":
781
+ try:
782
+ message = json.loads(message_payload)
783
+ # Keys in the JSON object that correspond to supported transport protocols must have
784
+ # simple JSON string values.
785
+ # Non-string values will cause the key to be ignored.
786
+ message = {
787
+ key: field for key, field in message.items() if isinstance(field, str)
788
+ }
789
+ if "default" not in message:
790
+ raise InvalidParameterException(
791
+ "Invalid parameter: Message Structure - No default entry in JSON message body"
792
+ )
793
+ entry["Message"] = message # noqa
794
+ except json.JSONDecodeError:
795
+ raise InvalidParameterException(
796
+ "Invalid parameter: Message Structure - JSON message body failed to parse"
797
+ )
798
+
799
+ if is_fifo := (topic_arn.endswith(".fifo")):
800
+ if not all("MessageGroupId" in entry for entry in publish_batch_request_entries):
801
+ raise InvalidParameterException(
802
+ "Invalid parameter: The MessageGroupId parameter is required for FIFO topics"
803
+ )
804
+ if topic["attributes"]["ContentBasedDeduplication"] == "false":
805
+ if not all(
806
+ "MessageDeduplicationId" in entry for entry in publish_batch_request_entries
807
+ ):
808
+ raise InvalidParameterException(
809
+ "Invalid parameter: The topic should either have ContentBasedDeduplication enabled or MessageDeduplicationId provided explicitly",
810
+ )
811
+
812
+ msg_ctx = SnsMessage.from_batch_entry(entry, is_fifo=is_fifo)
813
+ message_contexts.append(msg_ctx)
814
+ success = PublishBatchResultEntry(
815
+ Id=entry["Id"],
816
+ MessageId=msg_ctx.message_id,
817
+ )
818
+ if is_fifo:
819
+ success["SequenceNumber"] = msg_ctx.sequencer_number
820
+ response["Successful"].append(success)
821
+
822
+ if total_batch_size > MAXIMUM_MESSAGE_LENGTH:
823
+ raise CommonServiceException(
824
+ code="BatchRequestTooLong",
825
+ message="The length of all the messages put together is more than the limit.",
826
+ sender_fault=True,
827
+ )
828
+
829
+ publish_ctx = SnsBatchPublishContext(
830
+ messages=message_contexts,
831
+ store=store,
832
+ request_headers=context.request.headers,
833
+ topic_attributes=topic["attributes"],
834
+ )
835
+ self._publisher.publish_batch_to_topic(publish_ctx, topic_arn)
836
+
837
+ return response
838
+
560
839
  #
561
840
  # PlatformApplications
562
841
  #
@@ -826,16 +1105,15 @@ class SnsProvider(SnsApi):
826
1105
  def get_store(account_id: str, region: str) -> SnsStore:
827
1106
  return sns_stores[account_id][region]
828
1107
 
829
- # TODO: reintroduce multi-region parameter (latest before final migration from v1)
830
1108
  @staticmethod
831
- def _get_topic(arn: str, context: RequestContext) -> Topic:
1109
+ def _get_topic(arn: str, context: RequestContext, multi_region: bool = False) -> Topic:
832
1110
  """
833
1111
  :param arn: the Topic ARN
834
1112
  :param context: the RequestContext of the request
835
1113
  :return: the model Topic
836
1114
  """
837
1115
  arn_data = parse_and_validate_topic_arn(arn)
838
- if context.region != arn_data["region"]:
1116
+ if not multi_region and context.region != arn_data["region"]:
839
1117
  raise InvalidParameterException("Invalid parameter: TopicArn")
840
1118
  try:
841
1119
  store = SnsProvider.get_store(context.account_id, context.region)
@@ -887,7 +1165,6 @@ def _default_attributes(topic: Topic, context: RequestContext) -> TopicAttribute
887
1165
  {
888
1166
  "ContentBasedDeduplication": "false",
889
1167
  "FifoTopic": "false",
890
- "SignatureVersion": "2",
891
1168
  }
892
1169
  )
893
1170
  return default_attributes
@@ -921,6 +1198,94 @@ def _create_default_topic_policy(topic: Topic, context: RequestContext) -> str:
921
1198
  )
922
1199
 
923
1200
 
1201
+ def _validate_message_attributes(
1202
+ message_attributes: MessageAttributeMap, position: int | None = None
1203
+ ) -> None:
1204
+ """
1205
+ Validate the message attributes, and raises an exception if those do not follow AWS validation
1206
+ See: https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html
1207
+ Regex from: https://stackoverflow.com/questions/40718851/regex-that-does-not-allow-consecutive-dots
1208
+ :param message_attributes: the message attributes map for the message
1209
+ :param position: given to give the Batch Entry position if coming from `publishBatch`
1210
+ :raises: InvalidParameterValueException
1211
+ :return: None
1212
+ """
1213
+ for attr_name, attr in message_attributes.items():
1214
+ if len(attr_name) > 256:
1215
+ raise InvalidParameterValueException(
1216
+ "Length of message attribute name must be less than 256 bytes."
1217
+ )
1218
+ _validate_message_attribute_name(attr_name)
1219
+ # `DataType` is a required field for MessageAttributeValue
1220
+ if (data_type := attr.get("DataType")) is None:
1221
+ if position:
1222
+ at = f"publishBatchRequestEntries.{position}.member.messageAttributes.{attr_name}.member.dataType"
1223
+ else:
1224
+ at = f"messageAttributes.{attr_name}.member.dataType"
1225
+
1226
+ raise CommonServiceException(
1227
+ code="ValidationError",
1228
+ message=f"1 validation error detected: Value null at '{at}' failed to satisfy constraint: Member must not be null",
1229
+ sender_fault=True,
1230
+ )
1231
+
1232
+ if data_type not in (
1233
+ "String",
1234
+ "Number",
1235
+ "Binary",
1236
+ ) and not ATTR_TYPE_REGEX.match(data_type):
1237
+ raise InvalidParameterValueException(
1238
+ f"The message attribute '{attr_name}' has an invalid message attribute type, the set of supported type prefixes is Binary, Number, and String."
1239
+ )
1240
+ if not any(attr_value.endswith("Value") for attr_value in attr):
1241
+ raise InvalidParameterValueException(
1242
+ f"The message attribute '{attr_name}' must contain non-empty message attribute value for message attribute type '{data_type}'."
1243
+ )
1244
+
1245
+ value_key_data_type = "Binary" if data_type.startswith("Binary") else "String"
1246
+ value_key = f"{value_key_data_type}Value"
1247
+ if value_key not in attr:
1248
+ raise InvalidParameterValueException(
1249
+ f"The message attribute '{attr_name}' with type '{data_type}' must use field '{value_key_data_type}'."
1250
+ )
1251
+ elif not attr[value_key]:
1252
+ raise InvalidParameterValueException(
1253
+ f"The message attribute '{attr_name}' must contain non-empty message attribute value for message attribute type '{data_type}'.",
1254
+ )
1255
+
1256
+
1257
+ def _validate_message_attribute_name(name: str) -> None:
1258
+ """
1259
+ Validate the message attribute name with the specification of AWS.
1260
+ The message attribute name can contain the following characters: A-Z, a-z, 0-9, underscore(_), hyphen(-), and period (.). The name must not start or end with a period, and it should not have successive periods.
1261
+ :param name: message attribute name
1262
+ :raises InvalidParameterValueException: if the name does not conform to the spec
1263
+ """
1264
+ if not MSG_ATTR_NAME_REGEX.match(name):
1265
+ # find the proper exception
1266
+ if name[0] == ".":
1267
+ raise InvalidParameterValueException(
1268
+ "Invalid message attribute name starting with character '.' was found."
1269
+ )
1270
+ elif name[-1] == ".":
1271
+ raise InvalidParameterValueException(
1272
+ "Invalid message attribute name ending with character '.' was found."
1273
+ )
1274
+
1275
+ for idx, char in enumerate(name):
1276
+ if char not in VALID_MSG_ATTR_NAME_CHARS:
1277
+ # change prefix from 0x to #x, without capitalizing the x
1278
+ hex_char = "#x" + hex(ord(char)).upper()[2:]
1279
+ raise InvalidParameterValueException(
1280
+ f"Invalid non-alphanumeric character '{hex_char}' was found in the message attribute name. Can only include alphanumeric characters, hyphens, underscores, or dots."
1281
+ )
1282
+ # even if we go negative index, it will be covered by starting/ending with dot
1283
+ if char == "." and name[idx - 1] == ".":
1284
+ raise InvalidParameterValueException(
1285
+ "Message attribute name can not have successive '.' character."
1286
+ )
1287
+
1288
+
924
1289
  def _validate_platform_application_name(name: str) -> None:
925
1290
  reason = ""
926
1291
  if not name:
@@ -997,3 +1362,269 @@ def _check_matching_tags(topic_arn: str, tags: TagList | None, store: SnsStore)
997
1362
  if existing_tags is not None and tag not in existing_tags:
998
1363
  return False
999
1364
  return True
1365
+
1366
+
1367
+ def _get_total_publish_size(
1368
+ message_body: str, message_attributes: MessageAttributeMap | None
1369
+ ) -> int:
1370
+ size = _get_byte_size(message_body)
1371
+ if message_attributes:
1372
+ # https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html
1373
+ # All parts of the message attribute, including name, type, and value, are included in the message size
1374
+ # restriction, which is 256 KB.
1375
+ # iterate over the Keys and Attributes, adding the length of the Key to the length of all Attributes values
1376
+ # (DataType and StringValue or BinaryValue)
1377
+ size += sum(
1378
+ _get_byte_size(key) + sum(_get_byte_size(attr_value) for attr_value in attr.values())
1379
+ for key, attr in message_attributes.items()
1380
+ )
1381
+
1382
+ return size
1383
+
1384
+
1385
+ def _get_byte_size(payload: str | bytes) -> int:
1386
+ # Calculate the real length of the byte object if the object is a string
1387
+ return len(to_bytes(payload))
1388
+
1389
+
1390
+ def _register_sns_api_resource(router: Router):
1391
+ """Register the retrospection endpoints as internal LocalStack endpoints."""
1392
+ router.add(SNSServicePlatformEndpointMessagesApiResource())
1393
+ router.add(SNSServiceSMSMessagesApiResource())
1394
+ router.add(SNSServiceSubscriptionTokenApiResource())
1395
+
1396
+
1397
+ class SNSInternalResource:
1398
+ resource_type: str
1399
+ """Base class with helper to properly track usage of internal endpoints"""
1400
+
1401
+ def count_usage(self):
1402
+ internal_api_calls.labels(resource_type=self.resource_type).increment()
1403
+
1404
+
1405
+ def count_usage(f):
1406
+ @functools.wraps(f)
1407
+ def _wrapper(self, *args, **kwargs):
1408
+ self.count_usage()
1409
+ return f(self, *args, **kwargs)
1410
+
1411
+ return _wrapper
1412
+
1413
+
1414
+ class SNSServicePlatformEndpointMessagesApiResource(SNSInternalResource):
1415
+ resource_type = "platform-endpoint-message"
1416
+ """Provides a REST API for retrospective access to platform endpoint messages sent via SNS.
1417
+
1418
+ This is registered as a LocalStack internal HTTP resource.
1419
+
1420
+ This endpoint accepts:
1421
+ - GET param `accountId`: selector for AWS account. If not specified, return fallback `000000000000` test ID
1422
+ - GET param `region`: selector for AWS `region`. If not specified, return default "us-east-1"
1423
+ - GET param `endpointArn`: filter for `endpointArn` resource in SNS
1424
+ - DELETE param `accountId`: selector for AWS account
1425
+ - DELETE param `region`: will delete saved messages for `region`
1426
+ - DELETE param `endpointArn`: will delete saved messages for `endpointArn`
1427
+ """
1428
+
1429
+ _PAYLOAD_FIELDS = [
1430
+ "TargetArn",
1431
+ "TopicArn",
1432
+ "Message",
1433
+ "MessageAttributes",
1434
+ "MessageStructure",
1435
+ "Subject",
1436
+ "MessageId",
1437
+ ]
1438
+
1439
+ @route(PLATFORM_ENDPOINT_MSGS_ENDPOINT, methods=["GET"])
1440
+ @count_usage
1441
+ def on_get(self, request: Request):
1442
+ filter_endpoint_arn = request.args.get("endpointArn")
1443
+ account_id = (
1444
+ request.args.get("accountId", DEFAULT_AWS_ACCOUNT_ID)
1445
+ if not filter_endpoint_arn
1446
+ else extract_account_id_from_arn(filter_endpoint_arn)
1447
+ )
1448
+ region = (
1449
+ request.args.get("region", AWS_REGION_US_EAST_1)
1450
+ if not filter_endpoint_arn
1451
+ else extract_region_from_arn(filter_endpoint_arn)
1452
+ )
1453
+ store: SnsStore = sns_stores[account_id][region]
1454
+ if filter_endpoint_arn:
1455
+ messages = store.platform_endpoint_messages.get(filter_endpoint_arn, [])
1456
+ messages = _format_messages(messages, self._PAYLOAD_FIELDS)
1457
+ return {
1458
+ "platform_endpoint_messages": {filter_endpoint_arn: messages},
1459
+ "region": region,
1460
+ }
1461
+
1462
+ platform_endpoint_messages = {
1463
+ endpoint_arn: _format_messages(messages, self._PAYLOAD_FIELDS)
1464
+ for endpoint_arn, messages in store.platform_endpoint_messages.items()
1465
+ }
1466
+ return {
1467
+ "platform_endpoint_messages": platform_endpoint_messages,
1468
+ "region": region,
1469
+ }
1470
+
1471
+ @route(PLATFORM_ENDPOINT_MSGS_ENDPOINT, methods=["DELETE"])
1472
+ @count_usage
1473
+ def on_delete(self, request: Request) -> Response:
1474
+ filter_endpoint_arn = request.args.get("endpointArn")
1475
+ account_id = (
1476
+ request.args.get("accountId", DEFAULT_AWS_ACCOUNT_ID)
1477
+ if not filter_endpoint_arn
1478
+ else extract_account_id_from_arn(filter_endpoint_arn)
1479
+ )
1480
+ region = (
1481
+ request.args.get("region", AWS_REGION_US_EAST_1)
1482
+ if not filter_endpoint_arn
1483
+ else extract_region_from_arn(filter_endpoint_arn)
1484
+ )
1485
+ store: SnsStore = sns_stores[account_id][region]
1486
+ if filter_endpoint_arn:
1487
+ store.platform_endpoint_messages.pop(filter_endpoint_arn, None)
1488
+ return Response("", status=204)
1489
+
1490
+ store.platform_endpoint_messages.clear()
1491
+ return Response("", status=204)
1492
+
1493
+
1494
+ def register_sns_api_resource(router: Router):
1495
+ """Register the retrospection endpoints as internal LocalStack endpoints."""
1496
+ router.add(SNSServicePlatformEndpointMessagesApiResource())
1497
+ router.add(SNSServiceSMSMessagesApiResource())
1498
+ router.add(SNSServiceSubscriptionTokenApiResource())
1499
+
1500
+
1501
+ def _format_messages(sent_messages: list[dict[str, str]], validated_keys: list[str]):
1502
+ """
1503
+ This method format the messages to be more readable and undo the format change that was needed for Moto
1504
+ Should be removed once we refactor SNS.
1505
+ """
1506
+ formatted_messages = []
1507
+ for sent_message in sent_messages:
1508
+ msg = {
1509
+ key: json.dumps(value)
1510
+ if key == "Message" and sent_message.get("MessageStructure") == "json"
1511
+ else value
1512
+ for key, value in sent_message.items()
1513
+ if key in validated_keys
1514
+ }
1515
+ formatted_messages.append(msg)
1516
+
1517
+ return formatted_messages
1518
+
1519
+
1520
+ class SNSServiceSMSMessagesApiResource(SNSInternalResource):
1521
+ resource_type = "sms-message"
1522
+ """Provides a REST API for retrospective access to SMS messages sent via SNS.
1523
+
1524
+ This is registered as a LocalStack internal HTTP resource.
1525
+
1526
+ This endpoint accepts:
1527
+ - GET param `accountId`: selector for AWS account. If not specified, return fallback `000000000000` test ID
1528
+ - GET param `region`: selector for AWS `region`. If not specified, return default "us-east-1"
1529
+ - GET param `phoneNumber`: filter for `phoneNumber` resource in SNS
1530
+ """
1531
+
1532
+ _PAYLOAD_FIELDS = [
1533
+ "PhoneNumber",
1534
+ "TopicArn",
1535
+ "SubscriptionArn",
1536
+ "MessageId",
1537
+ "Message",
1538
+ "MessageAttributes",
1539
+ "MessageStructure",
1540
+ "Subject",
1541
+ ]
1542
+
1543
+ @route(SMS_MSGS_ENDPOINT, methods=["GET"])
1544
+ @count_usage
1545
+ def on_get(self, request: Request):
1546
+ account_id = request.args.get("accountId", DEFAULT_AWS_ACCOUNT_ID)
1547
+ region = request.args.get("region", AWS_REGION_US_EAST_1)
1548
+ filter_phone_number = request.args.get("phoneNumber")
1549
+ store: SnsStore = sns_stores[account_id][region]
1550
+ if filter_phone_number:
1551
+ messages = [
1552
+ m for m in store.sms_messages if m.get("PhoneNumber") == filter_phone_number
1553
+ ]
1554
+ messages = _format_messages(messages, self._PAYLOAD_FIELDS)
1555
+ return {
1556
+ "sms_messages": {filter_phone_number: messages},
1557
+ "region": region,
1558
+ }
1559
+
1560
+ sms_messages = {}
1561
+
1562
+ for m in _format_messages(store.sms_messages, self._PAYLOAD_FIELDS):
1563
+ sms_messages.setdefault(m.get("PhoneNumber"), []).append(m)
1564
+
1565
+ return {
1566
+ "sms_messages": sms_messages,
1567
+ "region": region,
1568
+ }
1569
+
1570
+ @route(SMS_MSGS_ENDPOINT, methods=["DELETE"])
1571
+ @count_usage
1572
+ def on_delete(self, request: Request) -> Response:
1573
+ account_id = request.args.get("accountId", DEFAULT_AWS_ACCOUNT_ID)
1574
+ region = request.args.get("region", AWS_REGION_US_EAST_1)
1575
+ filter_phone_number = request.args.get("phoneNumber")
1576
+ store: SnsStore = sns_stores[account_id][region]
1577
+ if filter_phone_number:
1578
+ store.sms_messages = [
1579
+ m for m in store.sms_messages if m.get("PhoneNumber") != filter_phone_number
1580
+ ]
1581
+ return Response("", status=204)
1582
+
1583
+ store.sms_messages.clear()
1584
+ return Response("", status=204)
1585
+
1586
+
1587
+ class SNSServiceSubscriptionTokenApiResource(SNSInternalResource):
1588
+ resource_type = "subscription-token"
1589
+ """Provides a REST API for retrospective access to Subscription Confirmation Tokens to confirm subscriptions.
1590
+ Those are not sent for email, and sometimes inaccessible when working with external HTTPS endpoint which won't be
1591
+ able to reach your local host.
1592
+
1593
+ This is registered as a LocalStack internal HTTP resource.
1594
+
1595
+ This endpoint has the following parameter:
1596
+ - GET `subscription_arn`: `subscriptionArn`resource in SNS for which you want the SubscriptionToken
1597
+ """
1598
+
1599
+ @route(f"{SUBSCRIPTION_TOKENS_ENDPOINT}/<path:subscription_arn>", methods=["GET"])
1600
+ @count_usage
1601
+ def on_get(self, _request: Request, subscription_arn: str):
1602
+ try:
1603
+ parsed_arn = parse_arn(subscription_arn)
1604
+ except InvalidArnException:
1605
+ response = Response("", 400)
1606
+ response.set_json(
1607
+ {
1608
+ "error": "The provided SubscriptionARN is invalid",
1609
+ "subscription_arn": subscription_arn,
1610
+ }
1611
+ )
1612
+ return response
1613
+
1614
+ store: SnsStore = sns_stores[parsed_arn["account"]][parsed_arn["region"]]
1615
+
1616
+ for token, sub_arn in store.subscription_tokens.items():
1617
+ if sub_arn == subscription_arn:
1618
+ return {
1619
+ "subscription_token": token,
1620
+ "subscription_arn": subscription_arn,
1621
+ }
1622
+
1623
+ response = Response("", 404)
1624
+ response.set_json(
1625
+ {
1626
+ "error": "The provided SubscriptionARN is not found",
1627
+ "subscription_arn": subscription_arn,
1628
+ }
1629
+ )
1630
+ return response