localstack-core 4.7.1.dev49__py3-none-any.whl → 4.10.1.dev12__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 (253) hide show
  1. localstack/aws/api/cloudformation/__init__.py +18 -4
  2. localstack/aws/api/cloudwatch/__init__.py +41 -1
  3. localstack/aws/api/config/__init__.py +4 -0
  4. localstack/aws/api/core.py +6 -2
  5. localstack/aws/api/dynamodb/__init__.py +30 -0
  6. localstack/aws/api/ec2/__init__.py +1522 -65
  7. localstack/aws/api/iam/__init__.py +7 -0
  8. localstack/aws/api/kinesis/__init__.py +19 -0
  9. localstack/aws/api/kms/__init__.py +6 -0
  10. localstack/aws/api/lambda_/__init__.py +13 -0
  11. localstack/aws/api/logs/__init__.py +15 -0
  12. localstack/aws/api/redshift/__init__.py +9 -3
  13. localstack/aws/api/route53/__init__.py +5 -0
  14. localstack/aws/api/s3/__init__.py +12 -0
  15. localstack/aws/api/s3control/__init__.py +54 -0
  16. localstack/aws/api/ssm/__init__.py +2 -0
  17. localstack/aws/api/transcribe/__init__.py +17 -0
  18. localstack/aws/client.py +7 -2
  19. localstack/aws/forwarder.py +52 -5
  20. localstack/aws/handlers/analytics.py +1 -1
  21. localstack/aws/handlers/internal_requests.py +6 -1
  22. localstack/aws/handlers/logging.py +12 -2
  23. localstack/aws/handlers/metric_handler.py +41 -1
  24. localstack/aws/handlers/service.py +40 -20
  25. localstack/aws/mocking.py +2 -2
  26. localstack/aws/patches.py +2 -2
  27. localstack/aws/protocol/parser.py +459 -32
  28. localstack/aws/protocol/serializer.py +689 -69
  29. localstack/aws/protocol/service_router.py +120 -20
  30. localstack/aws/protocol/validate.py +1 -1
  31. localstack/aws/scaffold.py +1 -1
  32. localstack/aws/skeleton.py +4 -2
  33. localstack/aws/spec-patches.json +58 -0
  34. localstack/aws/spec.py +37 -16
  35. localstack/cli/exceptions.py +1 -1
  36. localstack/cli/localstack.py +6 -6
  37. localstack/cli/lpm.py +3 -4
  38. localstack/cli/plugins.py +1 -1
  39. localstack/cli/profiles.py +1 -2
  40. localstack/config.py +25 -18
  41. localstack/constants.py +4 -29
  42. localstack/dev/kubernetes/__main__.py +130 -7
  43. localstack/dev/run/configurators.py +1 -4
  44. localstack/dev/run/paths.py +1 -1
  45. localstack/dns/plugins.py +5 -1
  46. localstack/dns/server.py +13 -4
  47. localstack/logging/format.py +3 -3
  48. localstack/packages/api.py +9 -8
  49. localstack/packages/core.py +2 -2
  50. localstack/packages/plugins.py +0 -8
  51. localstack/runtime/analytics.py +3 -0
  52. localstack/runtime/hooks.py +1 -1
  53. localstack/runtime/init.py +2 -2
  54. localstack/runtime/main.py +5 -5
  55. localstack/runtime/patches.py +2 -2
  56. localstack/services/apigateway/helpers.py +1 -4
  57. localstack/services/apigateway/legacy/helpers.py +7 -8
  58. localstack/services/apigateway/legacy/integration.py +4 -3
  59. localstack/services/apigateway/legacy/invocations.py +6 -5
  60. localstack/services/apigateway/legacy/provider.py +148 -68
  61. localstack/services/apigateway/legacy/templates.py +1 -1
  62. localstack/services/apigateway/next_gen/execute_api/handlers/method_request.py +7 -2
  63. localstack/services/apigateway/next_gen/execute_api/handlers/resource_router.py +1 -2
  64. localstack/services/apigateway/next_gen/execute_api/integrations/aws.py +3 -0
  65. localstack/services/apigateway/next_gen/execute_api/integrations/http.py +3 -3
  66. localstack/services/apigateway/next_gen/execute_api/template_mapping.py +2 -2
  67. localstack/services/apigateway/next_gen/execute_api/test_invoke.py +114 -9
  68. localstack/services/apigateway/next_gen/provider.py +5 -0
  69. localstack/services/apigateway/resource_providers/aws_apigateway_resource.py +1 -1
  70. localstack/services/cloudformation/api_utils.py +4 -8
  71. localstack/services/cloudformation/cfn_utils.py +1 -1
  72. localstack/services/cloudformation/engine/entities.py +14 -4
  73. localstack/services/cloudformation/engine/template_deployer.py +6 -4
  74. localstack/services/cloudformation/engine/transformers.py +6 -4
  75. localstack/services/cloudformation/engine/v2/change_set_model.py +201 -13
  76. localstack/services/cloudformation/engine/v2/change_set_model_describer.py +52 -3
  77. localstack/services/cloudformation/engine/v2/change_set_model_executor.py +117 -76
  78. localstack/services/cloudformation/engine/v2/change_set_model_preproc.py +205 -52
  79. localstack/services/cloudformation/engine/v2/change_set_model_transform.py +350 -116
  80. localstack/services/cloudformation/engine/v2/change_set_model_validator.py +56 -14
  81. localstack/services/cloudformation/engine/v2/change_set_model_visitor.py +1 -0
  82. localstack/services/cloudformation/engine/v2/resolving.py +7 -5
  83. localstack/services/cloudformation/engine/yaml_parser.py +9 -2
  84. localstack/services/cloudformation/provider.py +7 -5
  85. localstack/services/cloudformation/resource_provider.py +7 -1
  86. localstack/services/cloudformation/resources.py +24149 -0
  87. localstack/services/cloudformation/service_models.py +2 -2
  88. localstack/services/cloudformation/v2/entities.py +19 -9
  89. localstack/services/cloudformation/v2/provider.py +336 -106
  90. localstack/services/cloudformation/v2/types.py +13 -7
  91. localstack/services/cloudformation/v2/utils.py +4 -1
  92. localstack/services/cloudwatch/alarm_scheduler.py +4 -1
  93. localstack/services/cloudwatch/provider.py +18 -13
  94. localstack/services/cloudwatch/provider_v2.py +25 -28
  95. localstack/services/dynamodb/packages.py +2 -1
  96. localstack/services/dynamodb/provider.py +42 -0
  97. localstack/services/dynamodb/server.py +2 -2
  98. localstack/services/dynamodb/v2/provider.py +42 -0
  99. localstack/services/ecr/resource_providers/aws_ecr_repository.py +5 -2
  100. localstack/services/edge.py +1 -1
  101. localstack/services/es/provider.py +2 -2
  102. localstack/services/events/event_rule_engine.py +31 -13
  103. localstack/services/events/models.py +4 -5
  104. localstack/services/events/provider.py +17 -14
  105. localstack/services/events/target.py +17 -9
  106. localstack/services/events/v1/provider.py +5 -5
  107. localstack/services/firehose/provider.py +14 -4
  108. localstack/services/iam/provider.py +11 -116
  109. localstack/services/iam/resources/policy_simulator.py +133 -0
  110. localstack/services/kinesis/models.py +15 -2
  111. localstack/services/kinesis/provider.py +86 -3
  112. localstack/services/kms/provider.py +14 -5
  113. localstack/services/lambda_/api_utils.py +6 -3
  114. localstack/services/lambda_/invocation/docker_runtime_executor.py +1 -1
  115. localstack/services/lambda_/invocation/event_manager.py +1 -1
  116. localstack/services/lambda_/invocation/internal_sqs_queue.py +5 -9
  117. localstack/services/lambda_/invocation/lambda_models.py +10 -7
  118. localstack/services/lambda_/invocation/lambda_service.py +5 -1
  119. localstack/services/lambda_/packages.py +1 -1
  120. localstack/services/lambda_/provider.py +4 -3
  121. localstack/services/lambda_/provider_utils.py +1 -1
  122. localstack/services/logs/provider.py +36 -19
  123. localstack/services/moto.py +2 -1
  124. localstack/services/opensearch/cluster.py +15 -7
  125. localstack/services/opensearch/packages.py +26 -7
  126. localstack/services/opensearch/provider.py +8 -2
  127. localstack/services/opensearch/versions.py +56 -7
  128. localstack/services/plugins.py +11 -7
  129. localstack/services/providers.py +10 -2
  130. localstack/services/redshift/provider.py +0 -21
  131. localstack/services/s3/constants.py +5 -2
  132. localstack/services/s3/cors.py +4 -4
  133. localstack/services/s3/models.py +1 -1
  134. localstack/services/s3/notifications.py +55 -39
  135. localstack/services/s3/presigned_url.py +35 -54
  136. localstack/services/s3/provider.py +73 -15
  137. localstack/services/s3/utils.py +42 -22
  138. localstack/services/s3/validation.py +46 -32
  139. localstack/services/s3/website_hosting.py +4 -2
  140. localstack/services/ses/provider.py +18 -8
  141. localstack/services/sns/constants.py +7 -1
  142. localstack/services/sns/executor.py +9 -2
  143. localstack/services/sns/provider.py +8 -5
  144. localstack/services/sns/publisher.py +31 -16
  145. localstack/services/sns/v2/models.py +167 -0
  146. localstack/services/sns/v2/provider.py +867 -0
  147. localstack/services/sns/v2/utils.py +130 -0
  148. localstack/services/sqs/constants.py +1 -1
  149. localstack/services/sqs/developer_api.py +205 -0
  150. localstack/services/sqs/models.py +48 -5
  151. localstack/services/sqs/provider.py +38 -311
  152. localstack/services/sqs/query_api.py +6 -2
  153. localstack/services/sqs/utils.py +121 -2
  154. localstack/services/ssm/provider.py +1 -1
  155. localstack/services/stepfunctions/asl/component/intrinsic/member.py +1 -1
  156. localstack/services/stepfunctions/asl/component/state/state_choice/comparison/comparison.py +5 -11
  157. localstack/services/stepfunctions/asl/component/state/state_choice/state_choice.py +2 -2
  158. localstack/services/stepfunctions/asl/component/state/state_execution/state_map/state_map.py +2 -2
  159. localstack/services/stepfunctions/asl/component/state/state_execution/state_parallel/state_parallel.py +1 -1
  160. localstack/services/stepfunctions/asl/component/state/state_execution/state_task/state_task.py +2 -2
  161. localstack/services/stepfunctions/asl/component/state/state_fail/state_fail.py +1 -1
  162. localstack/services/stepfunctions/asl/component/state/state_pass/state_pass.py +2 -2
  163. localstack/services/stepfunctions/asl/component/state/state_succeed/state_succeed.py +1 -1
  164. localstack/services/stepfunctions/asl/component/state/state_wait/state_wait.py +1 -1
  165. localstack/services/stepfunctions/asl/eval/environment.py +1 -1
  166. localstack/services/stepfunctions/asl/jsonata/jsonata.py +1 -1
  167. localstack/services/stepfunctions/backend/execution.py +2 -1
  168. localstack/services/stores.py +1 -1
  169. localstack/services/transcribe/provider.py +6 -1
  170. localstack/state/codecs.py +61 -0
  171. localstack/state/core.py +11 -5
  172. localstack/state/pickle.py +10 -49
  173. localstack/testing/aws/cloudformation_utils.py +1 -1
  174. localstack/testing/pytest/cloudformation/fixtures.py +3 -3
  175. localstack/testing/pytest/cloudformation/transformers.py +0 -0
  176. localstack/testing/pytest/container.py +4 -5
  177. localstack/testing/pytest/fixtures.py +33 -31
  178. localstack/testing/pytest/in_memory_localstack.py +0 -4
  179. localstack/testing/pytest/marking.py +38 -11
  180. localstack/testing/pytest/stepfunctions/utils.py +4 -3
  181. localstack/testing/pytest/util.py +1 -1
  182. localstack/testing/pytest/validation_tracking.py +1 -2
  183. localstack/testing/snapshots/transformer_utility.py +6 -1
  184. localstack/utils/analytics/events.py +2 -2
  185. localstack/utils/analytics/metadata.py +6 -4
  186. localstack/utils/analytics/metrics/counter.py +8 -15
  187. localstack/utils/analytics/publisher.py +1 -2
  188. localstack/utils/analytics/service_providers.py +19 -0
  189. localstack/utils/analytics/service_request_aggregator.py +2 -2
  190. localstack/utils/archives.py +11 -11
  191. localstack/utils/asyncio.py +2 -2
  192. localstack/utils/aws/arns.py +24 -29
  193. localstack/utils/aws/aws_responses.py +8 -8
  194. localstack/utils/aws/aws_stack.py +2 -3
  195. localstack/utils/aws/dead_letter_queue.py +1 -5
  196. localstack/utils/aws/message_forwarding.py +1 -2
  197. localstack/utils/aws/request_context.py +4 -5
  198. localstack/utils/aws/resources.py +1 -1
  199. localstack/utils/aws/templating.py +1 -1
  200. localstack/utils/batch_policy.py +3 -3
  201. localstack/utils/bootstrap.py +21 -13
  202. localstack/utils/catalog/catalog.py +139 -0
  203. localstack/utils/catalog/catalog_loader.py +119 -0
  204. localstack/utils/catalog/common.py +58 -0
  205. localstack/utils/catalog/plugins.py +28 -0
  206. localstack/utils/cloudwatch/cloudwatch_util.py +5 -5
  207. localstack/utils/collections.py +7 -8
  208. localstack/utils/config_listener.py +1 -1
  209. localstack/utils/container_networking.py +2 -3
  210. localstack/utils/container_utils/container_client.py +135 -136
  211. localstack/utils/container_utils/docker_cmd_client.py +85 -69
  212. localstack/utils/container_utils/docker_sdk_client.py +69 -66
  213. localstack/utils/crypto.py +10 -10
  214. localstack/utils/diagnose.py +3 -4
  215. localstack/utils/docker_utils.py +9 -5
  216. localstack/utils/files.py +33 -13
  217. localstack/utils/functions.py +4 -3
  218. localstack/utils/http.py +11 -11
  219. localstack/utils/json.py +20 -6
  220. localstack/utils/kinesis/kinesis_connector.py +2 -1
  221. localstack/utils/net.py +15 -9
  222. localstack/utils/no_exit_argument_parser.py +2 -2
  223. localstack/utils/numbers.py +9 -2
  224. localstack/utils/objects.py +7 -6
  225. localstack/utils/patch.py +10 -3
  226. localstack/utils/run.py +12 -11
  227. localstack/utils/scheduler.py +11 -11
  228. localstack/utils/server/tcp_proxy.py +2 -2
  229. localstack/utils/serving.py +3 -4
  230. localstack/utils/strings.py +15 -16
  231. localstack/utils/sync.py +126 -1
  232. localstack/utils/tagging.py +8 -6
  233. localstack/utils/testutil.py +8 -8
  234. localstack/utils/threads.py +2 -2
  235. localstack/utils/time.py +12 -4
  236. localstack/utils/urls.py +1 -3
  237. localstack/utils/xray/traceid.py +1 -1
  238. localstack/version.py +16 -3
  239. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/METADATA +18 -14
  240. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/RECORD +248 -239
  241. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/entry_points.txt +8 -4
  242. localstack_core-4.10.1.dev12.dist-info/plux.json +1 -0
  243. localstack/packages/terraform.py +0 -46
  244. localstack/services/cloudformation/deploy.html +0 -144
  245. localstack/services/cloudformation/deploy_ui.py +0 -47
  246. localstack/services/cloudformation/plugins.py +0 -12
  247. localstack_core-4.7.1.dev49.dist-info/plux.json +0 -1
  248. {localstack_core-4.7.1.dev49.data → localstack_core-4.10.1.dev12.data}/scripts/localstack +0 -0
  249. {localstack_core-4.7.1.dev49.data → localstack_core-4.10.1.dev12.data}/scripts/localstack-supervisor +0 -0
  250. {localstack_core-4.7.1.dev49.data → localstack_core-4.10.1.dev12.data}/scripts/localstack.bat +0 -0
  251. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/WHEEL +0 -0
  252. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/licenses/LICENSE.txt +0 -0
  253. {localstack_core-4.7.1.dev49.dist-info → localstack_core-4.10.1.dev12.dist-info}/top_level.txt +0 -0
@@ -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
 
@@ -234,13 +247,21 @@ class RequestParser(abc.ABC):
234
247
  if location is not None:
235
248
  if location == "header":
236
249
  header_name = shape.serialization.get("name")
237
- payload = request.headers.get(header_name)
238
- if payload and shape.type_name == "list":
250
+ if shape.type_name == "list":
239
251
  # headers may contain a comma separated list of values (e.g., the ObjectAttributes member in
240
252
  # s3.GetObjectAttributes), so we prepare it here for the handler, which will be `_parse_list`.
241
253
  # Header lists can contain optional whitespace, so we strip it
242
254
  # https://www.rfc-editor.org/rfc/rfc9110.html#name-lists-rule-abnf-extension
243
- payload = [value.strip() for value in payload.split(",")]
255
+ # It can also directly contain a list of headers
256
+ # See https://datatracker.ietf.org/doc/html/rfc2616
257
+ payload = request.headers.getlist(header_name) or None
258
+ if payload:
259
+ headers = ",".join(payload)
260
+ payload = [value.strip() for value in headers.split(",")]
261
+
262
+ else:
263
+ payload = request.headers.get(header_name)
264
+
244
265
  elif location == "headers":
245
266
  payload = self._parse_header_map(shape, request.headers)
246
267
  # shapes with the location trait "headers" only contain strings and are not further processed
@@ -257,12 +278,12 @@ class RequestParser(abc.ABC):
257
278
  if uri_param_name in uri_params:
258
279
  payload = uri_params[uri_param_name]
259
280
  else:
260
- raise UnknownParserError("Unknown shape location '%s'." % location)
281
+ raise UnknownParserError(f"Unknown shape location '{location}'.")
261
282
  else:
262
283
  # If we don't have to use a specific location, we use the node
263
284
  payload = node
264
285
 
265
- fn_name = "_parse_%s" % shape.type_name
286
+ fn_name = f"_parse_{shape.type_name}"
266
287
  handler = getattr(self, fn_name, self._noop_parser)
267
288
  try:
268
289
  return handler(request, shape, payload, uri_params) if payload is not None else None
@@ -314,7 +335,7 @@ class RequestParser(abc.ABC):
314
335
  return True
315
336
  if value == "false":
316
337
  return False
317
- raise ValueError("cannot parse boolean value %s" % node)
338
+ raise ValueError(f"cannot parse boolean value {node}")
318
339
 
319
340
  @_text_content
320
341
  def _noop_parser(self, _, __, node: Any, ___):
@@ -324,11 +345,11 @@ class RequestParser(abc.ABC):
324
345
  _parse_double = _parse_float
325
346
  _parse_long = _parse_integer
326
347
 
327
- 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:
328
349
  if timestamp_format is None:
329
350
  timestamp_format = self.TIMESTAMP_FORMAT
330
351
  timestamp_format = timestamp_format.lower()
331
- converter = getattr(self, "_timestamp_%s" % timestamp_format)
352
+ converter = getattr(self, f"_timestamp_{timestamp_format}")
332
353
  final_value = converter(value)
333
354
  return final_value
334
355
 
@@ -338,11 +359,11 @@ class RequestParser(abc.ABC):
338
359
 
339
360
  @staticmethod
340
361
  def _timestamp_unixtimestamp(timestamp_string: str) -> datetime.datetime:
341
- return datetime.datetime.utcfromtimestamp(int(timestamp_string))
362
+ return datetime.datetime.fromtimestamp(int(timestamp_string), tz=datetime.UTC)
342
363
 
343
364
  @staticmethod
344
365
  def _timestamp_unixtimestampmillis(timestamp_string: str) -> datetime.datetime:
345
- return datetime.datetime.utcfromtimestamp(float(timestamp_string) / 1000)
366
+ return datetime.datetime.fromtimestamp(float(timestamp_string) / 1000, tz=datetime.UTC)
346
367
 
347
368
  @staticmethod
348
369
  def _timestamp_rfc822(datetime_string: str) -> datetime.datetime:
@@ -663,7 +684,7 @@ class RestXMLRequestParser(BaseRestRequestParser):
663
684
  """
664
685
 
665
686
  def __init__(self, service_model: ServiceModel):
666
- super(RestXMLRequestParser, self).__init__(service_model)
687
+ super().__init__(service_model)
667
688
  self.ignore_get_body_errors = True
668
689
  self._namespace_re = re.compile("{.*}")
669
690
 
@@ -732,7 +753,7 @@ class RestXMLRequestParser(BaseRestRequestParser):
732
753
  elif tag_name == value_location_name:
733
754
  val_name = self._parse_shape(request, value_shape, single_pair, uri_params)
734
755
  else:
735
- raise ProtocolParserError("Unknown tag: %s" % tag_name)
756
+ raise ProtocolParserError(f"Unknown tag: {tag_name}")
736
757
  parsed[key_name] = val_name
737
758
  return parsed
738
759
 
@@ -750,7 +771,7 @@ class RestXMLRequestParser(BaseRestRequestParser):
750
771
  # it's flattened, and if it's not, then we make it a one element list.
751
772
  if shape.serialization.get("flattened") and not isinstance(node, list):
752
773
  node = [node]
753
- return super(RestXMLRequestParser, self)._parse_list(request, shape, node, uri_params)
774
+ return super()._parse_list(request, shape, node, uri_params)
754
775
 
755
776
  def _node_tag(self, node: ETree.Element) -> str:
756
777
  return self._namespace_re.sub("", node.tag)
@@ -778,7 +799,7 @@ class RestXMLRequestParser(BaseRestRequestParser):
778
799
  root = parser.close()
779
800
  except ETree.ParseError as e:
780
801
  raise ProtocolParserError(
781
- "Unable to parse request (%s), invalid XML received:\n%s" % (e, xml_string)
802
+ f"Unable to parse request ({e}), invalid XML received:\n{xml_string}"
782
803
  ) from e
783
804
  return root
784
805
 
@@ -968,6 +989,405 @@ class RestJSONRequestParser(BaseRestRequestParser, BaseJSONRequestParser):
968
989
  raise NotImplementedError
969
990
 
970
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 initial 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
+
971
1391
  class EC2RequestParser(QueryRequestParser):
972
1392
  """
973
1393
  The ``EC2RequestParser`` is responsible for parsing incoming requests for services which use the ``ec2``
@@ -1146,11 +1566,12 @@ class SQSQueryRequestParser(QueryRequestParser):
1146
1566
 
1147
1567
 
1148
1568
  @functools.cache
1149
- def create_parser(service: ServiceModel) -> RequestParser:
1569
+ def create_parser(service: ServiceModel, protocol: ProtocolName | None = None) -> RequestParser:
1150
1570
  """
1151
1571
  Creates the right parser for the given service model.
1152
1572
 
1153
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
1154
1575
  :return: RequestParser which can handle the protocol of the service
1155
1576
  """
1156
1577
  # Unfortunately, some services show subtle differences in their parsing or operation detection behavior, even though
@@ -1168,14 +1589,20 @@ def create_parser(service: ServiceModel) -> RequestParser:
1168
1589
  "rest-json": RestJSONRequestParser,
1169
1590
  "rest-xml": RestXMLRequestParser,
1170
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``
1171
1596
  }
1172
1597
 
1598
+ service_protocol = protocol or service.protocol
1599
+
1173
1600
  # Try to select a service- and protocol-specific parser implementation
1174
1601
  if (
1175
1602
  service.service_name in service_specific_parsers
1176
- and service.protocol in service_specific_parsers[service.service_name]
1603
+ and service_protocol in service_specific_parsers[service.service_name]
1177
1604
  ):
1178
- return service_specific_parsers[service.service_name][service.protocol](service)
1605
+ return service_specific_parsers[service.service_name][service_protocol](service)
1179
1606
  else:
1180
1607
  # Otherwise, pick the protocol-specific parser for the protocol of the service
1181
- return protocol_specific_parsers[service.protocol](service)
1608
+ return protocol_specific_parsers[service_protocol](service)