-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_s3.py
2218 lines (2019 loc) · 89 KB
/
test_s3.py
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2015 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.
import re
from tests import temporary_file
from tests import unittest, mock, BaseSessionTest, create_session, ClientHTTPStubber
from nose.tools import assert_equal
import botocore.session
from botocore.config import Config
from botocore.compat import datetime, urlsplit, parse_qs
from botocore.exceptions import ParamValidationError, ClientError
from botocore.exceptions import InvalidS3UsEast1RegionalEndpointConfigError
from botocore.parsers import ResponseParserError
from botocore import UNSIGNED
class TestS3BucketValidation(unittest.TestCase):
def test_invalid_bucket_name_raises_error(self):
session = botocore.session.get_session()
s3 = session.create_client('s3')
with self.assertRaises(ParamValidationError):
s3.put_object(Bucket='adfgasdfadfs/bucket/name',
Key='foo', Body=b'asdf')
class BaseS3OperationTest(BaseSessionTest):
def setUp(self):
super(BaseS3OperationTest, self).setUp()
self.region = 'us-west-2'
self.client = self.session.create_client(
's3', self.region)
self.http_stubber = ClientHTTPStubber(self.client)
class BaseS3ClientConfigurationTest(BaseSessionTest):
def setUp(self):
super(BaseS3ClientConfigurationTest, self).setUp()
self.region = 'us-west-2'
def create_s3_client(self, **kwargs):
client_kwargs = {
'region_name': self.region
}
client_kwargs.update(kwargs)
return self.session.create_client('s3', **client_kwargs)
def set_config_file(self, fileobj, contents):
fileobj.write(contents)
fileobj.flush()
self.environ['AWS_CONFIG_FILE'] = fileobj.name
class TestS3ClientConfigResolution(BaseS3ClientConfigurationTest):
def test_no_s3_config(self):
client = self.create_s3_client()
self.assertIsNone(client.meta.config.s3)
def test_client_s3_dualstack_handles_uppercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_dualstack_endpoint = True'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['use_dualstack_endpoint'], True)
def test_client_s3_dualstack_handles_lowercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_dualstack_endpoint = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['use_dualstack_endpoint'], True)
def test_client_s3_accelerate_handles_uppercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_accelerate_endpoint = True'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['use_accelerate_endpoint'], True)
def test_client_s3_accelerate_handles_lowercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_accelerate_endpoint = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['use_accelerate_endpoint'], True)
def test_client_payload_signing_enabled_handles_uppercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' payload_signing_enabled = True'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['payload_signing_enabled'], True)
def test_client_payload_signing_enabled_handles_lowercase_true(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' payload_signing_enabled = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['payload_signing_enabled'], True)
def test_includes_unmodeled_s3_config_vars(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' unmodeled = unmodeled_val'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3['unmodeled'], 'unmodeled_val')
def test_mixed_modeled_and_unmodeled_config_vars(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' payload_signing_enabled = true\n'
' unmodeled = unmodeled_val'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'payload_signing_enabled': True,
'unmodeled': 'unmodeled_val'
}
)
def test_use_arn_region(self):
self.environ['AWS_S3_USE_ARN_REGION'] = 'true'
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': True,
}
)
def test_use_arn_region_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3_use_arn_region = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': True,
}
)
def test_use_arn_region_nested_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_arn_region = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': True,
}
)
def test_use_arn_region_is_case_insensitive(self):
self.environ['AWS_S3_USE_ARN_REGION'] = 'True'
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': True,
}
)
def test_use_arn_region_env_var_overrides_config_var(self):
self.environ['AWS_S3_USE_ARN_REGION'] = 'false'
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_arn_region = true'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': False,
}
)
def test_client_config_use_arn_region_overrides_env_var(self):
self.environ['AWS_S3_USE_ARN_REGION'] = 'true'
client = self.create_s3_client(
config=Config(
s3={'use_arn_region': False}
)
)
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': False,
}
)
def test_client_config_use_arn_region_overrides_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' use_arn_region = true'
)
client = self.create_s3_client(
config=Config(
s3={'use_arn_region': False}
)
)
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': False,
}
)
def test_use_arn_region_is_case_insensitive(self):
self.environ['AWS_S3_USE_ARN_REGION'] = 'True'
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'use_arn_region': True,
}
)
def test_us_east_1_regional_env_var(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'regional',
}
)
def test_us_east_1_regional_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3_us_east_1_regional_endpoint = regional'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'regional',
}
)
def test_us_east_1_regional_nested_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' us_east_1_regional_endpoint = regional'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'regional',
}
)
def test_us_east_1_regional_env_var_overrides_config_var(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' us_east_1_regional_endpoint = legacy'
)
client = self.create_s3_client()
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'regional',
}
)
def test_client_config_us_east_1_regional_overrides_env_var(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
client = self.create_s3_client(
config=Config(
s3={'us_east_1_regional_endpoint': 'legacy'}
)
)
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'legacy',
}
)
def test_client_config_us_east_1_regional_overrides_config_var(self):
with temporary_file('w') as f:
self.set_config_file(
f,
'[default]\n'
's3 = \n'
' us_east_1_regional_endpoint = legacy'
)
client = self.create_s3_client(
config=Config(
s3={'us_east_1_regional_endpoint': 'regional'}
)
)
self.assertEqual(
client.meta.config.s3,
{
'us_east_1_regional_endpoint': 'regional',
}
)
def test_client_validates_us_east_1_regional(self):
with self.assertRaises(InvalidS3UsEast1RegionalEndpointConfigError):
self.create_s3_client(
config=Config(
s3={'us_east_1_regional_endpoint': 'not-valid'}
)
)
def test_client_region_defaults_to_us_east_1(self):
client = self.create_s3_client(region_name=None)
self.assertEqual(client.meta.region_name, 'us-east-1')
def test_client_region_remains_us_east_1(self):
client = self.create_s3_client(region_name='us-east-1')
self.assertEqual(client.meta.region_name, 'us-east-1')
def test_client_region_remains_aws_global(self):
client = self.create_s3_client(region_name='aws-global')
self.assertEqual(client.meta.region_name, 'aws-global')
def test_client_region_defaults_to_aws_global_for_regional(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
client = self.create_s3_client(region_name=None)
self.assertEqual(client.meta.region_name, 'aws-global')
def test_client_region_remains_us_east_1_for_regional(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
client = self.create_s3_client(region_name='us-east-1')
self.assertEqual(client.meta.region_name, 'us-east-1')
def test_client_region_remains_aws_global_for_regional(self):
self.environ['AWS_S3_US_EAST_1_REGIONAL_ENDPOINT'] = 'regional'
client = self.create_s3_client(region_name='aws-global')
self.assertEqual(client.meta.region_name, 'aws-global')
class TestS3Copy(BaseS3OperationTest):
def create_s3_client(self, **kwargs):
client_kwargs = {
'region_name': self.region
}
client_kwargs.update(kwargs)
return self.session.create_client('s3', **client_kwargs)
def create_stubbed_s3_client(self, **kwargs):
client = self.create_s3_client(**kwargs)
http_stubber = ClientHTTPStubber(client)
http_stubber.start()
return client, http_stubber
def test_s3_copy_object_with_empty_response(self):
self.client, self.http_stubber = self.create_stubbed_s3_client(
region_name='us-east-1'
)
empty_body = b''
complete_body = (
b'<?xml version="1.0" encoding="UTF-8"?>\n\n'
b'<CopyObjectResult '
b'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">'
b'<LastModified>2020-04-21T21:03:31.000Z</LastModified>'
b'<ETag>"s0mEcH3cK5uM"</ETag></CopyObjectResult>'
)
self.http_stubber.add_response(status=200, body=empty_body)
self.http_stubber.add_response(status=200, body=complete_body)
response = self.client.copy_object(
Bucket='bucket',
CopySource='other-bucket/test.txt',
Key='test.txt',
)
# Validate we retried and got second body
self.assertEquals(len(self.http_stubber.requests), 2)
self.assertEquals(response['ResponseMetadata']['HTTPStatusCode'], 200)
self.assertTrue('CopyObjectResult' in response)
def test_s3_copy_object_with_incomplete_response(self):
self.client, self.http_stubber = self.create_stubbed_s3_client(
region_name='us-east-1'
)
incomplete_body = b'<?xml version="1.0" encoding="UTF-8"?>\n\n\n'
self.http_stubber.add_response(status=200, body=incomplete_body)
with self.assertRaises(ResponseParserError):
self.client.copy_object(
Bucket='bucket',
CopySource='other-bucket/test.txt',
Key='test.txt',
)
class TestAccesspointArn(BaseS3ClientConfigurationTest):
_V4_AUTH_REGEX = re.compile(
r'AWS4-HMAC-SHA256 '
r'Credential=\w+/\d+/'
r'(?P<signing_region>[a-z0-9-]+)/'
r'(?P<signing_name>[a-z0-9-]+)/'
)
def setUp(self):
super(TestAccesspointArn, self).setUp()
self.client, self.http_stubber = self.create_stubbed_s3_client()
def create_stubbed_s3_client(self, **kwargs):
client = self.create_s3_client(**kwargs)
http_stubber = ClientHTTPStubber(client)
http_stubber.start()
return client, http_stubber
def assert_signing_region(self, request, expected_region):
auth_header = request.headers['Authorization'].decode('utf-8')
actual_region = self._V4_AUTH_REGEX.match(
auth_header).group('signing_region')
self.assertEqual(expected_region, actual_region)
def assert_signing_name(self, request, expected_name):
auth_header = request.headers['Authorization'].decode('utf-8')
actual_name = self._V4_AUTH_REGEX.match(
auth_header).group('signing_name')
self.assertEqual(expected_name, actual_name)
def assert_signing_region_in_url(self, url, expected_region):
qs_components = parse_qs(urlsplit(url).query)
self.assertIn(expected_region, qs_components['X-Amz-Credential'][0])
def assert_expected_copy_source_header(self,
http_stubber, expected_copy_source):
request = self.http_stubber.requests[0]
self.assertIn('x-amz-copy-source', request.headers)
self.assertEqual(
request.headers['x-amz-copy-source'], expected_copy_source)
def add_copy_object_response(self, http_stubber):
http_stubber.add_response(
body=b'<CopyObjectResult></CopyObjectResult>'
)
def assert_endpoint(self, request, expected_endpoint):
actual_endpoint = urlsplit(request.url).netloc
self.assertEqual(actual_endpoint, expected_endpoint)
def test_missing_region_in_arn(self):
accesspoint_arn = (
'arn:aws:s3::123456789012:accesspoint:myendpoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_missing_account_id_in_arn(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2::accesspoint:myendpoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_missing_accesspoint_name_in_arn(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_includes_asterisk(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:*'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_includes_dot(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:my.endpoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_arn_contains_subresources(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint:object'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_arn_with_custom_endpoint(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
endpoint_url='https://custom.com')
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_arn_with_s3_accelerate(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
config=Config(s3={'use_accelerate_endpoint': True}))
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_arn_cross_partition(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
region_name='cn-north-1')
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_accesspoint_arn_cross_partition_use_client_region(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
region_name='cn-north-1',
config=Config(s3={'use_accelerate_endpoint': True})
)
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=accesspoint_arn)
def test_signs_with_arn_region(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, self.http_stubber = self.create_stubbed_s3_client(
region_name='us-east-1')
self.http_stubber.add_response()
self.client.list_objects(Bucket=accesspoint_arn)
self.assert_signing_region(self.http_stubber.requests[0], 'us-west-2')
def test_signs_with_client_region_when_use_arn_region_false(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, self.http_stubber = self.create_stubbed_s3_client(
region_name='us-east-1',
config=Config(s3={'use_arn_region': False})
)
self.http_stubber.add_response()
self.client.list_objects(Bucket=accesspoint_arn)
self.assert_signing_region(self.http_stubber.requests[0], 'us-east-1')
def test_presign_signs_with_arn_region(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
region_name='us-east-1',
config=Config(signature_version='s3v4')
)
url = self.client.generate_presigned_url(
'get_object', {'Bucket': accesspoint_arn, 'Key': 'mykey'})
self.assert_signing_region_in_url(url, 'us-west-2')
def test_presign_signs_with_client_region_when_use_arn_region_false(self):
accesspoint_arn = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint'
)
self.client, _ = self.create_stubbed_s3_client(
region_name='us-east-1',
config=Config(
signature_version='s3v4', s3={'use_arn_region': False}
)
)
url = self.client.generate_presigned_url(
'get_object', {'Bucket': accesspoint_arn, 'Key': 'mykey'})
self.assert_signing_region_in_url(url, 'us-east-1')
def test_copy_source_str_with_accesspoint_arn(self):
copy_source = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint/'
'object/myprefix/myobject'
)
self.client, self.http_stubber = self.create_stubbed_s3_client()
self.add_copy_object_response(self.http_stubber)
self.client.copy_object(
Bucket='mybucket', Key='mykey', CopySource=copy_source
)
self.assert_expected_copy_source_header(
self.http_stubber,
expected_copy_source=(
b'arn%3Aaws%3As3%3Aus-west-2%3A123456789012%3Aaccesspoint%3A'
b'myendpoint/object/myprefix/myobject'
)
)
def test_copy_source_str_with_accesspoint_arn_and_version_id(self):
copy_source = (
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint/'
'object/myprefix/myobject?versionId=myversionid'
)
self.client, self.http_stubber = self.create_stubbed_s3_client()
self.add_copy_object_response(self.http_stubber)
self.client.copy_object(
Bucket='mybucket', Key='mykey', CopySource=copy_source
)
self.assert_expected_copy_source_header(
self.http_stubber,
expected_copy_source=(
b'arn%3Aaws%3As3%3Aus-west-2%3A123456789012%3Aaccesspoint%3A'
b'myendpoint/object/myprefix/myobject?versionId=myversionid'
)
)
def test_copy_source_dict_with_accesspoint_arn(self):
copy_source = {
'Bucket':
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint',
'Key': 'myprefix/myobject',
}
self.client, self.http_stubber = self.create_stubbed_s3_client()
self.add_copy_object_response(self.http_stubber)
self.client.copy_object(
Bucket='mybucket', Key='mykey', CopySource=copy_source
)
self.assert_expected_copy_source_header(
self.http_stubber,
expected_copy_source=(
b'arn%3Aaws%3As3%3Aus-west-2%3A123456789012%3Aaccesspoint%3A'
b'myendpoint/object/myprefix/myobject'
)
)
def test_copy_source_dict_with_accesspoint_arn_and_version_id(self):
copy_source = {
'Bucket':
'arn:aws:s3:us-west-2:123456789012:accesspoint:myendpoint',
'Key': 'myprefix/myobject',
'VersionId': 'myversionid'
}
self.client, self.http_stubber = self.create_stubbed_s3_client()
self.add_copy_object_response(self.http_stubber)
self.client.copy_object(
Bucket='mybucket', Key='mykey', CopySource=copy_source
)
self.assert_expected_copy_source_header(
self.http_stubber,
expected_copy_source=(
b'arn%3Aaws%3As3%3Aus-west-2%3A123456789012%3Aaccesspoint%3A'
b'myendpoint/object/myprefix/myobject?versionId=myversionid'
)
)
def test_basic_outpost_arn(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-01234567890123456:accesspoint:myaccesspoint'
)
self.client, self.http_stubber = self.create_stubbed_s3_client(
region_name='us-east-1')
self.http_stubber.add_response()
self.client.list_objects(Bucket=outpost_arn)
request = self.http_stubber.requests[0]
self.assert_signing_name(request, 's3-outposts')
self.assert_signing_region(request, 'us-west-2')
expected_endpoint = (
'myaccesspoint-123456789012.op-01234567890123456.'
's3-outposts.us-west-2.amazonaws.com'
)
self.assert_endpoint(request, expected_endpoint)
def test_outpost_arn_with_s3_accelerate(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-01234567890123456:accesspoint:myaccesspoint'
)
self.client, _ = self.create_stubbed_s3_client(
config=Config(s3={'use_accelerate_endpoint': True}))
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=outpost_arn)
def test_outpost_arn_with_s3_dualstack(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-01234567890123456:accesspoint:myaccesspoint'
)
self.client, _ = self.create_stubbed_s3_client(
config=Config(s3={'use_dualstack_endpoint': True}))
with self.assertRaises(
botocore.exceptions.
UnsupportedS3AccesspointConfigurationError):
self.client.list_objects(Bucket=outpost_arn)
def test_incorrect_outpost_format(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=outpost_arn)
def test_incorrect_outpost_no_accesspoint(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-01234567890123456'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=outpost_arn)
def test_incorrect_outpost_resource_format(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:myaccesspoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=outpost_arn)
def test_incorrect_outpost_sub_resources(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-01234567890123456:accesspoint:mybucket:object:foo'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=outpost_arn)
def test_incorrect_outpost_invalid_character(self):
outpost_arn = (
'arn:aws:s3-outposts:us-west-2:123456789012:outpost:'
'op-0123456.890123456:accesspoint:myaccesspoint'
)
with self.assertRaises(botocore.exceptions.ParamValidationError):
self.client.list_objects(Bucket=outpost_arn)
class TestOnlyAsciiCharsAllowed(BaseS3OperationTest):
def test_validates_non_ascii_chars_trigger_validation_error(self):
self.http_stubber.add_response()
with self.http_stubber:
with self.assertRaises(ParamValidationError):
self.client.put_object(
Bucket='foo', Key='bar', Metadata={
'goodkey': 'good', 'non-ascii': u'\u2713'})
class TestS3GetBucketLifecycle(BaseS3OperationTest):
def test_multiple_transitions_returns_one(self):
response_body = (
'<?xml version="1.0" ?>'
'<LifecycleConfiguration xmlns="http://s3.amazonaws.'
'com/doc/2006-03-01/">'
' <Rule>'
' <ID>transitionRule</ID>'
' <Prefix>foo</Prefix>'
' <Status>Enabled</Status>'
' <Transition>'
' <Days>40</Days>'
' <StorageClass>STANDARD_IA</StorageClass>'
' </Transition>'
' <Transition>'
' <Days>70</Days>'
' <StorageClass>GLACIER</StorageClass>'
' </Transition>'
' </Rule>'
' <Rule>'
' <ID>noncurrentVersionRule</ID>'
' <Prefix>bar</Prefix>'
' <Status>Enabled</Status>'
' <NoncurrentVersionTransition>'
' <NoncurrentDays>40</NoncurrentDays>'
' <StorageClass>STANDARD_IA</StorageClass>'
' </NoncurrentVersionTransition>'
' <NoncurrentVersionTransition>'
' <NoncurrentDays>70</NoncurrentDays>'
' <StorageClass>GLACIER</StorageClass>'
' </NoncurrentVersionTransition>'
' </Rule>'
'</LifecycleConfiguration>'
).encode('utf-8')
s3 = self.session.create_client('s3')
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response(body=response_body)
response = s3.get_bucket_lifecycle(Bucket='mybucket')
# Each Transition member should have at least one of the
# transitions provided.
self.assertEqual(
response['Rules'][0]['Transition'],
{'Days': 40, 'StorageClass': 'STANDARD_IA'}
)
self.assertEqual(
response['Rules'][1]['NoncurrentVersionTransition'],
{'NoncurrentDays': 40, 'StorageClass': 'STANDARD_IA'}
)
class TestS3PutObject(BaseS3OperationTest):
def test_500_error_with_non_xml_body(self):
# Note: This exact test case may not be applicable from
# an integration standpoint if the issue is fixed in the future.
#
# The issue is that:
# S3 returns a 200 response but the received response from urllib3 has
# a 500 status code and the headers are in the body of the
# the response. Botocore will try to parse out the error body as xml,
# but the body is invalid xml because it is full of headers.
# So instead of blowing up on an XML parsing error, we
# should at least use the 500 status code because that can be
# retried.
#
# We are unsure of what exactly causes the response to be mangled
# but we expect it to be how 100 continues are handled.
non_xml_content = (
'x-amz-id-2: foo\r\n'
'x-amz-request-id: bar\n'
'Date: Tue, 06 Oct 2015 03:20:38 GMT\r\n'
'ETag: "a6d856bc171fc6aa1b236680856094e2"\r\n'
'Content-Length: 0\r\n'
'Server: AmazonS3\r\n'
).encode('utf-8')
s3 = self.session.create_client('s3')
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response(status=500, body=non_xml_content)
http_stubber.add_response()
response = s3.put_object(Bucket='mybucket', Key='mykey', Body=b'foo')
# The first response should have been retried even though the xml is
# invalid and eventually return the 200 response.
self.assertEqual(response['ResponseMetadata']['HTTPStatusCode'], 200)
self.assertEqual(len(http_stubber.requests), 2)
class TestS3SigV4(BaseS3OperationTest):
def setUp(self):
super(TestS3SigV4, self).setUp()
self.client = self.session.create_client(
's3', self.region, config=Config(signature_version='s3v4'))
self.http_stubber = ClientHTTPStubber(self.client)
self.http_stubber.add_response()
def get_sent_headers(self):
return self.http_stubber.requests[0].headers
def test_content_md5_set(self):
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='baz')
self.assertIn('content-md5', self.get_sent_headers())
def test_content_md5_set_empty_body(self):
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='')
self.assertIn('content-md5', self.get_sent_headers())
def test_content_md5_set_empty_file(self):
with self.http_stubber:
with temporary_file('rb') as f:
assert f.read() == b''
self.client.put_object(Bucket='foo', Key='bar', Body=f)
self.assertIn('content-md5', self.get_sent_headers())
def test_content_sha256_set_if_config_value_is_true(self):
config = Config(signature_version='s3v4', s3={
'payload_signing_enabled': True
})
self.client = self.session.create_client(
's3', self.region, config=config)
self.http_stubber = ClientHTTPStubber(self.client)
self.http_stubber.add_response()
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='baz')
sent_headers = self.get_sent_headers()
sha_header = sent_headers.get('x-amz-content-sha256')
self.assertNotEqual(sha_header, b'UNSIGNED-PAYLOAD')
def test_content_sha256_not_set_if_config_value_is_false(self):
config = Config(signature_version='s3v4', s3={
'payload_signing_enabled': False
})
self.client = self.session.create_client(
's3', self.region, config=config)
self.http_stubber = ClientHTTPStubber(self.client)
self.http_stubber.add_response()
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='baz')
sent_headers = self.get_sent_headers()
sha_header = sent_headers.get('x-amz-content-sha256')
self.assertEqual(sha_header, b'UNSIGNED-PAYLOAD')
def test_content_sha256_set_if_md5_is_unavailable(self):
with mock.patch('botocore.auth.MD5_AVAILABLE', False):
with mock.patch('botocore.utils.MD5_AVAILABLE', False):
with self.http_stubber:
self.client.put_object(Bucket='foo', Key='bar', Body='baz')
sent_headers = self.get_sent_headers()
unsigned = 'UNSIGNED-PAYLOAD'
self.assertNotEqual(sent_headers['x-amz-content-sha256'], unsigned)
self.assertNotIn('content-md5', sent_headers)
class TestCanSendIntegerHeaders(BaseSessionTest):
def test_int_values_with_sigv4(self):
s3 = self.session.create_client(
's3', config=Config(signature_version='s3v4'))
with ClientHTTPStubber(s3) as http_stubber:
http_stubber.add_response()
s3.upload_part(Bucket='foo', Key='bar', Body=b'foo',
UploadId='bar', PartNumber=1, ContentLength=3)
headers = http_stubber.requests[0].headers
# Verify that the request integer value of 3 has been converted to
# string '3'. This also means we've made it pass the signer which
# expects string values in order to sign properly.
self.assertEqual(headers['Content-Length'], b'3')
class TestRegionRedirect(BaseS3OperationTest):
def setUp(self):
super(TestRegionRedirect, self).setUp()
self.client = self.session.create_client(
's3', 'us-west-2', config=Config(
signature_version='s3v4',
s3={'addressing_style': 'path'},
))
self.http_stubber = ClientHTTPStubber(self.client)
self.redirect_response = {