Skip to content

Commit 199da47

Browse files
feat: allow the AWS_DEFAULT_REGION environment variable (#721)
Amazon has this variable documented, and apparently people are trying to use it, so we should support it
1 parent d80c85f commit 199da47

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

google/auth/aws.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ def retrieve_subject_token(self, request):
424424
425425
The logic is summarized as:
426426
427-
Retrieve the AWS region from the AWS_REGION environment variable or from
428-
the AWS metadata server availability-zone if not found in the
429-
environment variable.
427+
Retrieve the AWS region from the AWS_REGION or AWS_DEFAULT_REGION
428+
environment variable or from the AWS metadata server availability-zone
429+
if not found in the environment variable.
430430
431431
Check AWS credentials in environment variables. If not found, retrieve
432432
from the AWS metadata server security-credentials endpoint.
@@ -504,8 +504,8 @@ def retrieve_subject_token(self, request):
504504
)
505505

506506
def _get_region(self, request, url):
507-
"""Retrieves the current AWS region from either the AWS_REGION
508-
environment variable or from the AWS metadata server.
507+
"""Retrieves the current AWS region from either the AWS_REGION or
508+
AWS_DEFAULT_REGION environment variable or from the AWS metadata server.
509509
510510
Args:
511511
request (google.auth.transport.Request): A callable used to make
@@ -526,6 +526,10 @@ def _get_region(self, request, url):
526526
if env_aws_region is not None:
527527
return env_aws_region
528528

529+
env_aws_region = os.environ.get(environment_vars.AWS_DEFAULT_REGION)
530+
if env_aws_region is not None:
531+
return env_aws_region
532+
529533
if not self._region_url:
530534
raise exceptions.RefreshError("Unable to determine AWS region")
531535
response = request(url=self._region_url, method="GET")

google/auth/environment_vars.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@
6969
AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"
7070
AWS_SESSION_TOKEN = "AWS_SESSION_TOKEN"
7171
AWS_REGION = "AWS_REGION"
72+
AWS_DEFAULT_REGION = "AWS_DEFAULT_REGION"

tests/test_aws.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,56 @@ def test_retrieve_subject_token_success_environment_vars(self, utcnow, monkeypat
10431043
}
10441044
)
10451045

1046+
@mock.patch("google.auth._helpers.utcnow")
1047+
def test_retrieve_subject_token_success_environment_vars_with_default_region(
1048+
self, utcnow, monkeypatch
1049+
):
1050+
monkeypatch.setenv(environment_vars.AWS_ACCESS_KEY_ID, ACCESS_KEY_ID)
1051+
monkeypatch.setenv(environment_vars.AWS_SECRET_ACCESS_KEY, SECRET_ACCESS_KEY)
1052+
monkeypatch.setenv(environment_vars.AWS_SESSION_TOKEN, TOKEN)
1053+
monkeypatch.setenv(environment_vars.AWS_DEFAULT_REGION, self.AWS_REGION)
1054+
utcnow.return_value = datetime.datetime.strptime(
1055+
self.AWS_SIGNATURE_TIME, "%Y-%m-%dT%H:%M:%SZ"
1056+
)
1057+
credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE)
1058+
1059+
subject_token = credentials.retrieve_subject_token(None)
1060+
1061+
assert subject_token == self.make_serialized_aws_signed_request(
1062+
{
1063+
"access_key_id": ACCESS_KEY_ID,
1064+
"secret_access_key": SECRET_ACCESS_KEY,
1065+
"security_token": TOKEN,
1066+
}
1067+
)
1068+
1069+
@mock.patch("google.auth._helpers.utcnow")
1070+
def test_retrieve_subject_token_success_environment_vars_with_both_regions_set(
1071+
self, utcnow, monkeypatch
1072+
):
1073+
monkeypatch.setenv(environment_vars.AWS_ACCESS_KEY_ID, ACCESS_KEY_ID)
1074+
monkeypatch.setenv(environment_vars.AWS_SECRET_ACCESS_KEY, SECRET_ACCESS_KEY)
1075+
monkeypatch.setenv(environment_vars.AWS_SESSION_TOKEN, TOKEN)
1076+
monkeypatch.setenv(environment_vars.AWS_DEFAULT_REGION, "Malformed AWS Region")
1077+
# This test makes sure that the AWS_REGION gets used over AWS_DEFAULT_REGION,
1078+
# So, AWS_DEFAULT_REGION is set to something that would cause the test to fail,
1079+
# And AWS_REGION is set to the a valid value, and it should succeed
1080+
monkeypatch.setenv(environment_vars.AWS_REGION, self.AWS_REGION)
1081+
utcnow.return_value = datetime.datetime.strptime(
1082+
self.AWS_SIGNATURE_TIME, "%Y-%m-%dT%H:%M:%SZ"
1083+
)
1084+
credentials = self.make_credentials(credential_source=self.CREDENTIAL_SOURCE)
1085+
1086+
subject_token = credentials.retrieve_subject_token(None)
1087+
1088+
assert subject_token == self.make_serialized_aws_signed_request(
1089+
{
1090+
"access_key_id": ACCESS_KEY_ID,
1091+
"secret_access_key": SECRET_ACCESS_KEY,
1092+
"security_token": TOKEN,
1093+
}
1094+
)
1095+
10461096
@mock.patch("google.auth._helpers.utcnow")
10471097
def test_retrieve_subject_token_success_environment_vars_no_session_token(
10481098
self, utcnow, monkeypatch

0 commit comments

Comments
 (0)