-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[v2] Implement full object checksum validation for multipart downloads #9734
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
Open
hssyoo
wants to merge
12
commits into
aws:full-object-checksum
Choose a base branch
from
hssyoo:full-object-checksum
base: full-object-checksum
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
00cbb97
[DROP] combine_crc32 function for dev
hssyoo b51a56d
[botocore] Expose int_crc in CRC checksum classes
hssyoo ab4d661
Provide stored checksum to transfer meta
hssyoo 13cf5b1
Implement checksum classes for full object calculation
hssyoo 6314509
Implement multipart download validation
hssyoo 5003b8f
Docstrings
hssyoo 7e22525
Add unit tests for checksums.py
hssyoo f9eb2c7
Add unit tests for TransferMeta
hssyoo 124b1da
Add tests for download.py
hssyoo 7a3d484
Add unit tests for subscribers
hssyoo 86aebb5
Update existing tests
hssyoo 31b593c
Add validation debug log
hssyoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
""" | ||
NOTE: All classes and functions in this module are considered private and are | ||
subject to abrupt breaking changes. Please do not use them directly. | ||
""" | ||
|
||
import base64 | ||
import logging | ||
from copy import copy | ||
from functools import cached_property | ||
|
||
from botocore.httpchecksum import CrtCrc32Checksum | ||
from s3transfer.exceptions import S3ValidationError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PartStreamingChecksumBody: | ||
def __init__(self, stream, starting_index, full_object_checksum): | ||
self._stream = stream | ||
self._starting_index = starting_index | ||
self._checksum = CRC_CHECKSUM_CLS[ | ||
full_object_checksum.checksum_algorithm | ||
]() | ||
self._full_object_checksum = full_object_checksum | ||
# If the underlying stream already has a checksum object | ||
# it's updating (eg `botocore.httpchecksum.StreamingChecksumBody`), | ||
# reuse its calculated value. | ||
self._reuse_checksum = hasattr(self._stream, 'checksum') | ||
|
||
@property | ||
def checksum(self): | ||
return self._checksum | ||
|
||
def read(self, *args, **kwargs): | ||
value = self._stream.read(*args, **kwargs) | ||
if not self._reuse_checksum: | ||
self._checksum.update(value) | ||
if not value: | ||
self._set_part_checksum() | ||
return value | ||
|
||
def _set_part_checksum(self): | ||
if not self._reuse_checksum: | ||
value = self._checksum.int_crc | ||
else: | ||
value = self._stream.checksum.int_crc | ||
self._full_object_checksum.set_part_checksum( | ||
self._starting_index, | ||
value, | ||
) | ||
|
||
|
||
class FullObjectChecksum: | ||
def __init__(self, checksum_algorithm, content_length): | ||
self.checksum_algorithm = checksum_algorithm | ||
self._content_length = content_length | ||
self._combine_function = _CRC_CHECKSUM_TO_COMBINE_FUNCTION[ | ||
self.checksum_algorithm | ||
] | ||
self._stored_checksum = None | ||
self._part_checksums = None | ||
self._calculated_checksum = None | ||
|
||
@cached_property | ||
def calculated_checksum(self): | ||
if self._calculated_checksum is None: | ||
self._combine_part_checksums() | ||
return self._calculated_checksum | ||
|
||
def set_stored_checksum(self, stored_checksum): | ||
self._stored_checksum = stored_checksum | ||
|
||
def set_part_checksum(self, offset, checksum): | ||
if self._part_checksums is None: | ||
self._part_checksums = {} | ||
self._part_checksums[offset] = checksum | ||
|
||
def _combine_part_checksums(self): | ||
if self._part_checksums is None: | ||
return | ||
|
||
sorted_offsets = sorted(self._part_checksums.keys()) | ||
# Initialize the combined checksum to the first part's checksum value. | ||
combined = self._part_checksums[sorted_offsets[0]] | ||
# To calculate part length, take the current offset and subtract from | ||
# the next offset. If the current offset is the start of the last part, | ||
# then subtract from the total content length. eg, | ||
# (8388608, 16777216) | ||
# (16777216, self._content_length) | ||
remaining_offsets = sorted_offsets[1:] | ||
next_offsets = sorted_offsets[2:] + [self._content_length] | ||
for offset, next_offset in zip(remaining_offsets, next_offsets): | ||
part_checksum = self._part_checksums[offset] | ||
offset_len = next_offset - offset | ||
combined = self._combine_function( | ||
combined, part_checksum, offset_len | ||
) | ||
|
||
self._calculated_checksum = base64.b64encode( | ||
combined.to_bytes(4, byteorder='big') | ||
).decode('ascii') | ||
|
||
def validate(self): | ||
if self.calculated_checksum != self._stored_checksum: | ||
raise S3ValidationError( | ||
f"Calculated checksum {self.calculated_checksum} does not match " | ||
f"stored checksum {self._stored_checksum}" | ||
) | ||
logger.debug( | ||
f"Successfully validated stored checksum {self._stored_checksum} " | ||
f"against calculated checksum {self.calculated_checksum}" | ||
) | ||
|
||
|
||
def provide_checksum_to_meta(response, transfer_meta): | ||
stored_checksum = None | ||
checksum_algorithm = None | ||
checksum_type = response.get("ChecksumType") | ||
if checksum_type and checksum_type == "FULL_OBJECT": | ||
for crc_checksum in CRC_CHECKSUMS: | ||
if checksum_value := response.get(crc_checksum): | ||
stored_checksum = checksum_value | ||
checksum_algorithm = crc_checksum | ||
break | ||
transfer_meta.provide_checksum_algorithm(checksum_algorithm) | ||
transfer_meta.provide_stored_checksum(stored_checksum) | ||
|
||
|
||
def combine_crc32(crc1, crc2, len2): | ||
"""Combine two CRC32 values. | ||
|
||
:type crc1: int | ||
:param crc1: Current CRC32 integer value. | ||
|
||
:type crc2: int | ||
:param crc2: Second CRC32 integer value to combine. | ||
|
||
:type len2: int | ||
:param len2: Length of data that produced `crc2`. | ||
|
||
:rtype: int | ||
:returns: Combined CRC32 integer value. | ||
""" | ||
_GF2_DIM = 32 | ||
_CRC32_POLY = 0xEDB88320 | ||
_MASK_32BIT = 0xFFFFFFFF | ||
|
||
def _gf2_matrix_times(mat, vec): | ||
res = 0 | ||
idx = 0 | ||
while vec != 0: | ||
if vec & 1: | ||
res ^= mat[idx] | ||
vec >>= 1 | ||
idx += 1 | ||
return res | ||
|
||
def _gf2_matrix_square(square, mat): | ||
res = copy(square) | ||
for n in range(_GF2_DIM): | ||
d = mat[n] | ||
res[n] = _gf2_matrix_times(mat, d) | ||
return res | ||
|
||
even = [0] * _GF2_DIM | ||
odd = [0] * _GF2_DIM | ||
|
||
if len2 <= 0: | ||
return crc1 | ||
|
||
odd[0] = _CRC32_POLY | ||
row = 1 | ||
for i in range(1, _GF2_DIM): | ||
odd[i] = row | ||
row <<= 1 | ||
|
||
even = _gf2_matrix_square(even, odd) | ||
odd = _gf2_matrix_square(odd, even) | ||
|
||
while True: | ||
even = _gf2_matrix_square(even, odd) | ||
if len2 & 1: | ||
crc1 = _gf2_matrix_times(even, crc1) | ||
len2 >>= 1 | ||
|
||
if len2 == 0: | ||
break | ||
|
||
odd = _gf2_matrix_square(odd, even) | ||
if len2 & 1: | ||
crc1 = _gf2_matrix_times(odd, crc1) | ||
len2 >>= 1 | ||
|
||
if len2 == 0: | ||
break | ||
|
||
return (crc1 ^ crc2) & _MASK_32BIT | ||
|
||
|
||
_CRC_CHECKSUM_TO_COMBINE_FUNCTION = { | ||
"ChecksumCRC32": combine_crc32, | ||
} | ||
|
||
|
||
CRC_CHECKSUM_CLS = { | ||
"ChecksumCRC32": CrtCrc32Checksum, | ||
} | ||
|
||
|
||
CRC_CHECKSUMS = _CRC_CHECKSUM_TO_COMBINE_FUNCTION.keys() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually dependent on the checksum algorithm. I think instead I'll instantiate the appropriate checksum object, set the
int_crc
property to the combined value, then call thedigest()
method