localstack-core 4.8.2.dev1__py3-none-any.whl → 4.8.2.dev3__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.

Potentially problematic release.


This version of localstack-core might be problematic. Click here for more details.

@@ -19,22 +19,24 @@ 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│
25
+ └──────────────────┘ └─────────────────────┘ └─────────────────────┘ └─────────────────────┘
26
+ ▲ ▲ ▲ ▲ ▲
27
+ ┌───────┴────────┐ ┌─────────┴──────────┐ │ │ ┌────────┴────────┐
28
+ │EC2RequestParser│ │RestXMLRequestParser│ │ │ JSONRequestParser│ │
29
+ └────────────────┘ └────────────────────┘ │ │ └─────────────────┘
30
+ ┌────────────────┴───┴┐ ▲ │
31
+ │RestJSONRequestParser│ ┌───┴──────┴──────┐
32
+ └─────────────────────┘ │CBORRequestParser│
33
+ └─────────────────┘
34
+
33
35
  ::
34
36
 
35
37
  The ``RequestParser`` contains the logic that is used among all the
36
38
  different protocols (``query``, ``json``, ``rest-json``, ``rest-xml``,
37
- and ``ec2``).
39
+ ``cbor`` and ``ec2``).
38
40
  The relation between the different protocols is described in the
39
41
  ``serializer``.
40
42
 
@@ -46,11 +48,16 @@ The classes are structured as follows:
46
48
  protocol specifics (i.e. specific HTTP metadata parsing).
47
49
  * The ``BaseJSONRequestParser`` contains the logic for the JSON body
48
50
  parsing.
51
+ * The ``BaseCBORRequestParser`` contains the logic for the CBOR body
52
+ parsing.
49
53
  * The ``RestJSONRequestParser`` inherits the ReST specific logic from
50
54
  the ``BaseRestRequestParser`` and the JSON body parsing from the
51
55
  ``BaseJSONRequestParser``.
52
- * The ``QueryRequestParser``, ``RestXMLRequestParser``, and the
53
- ``JSONRequestParser`` have a conventional inheritance structure.
56
+ * The ``CBORRequestParser`` inherits the ``json``-protocol specific
57
+ logic from the ``JSONRequestParser`` and the CBOR body parsing
58
+ from the ``BaseCBORRequestParser``.
59
+ * The ``QueryRequestParser``, ``RestXMLRequestParser`` and
60
+ ``JSONRequestParser`` have a conventional inheritance structure.
54
61
 
55
62
  The services and their protocols are defined by using AWS's Smithy
56
63
  (a language to define services in a - somewhat - protocol-agnostic
@@ -66,7 +73,10 @@ import abc
66
73
  import base64
67
74
  import datetime
68
75
  import functools
76
+ import io
77
+ import os
69
78
  import re
79
+ import struct
70
80
  from abc import ABC
71
81
  from collections.abc import Mapping
72
82
  from email.utils import parsedate_to_datetime
@@ -332,7 +342,7 @@ class RequestParser(abc.ABC):
332
342
  _parse_double = _parse_float
333
343
  _parse_long = _parse_integer
334
344
 
335
- def _convert_str_to_timestamp(self, value: str, timestamp_format=None):
345
+ def _convert_str_to_timestamp(self, value: str, timestamp_format=None) -> datetime.datetime:
336
346
  if timestamp_format is None:
337
347
  timestamp_format = self.TIMESTAMP_FORMAT
338
348
  timestamp_format = timestamp_format.lower()
@@ -346,11 +356,13 @@ class RequestParser(abc.ABC):
346
356
 
347
357
  @staticmethod
348
358
  def _timestamp_unixtimestamp(timestamp_string: str) -> datetime.datetime:
349
- return datetime.datetime.utcfromtimestamp(int(timestamp_string))
359
+ dt = datetime.datetime.fromtimestamp(int(timestamp_string), tz=datetime.UTC)
360
+ return dt.replace(tzinfo=None)
350
361
 
351
362
  @staticmethod
352
363
  def _timestamp_unixtimestampmillis(timestamp_string: str) -> datetime.datetime:
353
- return datetime.datetime.utcfromtimestamp(float(timestamp_string) / 1000)
364
+ dt = datetime.datetime.fromtimestamp(float(timestamp_string) / 1000, tz=datetime.UTC)
365
+ return dt.replace(tzinfo=None)
354
366
 
355
367
  @staticmethod
356
368
  def _timestamp_rfc822(datetime_string: str) -> datetime.datetime:
@@ -976,6 +988,262 @@ class RestJSONRequestParser(BaseRestRequestParser, BaseJSONRequestParser):
976
988
  raise NotImplementedError
977
989
 
978
990
 
991
+ class BaseCBORRequestParser(RequestParser, ABC):
992
+ """
993
+ The ``BaseCBORRequestParser`` is the base class for all CBOR-based AWS service protocols.
994
+ This base-class handles parsing the payload / body as CBOR.
995
+ """
996
+
997
+ INDEFINITE_ITEM_ADDITIONAL_INFO = 31
998
+ BREAK_CODE = 0xFF
999
+ # timestamp format for requests with CBOR content type
1000
+ TIMESTAMP_FORMAT = "unixtimestamp"
1001
+
1002
+ @functools.cached_property
1003
+ def major_type_to_parsing_method_map(self):
1004
+ return {
1005
+ 0: self._parse_type_unsigned_integer,
1006
+ 1: self._parse_type_negative_integer,
1007
+ 2: self._parse_type_byte_string,
1008
+ 3: self._parse_type_text_string,
1009
+ 4: self._parse_type_array,
1010
+ 5: self._parse_type_map,
1011
+ 6: self._parse_type_tag,
1012
+ 7: self._parse_type_simple_and_float,
1013
+ }
1014
+
1015
+ @staticmethod
1016
+ def get_peekable_stream_from_bytes(_bytes: bytes) -> io.BufferedReader:
1017
+ return io.BufferedReader(io.BytesIO(_bytes))
1018
+
1019
+ def parse_data_item(self, stream: io.BufferedReader) -> Any:
1020
+ # CBOR data is divided into "data items", and each data item starts
1021
+ # with an initial byte that describes how the following bytes should be parsed
1022
+ initial_byte = self._read_bytes_as_int(stream, 1)
1023
+ # The highest order three bits of the initial byte describe the CBOR major type
1024
+ major_type = initial_byte >> 5
1025
+ # The lowest order 5 bits of the initial byte tells us more information about
1026
+ # how the bytes should be parsed that will be used
1027
+ additional_info: int = initial_byte & 0b00011111
1028
+
1029
+ if major_type in self.major_type_to_parsing_method_map:
1030
+ method = self.major_type_to_parsing_method_map[major_type]
1031
+ return method(stream, additional_info)
1032
+ else:
1033
+ raise ProtocolParserError(
1034
+ f"Unsupported inital byte found for data item- "
1035
+ f"Major type:{major_type}, Additional info: "
1036
+ f"{additional_info}"
1037
+ )
1038
+
1039
+ # Major type 0 - unsigned integers
1040
+ def _parse_type_unsigned_integer(self, stream: io.BufferedReader, additional_info: int) -> int:
1041
+ additional_info_to_num_bytes = {
1042
+ 24: 1,
1043
+ 25: 2,
1044
+ 26: 4,
1045
+ 27: 8,
1046
+ }
1047
+ # Values under 24 don't need a full byte to be stored; their values are
1048
+ # instead stored as the "additional info" in the initial byte
1049
+ if additional_info < 24:
1050
+ return additional_info
1051
+ elif additional_info in additional_info_to_num_bytes:
1052
+ num_bytes = additional_info_to_num_bytes[additional_info]
1053
+ return self._read_bytes_as_int(stream, num_bytes)
1054
+ else:
1055
+ raise ProtocolParserError(
1056
+ "Invalid CBOR integer returned from the service; unparsable "
1057
+ f"additional info found for major type 0 or 1: {additional_info}"
1058
+ )
1059
+
1060
+ # Major type 1 - negative integers
1061
+ def _parse_type_negative_integer(self, stream: io.BufferedReader, additional_info: int) -> int:
1062
+ return -1 - self._parse_type_unsigned_integer(stream, additional_info)
1063
+
1064
+ # Major type 2 - byte string
1065
+ def _parse_type_byte_string(self, stream: io.BufferedReader, additional_info: int) -> bytes:
1066
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1067
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1068
+ return self._read_from_stream(stream, length)
1069
+ else:
1070
+ chunks = []
1071
+ while True:
1072
+ if self._handle_break_code(stream):
1073
+ break
1074
+ initial_byte = self._read_bytes_as_int(stream, 1)
1075
+ additional_info = initial_byte & 0b00011111
1076
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1077
+ chunks.append(self._read_from_stream(stream, length))
1078
+ return b"".join(chunks)
1079
+
1080
+ # Major type 3 - text string
1081
+ def _parse_type_text_string(self, stream: io.BufferedReader, additional_info: int) -> str:
1082
+ return self._parse_type_byte_string(stream, additional_info).decode("utf-8")
1083
+
1084
+ # Major type 4 - lists
1085
+ def _parse_type_array(self, stream: io.BufferedReader, additional_info: int) -> list:
1086
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1087
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1088
+ return [self.parse_data_item(stream) for _ in range(length)]
1089
+ else:
1090
+ items = []
1091
+ while not self._handle_break_code(stream):
1092
+ items.append(self.parse_data_item(stream))
1093
+ return items
1094
+
1095
+ # Major type 5 - maps
1096
+ def _parse_type_map(self, stream: io.BufferedReader, additional_info: int) -> dict:
1097
+ items = {}
1098
+ if additional_info != self.INDEFINITE_ITEM_ADDITIONAL_INFO:
1099
+ length = self._parse_type_unsigned_integer(stream, additional_info)
1100
+ for _ in range(length):
1101
+ self._parse_type_key_value_pair(stream, items)
1102
+ return items
1103
+
1104
+ else:
1105
+ while not self._handle_break_code(stream):
1106
+ self._parse_type_key_value_pair(stream, items)
1107
+ return items
1108
+
1109
+ def _parse_type_key_value_pair(self, stream: io.BufferedReader, items: dict) -> None:
1110
+ key = self.parse_data_item(stream)
1111
+ value = self.parse_data_item(stream)
1112
+ if value is not None:
1113
+ items[key] = value
1114
+
1115
+ # Major type 6 is tags. The only tag we currently support is tag 1 for unix
1116
+ # timestamps
1117
+ def _parse_type_tag(self, stream: io.BufferedReader, additional_info: int):
1118
+ tag = self._parse_type_unsigned_integer(stream, additional_info)
1119
+ value = self.parse_data_item(stream)
1120
+ if tag == 1: # Epoch-based date/time in milliseconds
1121
+ return self._parse_type_datetime(value)
1122
+ else:
1123
+ raise ProtocolParserError(f"Found CBOR tag not supported by botocore: {tag}")
1124
+
1125
+ def _parse_type_datetime(self, value: int | float) -> datetime.datetime:
1126
+ if isinstance(value, (int, float)):
1127
+ return self._convert_str_to_timestamp(str(value))
1128
+ else:
1129
+ raise ProtocolParserError(f"Unable to parse datetime value: {value}")
1130
+
1131
+ # Major type 7 includes floats and "simple" types. Supported simple types are
1132
+ # currently boolean values, CBOR's null, and CBOR's undefined type. All other
1133
+ # values are either floats or invalid.
1134
+ def _parse_type_simple_and_float(
1135
+ self, stream: io.BufferedReader, additional_info: int
1136
+ ) -> bool | float | None:
1137
+ # For major type 7, values 20-23 correspond to CBOR "simple" values
1138
+ additional_info_simple_values = {
1139
+ 20: False, # CBOR false
1140
+ 21: True, # CBOR true
1141
+ 22: None, # CBOR null
1142
+ 23: None, # CBOR undefined
1143
+ }
1144
+ # First we check if the additional info corresponds to a supported simple value
1145
+ if additional_info in additional_info_simple_values:
1146
+ return additional_info_simple_values[additional_info]
1147
+
1148
+ # If it's not a simple value, we need to parse it into the correct format and
1149
+ # number fo bytes
1150
+ float_formats = {
1151
+ 25: (">e", 2),
1152
+ 26: (">f", 4),
1153
+ 27: (">d", 8),
1154
+ }
1155
+
1156
+ if additional_info in float_formats:
1157
+ float_format, num_bytes = float_formats[additional_info]
1158
+ return struct.unpack(float_format, self._read_from_stream(stream, num_bytes))[0]
1159
+ raise ProtocolParserError(
1160
+ f"Invalid additional info found for major type 7: {additional_info}. "
1161
+ f"This indicates an unsupported simple type or an indefinite float value"
1162
+ )
1163
+
1164
+ @_text_content
1165
+ def _parse_blob(self, _, __, node: bytes, ___) -> bytes:
1166
+ return node
1167
+
1168
+ # This helper method is intended for use when parsing indefinite length items.
1169
+ # It does nothing if the next byte is not the break code. If the next byte is
1170
+ # the break code, it advances past that byte and returns True so the calling
1171
+ # method knows to stop parsing that data item.
1172
+ def _handle_break_code(self, stream: io.BufferedReader) -> bool | None:
1173
+ if int.from_bytes(stream.peek(1)[:1], "big") == self.BREAK_CODE:
1174
+ stream.seek(1, os.SEEK_CUR)
1175
+ return True
1176
+
1177
+ def _read_bytes_as_int(self, stream: IO[bytes], num_bytes: int) -> int:
1178
+ byte = self._read_from_stream(stream, num_bytes)
1179
+ return int.from_bytes(byte, "big")
1180
+
1181
+ @staticmethod
1182
+ def _read_from_stream(stream: IO[bytes], num_bytes: int) -> bytes:
1183
+ value = stream.read(num_bytes)
1184
+ if len(value) != num_bytes:
1185
+ raise ProtocolParserError(
1186
+ "End of stream reached; this indicates a "
1187
+ "malformed CBOR response from the server or an "
1188
+ "issue in botocore"
1189
+ )
1190
+ return value
1191
+
1192
+
1193
+ class CBORRequestParser(BaseCBORRequestParser, JSONRequestParser):
1194
+ """
1195
+ The ``CBORRequestParser`` is responsible for parsing incoming requests for services which use the ``cbor``
1196
+ protocol.
1197
+ The requests for these services encode the majority of their parameters as CBOR in the request body.
1198
+ The operation is defined in an HTTP header field.
1199
+ This protocol is not properly defined in the specs, but it is derived from the ``json`` protocol. Only Kinesis uses
1200
+ it for now.
1201
+ """
1202
+
1203
+ # timestamp format is different from traditional CBOR
1204
+ TIMESTAMP_FORMAT = "unixtimestampmillis"
1205
+
1206
+ def _do_parse(
1207
+ self, request: Request, shape: Shape, uri_params: Mapping[str, Any] = None
1208
+ ) -> dict:
1209
+ parsed = {}
1210
+ if shape is not None:
1211
+ event_name = shape.event_stream_name
1212
+ if event_name:
1213
+ parsed = self._handle_event_stream(request, shape, event_name)
1214
+ else:
1215
+ self._parse_payload(request, shape, parsed, uri_params)
1216
+ return parsed
1217
+
1218
+ def _handle_event_stream(self, request: Request, shape: Shape, event_name: str):
1219
+ # TODO handle event streams
1220
+ raise NotImplementedError
1221
+
1222
+ def _parse_payload(
1223
+ self,
1224
+ request: Request,
1225
+ shape: Shape,
1226
+ final_parsed: dict,
1227
+ uri_params: Mapping[str, Any] = None,
1228
+ ) -> None:
1229
+ original_parsed = self._initial_body_parse(request)
1230
+ body_parsed = self._parse_shape(request, shape, original_parsed, uri_params)
1231
+ final_parsed.update(body_parsed)
1232
+
1233
+ def _initial_body_parse(self, request: Request) -> Any:
1234
+ body_contents = request.data
1235
+ if body_contents == b"":
1236
+ return body_contents
1237
+ body_contents_stream = self.get_peekable_stream_from_bytes(body_contents)
1238
+ return self.parse_data_item(body_contents_stream)
1239
+
1240
+ def _parse_timestamp(
1241
+ self, request: Request, shape: Shape, node: str, uri_params: Mapping[str, Any] = None
1242
+ ) -> datetime.datetime:
1243
+ # TODO: remove once CBOR support has been removed from `JSONRequestParser`
1244
+ return super()._parse_timestamp(request, shape, node, uri_params)
1245
+
1246
+
979
1247
  class EC2RequestParser(QueryRequestParser):
980
1248
  """
981
1249
  The ``EC2RequestParser`` is responsible for parsing incoming requests for services which use the ``ec2``
@@ -1176,6 +1444,9 @@ def create_parser(service: ServiceModel) -> RequestParser:
1176
1444
  "rest-json": RestJSONRequestParser,
1177
1445
  "rest-xml": RestXMLRequestParser,
1178
1446
  "ec2": EC2RequestParser,
1447
+ # TODO: implement multi-protocol support for Kinesis, so that it can uses the `cbor` protocol and remove
1448
+ # CBOR handling from JSONRequestParser
1449
+ # this is not an "official" protocol defined from the spec, but is derived from ``json``
1179
1450
  }
1180
1451
 
1181
1452
  # Try to select a service- and protocol-specific parser implementation