localstack-core 4.7.1.dev139__py3-none-any.whl → 4.10.1.dev7__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 (173) hide show
  1. localstack/aws/api/cloudformation/__init__.py +1 -0
  2. localstack/aws/api/cloudwatch/__init__.py +41 -1
  3. localstack/aws/api/config/__init__.py +4 -0
  4. localstack/aws/api/core.py +4 -0
  5. localstack/aws/api/ec2/__init__.py +1113 -56
  6. localstack/aws/api/iam/__init__.py +7 -0
  7. localstack/aws/api/kinesis/__init__.py +19 -0
  8. localstack/aws/api/kms/__init__.py +6 -0
  9. localstack/aws/api/lambda_/__init__.py +13 -0
  10. localstack/aws/api/logs/__init__.py +15 -0
  11. localstack/aws/api/redshift/__init__.py +9 -3
  12. localstack/aws/api/route53/__init__.py +2 -0
  13. localstack/aws/api/s3/__init__.py +12 -0
  14. localstack/aws/api/s3control/__init__.py +32 -0
  15. localstack/aws/api/ssm/__init__.py +2 -0
  16. localstack/aws/client.py +7 -2
  17. localstack/aws/forwarder.py +52 -5
  18. localstack/aws/handlers/analytics.py +1 -1
  19. localstack/aws/handlers/logging.py +12 -2
  20. localstack/aws/handlers/metric_handler.py +41 -1
  21. localstack/aws/handlers/service.py +32 -9
  22. localstack/aws/protocol/parser.py +440 -21
  23. localstack/aws/protocol/serializer.py +684 -64
  24. localstack/aws/protocol/service_router.py +120 -20
  25. localstack/aws/skeleton.py +4 -2
  26. localstack/aws/spec-patches.json +58 -0
  27. localstack/aws/spec.py +33 -13
  28. localstack/cli/exceptions.py +1 -1
  29. localstack/cli/localstack.py +4 -4
  30. localstack/cli/lpm.py +3 -4
  31. localstack/cli/profiles.py +1 -2
  32. localstack/config.py +18 -12
  33. localstack/constants.py +4 -29
  34. localstack/dev/kubernetes/__main__.py +1 -1
  35. localstack/dev/run/paths.py +1 -1
  36. localstack/dns/plugins.py +5 -1
  37. localstack/dns/server.py +12 -3
  38. localstack/packages/api.py +9 -8
  39. localstack/packages/core.py +2 -2
  40. localstack/packages/plugins.py +0 -8
  41. localstack/runtime/init.py +1 -1
  42. localstack/services/apigateway/legacy/provider.py +53 -3
  43. localstack/services/apigateway/next_gen/execute_api/integrations/aws.py +3 -0
  44. localstack/services/apigateway/next_gen/execute_api/integrations/http.py +3 -3
  45. localstack/services/apigateway/next_gen/execute_api/test_invoke.py +50 -6
  46. localstack/services/apigateway/next_gen/provider.py +5 -0
  47. localstack/services/cloudformation/engine/entities.py +12 -1
  48. localstack/services/cloudformation/engine/v2/change_set_model.py +0 -3
  49. localstack/services/cloudformation/engine/v2/change_set_model_describer.py +14 -0
  50. localstack/services/cloudformation/engine/v2/change_set_model_executor.py +13 -15
  51. localstack/services/cloudformation/engine/v2/change_set_model_preproc.py +118 -24
  52. localstack/services/cloudformation/engine/v2/change_set_model_transform.py +4 -1
  53. localstack/services/cloudformation/engine/v2/change_set_model_validator.py +5 -14
  54. localstack/services/cloudformation/engine/v2/change_set_model_visitor.py +1 -0
  55. localstack/services/cloudformation/engine/v2/resolving.py +6 -4
  56. localstack/services/cloudformation/engine/yaml_parser.py +9 -2
  57. localstack/services/cloudformation/resource_provider.py +5 -1
  58. localstack/services/cloudformation/resources.py +24149 -0
  59. localstack/services/cloudformation/v2/entities.py +6 -3
  60. localstack/services/cloudformation/v2/provider.py +172 -27
  61. localstack/services/cloudformation/v2/types.py +8 -4
  62. localstack/services/cloudwatch/provider_v2.py +25 -28
  63. localstack/services/dynamodb/packages.py +2 -1
  64. localstack/services/dynamodb/provider.py +42 -0
  65. localstack/services/dynamodb/v2/provider.py +42 -0
  66. localstack/services/ecr/resource_providers/aws_ecr_repository.py +5 -2
  67. localstack/services/es/provider.py +2 -2
  68. localstack/services/events/event_rule_engine.py +31 -13
  69. localstack/services/events/models.py +4 -5
  70. localstack/services/events/target.py +17 -9
  71. localstack/services/iam/provider.py +11 -116
  72. localstack/services/iam/resources/policy_simulator.py +133 -0
  73. localstack/services/kinesis/models.py +15 -2
  74. localstack/services/kinesis/provider.py +77 -0
  75. localstack/services/kms/provider.py +14 -5
  76. localstack/services/lambda_/invocation/internal_sqs_queue.py +5 -9
  77. localstack/services/lambda_/packages.py +1 -1
  78. localstack/services/logs/provider.py +1 -1
  79. localstack/services/moto.py +2 -1
  80. localstack/services/opensearch/cluster.py +15 -7
  81. localstack/services/opensearch/packages.py +26 -7
  82. localstack/services/opensearch/provider.py +6 -1
  83. localstack/services/opensearch/versions.py +56 -7
  84. localstack/services/s3/constants.py +5 -2
  85. localstack/services/s3/cors.py +4 -4
  86. localstack/services/s3/notifications.py +1 -1
  87. localstack/services/s3/presigned_url.py +27 -43
  88. localstack/services/s3/provider.py +67 -11
  89. localstack/services/s3/utils.py +42 -11
  90. localstack/services/ses/provider.py +16 -7
  91. localstack/services/sns/constants.py +7 -1
  92. localstack/services/sns/v2/models.py +167 -0
  93. localstack/services/sns/v2/provider.py +860 -2
  94. localstack/services/sns/v2/utils.py +130 -0
  95. localstack/services/sqs/developer_api.py +205 -0
  96. localstack/services/sqs/models.py +42 -3
  97. localstack/services/sqs/provider.py +8 -309
  98. localstack/services/sqs/query_api.py +1 -1
  99. localstack/services/sqs/utils.py +121 -2
  100. localstack/services/stepfunctions/asl/jsonata/jsonata.py +1 -1
  101. localstack/testing/aws/cloudformation_utils.py +1 -1
  102. localstack/testing/pytest/cloudformation/fixtures.py +3 -3
  103. localstack/testing/pytest/container.py +4 -5
  104. localstack/testing/pytest/fixtures.py +20 -19
  105. localstack/testing/pytest/in_memory_localstack.py +0 -4
  106. localstack/testing/pytest/marking.py +13 -4
  107. localstack/testing/pytest/stepfunctions/utils.py +4 -3
  108. localstack/testing/pytest/util.py +1 -1
  109. localstack/testing/pytest/validation_tracking.py +1 -2
  110. localstack/testing/snapshots/transformer_utility.py +5 -0
  111. localstack/utils/analytics/events.py +2 -2
  112. localstack/utils/analytics/metadata.py +1 -2
  113. localstack/utils/analytics/metrics/counter.py +6 -8
  114. localstack/utils/analytics/publisher.py +1 -2
  115. localstack/utils/analytics/service_request_aggregator.py +2 -2
  116. localstack/utils/archives.py +11 -11
  117. localstack/utils/aws/arns.py +17 -9
  118. localstack/utils/aws/aws_responses.py +7 -7
  119. localstack/utils/aws/aws_stack.py +2 -3
  120. localstack/utils/aws/message_forwarding.py +1 -2
  121. localstack/utils/aws/request_context.py +4 -5
  122. localstack/utils/batch_policy.py +3 -3
  123. localstack/utils/bootstrap.py +7 -7
  124. localstack/utils/catalog/catalog.py +139 -0
  125. localstack/utils/catalog/catalog_loader.py +11 -0
  126. localstack/utils/catalog/common.py +58 -0
  127. localstack/utils/catalog/plugins.py +28 -0
  128. localstack/utils/cloudwatch/cloudwatch_util.py +5 -5
  129. localstack/utils/collections.py +7 -8
  130. localstack/utils/config_listener.py +1 -1
  131. localstack/utils/container_networking.py +2 -3
  132. localstack/utils/container_utils/container_client.py +115 -131
  133. localstack/utils/container_utils/docker_cmd_client.py +42 -42
  134. localstack/utils/container_utils/docker_sdk_client.py +63 -62
  135. localstack/utils/diagnose.py +2 -3
  136. localstack/utils/docker_utils.py +3 -4
  137. localstack/utils/files.py +31 -7
  138. localstack/utils/functions.py +3 -2
  139. localstack/utils/http.py +4 -5
  140. localstack/utils/json.py +19 -5
  141. localstack/utils/kinesis/kinesis_connector.py +2 -1
  142. localstack/utils/net.py +6 -6
  143. localstack/utils/no_exit_argument_parser.py +2 -2
  144. localstack/utils/numbers.py +9 -2
  145. localstack/utils/objects.py +6 -5
  146. localstack/utils/patch.py +2 -1
  147. localstack/utils/run.py +10 -9
  148. localstack/utils/scheduler.py +11 -11
  149. localstack/utils/server/tcp_proxy.py +2 -2
  150. localstack/utils/serving.py +2 -3
  151. localstack/utils/strings.py +10 -11
  152. localstack/utils/sync.py +126 -1
  153. localstack/utils/tagging.py +1 -4
  154. localstack/utils/testutil.py +5 -4
  155. localstack/utils/threads.py +2 -2
  156. localstack/utils/time.py +11 -3
  157. localstack/utils/urls.py +1 -3
  158. localstack/version.py +2 -2
  159. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/METADATA +17 -12
  160. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/RECORD +168 -164
  161. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/entry_points.txt +4 -2
  162. localstack_core-4.10.1.dev7.dist-info/plux.json +1 -0
  163. localstack/packages/terraform.py +0 -46
  164. localstack/services/cloudformation/deploy.html +0 -144
  165. localstack/services/cloudformation/deploy_ui.py +0 -47
  166. localstack/services/cloudformation/plugins.py +0 -12
  167. localstack_core-4.7.1.dev139.dist-info/plux.json +0 -1
  168. {localstack_core-4.7.1.dev139.data → localstack_core-4.10.1.dev7.data}/scripts/localstack +0 -0
  169. {localstack_core-4.7.1.dev139.data → localstack_core-4.10.1.dev7.data}/scripts/localstack-supervisor +0 -0
  170. {localstack_core-4.7.1.dev139.data → localstack_core-4.10.1.dev7.data}/scripts/localstack.bat +0 -0
  171. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/WHEEL +0 -0
  172. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/licenses/LICENSE.txt +0 -0
  173. {localstack_core-4.7.1.dev139.dist-info → localstack_core-4.10.1.dev7.dist-info}/top_level.txt +0 -0
@@ -2,6 +2,7 @@
2
2
 
3
3
  import logging
4
4
  import traceback
5
+ import types
5
6
  from collections import defaultdict
6
7
  from typing import Any
7
8
 
@@ -9,15 +10,15 @@ from botocore.model import OperationModel, ServiceModel
9
10
 
10
11
  from localstack import config
11
12
  from localstack.http import Response
12
- from localstack.utils.coverage_docs import get_coverage_link_for_service
13
13
 
14
+ from ...utils.coverage_docs import get_coverage_link_for_service
14
15
  from ..api import CommonServiceException, RequestContext, ServiceException
15
16
  from ..api.core import ServiceOperation
16
17
  from ..chain import CompositeResponseHandler, ExceptionHandler, Handler, HandlerChain
17
18
  from ..client import parse_response, parse_service_exception
18
19
  from ..protocol.parser import RequestParser, create_parser
19
20
  from ..protocol.serializer import create_serializer
20
- from ..protocol.service_router import determine_aws_service_model
21
+ from ..protocol.service_router import determine_aws_protocol, determine_aws_service_model
21
22
  from ..skeleton import Skeleton, create_skeleton
22
23
 
23
24
  LOG = logging.getLogger(__name__)
@@ -33,6 +34,8 @@ class ServiceNameParser(Handler):
33
34
  # example). If it is already set, we can skip the parsing of the request. It is very important for S3, because
34
35
  # parsing the request will consume the data stream and prevent streaming.
35
36
  if context.service:
37
+ if not context.protocol:
38
+ context.protocol = determine_aws_protocol(context.request, context.service)
36
39
  return
37
40
 
38
41
  service_model = determine_aws_service_model(context.request)
@@ -41,6 +44,7 @@ class ServiceNameParser(Handler):
41
44
  return
42
45
 
43
46
  context.service = service_model
47
+ context.protocol = determine_aws_protocol(context.request, service_model)
44
48
 
45
49
 
46
50
  class ServiceRequestParser(Handler):
@@ -63,7 +67,7 @@ class ServiceRequestParser(Handler):
63
67
  return self.parse_and_enrich(context)
64
68
 
65
69
  def parse_and_enrich(self, context: RequestContext):
66
- parser = create_parser(context.service)
70
+ parser = create_parser(context.service, context.protocol)
67
71
  operation, instance = parser.parse(context.request)
68
72
 
69
73
  # enrich context
@@ -137,7 +141,7 @@ class ServiceRequestRouter(Handler):
137
141
  operation_name = operation.name
138
142
  message = f"no handler for operation '{operation_name}' on service '{service_name}'"
139
143
  error = CommonServiceException("InternalFailure", message, status_code=501)
140
- serializer = create_serializer(context.service)
144
+ serializer = create_serializer(context.service, context.protocol)
141
145
  return serializer.serialize_error_to_response(
142
146
  error, operation, context.request.headers, context.request_id
143
147
  )
@@ -153,6 +157,15 @@ class ServiceExceptionSerializer(ExceptionHandler):
153
157
  def __init__(self):
154
158
  self.handle_internal_failures = True
155
159
 
160
+ try:
161
+ import moto.core.exceptions
162
+
163
+ self._moto_service_exception = moto.core.exceptions.ServiceException
164
+ except (ModuleNotFoundError, AttributeError) as exc:
165
+ # Moto may not be available in stripped-down versions of LocalStack, like LocalStack S3 image.
166
+ LOG.debug("Unable to set up Moto ServiceException translation: %s", exc)
167
+ self._moto_service_exception = types.EllipsisType
168
+
156
169
  def __call__(
157
170
  self,
158
171
  chain: HandlerChain,
@@ -176,9 +189,16 @@ class ServiceExceptionSerializer(ExceptionHandler):
176
189
  action_name = operation.name
177
190
  exception_message: str | None = exception.args[0] if exception.args else None
178
191
  message = exception_message or get_coverage_link_for_service(service_name, action_name)
179
- LOG.info(message)
180
192
  error = CommonServiceException("InternalFailure", message, status_code=501)
181
- context.service_exception = error
193
+ LOG.info(message)
194
+
195
+ elif isinstance(exception, self._moto_service_exception):
196
+ # Translate Moto ServiceException to native ServiceException if Moto is available.
197
+ # This allows handler chain to gracefully handles Moto errors when provider handlers invoke Moto methods directly.
198
+ error = CommonServiceException(
199
+ code=exception.code,
200
+ message=exception.message,
201
+ )
182
202
 
183
203
  elif not isinstance(exception, ServiceException):
184
204
  if not self.handle_internal_failures:
@@ -207,9 +227,10 @@ class ServiceExceptionSerializer(ExceptionHandler):
207
227
  error = CommonServiceException(
208
228
  "InternalError", msg, status_code=status_code
209
229
  ).with_traceback(exception.__traceback__)
210
- context.service_exception = error
211
230
 
212
- serializer = create_serializer(context.service) # TODO: serializer cache
231
+ context.service_exception = error
232
+
233
+ serializer = create_serializer(context.service, context.protocol)
213
234
  return serializer.serialize_error_to_response(
214
235
  error, operation, context.request.headers, context.request_id
215
236
  )
@@ -252,7 +273,9 @@ class ServiceResponseParser(Handler):
252
273
  return
253
274
 
254
275
  # in this case we need to parse the raw response
255
- parsed = parse_response(context.operation, response, include_response_metadata=False)
276
+ parsed = parse_response(
277
+ context.operation, context.protocol, response, include_response_metadata=False
278
+ )
256
279
  if service_exception := parse_service_exception(response, parsed):
257
280
  context.service_exception = service_exception
258
281
  else:
@@ -19,22 +19,23 @@ The class hierarchy looks as follows:
19
19
  │RequestParser│
20
20
  └─────────────┘
21
21
  ▲ ▲ ▲
22
- ┌─────────────────┘ │ └────────────────────┐
23
- ┌────────┴─────────┐ ┌─────────┴───────────┐ ┌──────────┴──────────┐
24
- │QueryRequestParser│ │BaseRestRequestParser│ │BaseJSONRequestParser│
25
- └──────────────────┘ └─────────────────────┘ └─────────────────────┘
26
- ▲ ▲ ▲ ▲ ▲
27
- ┌───────┴────────┐ ┌─────────┴──────────┐ │ │
28
- │EC2RequestParser│ │RestXMLRequestParser│ │ │
29
- └────────────────┘ └────────────────────┘ │ │
30
- ┌────────────────┴───┴┐ ┌────────┴────────┐
31
- │RestJSONRequestParser│ │JSONRequestParser│
32
- └─────────────────────┘ └─────────────────┘
22
+ ┌─────────────────┘ │ └────────────────────┬───────────────────────┬───────────────────────┐
23
+ ┌────────┴─────────┐ ┌─────────┴───────────┐ ┌──────────┴──────────┐ ┌──────────┴──────────┐ ┌──────────┴───────────┐
24
+ │QueryRequestParser│ │BaseRestRequestParser│ │BaseJSONRequestParser│ │BaseCBORRequestParser│ │BaseRpcV2RequestParser│
25
+ └──────────────────┘ └─────────────────────┘ └─────────────────────┘ └─────────────────────┘ └──────────────────────┘
26
+ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
27
+ ┌───────┴────────┐ ┌─────────┴──────────┐ │ │ ┌────────┴────────┐ ┌───┴─────────────┴────┐
28
+ │EC2RequestParser│ │RestXMLRequestParser│ │ │ JSONRequestParser│ │ │RpcV2CBORRequestParser│
29
+ └────────────────┘ └────────────────────┘ │ │ └─────────────────┘ └──────────────────────┘
30
+ ┌────────────────┴───┴┐ ▲ │
31
+ │RestJSONRequestParser│ ┌───┴──────┴──────┐
32
+ └─────────────────────┘ │CBORRequestParser│
33
+ └─────────────────┘
33
34
  ::
34
35
 
35
36
  The ``RequestParser`` contains the logic that is used among all the
36
37
  different protocols (``query``, ``json``, ``rest-json``, ``rest-xml``,
37
- and ``ec2``).
38
+ ``cbor`` and ``ec2``).
38
39
  The relation between the different protocols is described in the
39
40
  ``serializer``.
40
41
 
@@ -44,13 +45,21 @@ The classes are structured as follows:
44
45
  which is shared among all different protocols.
45
46
  * The ``BaseRestRequestParser`` contains the logic for the REST
46
47
  protocol specifics (i.e. specific HTTP metadata parsing).
48
+ * The ``BaseRpcV2RequestParser`` contains the logic for the RPC v2
49
+ protocol specifics (special path routing, no logic about body decoding)
47
50
  * The ``BaseJSONRequestParser`` contains the logic for the JSON body
48
51
  parsing.
52
+ * The ``BaseCBORRequestParser`` contains the logic for the CBOR body
53
+ parsing.
49
54
  * The ``RestJSONRequestParser`` inherits the ReST specific logic from
50
55
  the ``BaseRestRequestParser`` and the JSON body parsing from the
51
56
  ``BaseJSONRequestParser``.
52
- * The ``QueryRequestParser``, ``RestXMLRequestParser``, and the
53
- ``JSONRequestParser`` have a conventional inheritance structure.
57
+ * The ``CBORRequestParser`` inherits the ``json``-protocol specific
58
+ logic from the ``JSONRequestParser`` and the CBOR body parsing
59
+ from the ``BaseCBORRequestParser``.
60
+ * The ``QueryRequestParser``, ``RestXMLRequestParser``,
61
+ ``RpcV2CBORRequestParser`` and ``JSONRequestParser`` have a
62
+ conventional inheritance structure.
54
63
 
55
64
  The services and their protocols are defined by using AWS's Smithy
56
65
  (a language to define services in a - somewhat - protocol-agnostic
@@ -66,7 +75,10 @@ import abc
66
75
  import base64
67
76
  import datetime
68
77
  import functools
78
+ import io
79
+ import os
69
80
  import re
81
+ import struct
70
82
  from abc import ABC
71
83
  from collections.abc import Mapping
72
84
  from email.utils import parsedate_to_datetime
@@ -89,6 +101,7 @@ from cbor2._decoder import loads as cbor2_loads
89
101
  from werkzeug.exceptions import BadRequest, NotFound
90
102
 
91
103
  from localstack.aws.protocol.op_router import RestServiceOperationRouter
104
+ from localstack.aws.spec import ProtocolName
92
105
  from localstack.http import Request
93
106
 
94
107
 
@@ -332,7 +345,7 @@ class RequestParser(abc.ABC):
332
345
  _parse_double = _parse_float
333
346
  _parse_long = _parse_integer
334
347
 
335
- def _convert_str_to_timestamp(self, value: str, timestamp_format=None):
348
+ def _convert_str_to_timestamp(self, value: str, timestamp_format=None) -> datetime.datetime:
336
349
  if timestamp_format is None:
337
350
  timestamp_format = self.TIMESTAMP_FORMAT
338
351
  timestamp_format = timestamp_format.lower()
@@ -346,11 +359,11 @@ class RequestParser(abc.ABC):
346
359
 
347
360
  @staticmethod
348
361
  def _timestamp_unixtimestamp(timestamp_string: str) -> datetime.datetime:
349
- return datetime.datetime.utcfromtimestamp(int(timestamp_string))
362
+ return datetime.datetime.fromtimestamp(int(timestamp_string), tz=datetime.UTC)
350
363
 
351
364
  @staticmethod
352
365
  def _timestamp_unixtimestampmillis(timestamp_string: str) -> datetime.datetime:
353
- return datetime.datetime.utcfromtimestamp(float(timestamp_string) / 1000)
366
+ return datetime.datetime.fromtimestamp(float(timestamp_string) / 1000, tz=datetime.UTC)
354
367
 
355
368
  @staticmethod
356
369
  def _timestamp_rfc822(datetime_string: str) -> datetime.datetime:
@@ -976,6 +989,405 @@ class RestJSONRequestParser(BaseRestRequestParser, BaseJSONRequestParser):
976
989
  raise NotImplementedError
977
990
 
978
991
 
992
+ class BaseCBORRequestParser(RequestParser, ABC):
993
+ """
994
+ The ``BaseCBORRequestParser`` is the base class for all CBOR-based AWS service protocols.
995
+ This base-class handles parsing the payload / body as CBOR.
996
+ """
997
+
998
+ INDEFINITE_ITEM_ADDITIONAL_INFO = 31
999
+ BREAK_CODE = 0xFF
1000
+ # timestamp format for requests with CBOR content type
1001
+ TIMESTAMP_FORMAT = "unixtimestamp"
1002
+
1003
+ @functools.cached_property
1004
+ def major_type_to_parsing_method_map(self):
1005
+ return {
1006
+ 0: self._parse_type_unsigned_integer,
1007
+ 1: self._parse_type_negative_integer,
1008
+ 2: self._parse_type_byte_string,
1009
+ 3: self._parse_type_text_string,
1010
+ 4: self._parse_type_array,
1011
+ 5: self._parse_type_map,
1012
+ 6: self._parse_type_tag,
1013
+ 7: self._parse_type_simple_and_float,
1014
+ }
1015
+
1016
+ @staticmethod
1017
+ def get_peekable_stream_from_bytes(_bytes: bytes) -> io.BufferedReader:
1018
+ return io.BufferedReader(io.BytesIO(_bytes))
1019
+
1020
+ def parse_data_item(self, stream: io.BufferedReader) -> Any:
1021
+ # CBOR data is divided into "data items", and each data item starts
1022
+ # with an initial byte that describes how the following bytes should be parsed
1023
+ initial_byte = self._read_bytes_as_int(stream, 1)
1024
+ # The highest order three bits of the initial byte describe the CBOR major type
1025
+ major_type = initial_byte >> 5
1026
+ # The lowest order 5 bits of the initial byte tells us more information about
1027
+ # how the bytes should be parsed that will be used
1028
+ additional_info: int = initial_byte & 0b00011111
1029
+
1030
+ if major_type in self.major_type_to_parsing_method_map:
1031
+ method = self.major_type_to_parsing_method_map[major_type]
1032
+ return method(stream, additional_info)
1033
+ else:
1034
+ raise ProtocolParserError(
1035
+ f"Unsupported inital byte found for data item- "
1036
+ f"Major type:{major_type}, Additional info: "
1037
+ f"{additional_info}"
1038
+ )
1039
+
1040
+ # Major type 0 - unsigned integers
1041
+ def _parse_type_unsigned_integer(self, stream: io.BufferedReader, additional_info: int) -> int:
1042
+ additional_info_to_num_bytes = {
1043
+ 24: 1,
1044
+ 25: 2,
1045
+ 26: 4,
1046
+ 27: 8,
1047
+ }
1048
+ # Values under 24 don't need a full byte to be stored; their values are
1049
+ # instead stored as the "additional info" in the initial byte
1050
+ if additional_info < 24:
1051
+ return additional_info
1052
+ elif additional_info in additional_info_to_num_bytes:
1053
+ num_bytes = additional_info_to_num_bytes[additional_info]
1054
+ return self._read_bytes_as_int(stream, num_bytes)
1055
+ else:
1056
+ raise ProtocolParserError(
1057
+ "Invalid CBOR integer returned from the service; unparsable "
1058
+ f"additional info found for major type 0 or 1: {additional_info}"
1059
+ )
1060
+
1061
+ # Major type 1 - negative integers
1062
+ def _parse_type_negative_integer(self, stream: io.BufferedReader, additional_info: int) -> int:
1063
+ return -1 - self._parse_type_unsigned_integer(stream, additional_info)
1064
+
1065
+ # Major type 2 - byte string
1066
+ def _parse_type_byte_string(self, stream: io.BufferedReader, additional_info: int) -> bytes:
1067
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1068
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1069
+ return self._read_from_stream(stream, length)
1070
+ else:
1071
+ chunks = []
1072
+ while True:
1073
+ if self._handle_break_code(stream):
1074
+ break
1075
+ initial_byte = self._read_bytes_as_int(stream, 1)
1076
+ additional_info = initial_byte & 0b00011111
1077
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1078
+ chunks.append(self._read_from_stream(stream, length))
1079
+ return b"".join(chunks)
1080
+
1081
+ # Major type 3 - text string
1082
+ def _parse_type_text_string(self, stream: io.BufferedReader, additional_info: int) -> str:
1083
+ return self._parse_type_byte_string(stream, additional_info).decode("utf-8")
1084
+
1085
+ # Major type 4 - lists
1086
+ def _parse_type_array(self, stream: io.BufferedReader, additional_info: int) -> list:
1087
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1088
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1089
+ return [self.parse_data_item(stream) for _ in range(length)]
1090
+ else:
1091
+ items = []
1092
+ while not self._handle_break_code(stream):
1093
+ items.append(self.parse_data_item(stream))
1094
+ return items
1095
+
1096
+ # Major type 5 - maps
1097
+ def _parse_type_map(self, stream: io.BufferedReader, additional_info: int) -> dict:
1098
+ items = {}
1099
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1100
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1101
+ for _ in range(length):
1102
+ self._parse_type_key_value_pair(stream, items)
1103
+ return items
1104
+
1105
+ else:
1106
+ while not self._handle_break_code(stream):
1107
+ self._parse_type_key_value_pair(stream, items)
1108
+ return items
1109
+
1110
+ def _parse_type_key_value_pair(self, stream: io.BufferedReader, items: dict) -> None:
1111
+ key = self.parse_data_item(stream)
1112
+ value = self.parse_data_item(stream)
1113
+ if value is not None:
1114
+ items[key] = value
1115
+
1116
+ # Major type 6 is tags. The only tag we currently support is tag 1 for unix
1117
+ # timestamps
1118
+ def _parse_type_tag(self, stream: io.BufferedReader, additional_info: int):
1119
+ tag = self._parse_type_unsigned_integer(stream, additional_info)
1120
+ value = self.parse_data_item(stream)
1121
+ if tag == 1: # Epoch-based date/time in milliseconds
1122
+ return self._parse_type_datetime(value)
1123
+ else:
1124
+ raise ProtocolParserError(f"Found CBOR tag not supported by botocore: {tag}")
1125
+
1126
+ def _parse_type_datetime(self, value: int | float) -> datetime.datetime:
1127
+ if isinstance(value, (int, float)):
1128
+ return self._convert_str_to_timestamp(str(value))
1129
+ else:
1130
+ raise ProtocolParserError(f"Unable to parse datetime value: {value}")
1131
+
1132
+ # Major type 7 includes floats and "simple" types. Supported simple types are
1133
+ # currently boolean values, CBOR's null, and CBOR's undefined type. All other
1134
+ # values are either floats or invalid.
1135
+ def _parse_type_simple_and_float(
1136
+ self, stream: io.BufferedReader, additional_info: int
1137
+ ) -> bool | float | None:
1138
+ # For major type 7, values 20-23 correspond to CBOR "simple" values
1139
+ additional_info_simple_values = {
1140
+ 20: False, # CBOR false
1141
+ 21: True, # CBOR true
1142
+ 22: None, # CBOR null
1143
+ 23: None, # CBOR undefined
1144
+ }
1145
+ # First we check if the additional info corresponds to a supported simple value
1146
+ if additional_info in additional_info_simple_values:
1147
+ return additional_info_simple_values[additional_info]
1148
+
1149
+ # If it's not a simple value, we need to parse it into the correct format and
1150
+ # number fo bytes
1151
+ float_formats = {
1152
+ 25: (">e", 2),
1153
+ 26: (">f", 4),
1154
+ 27: (">d", 8),
1155
+ }
1156
+
1157
+ if additional_info in float_formats:
1158
+ float_format, num_bytes = float_formats[additional_info]
1159
+ return struct.unpack(float_format, self._read_from_stream(stream, num_bytes))[0]
1160
+ raise ProtocolParserError(
1161
+ f"Invalid additional info found for major type 7: {additional_info}. "
1162
+ f"This indicates an unsupported simple type or an indefinite float value"
1163
+ )
1164
+
1165
+ @_text_content
1166
+ def _parse_blob(self, _, __, node: bytes, ___) -> bytes:
1167
+ return node
1168
+
1169
+ @_text_content
1170
+ def _parse_timestamp(
1171
+ self, _, shape: Shape, node: datetime.datetime | str, ___
1172
+ ) -> datetime.datetime:
1173
+ if isinstance(node, datetime.datetime):
1174
+ return node
1175
+ return super()._parse_timestamp(_, shape, node, ___)
1176
+
1177
+ @_text_content
1178
+ def _parse_boolean(self, _, __, node: str | bool, ___) -> bool:
1179
+ if isinstance(node, str):
1180
+ value = node.lower()
1181
+ if value == "true":
1182
+ return True
1183
+ if value == "false":
1184
+ return False
1185
+ raise ValueError(f"cannot parse boolean value {node}")
1186
+ return node
1187
+
1188
+ # This helper method is intended for use when parsing indefinite length items.
1189
+ # It does nothing if the next byte is not the break code. If the next byte is
1190
+ # the break code, it advances past that byte and returns True so the calling
1191
+ # method knows to stop parsing that data item.
1192
+ def _handle_break_code(self, stream: io.BufferedReader) -> bool | None:
1193
+ if int.from_bytes(stream.peek(1)[:1], "big") == self.BREAK_CODE:
1194
+ stream.seek(1, os.SEEK_CUR)
1195
+ return True
1196
+
1197
+ def _read_bytes_as_int(self, stream: IO[bytes], num_bytes: int) -> int:
1198
+ byte = self._read_from_stream(stream, num_bytes)
1199
+ return int.from_bytes(byte, "big")
1200
+
1201
+ @staticmethod
1202
+ def _read_from_stream(stream: IO[bytes], num_bytes: int) -> bytes:
1203
+ value = stream.read(num_bytes)
1204
+ if len(value) != num_bytes:
1205
+ raise ProtocolParserError(
1206
+ "End of stream reached; this indicates a "
1207
+ "malformed CBOR response from the server or an "
1208
+ "issue in botocore"
1209
+ )
1210
+ return value
1211
+
1212
+
1213
+ class CBORRequestParser(BaseCBORRequestParser, JSONRequestParser):
1214
+ """
1215
+ The ``CBORRequestParser`` is responsible for parsing incoming requests for services which use the ``cbor``
1216
+ protocol.
1217
+ The requests for these services encode the majority of their parameters as CBOR in the request body.
1218
+ The operation is defined in an HTTP header field.
1219
+ This protocol is not properly defined in the specs, but it is derived from the ``json`` protocol. Only Kinesis uses
1220
+ it for now.
1221
+ """
1222
+
1223
+ # timestamp format is different from traditional CBOR, and is encoded as a milliseconds integer
1224
+ TIMESTAMP_FORMAT = "unixtimestampmillis"
1225
+
1226
+ def _do_parse(
1227
+ self, request: Request, shape: Shape, uri_params: Mapping[str, Any] = None
1228
+ ) -> dict:
1229
+ parsed = {}
1230
+ if shape is not None:
1231
+ event_name = shape.event_stream_name
1232
+ if event_name:
1233
+ parsed = self._handle_event_stream(request, shape, event_name)
1234
+ else:
1235
+ self._parse_payload(request, shape, parsed, uri_params)
1236
+ return parsed
1237
+
1238
+ def _handle_event_stream(self, request: Request, shape: Shape, event_name: str):
1239
+ # TODO handle event streams
1240
+ raise NotImplementedError
1241
+
1242
+ def _parse_payload(
1243
+ self,
1244
+ request: Request,
1245
+ shape: Shape,
1246
+ final_parsed: dict,
1247
+ uri_params: Mapping[str, Any] = None,
1248
+ ) -> None:
1249
+ original_parsed = self._initial_body_parse(request)
1250
+ body_parsed = self._parse_shape(request, shape, original_parsed, uri_params)
1251
+ final_parsed.update(body_parsed)
1252
+
1253
+ def _initial_body_parse(self, request: Request) -> Any:
1254
+ body_contents = request.data
1255
+ if body_contents == b"":
1256
+ return body_contents
1257
+ body_contents_stream = self.get_peekable_stream_from_bytes(body_contents)
1258
+ return self.parse_data_item(body_contents_stream)
1259
+
1260
+ def _parse_timestamp(
1261
+ self, request: Request, shape: Shape, node: str, uri_params: Mapping[str, Any] = None
1262
+ ) -> datetime.datetime:
1263
+ # TODO: remove once CBOR support has been removed from `JSONRequestParser`
1264
+ return super()._parse_timestamp(request, shape, node, uri_params)
1265
+
1266
+
1267
+ class BaseRpcV2RequestParser(RequestParser):
1268
+ """
1269
+ The ``BaseRpcV2RequestParser`` is the base class for all RPC V2-based AWS service protocols.
1270
+ This base class handles the routing of the request, which is specific based on the path.
1271
+ The body decoding is done in the respective subclasses.
1272
+ """
1273
+
1274
+ @_handle_exceptions
1275
+ def parse(self, request: Request) -> tuple[OperationModel, Any]:
1276
+ # see https://smithy.io/2.0/additional-specs/protocols/smithy-rpc-v2.html
1277
+ if request.method != "POST":
1278
+ raise ProtocolParserError("RPC v2 only accepts POST requests.")
1279
+
1280
+ headers = request.headers
1281
+ if "X-Amz-Target" in headers or "X-Amzn-Target" in headers:
1282
+ raise ProtocolParserError(
1283
+ "RPC v2 does not accept 'X-Amz-Target' or 'X-Amzn-Target'. "
1284
+ "Such requests are rejected for security reasons."
1285
+ )
1286
+ # The Smithy RPCv2 CBOR protocol will only use the last four segments of the URL when routing requests.
1287
+ rpc_v2_params = request.path.lstrip("/").split("/")
1288
+ if len(rpc_v2_params) < 4 or not (
1289
+ operation := self.service.operation_model(rpc_v2_params[-1])
1290
+ ):
1291
+ raise OperationNotFoundParserError(
1292
+ f"Unable to find operation for request to service "
1293
+ f"{self.service.service_name}: {request.method} {request.path}"
1294
+ )
1295
+
1296
+ # there are no URI params in RPC v2
1297
+ uri_params = {}
1298
+ shape: StructureShape = operation.input_shape
1299
+ final_parsed = self._do_parse(request, shape, uri_params)
1300
+ return operation, final_parsed
1301
+
1302
+ @_handle_exceptions
1303
+ def _do_parse(
1304
+ self, request: Request, shape: Shape, uri_params: Mapping[str, Any] = None
1305
+ ) -> dict[str, Any]:
1306
+ parsed = {}
1307
+ if shape is not None:
1308
+ event_stream_name = shape.event_stream_name
1309
+ if event_stream_name:
1310
+ parsed = self._handle_event_stream(request, shape, event_stream_name)
1311
+ else:
1312
+ parsed = {}
1313
+ self._parse_payload(request, shape, parsed, uri_params)
1314
+
1315
+ return parsed
1316
+
1317
+ def _handle_event_stream(self, request: Request, shape: Shape, event_name: str):
1318
+ # TODO handle event streams
1319
+ raise NotImplementedError
1320
+
1321
+ def _parse_structure(
1322
+ self,
1323
+ request: Request,
1324
+ shape: StructureShape,
1325
+ node: dict | None,
1326
+ uri_params: Mapping[str, Any] = None,
1327
+ ):
1328
+ if shape.is_document_type:
1329
+ final_parsed = node
1330
+ else:
1331
+ if node is None:
1332
+ # If the comes across the wire as "null" (None in python),
1333
+ # we should be returning this unchanged, instead of as an
1334
+ # empty dict.
1335
+ return None
1336
+ final_parsed = {}
1337
+ members = shape.members
1338
+ if shape.is_tagged_union:
1339
+ cleaned_value = node.copy()
1340
+ cleaned_value.pop("__type", None)
1341
+ cleaned_value = {k: v for k, v in cleaned_value.items() if v is not None}
1342
+ if len(cleaned_value) != 1:
1343
+ raise ProtocolParserError(
1344
+ f"Invalid service response: {shape.name} must have one and only one member set."
1345
+ )
1346
+
1347
+ for member_name, member_shape in members.items():
1348
+ member_value = node.get(member_name)
1349
+ if member_value is not None:
1350
+ final_parsed[member_name] = self._parse_shape(
1351
+ request, member_shape, member_value, uri_params
1352
+ )
1353
+
1354
+ return final_parsed
1355
+
1356
+ def _parse_payload(
1357
+ self,
1358
+ request: Request,
1359
+ shape: Shape,
1360
+ final_parsed: dict,
1361
+ uri_params: Mapping[str, Any] = None,
1362
+ ) -> None:
1363
+ original_parsed = self._initial_body_parse(request)
1364
+ body_parsed = self._parse_shape(request, shape, original_parsed, uri_params)
1365
+ final_parsed.update(body_parsed)
1366
+
1367
+ def _initial_body_parse(self, request: Request):
1368
+ # This method should do the initial parsing of the
1369
+ # body. We still need to walk the parsed body in order
1370
+ # to convert types, but this method will do the first round
1371
+ # of parsing.
1372
+ raise NotImplementedError("_initial_body_parse")
1373
+
1374
+
1375
+ class RpcV2CBORRequestParser(BaseRpcV2RequestParser, BaseCBORRequestParser):
1376
+ """
1377
+ The ``RpcV2CBORRequestParser`` is responsible for parsing incoming requests for services which use the
1378
+ ``rpc-v2-cbor`` protocol. The requests for these services encode all of their parameters as CBOR in the
1379
+ request body.
1380
+ """
1381
+
1382
+ # TODO: investigate datetime format for RpcV2CBOR protocol, which might be different than Kinesis CBOR
1383
+ def _initial_body_parse(self, request: Request):
1384
+ body_contents = request.data
1385
+ if body_contents == b"":
1386
+ return body_contents
1387
+ body_contents_stream = self.get_peekable_stream_from_bytes(body_contents)
1388
+ return self.parse_data_item(body_contents_stream)
1389
+
1390
+
979
1391
  class EC2RequestParser(QueryRequestParser):
980
1392
  """
981
1393
  The ``EC2RequestParser`` is responsible for parsing incoming requests for services which use the ``ec2``
@@ -1154,11 +1566,12 @@ class SQSQueryRequestParser(QueryRequestParser):
1154
1566
 
1155
1567
 
1156
1568
  @functools.cache
1157
- def create_parser(service: ServiceModel) -> RequestParser:
1569
+ def create_parser(service: ServiceModel, protocol: ProtocolName | None = None) -> RequestParser:
1158
1570
  """
1159
1571
  Creates the right parser for the given service model.
1160
1572
 
1161
1573
  :param service: to create the parser for
1574
+ :param protocol: the protocol for the parser. If not provided, fallback to the service's default protocol
1162
1575
  :return: RequestParser which can handle the protocol of the service
1163
1576
  """
1164
1577
  # Unfortunately, some services show subtle differences in their parsing or operation detection behavior, even though
@@ -1176,14 +1589,20 @@ def create_parser(service: ServiceModel) -> RequestParser:
1176
1589
  "rest-json": RestJSONRequestParser,
1177
1590
  "rest-xml": RestXMLRequestParser,
1178
1591
  "ec2": EC2RequestParser,
1592
+ "smithy-rpc-v2-cbor": RpcV2CBORRequestParser,
1593
+ # TODO: implement multi-protocol support for Kinesis, so that it can uses the `cbor` protocol and remove
1594
+ # CBOR handling from JSONRequestParser
1595
+ # this is not an "official" protocol defined from the spec, but is derived from ``json``
1179
1596
  }
1180
1597
 
1598
+ service_protocol = protocol or service.protocol
1599
+
1181
1600
  # Try to select a service- and protocol-specific parser implementation
1182
1601
  if (
1183
1602
  service.service_name in service_specific_parsers
1184
- and service.protocol in service_specific_parsers[service.service_name]
1603
+ and service_protocol in service_specific_parsers[service.service_name]
1185
1604
  ):
1186
- return service_specific_parsers[service.service_name][service.protocol](service)
1605
+ return service_specific_parsers[service.service_name][service_protocol](service)
1187
1606
  else:
1188
1607
  # Otherwise, pick the protocol-specific parser for the protocol of the service
1189
- return protocol_specific_parsers[service.protocol](service)
1608
+ return protocol_specific_parsers[service_protocol](service)