1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
|
# Copyright (c) 2010-2023 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
import binascii
import botocore
import hashlib
from unittest import SkipTest
from swift.common.utils import base64_str
from swift.common.utils.checksum import crc32c
from test.s3api import BaseS3TestCaseWithBucket
TEST_BODY = b'123456789'
def boto_at_least(*version):
return tuple(int(x) for x in botocore.__version__.split('.')) >= version
class ObjectChecksumMixin(object):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.client = cls.get_s3_client(1)
cls.use_tls = cls.client._endpoint.host.startswith('https:')
cls.CHECKSUM_HDR = 'x-amz-checksum-' + cls.ALGORITHM.lower()
def assert_error(self, resp, err_code, err_msg, obj_name, **extra):
self.assertEqual(400, resp['ResponseMetadata']['HTTPStatusCode'])
self.assertEqual(err_code, resp['Error']['Code'])
self.assertEqual(err_msg, resp['Error']['Message'])
self.assertEqual({k: resp['Error'].get(k) for k in extra}, extra)
# Sanity check: object was not created
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.head_object(Bucket=self.bucket_name, Key=obj_name)
resp = caught.exception.response
self.assertEqual(404, resp['ResponseMetadata']['HTTPStatusCode'])
def test_let_sdk_compute(self):
obj_name = self.create_name(self.ALGORITHM + '-sdk')
resp = self.client.put_object(
Bucket=self.bucket_name,
Key=obj_name,
Body=TEST_BODY,
ChecksumAlgorithm=self.ALGORITHM,
)
self.assertEqual(200, resp['ResponseMetadata']['HTTPStatusCode'])
def test_good_checksum(self):
obj_name = self.create_name(self.ALGORITHM + '-with-algo-header')
resp = self.client.put_object(
Bucket=self.bucket_name,
Key=obj_name,
Body=TEST_BODY,
ChecksumAlgorithm=self.ALGORITHM,
**{'Checksum' + self.ALGORITHM: self.EXPECTED}
)
self.assertEqual(200, resp['ResponseMetadata']['HTTPStatusCode'])
def test_good_checksum_no_algorithm_header(self):
obj_name = self.create_name(self.ALGORITHM + '-no-algo-header')
resp = self.client.put_object(
Bucket=self.bucket_name,
Key=obj_name,
Body=TEST_BODY,
**{'Checksum' + self.ALGORITHM: self.EXPECTED}
)
self.assertEqual(200, resp['ResponseMetadata']['HTTPStatusCode'])
def test_invalid_checksum(self):
obj_name = self.create_name(self.ALGORITHM + '-invalid')
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(
Bucket=self.bucket_name,
Key=obj_name,
Body=TEST_BODY,
ChecksumAlgorithm=self.ALGORITHM,
**{'Checksum' + self.ALGORITHM: self.INVALID}
)
self.assert_error(
caught.exception.response,
'InvalidRequest',
'Value for %s header is invalid.' % self.CHECKSUM_HDR,
obj_name,
)
def test_bad_checksum(self):
obj_name = self.create_name(self.ALGORITHM + '-bad')
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(
Bucket=self.bucket_name,
Key=obj_name,
Body=TEST_BODY,
ChecksumAlgorithm=self.ALGORITHM,
**{'Checksum' + self.ALGORITHM: self.BAD}
)
self.assert_error(
caught.exception.response,
'BadDigest',
'The %s you specified did not match the calculated checksum.'
% self.ALGORITHM,
obj_name,
)
def test_mpu_upload_part_invalid_checksum(self):
obj_name = self.create_name(
self.ALGORITHM + '-mpu-upload-part-invalid-checksum')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm=self.ALGORITHM)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
**{'Checksum' + self.ALGORITHM: self.INVALID},
)
self.assert_error(
caught.exception.response,
'InvalidRequest',
'Value for %s header is invalid.' % self.CHECKSUM_HDR,
obj_name,
)
def test_mpu_upload_part_bad_checksum(self):
obj_name = self.create_name(
self.ALGORITHM + '-mpu-upload-part-bad-checksum')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm=self.ALGORITHM)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
**{'Checksum' + self.ALGORITHM: self.BAD},
)
self.assert_error(
caught.exception.response,
'BadDigest',
'The %s you specified did not match the calculated '
'checksum.' % self.ALGORITHM,
obj_name,
)
def test_mpu_upload_part_good_checksum(self):
obj_name = self.create_name(self.ALGORITHM + '-mpu-upload-part-good')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm=self.ALGORITHM)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
**{'Checksum' + self.ALGORITHM: self.EXPECTED},
)
self.assertEqual(200, part_resp[
'ResponseMetadata']['HTTPStatusCode'])
def test_mpu_complete_good_checksum(self):
checksum_kwargs = {
'ChecksumAlgorithm': self.ALGORITHM,
}
if boto_at_least(1, 36):
if self.ALGORITHM == 'CRC64NVME':
# crc64nvme only allows full-object
checksum_kwargs['ChecksumType'] = 'FULL_OBJECT'
else:
checksum_kwargs['ChecksumType'] = 'COMPOSITE'
obj_name = self.create_name(self.ALGORITHM + '-mpu-complete-good')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
**checksum_kwargs)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
**{'Checksum' + self.ALGORITHM: self.EXPECTED},
)
self.assertEqual(200, part_resp[
'ResponseMetadata']['HTTPStatusCode'])
complete_mpu_resp = self.client.complete_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
MultipartUpload={
'Parts': [
{
'ETag': part_resp['ETag'],
'PartNumber': 1,
'Checksum' + self.ALGORITHM: self.EXPECTED,
},
],
},
UploadId=upload_id,
)
self.assertEqual(200, complete_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
class TestObjectChecksumCRC32(ObjectChecksumMixin, BaseS3TestCaseWithBucket):
ALGORITHM = 'CRC32'
EXPECTED = 'y/Q5Jg=='
INVALID = 'y/Q5Jh=='
BAD = 'z/Q5Jg=='
class TestObjectChecksumCRC32C(ObjectChecksumMixin, BaseS3TestCaseWithBucket):
ALGORITHM = 'CRC32C'
EXPECTED = '4waSgw=='
INVALID = '4waSgx=='
BAD = '5waSgw=='
@classmethod
def setUpClass(cls):
if not botocore.httpchecksum.HAS_CRT:
raise SkipTest('botocore cannot crc32c (run `pip install awscrt`)')
super().setUpClass()
class TestObjectChecksumCRC64NVME(ObjectChecksumMixin,
BaseS3TestCaseWithBucket):
ALGORITHM = 'CRC64NVME'
EXPECTED = 'rosUhgp5mIg='
INVALID = 'rosUhgp5mIh='
BAD = 'sosUhgp5mIg='
@classmethod
def setUpClass(cls):
if [int(x) for x in botocore.__version__.split('.')] < [1, 36]:
raise SkipTest('botocore cannot crc64nvme (run '
'`pip install -U boto3 botocore`)')
if not botocore.httpchecksum.HAS_CRT:
raise SkipTest('botocore cannot crc64nvme (run '
'`pip install awscrt`)')
super().setUpClass()
class TestObjectChecksumSHA1(ObjectChecksumMixin, BaseS3TestCaseWithBucket):
ALGORITHM = 'SHA1'
EXPECTED = '98O8HYCOBHMq32eZZczDTKeuNEE='
INVALID = '98O8HYCOBHMq32eZZczDTKeuNEF='
BAD = '+8O8HYCOBHMq32eZZczDTKeuNEE='
class TestObjectChecksumSHA256(ObjectChecksumMixin, BaseS3TestCaseWithBucket):
ALGORITHM = 'SHA256'
EXPECTED = 'FeKw08M4keuw8e9gnsQZQgwg4yDOlMZfvIwzEkSOsiU='
INVALID = 'FeKw08M4keuw8e9gnsQZQgwg4yDOlMZfvIwzEkSOsiV='
BAD = 'GeKw08M4keuw8e9gnsQZQgwg4yDOlMZfvIwzEkSOsiU='
class TestObjectChecksums(BaseS3TestCaseWithBucket):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.client = cls.get_s3_client(1)
cls.use_tls = cls.client._endpoint.host.startswith('https:')
def test_multi_checksum(self):
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(
Bucket=self.bucket_name,
Key=self.create_name('multi-checksum'),
Body=TEST_BODY,
# Note: Both valid! Ought to be able to validate & store both
ChecksumCRC32='y/Q5Jg==',
ChecksumSHA1='98O8HYCOBHMq32eZZczDTKeuNEE=',
)
resp = caught.exception.response
code = resp['ResponseMetadata']['HTTPStatusCode']
self.assertEqual(400, code)
self.assertEqual('InvalidRequest', resp['Error']['Code'])
self.assertEqual(
resp['Error']['Message'],
'Expecting a single x-amz-checksum- header. '
'Multiple checksum Types are not allowed.')
def test_different_checksum_requested(self):
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(
Bucket=self.bucket_name,
Key=self.create_name('different-checksum'),
Body=TEST_BODY,
ChecksumCRC32='y/Q5Jg==',
ChecksumAlgorithm='SHA1',
)
resp = caught.exception.response
code = resp['ResponseMetadata']['HTTPStatusCode']
self.assertEqual(400, code)
self.assertEqual('InvalidRequest', resp['Error']['Code'])
if boto_at_least(1, 36):
self.assertEqual(
resp['Error']['Message'],
'Value for x-amz-sdk-checksum-algorithm header is invalid.')
else:
self.assertEqual(
resp['Error']['Message'],
'Expecting a single x-amz-checksum- header')
def assert_invalid(self, resp):
code = resp['ResponseMetadata']['HTTPStatusCode']
self.assertEqual(400, code)
self.assertEqual('InvalidRequest', resp['Error']['Code'])
self.assertEqual(
resp['Error']['Message'],
'Value for x-amz-checksum-crc32 header is invalid.')
def test_invalid_base64_invalid_length(self):
put_kwargs = {
'Bucket': self.bucket_name,
'Key': self.create_name('invalid-bad-length'),
'Body': TEST_BODY,
'ChecksumCRC32': 'short===', # invalid length for base64
}
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(**put_kwargs)
self.assert_invalid(caught.exception.response)
def test_invalid_base64_too_short(self):
put_kwargs = {
'Bucket': self.bucket_name,
'Key': self.create_name('invalid-short'),
'Body': TEST_BODY,
'ChecksumCRC32': 'shrt', # only 3 bytes
}
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(**put_kwargs)
self.assert_invalid(caught.exception.response)
def test_invalid_base64_too_long(self):
put_kwargs = {
'Bucket': self.bucket_name,
'Key': self.create_name('invalid-long'),
'Body': TEST_BODY,
'ChecksumCRC32': 'toolong=', # 5 bytes
}
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(**put_kwargs)
self.assert_invalid(caught.exception.response)
def test_invalid_base64_all_invalid_chars(self):
put_kwargs = {
'Bucket': self.bucket_name,
'Key': self.create_name('purely-invalid'),
'Body': TEST_BODY,
'ChecksumCRC32': '^^^^^^==', # all invalid char
}
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(**put_kwargs)
self.assert_invalid(caught.exception.response)
def test_invalid_base64_includes_invalid_chars(self):
put_kwargs = {
'Bucket': self.bucket_name,
'Key': self.create_name('contains-invalid'),
'Body': TEST_BODY,
'ChecksumCRC32': 'y^/^Q5^J^g==', # spaced out with invalid chars
}
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.put_object(**put_kwargs)
self.assert_invalid(caught.exception.response)
def test_mpu_no_checksum_upload_part_invalid_checksum(self):
obj_name = self.create_name('no-checksum-mpu')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
ChecksumCRC32=TestObjectChecksumCRC32.INVALID,
)
self.assert_invalid(caught.exception.response)
def test_mpu_has_no_checksum(self):
# Clients don't need to be thinking about checksums at all
obj_name = self.create_name('no-checksum-mpu')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name)
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
)
complete_mpu_resp = self.client.complete_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
MultipartUpload={
'Parts': [
{
'ETag': part_resp['ETag'],
'PartNumber': 1,
},
],
},
UploadId=upload_id,
)
self.assertEqual(200, complete_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
head_resp = self.client.head_object(
Bucket=self.bucket_name, Key=obj_name)
self.assertFalse([k for k in head_resp
if k.startswith('Checksum')])
def test_mpu_upload_part_multi_checksum(self):
obj_name = self.create_name('multi-checksum-mpu')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm='CRC32C')
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=TEST_BODY,
# Both valid!
ChecksumCRC32=TestObjectChecksumCRC32.EXPECTED,
ChecksumCRC32C=TestObjectChecksumCRC32C.EXPECTED,
)
resp = caught.exception.response
self.assertEqual(400, resp['ResponseMetadata']['HTTPStatusCode'])
self.assertEqual(resp['Error'], {
'Code': 'InvalidRequest',
'Message': ('Expecting a single x-amz-checksum- header. '
'Multiple checksum Types are not allowed.'),
})
# You'd think we ought to be able to validate & store both...
def test_multipart_mpu(self):
obj_name = self.create_name('multipart-mpu')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm='CRC32C')
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
part_body = b'\x00' * 5 * 1024 * 1024
part_crc32c = base64_str(crc32c(part_body).digest())
upload_part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=part_body,
ChecksumCRC32C=part_crc32c,
)
self.assertEqual(200, upload_part_resp[
'ResponseMetadata']['HTTPStatusCode'])
# then do another
upload_part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=2,
Body=part_body,
ChecksumCRC32C=part_crc32c,
)
self.assertEqual(200, upload_part_resp[
'ResponseMetadata']['HTTPStatusCode'])
complete_mpu_resp = self.client.complete_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
MultipartUpload={
'Parts': [
{
'PartNumber': 1,
'ETag': upload_part_resp['ETag'],
'ChecksumCRC32C': part_crc32c,
},
{
'PartNumber': 2,
'ETag': upload_part_resp['ETag'],
'ChecksumCRC32C': part_crc32c,
},
],
},
UploadId=upload_id,
)
self.assertEqual(200, complete_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
mpu_etag = '"' + hashlib.md5(binascii.unhexlify(
upload_part_resp['ETag'].strip('"')) * 2).hexdigest() + '-2"'
self.assertEqual(mpu_etag,
complete_mpu_resp['ETag'])
def test_multipart_mpu_no_etags(self):
obj_name = self.create_name('multipart-mpu')
create_mpu_resp = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
ChecksumAlgorithm='CRC32C')
self.assertEqual(200, create_mpu_resp[
'ResponseMetadata']['HTTPStatusCode'])
upload_id = create_mpu_resp['UploadId']
part_body = b'\x00' * 5 * 1024 * 1024
part_crc32c = base64_str(crc32c(part_body).digest())
upload_part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=1,
Body=part_body,
ChecksumCRC32C=part_crc32c,
)
self.assertEqual(200, upload_part_resp[
'ResponseMetadata']['HTTPStatusCode'])
# then do another
upload_part_resp = self.client.upload_part(
Bucket=self.bucket_name,
Key=obj_name,
UploadId=upload_id,
PartNumber=2,
Body=part_body,
ChecksumCRC32C=part_crc32c,
)
self.assertEqual(200, upload_part_resp[
'ResponseMetadata']['HTTPStatusCode'])
with self.assertRaises(botocore.exceptions.ClientError) as caught:
self.client.complete_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
MultipartUpload={
'Parts': [
{
'PartNumber': 1,
'ChecksumCRC32C': part_crc32c,
},
{
'PartNumber': 2,
'ChecksumCRC32C': part_crc32c,
},
],
},
UploadId=upload_id,
)
resp = caught.exception.response
self.assertEqual(400, resp['ResponseMetadata']['HTTPStatusCode'])
self.assertEqual(resp['Error']['Code'], 'MalformedXML')
self.assertEqual(
resp['Error']['Message'],
'The XML you provided was not well-formed or did not validate '
'against our published schema'
)
abort_resp = self.client.abort_multipart_upload(
Bucket=self.bucket_name, Key=obj_name,
UploadId=upload_id,
)
self.assertEqual(204, abort_resp[
'ResponseMetadata']['HTTPStatusCode'])
|