8000 fix CBOR datetime encoding by alexrashed · Pull Request #6791 · localstack/localstack · GitHub
[go: up one dir, main page]

Skip to content

fix CBOR datetime encoding #6791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions localstack/aws/forwarder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from urllib.parse import urlsplit

from botocore.awsrequest import AWSPreparedRequest
from botocore.config import Config as BotoConfig
from werkzeug.datastructures import Headers

from localstack import config
Expand Down Expand Up @@ -145,9 +146,14 @@ def create_aws_request_context(
service = load_service(service_name)
operation = service.operation_model(action)

# we re-use botocore internals here to serialize the HTTP request, but don't send it
# we re-use botocore internals here to serialize the HTTP request,
# but deactivate validation (validation errors should be handled by the backend)
# and don't send it yet
client = aws_stack.connect_to_service(
service_name, endpoint_url=endpoint_url, region_name=region
service_name,
endpoint_url=endpoint_url,
region_name=region,
config=BotoConfig(parameter_validation=False),
)
request_context = {
"client_region": region,
Expand Down
22 changes: 17 additions & 5 deletions localstack/aws/protocol/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,12 @@ def _get_mime_type(self, headers: Optional[Dict | Headers]) -> str:
mime_type = mime_accept.best_match(self.SUPPORTED_MIME_TYPES)
if not mime_type:
# There is no match between the supported mime types and the requested one(s)
mime_type = self.SUPPORTED_MIME_TYPES[0]
LOG.debug(
"Determined accept type (%s) is not supported by this serializer.", accept_header
"Determined accept type (%s) is not supported by this serializer. Using default of this serializer: %s",
accept_header,
mime_type,
)
mime_type = self.SUPPORTED_MIME_TYPES[0]
return mime_type

# Some extra utility methods subclasses can use.
Expand All @@ -481,6 +483,10 @@ def _timestamp_iso8601(value: datetime) -> str:
def _timestamp_unixtimestamp(value: datetime) -> float:
return value.timestamp()

@staticmethod
def _timestamp_unixtimestampmillis(value: datetime) -> int:
return int(value.timestamp() * 1000)

def _timestamp_rfc822(self, value: datetime) -> str:
if isinstance(value, datetime):
value = self._timestamp_unixtimestamp(value)
Expand Down Expand Up @@ -1288,10 +1294,16 @@ def _serialize_type_list(
def _default_serialize(self, body: dict, value: Any, _, key: str, __):
body[key] = value

def _serialize_type_timestamp(self, body: dict, value: Any, shape: Shape, key: str, _):
body[key] = self._convert_timestamp_to_str(
value, shape.serialization.get("timestampFormat")
def _serialize_type_timestamp(
self, body: dict, value: Any, shape: Shape, key: str, mime_type: str
):
timestamp_format = (
shape.serialization.get("timestampFormat")
# CBOR always uses unix timestamp milliseconds
if mime_type not in self.CBOR_TYPES
else "unixtimestampmillis"
)
body[key] = self._convert_timestamp_to_str(value, timestamp_format)

def _serialize_type_blob(
self, body: dict, value: Union[str, bytes], _, key: str, mime_type: str
Expand Down
2 changes: 2 additions & 0 deletions localstack/logging/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
"s3transfer": logging.INFO,
"urllib3": logging.WARNING,
"werkzeug": logging.WARNING,
"localstack.aws.protocol.serializer": logging.INFO,
"localstack.aws.serving.wsgi": logging.WARNING,
"localstack.request": logging.INFO,
"localstack.request.internal": logging.WARNING,
}

trace_log_levels = {
"localstack.aws.protocol.serializer": logging.DEBUG,
"localstack.aws.serving.wsgi": logging.DEBUG,
"localstack.request": logging.DEBUG,
"localstack.request.internal": logging.INFO,
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/test_kinesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import cbor2
import pytest
import requests
from botocore.config import Config as BotoConfig
from botocore.exceptions import ClientError

from localstack import config, constants
from localstack.services.kinesis import provider as kinesis_provider
Expand Down Expand Up @@ -37,6 +39,13 @@ def kinesis_snapshot_transformer(snapshot):


class TestKinesis:
def test_create_stream_without_stream_name_raises(self):
boto_config = BotoConfig(parameter_validation=False)
kinesis_client = aws_stack.create_external_boto_client("kinesis", config=boto_config)
with pytest.raises(ClientError) as e:
kinesis_client.create_stream()
assert e.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400

@pytest.mark.aws_validated
def test_create_stream_without_shard_count(
self, kinesis_client, kinesis_create_stream, wait_for_stream_ready, snapshot
Expand Down Expand Up @@ -243,6 +252,11 @@ def test_get_records(self, kinesis_client, kinesis_create_stream, wait_for_strea
assert select_attributes(json_records[0], attrs) == select_attributes(
result["Records"][0], attrs
)
# ensure that the CBOR datetime format is unix timestamp millis
assert (
int(json_records[0]["ApproximateArrivalTimestamp"].timestamp() * 1000)
== result["Records"][0]["ApproximateArrivalTimestamp"]
)

def test_get_records_empty_stream(
self, kinesis_client, kinesis_create_stream, wait_for_stream_ready
Expand Down
0