Add CRC32 to DynamoDB responses (#3677)

* Add CRC32 to DynamoDB responses

* Change test assertion

* CRC32 - Align Py2/Py3 behaviour

Co-authored-by: Bert Blommers <info@bertblommers.nl>
This commit is contained in:
Jonathan Bergknoff 2021-02-12 08:26:06 -06:00 committed by GitHub
parent b60de10c79
commit 676d61bf5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 3 deletions

View File

@ -227,10 +227,14 @@ def gen_amz_crc32(response, headerdict=None):
if not isinstance(response, bytes): if not isinstance(response, bytes):
response = response.encode() response = response.encode()
crc = str(binascii.crc32(response)) crc = binascii.crc32(response)
if six.PY2:
# https://python.readthedocs.io/en/v2.7.2/library/binascii.html
# TLDR: Use bitshift to match Py3 behaviour
crc = crc & 0xFFFFFFFF
if headerdict is not None and isinstance(headerdict, dict): if headerdict is not None and isinstance(headerdict, dict):
headerdict.update({"x-amz-crc32": crc}) headerdict.update({"x-amz-crc32": str(crc)})
return crc return crc

View File

@ -8,7 +8,7 @@ import itertools
import six import six
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from moto.core.utils import camelcase_to_underscores, amzn_request_id from moto.core.utils import camelcase_to_underscores, amz_crc32, amzn_request_id
from .exceptions import ( from .exceptions import (
InvalidIndexNameError, InvalidIndexNameError,
ItemSizeTooLarge, ItemSizeTooLarge,
@ -80,6 +80,7 @@ class DynamoHandler(BaseResponse):
""" """
return dynamodb_backends[self.region] return dynamodb_backends[self.region]
@amz_crc32
@amzn_request_id @amzn_request_id
def call_action(self): def call_action(self):
self.body = json.loads(self.body or "{}") self.body = json.loads(self.body or "{}")

View File

@ -17,3 +17,4 @@ def test_table_list():
headers = {"X-Amz-Target": "TestTable.ListTables"} headers = {"X-Amz-Target": "TestTable.ListTables"}
res = test_client.get("/", headers=headers) res = test_client.get("/", headers=headers)
res.data.should.contain(b"TableNames") res.data.should.contain(b"TableNames")
res.headers.should.have.key("X-Amz-Crc32")