-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[v2] Validate full object checksum on multipart downloads #10180
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
5
commits into
v2
Choose a base branch
from
checksum-download
base: v2
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
5 commits
Select commit
Hold shift + click to select a range
831a04c
Add full object checksum validation for multipart downloads
hssyoo 1cf0790
Add and update tests
hssyoo 552c321
Changelog entry
hssyoo 6a9fb46
Wrap body in StreamingChecksumBody to reuse botocore logic
hssyoo bcd166a
Fix SSE-C tests for ChecksumMode on HeadObject
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "type": "enhancement", | ||
| "category": "``s3``", | ||
| "description": "Automatically calculate and validate full object checksums during multipart downloads, when available." | ||
| } |
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
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,105 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import base64 | ||
| import logging | ||
| from collections import namedtuple | ||
|
|
||
| from awscrt import checksums as crt_checksums | ||
| from botocore.httpchecksum import _CHECKSUM_CLS | ||
| from s3transfer.exceptions import S3DownloadChecksumError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| CrcCombineInfo = namedtuple('CrcCombineInfo', ['combine_fn', 'byte_length']) | ||
|
|
||
|
|
||
| PartChecksum = namedtuple('PartChecksum', ['crc_int', 'data_length']) | ||
|
|
||
|
|
||
| FullObjectChecksum = namedtuple( | ||
| 'FullObjectChecksum', ['algorithm', 'expected_b64'] | ||
| ) | ||
|
|
||
|
|
||
| _CRC_COMBINE_FUNCTIONS = { | ||
| 'crc32': CrcCombineInfo(crt_checksums.combine_crc32, 4), | ||
| 'crc32c': CrcCombineInfo(crt_checksums.combine_crc32c, 4), | ||
| 'crc64nvme': CrcCombineInfo(crt_checksums.combine_crc64nvme, 8), | ||
| } | ||
|
|
||
|
|
||
| _CHECKSUM_KEY_TO_ALGORITHM = { | ||
| 'ChecksumCRC32': 'crc32', | ||
| 'ChecksumCRC32C': 'crc32c', | ||
| 'ChecksumCRC64NVME': 'crc64nvme', | ||
| } | ||
|
|
||
|
|
||
| def resolve_full_object_checksum(response): | ||
| if response.get('ChecksumType', '').upper() != 'FULL_OBJECT': | ||
| return None | ||
| for key, algorithm in _CHECKSUM_KEY_TO_ALGORITHM.items(): | ||
| value = response.get(key) | ||
| if value: | ||
| return FullObjectChecksum(algorithm=algorithm, expected_b64=value) | ||
| return None | ||
|
|
||
|
|
||
| def create_checksum_for_algorithm(algorithm): | ||
| if checksum_cls := _CHECKSUM_CLS.get(algorithm): | ||
| return checksum_cls() | ||
| return None | ||
|
|
||
|
|
||
| class FullObjectChecksumCombiner: | ||
| def __init__(self, algorithm, num_parts, expected_b64=None): | ||
| self._algorithm = algorithm | ||
| self._expected_b64 = expected_b64 | ||
| self._num_parts = num_parts | ||
| self._combine_info = _CRC_COMBINE_FUNCTIONS[algorithm] | ||
| self._parts = {} | ||
| self._combined_bytes = None | ||
|
|
||
| @property | ||
| def algorithm(self): | ||
| return self._algorithm | ||
|
|
||
| def register_part(self, part_index, checksum, data_length): | ||
| crc_int = int.from_bytes(checksum.digest(), byteorder='big') | ||
| self._parts[part_index] = PartChecksum(crc_int, data_length) | ||
|
|
||
| def combine_and_validate(self): | ||
| combined_bytes = self._get_combined_bytes() | ||
| combined_b64 = base64.b64encode(combined_bytes).decode('ascii') | ||
| expected_bytes = base64.b64decode(self._expected_b64) | ||
| if combined_bytes != expected_bytes: | ||
| raise S3DownloadChecksumError( | ||
| f'Expected full object checksum ' | ||
| f'({self._algorithm}) {self._expected_b64} did not match ' | ||
| f'combined checksum: {combined_b64}' | ||
| ) | ||
| logger.debug( | ||
| 'Full object %s checksum validated: %s', | ||
| self._algorithm, | ||
| combined_b64, | ||
| ) | ||
|
|
||
| @property | ||
| def combined_b64(self): | ||
| combined_bytes = self._get_combined_bytes() | ||
| return base64.b64encode(combined_bytes).decode('ascii') | ||
|
|
||
| def _get_combined_bytes(self): | ||
| if self._combined_bytes is not None: | ||
| return self._combined_bytes | ||
| crc = self._parts[0].crc_int | ||
| for i in range(1, self._num_parts): | ||
| part = self._parts[i] | ||
| crc = self._combine_info.combine_fn( | ||
| crc, part.crc_int, part.data_length | ||
| ) | ||
| self._combined_bytes = crc.to_bytes( | ||
| self._combine_info.byte_length, byteorder='big' | ||
| ) | ||
| return self._combined_bytes |
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
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.
Looks like when a part download fails or the transfer is cancelld, the counter still hits zero and
_finalize_downloadruns. If some parts don't have their checksums registered with the combiner,combine_and_validatehits a KeyError on the missing part. Should we checkself._transfer_coordinator.exceptionat the top and bail out early if the transfer already failed?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.
As a secondary guard,
_get_combined_bytescould validate thatlen(self._parts) == self._num_partsbefore iterating.