Skip to content

Commit

Permalink
This change fixes region redirect issues for opt-in regions
Browse files Browse the repository at this point in the history
  • Loading branch information
ubaskota committed Nov 16, 2024
1 parent c05bbb3 commit 4ed5478
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions botocore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,13 +1776,18 @@ def redirect_from_error(self, request_dict, response, operation, **kwargs):
0
].status_code in (301, 302, 307)
is_permanent_redirect = error_code == 'PermanentRedirect'
is_opt_in_region = (
error_code == 'IllegalLocationConstraintException'
and operation.name == 'GetObject'
)
if not any(
[
is_special_head_object,
is_wrong_signing_region,
is_permanent_redirect,
is_special_head_bucket,
is_redirect_status,
is_opt_in_region,
]
):
return
Expand Down
91 changes: 91 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,37 @@ def test_redirects_301(self):
)
self.assertIsNone(redirect_response)

def test_redirect_get_opt_in_region(self):
request_dict = {
'url': 'https://il-central-1.amazonaws.com/foo',
'context': {
's3_redirect': {
'bucket': 'foo',
'redirected': False,
'params': {'Bucket': 'foo'},
},
'signing': {},
},
}
response = (
None,
{
'Error': {
'Code': 'IllegalLocationConstraintException',
'Message': 'Bad Request',
},
'ResponseMetadata': {
'HTTPHeaders': {'x-amz-bucket-region': 'eu-central-1'}
},
},
)

self.operation.name = 'GetObject'
redirect_response = self.redirector.redirect_from_error(
request_dict, response, self.operation
)
self.assertEqual(redirect_response, 0)

def test_redirects_400_head_bucket(self):
request_dict = {
'url': 'https://us-west-2.amazonaws.com/foo',
Expand Down Expand Up @@ -1879,6 +1910,66 @@ def test_does_not_redirect_if_None_response(self):
)
self.assertIsNone(redirect_response)

def test_redirect_in_get_opt_in_region(self):
request_dict = {
'url': 'https://il-central-1.amazonaws.com/foo',
'context': {
's3_redirect': {
'bucket': 'foo',
'redirected': False,
'params': {'Bucket': 'foo'},
},
'signing': {},
},
}
response = (
None,
{
'Error': {'Code': 'IllegalLocationConstraintException'},
'ResponseMetadata': {
'HTTPHeaders': {'x-amz-bucket-region': 'eu-central-1'}
},
},
)

self.operation.name = 'GetObject'
redirect_response = self.redirector.redirect_from_error(
request_dict, response, self.operation
)
self.assertEqual(redirect_response, 0)

def test_no_redirect_if_create_bucket_IllegalLocationConstraintException(
self,
):
request_dict = {
'url': 'https://us-west-2.amazonaws.com/foo',
'context': {
's3_redirect': {
'bucket': 'foo',
'redirected': False,
'params': {
'Bucket': 'foo',
'CreateBucketConfiguration': {
'LocationConstraint': 'eu-west-2',
},
},
},
'signing': {},
},
}
response = (
None,
{
'Error': {'Code': 'IllegalLocationConstraintException'},
},
)

self.operation.name = 'CreateBucket'
redirect_response = self.redirector.redirect_from_error(
request_dict, response, self.operation
)
self.assertIsNone(redirect_response)

def test_get_region_from_response(self):
response = (
None,
Expand Down

0 comments on commit 4ed5478

Please sign in to comment.