Skip to content

Commit

Permalink
Fix error for when crt is missing(#2906)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlm6693 authored Apr 26, 2023
1 parent 25cc865 commit e337519
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
34 changes: 22 additions & 12 deletions botocore/httpchecksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
from hashlib import sha1, sha256

from botocore.compat import HAS_CRT
from botocore.exceptions import AwsChunkedWrapperError, FlexibleChecksumError
from botocore.exceptions import (
AwsChunkedWrapperError,
FlexibleChecksumError,
MissingDependencyException,
)
from botocore.response import StreamingBody
from botocore.utils import (
conditionally_calculate_md5,
Expand Down Expand Up @@ -250,14 +254,15 @@ def resolve_request_checksum_algorithm(
supported_algorithms = _SUPPORTED_CHECKSUM_ALGORITHMS

algorithm_name = params[algorithm_member].lower()
if algorithm_name == "crc32c" and not HAS_CRT:
raise FlexibleChecksumError(
error_msg=(
"Using CRC32C requires an additional dependency. You will "
"need to pip install botocore[crt] before proceeding."
if algorithm_name not in supported_algorithms:
if not HAS_CRT and algorithm_name in _CRT_CHECKSUM_ALGORITHMS:
raise MissingDependencyException(
msg=(
f"Using {algorithm_name.upper()} requires an "
"additional dependency. You will need to pip install "
"botocore[crt] before proceeding."
)
)
)
elif algorithm_name not in supported_algorithms:
raise FlexibleChecksumError(
error_msg="Unsupported checksum algorithm: %s" % algorithm_name
)
Expand Down Expand Up @@ -462,12 +467,17 @@ def _handle_bytes_response(http_response, response, algorithm):
"sha1": Sha1Checksum,
"sha256": Sha256Checksum,
}

_CRT_CHECKSUM_ALGORITHMS = ["crc32", "crc32c"]
if HAS_CRT:
# Use CRT checksum implementations if available
_CHECKSUM_CLS.update(
{"crc32": CrtCrc32Checksum, "crc32c": CrtCrc32cChecksum}
_CRT_CHECKSUM_CLS = {
"crc32": CrtCrc32Checksum,
"crc32c": CrtCrc32cChecksum,
}
_CHECKSUM_CLS.update(_CRT_CHECKSUM_CLS)
# Validate this list isn't out of sync with _CRT_CHECKSUM_CLS keys
assert all(
name in _CRT_CHECKSUM_ALGORITHMS for name in _CRT_CHECKSUM_CLS.keys()
)

_SUPPORTED_CHECKSUM_ALGORITHMS = list(_CHECKSUM_CLS.keys())
_ALGORITHMS_PRIORITY_LIST = ['crc32c', 'crc32', 'sha1', 'sha256']
10 changes: 7 additions & 3 deletions tests/unit/test_httpchecksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

from botocore.awsrequest import AWSResponse
from botocore.compat import HAS_CRT
from botocore.exceptions import AwsChunkedWrapperError, FlexibleChecksumError
from botocore.exceptions import (
AwsChunkedWrapperError,
FlexibleChecksumError,
MissingDependencyException,
)
from botocore.httpchecksum import (
_CHECKSUM_CLS,
AwsChunkedWrapper,
Expand Down Expand Up @@ -166,7 +170,7 @@ def test_request_checksum_algorithm_model_unsupported_algorithm(self):
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"},
)
params = {"Algorithm": "crc32"}
params = {"Algorithm": "sha256"}

with self.assertRaises(FlexibleChecksumError):
resolve_request_checksum_algorithm(
Expand All @@ -180,7 +184,7 @@ def test_request_checksum_algorithm_model_no_crt_crc32c_unsupported(self):
http_checksum={"requestAlgorithmMember": "Algorithm"},
)
params = {"Algorithm": "crc32c"}
with self.assertRaises(FlexibleChecksumError) as context:
with self.assertRaises(MissingDependencyException) as context:
resolve_request_checksum_algorithm(
request, operation_model, params
)
Expand Down

0 comments on commit e337519

Please sign in to comment.