From 96f9896885063bec511e195885088a074fa4b5dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Tue, 20 Nov 2018 18:17:21 +0100 Subject: [PATCH 1/5] test(#1959): define dummy aws credentials for test --- tox.ini | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tox.ini b/tox.ini index 0f3f1466a..1830c2e1b 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,10 @@ deps = commands = {envpython} setup.py test nosetests {posargs} +setenv = + AWS_ACCESS_KEY_ID=dummy_key + AWS_SECRET_ACCESS_KEY=dummy_secret + AWS_DEFAULT_REGION=us-east-1 [flake8] ignore = E128,E501 From 9291ff533a467a08eb1fe50924036f4b05a0ce07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Tue, 20 Nov 2018 18:39:31 +0100 Subject: [PATCH 2/5] test(#1959): LocationConstraint us-east-1 should be refused --- tests/test_s3/test_s3.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 6e339abb6..18fb768ef 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -1300,6 +1300,16 @@ def test_bucket_create_duplicate(): exc.exception.response['Error']['Code'].should.equal('BucketAlreadyExists') +@mock_s3 +def test_bucket_create_force_us_east_1(): + s3 = boto3.resource('s3', region_name='us-east-1') + with assert_raises(ClientError) as exc: + s3.create_bucket(Bucket="blah", CreateBucketConfiguration={ + 'LocationConstraint': 'us-east-1', + }) + exc.exception.response['Error']['Code'].should.equal('InvalidLocationConstraint') + + @mock_s3 def test_boto3_bucket_create_eu_central(): s3 = boto3.resource('s3', region_name='eu-central-1') From 437eb892e23dbae7a159a67f16073d46f23977e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Tue, 20 Nov 2018 18:52:50 +0100 Subject: [PATCH 3/5] feat(#1959): LocationConstraint us-east-1 is not accepted by the CreateBucket operation --- moto/s3/responses.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 13e5f87d9..d27351e14 100755 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -432,8 +432,19 @@ class ResponseObject(_TemplateEnvironmentMixin): else: if body: + # us-east-1, the default AWS region behaves a bit differently + # - you should not use it as a location constraint --> it fails + # - querying the location constraint returns None try: - region_name = xmltodict.parse(body)['CreateBucketConfiguration']['LocationConstraint'] + forced_region = xmltodict.parse(body)['CreateBucketConfiguration']['LocationConstraint'] + + if forced_region == DEFAULT_REGION_NAME: + raise S3ClientError( + 'InvalidLocationConstraint', + 'The specified location-constraint is not valid' + ) + else: + region_name = forced_region except KeyError: pass From b0eb7b263e9a9f0aa1d33ef8cee54cba64a7976d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Tue, 20 Nov 2018 19:42:51 +0100 Subject: [PATCH 4/5] test(#1959): us-east-1 located bucket should return a None LocationConstraint --- tests/test_s3/test_s3.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 18fb768ef..6541e77b8 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -977,6 +977,15 @@ def test_bucket_location(): bucket.get_location().should.equal("us-west-2") +@mock_s3_deprecated +def test_bucket_location_us_east_1(): + cli = boto3.client('s3') + bucket_name = 'mybucket' + # No LocationConstraint ==> us-east-1 + cli.create_bucket(Bucket=bucket_name) + cli.get_bucket_location(Bucket=bucket_name)['LocationConstraint'].should.equal(None) + + @mock_s3_deprecated def test_ranged_get(): conn = boto.connect_s3() From 34ac5c72b992be546648bea960d56cbd28e0b993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Bataille?= Date: Tue, 20 Nov 2018 19:50:42 +0100 Subject: [PATCH 5/5] feat(#1959): bucket in us-east-1 return None as LocationConstraint --- moto/s3/responses.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index d27351e14..1eb010842 100755 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -193,7 +193,13 @@ class ResponseObject(_TemplateEnvironmentMixin): elif 'location' in querystring: bucket = self.backend.get_bucket(bucket_name) template = self.response_template(S3_BUCKET_LOCATION) - return template.render(location=bucket.location) + + location = bucket.location + # us-east-1 is different - returns a None location + if location == DEFAULT_REGION_NAME: + location = None + + return template.render(location=location) elif 'lifecycle' in querystring: bucket = self.backend.get_bucket(bucket_name) if not bucket.rules: @@ -1187,7 +1193,7 @@ S3_DELETE_BUCKET_WITH_ITEMS_ERROR = """ """ S3_BUCKET_LOCATION = """ -{{ location }}""" +{% if location != None %}{{ location }}{% endif %}""" S3_BUCKET_LIFECYCLE_CONFIGURATION = """