Skip to content
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

[v2] S3 high level checksums #8933

Open
wants to merge 19 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changes/next-release/feature-s3-46667.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "s3",
"description": "Adds support for checksums other than MD5 to high-level S3 commands."
}
36 changes: 35 additions & 1 deletion awscli/customizations/s3/subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@
)
}

CHECKSUM_MODE = {
'name': 'checksum-mode', 'choices': ['ENABLED'],
'help_text': 'To retrieve the checksum, this mode must be enabled.'
}

CHECKSUM_ALGORITHM = {
'name': 'checksum-algorithm', 'choices': ['CRC32', 'SHA256', 'SHA1', 'CRC32C'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: We should consider retrieving the choices from the service model instead of hardcoding the values here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback; could be worth looking into in a future PR.

'help_text': 'Indicates the algorithm used to create the checksum for the object.'
}

TRANSFER_ARGS = [DRYRUN, QUIET, INCLUDE, EXCLUDE, ACL,
FOLLOW_SYMLINKS, NO_FOLLOW_SYMLINKS, NO_GUESS_MIME_TYPE,
SSE, SSE_C, SSE_C_KEY, SSE_KMS_KEY_ID, SSE_C_COPY_SOURCE,
Expand All @@ -489,7 +499,7 @@
CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE,
EXPIRES, SOURCE_REGION, ONLY_SHOW_ERRORS, NO_PROGRESS,
PAGE_SIZE, IGNORE_GLACIER_WARNINGS, FORCE_GLACIER_TRANSFER,
REQUEST_PAYER]
REQUEST_PAYER, CHECKSUM_MODE, CHECKSUM_ALGORITHM]


class S3Command(BasicCommand):
Expand Down Expand Up @@ -1276,6 +1286,17 @@ def _validate_path_args(self):
if self._should_emit_validate_s3_paths_warning():
self._emit_validate_s3_paths_warning()

if params.get('checksum_algorithm'):
self._raise_if_paths_type_incorrect_for_param(
CHECKSUM_ALGORITHM['name'],
params['paths_type'],
['locals3', 's3s3'])
if params.get('checksum_mode'):
self._raise_if_paths_type_incorrect_for_param(
CHECKSUM_MODE['name'],
params['paths_type'],
['s3local'])

# If the user provided local path does not exist, hard fail because
# we know that we will not be able to upload the file.
if 'locals3' == params['paths_type'] and not params['is_stream']:
Expand Down Expand Up @@ -1359,6 +1380,19 @@ def _raise_if_mv_same_paths(self, src, dest):
f"{self.parameters['src']} - {self.parameters['dest']}"
)

def _raise_if_paths_type_incorrect_for_param(self, param, paths_type, allowed_paths):
if paths_type not in allowed_paths:
expected_usage_map = {
'locals3': '<LocalPath> <S3Uri>',
's3s3': '<S3Uri> <S3Uri>',
's3local': '<S3Uri> <LocalPath>',
's3': '<S3Uri>'
}
raise ParamValidationError(
f"Expected {param} parameter to be used with one of following path formats: "
f"{', '.join(allowed_paths)} but received {expected_usage_map[paths_type]}"
aemous marked this conversation as resolved.
Show resolved Hide resolved
)

def _normalize_s3_trailing_slash(self, paths):
for i, path in enumerate(paths):
if path.startswith('s3://'):
Expand Down
13 changes: 13 additions & 0 deletions awscli/customizations/s3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,14 @@ def map_put_object_params(cls, request_params, cli_params):
cls._set_sse_request_params(request_params, cli_params)
cls._set_sse_c_request_params(request_params, cli_params)
cls._set_request_payer_param(request_params, cli_params)
cls._set_checksum_algorithm_param(request_params, cli_params)

@classmethod
def map_get_object_params(cls, request_params, cli_params):
"""Map CLI params to GetObject request params"""
cls._set_sse_c_request_params(request_params, cli_params)
cls._set_request_payer_param(request_params, cli_params)
cls._set_checksum_mode_param(request_params, cli_params)

@classmethod
def map_get_object_tagging_params(cls, request_params, cli_params):
Expand All @@ -498,6 +500,7 @@ def map_copy_object_params(cls, request_params, cli_params):
cls._set_sse_c_and_copy_source_request_params(
request_params, cli_params)
cls._set_request_payer_param(request_params, cli_params)
cls._set_checksum_algorithm_param(request_params, cli_params)

@classmethod
def map_head_object_params(cls, request_params, cli_params):
Expand Down Expand Up @@ -540,6 +543,16 @@ def _set_request_payer_param(cls, request_params, cli_params):
if cli_params.get('request_payer'):
request_params['RequestPayer'] = cli_params['request_payer']

@classmethod
def _set_checksum_mode_param(cls, request_params, cli_params):
if cli_params.get('checksum_mode'):
request_params['ChecksumMode'] = cli_params['checksum_mode']

@classmethod
def _set_checksum_algorithm_param(cls, request_params, cli_params):
if cli_params.get('checksum_algorithm'):
request_params['ChecksumAlgorithm'] = cli_params['checksum_algorithm']

@classmethod
def _set_general_object_params(cls, request_params, cli_params):
# Parameters set in this method should be applicable to the following
Expand Down
107 changes: 107 additions & 0 deletions tests/functional/s3/test_cp_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,113 @@ def test_cp_with_error_and_warning_permissions(self):
self.assertIn('upload failed', stderr)
self.assertIn('warning: File has an invalid timestamp.', stderr)

def test_upload_with_checksum_algorithm_crc32(self):
aemous marked this conversation as resolved.
Show resolved Hide resolved
full_path = self.files.create_file('foo.txt', 'contents')
cmdline = f'{self.prefix} {full_path} s3://bucket/key.txt --checksum-algorithm CRC32'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('PutObject', {
'Bucket': 'bucket',
'Key': 'key.txt',
'ChecksumAlgorithm': 'CRC32',
'Body': mock.ANY,
'ContentType': 'text/plain'
})
)

def test_upload_with_checksum_algorithm_crc32c(self):
full_path = self.files.create_file('foo.txt', 'contents')
cmdline = f'{self.prefix} {full_path} s3://bucket/key.txt --checksum-algorithm CRC32C'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('PutObject', {
'Bucket': 'bucket',
'Key': 'key.txt',
'ChecksumAlgorithm': 'CRC32C',
'Body': mock.ANY,
'ContentType': 'text/plain'
})
)

def test_multipart_upload_with_checksum_algorithm_crc32(self):
full_path = self.files.create_file('foo.txt', 'a' * 10 * MB)
self.parsed_responses = [
{'UploadId': 'foo'},
{'ETag': 'foo-e1', 'ChecksumCRC32': 'foo-1'},
{'ETag': 'foo-e2', 'ChecksumCRC32': 'foo-2'},
{}
]
cmdline = ('%s %s s3://bucket/key2.txt'
' --checksum-algorithm CRC32' % (self.prefix, full_path))
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(len(self.operations_called), 4, self.operations_called)
self.assertEqual(self.operations_called[0][0].name,'CreateMultipartUpload')
self.assertEqual(self.operations_called[0][1]['ChecksumAlgorithm'],'CRC32')
self.assertEqual(self.operations_called[1][0].name, 'UploadPart')
self.assertEqual(self.operations_called[1][1]['ChecksumAlgorithm'], 'CRC32')
self.assertEqual(self.operations_called[3][0].name, 'CompleteMultipartUpload')
self.assertEqual(self.operations_called[3][1]['MultipartUpload']['Parts'][0]['ChecksumCRC32'], 'foo-1')
self.assertEqual(self.operations_called[3][1]['MultipartUpload']['Parts'][1]['ChecksumCRC32'], 'foo-2')

def test_copy_with_checksum_algorithm_crc32(self):
self.parsed_responses = [
self.head_object_response(),
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumCRC32': 'Tq0H4g==',
'Body': BytesIO(b'foo')
}
]
cmdline = f'{self.prefix} s3://bucket1/key.txt s3://bucket2/key.txt --checksum-algorithm CRC32'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('CopyObject', {
'CopySource': {
'Bucket': 'bucket1',
'Key': 'key.txt'
},
'Bucket': 'bucket2',
'Key': 'key.txt',
'ChecksumAlgorithm': 'CRC32'
})
)

def test_download_with_checksum_mode_crc32(self):
self.parsed_responses = [
self.head_object_response(),
# Mocked GetObject response with a checksum algorithm specified
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumCRC32': 'Tq0H4g==',
'Body': BytesIO(b'foo')
}
]
cmdline = f'{self.prefix} s3://bucket/foo {self.files.rootdir} --checksum-mode ENABLED'
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(self.operations_called[1][0].name, 'GetObject')
self.assertEqual(self.operations_called[1][1]['ChecksumMode'], 'ENABLED')

def test_download_with_checksum_mode_crc32c(self):
self.parsed_responses = [
self.head_object_response(),
# Mocked GetObject response with a checksum algorithm specified
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumCRC32C': 'checksum',
'Body': BytesIO(b'foo')
}
]
cmdline = f'{self.prefix} s3://bucket/foo {self.files.rootdir} --checksum-mode ENABLED'
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(self.operations_called[1][0].name, 'GetObject')
self.assertEqual(self.operations_called[1][1]['ChecksumMode'], 'ENABLED')


class TestStreamingCPCommand(BaseAWSCommandParamsTest):
def test_streaming_upload(self):
Expand Down
32 changes: 32 additions & 0 deletions tests/functional/s3/test_mv_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,38 @@ def test_mv_does_not_delete_source_on_failed_put_tagging(self):
]
)

def test_upload_with_checksum_algorithm_crc32(self):
full_path = self.files.create_file('foo.txt', 'contents')
cmdline = f'{self.prefix} {full_path} s3://bucket/key.txt --checksum-algorithm CRC32'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('PutObject', {
'Bucket': 'bucket',
'Key': 'key.txt',
'ChecksumAlgorithm': 'CRC32',
'Body': mock.ANY,
'ContentType': 'text/plain'
})
)

def test_download_with_checksum_mode_crc32(self):
self.parsed_responses = [
self.head_object_response(),
# Mocked GetObject response with a checksum algorithm specified
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumCRC32': 'checksum',
'Body': BytesIO(b'foo')
},
self.delete_object_response()
]
cmdline = f'{self.prefix} s3://bucket/foo {self.files.rootdir} --checksum-mode ENABLED'
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(self.operations_called[1][0].name, 'GetObject')
self.assertEqual(self.operations_called[1][1]['ChecksumMode'], 'ENABLED')


class TestMvWithCRTClient(BaseCRTTransferClientTest):
def test_upload_move_using_crt_client(self):
Expand Down
95 changes: 95 additions & 0 deletions tests/functional/s3/test_sync_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,101 @@ def test_with_copy_props(self):
]
)

def test_upload_with_checksum_algorithm_sha1(self):
aemous marked this conversation as resolved.
Show resolved Hide resolved
self.files.create_file('foo.txt', 'contents')
cmdline = f'{self.prefix} {self.files.rootdir} s3://bucket/ --checksum-algorithm SHA1'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('PutObject', {
'Bucket': 'bucket',
'Key': 'foo.txt',
'ChecksumAlgorithm': 'SHA1',
'Body': mock.ANY,
'ContentType': 'text/plain'
})
)

def test_copy_with_checksum_algorithm_update_sha1(self):
cmdline = f'{self.prefix} s3://src-bucket/ s3://dest-bucket/ --checksum-algorithm SHA1'
self.parsed_responses = [
# Response for ListObjects on source bucket
self.list_objects_response(['mykey'], override_kwargs={'ChecksumAlgorithm': 'CRC32'}),
# Response for ListObjects on destination bucket
self.list_objects_response([]),
# Response for CopyObject
{
'ChecksumSHA1': 'sha1-checksum'
}
]
self.run_cmd(cmdline, expected_rc=0)
self.assert_operations_called(
[
self.list_objects_request('src-bucket'),
self.list_objects_request('dest-bucket'),
(
'CopyObject', {
'CopySource': {
'Bucket': 'src-bucket',
'Key': 'mykey'
},
'Bucket': 'dest-bucket',
'Key': 'mykey',
'ChecksumAlgorithm': 'SHA1'
}
)
]
)

def test_upload_with_checksum_algorithm_sha256(self):
self.files.create_file('foo.txt', 'contents')
cmdline = f'{self.prefix} {self.files.rootdir} s3://bucket/ --checksum-algorithm SHA256'
self.run_cmd(cmdline, expected_rc=0)
self.assert_in_operations_called(
('PutObject', {
'Bucket': 'bucket',
'Key': 'foo.txt',
'ChecksumAlgorithm': 'SHA256',
'Body': mock.ANY,
'ContentType': 'text/plain'
})
)

def test_download_with_checksum_mode_sha1(self):
self.parsed_responses = [
self.list_objects_response(['bucket']),
# Mocked GetObject response with a checksum algorithm specified
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumSHA1': 'checksum',
'Body': BytesIO(b'foo')
}
]
cmdline = f'{self.prefix} s3://bucket/foo {self.files.rootdir} --checksum-mode ENABLED'
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(self.operations_called[0][0].name, 'ListObjectsV2')
aemous marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(self.operations_called[1][0].name, 'GetObject')
self.assertIn(('ChecksumMode', 'ENABLED'), self.operations_called[1][1].items())

def test_download_with_checksum_mode_sha256(self):
self.parsed_responses = [
self.list_objects_response(['bucket']),
# Mocked GetObject response with a checksum algorithm specified
{
'ContentLength': '100',
'LastModified': '00:00:00Z',
'ETag': 'foo-1',
'ChecksumSHA256': 'checksum',
'Body': BytesIO(b'foo')
}
]
cmdline = f'{self.prefix} s3://bucket/foo {self.files.rootdir} --checksum-mode ENABLED'
self.run_cmd(cmdline, expected_rc=0)
self.assertEqual(self.operations_called[0][0].name, 'ListObjectsV2')
self.assertEqual(self.operations_called[1][0].name, 'GetObject')
self.assertIn(('ChecksumMode', 'ENABLED'), self.operations_called[1][1].items())


class TestSyncSourceRegion(BaseS3CLIRunnerTest):
def test_respects_source_region(self):
Expand Down
Loading
Loading