Skip to content

Commit 045776e

Browse files
authored
fix: makes default token_url universe aware (#1514)
* fix: makes default token_url universe aware * fix defaulting
1 parent 1ed4e04 commit 045776e

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

google/auth/external_account.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# Cloud resource manager URL used to retrieve project information.
5353
_CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/"
5454
# Default Google sts token url.
55-
_DEFAULT_TOKEN_URL = "https://sts.googleapis.com/v1/token"
55+
_DEFAULT_TOKEN_URL = "https://sts.{universe_domain}/v1/token"
5656

5757

5858
@dataclass
@@ -147,7 +147,12 @@ def __init__(
147147
super(Credentials, self).__init__()
148148
self._audience = audience
149149
self._subject_token_type = subject_token_type
150+
self._universe_domain = universe_domain
150151
self._token_url = token_url
152+
if self._token_url == _DEFAULT_TOKEN_URL:
153+
self._token_url = self._token_url.replace(
154+
"{universe_domain}", self._universe_domain
155+
)
151156
self._token_info_url = token_info_url
152157
self._credential_source = credential_source
153158
self._service_account_impersonation_url = service_account_impersonation_url
@@ -160,7 +165,6 @@ def __init__(
160165
self._scopes = scopes
161166
self._default_scopes = default_scopes
162167
self._workforce_pool_user_project = workforce_pool_user_project
163-
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
164168
self._trust_boundary = {
165169
"locations": [],
166170
"encoded_locations": "0x0",

tests/test_aws.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,39 @@ def test_service_account_impersonation_url_custom(self):
12201220
url + SERVICE_ACCOUNT_IMPERSONATION_URL_ROUTE
12211221
)
12221222

1223+
def test_info_with_default_token_url(self):
1224+
credentials = aws.Credentials(
1225+
audience=AUDIENCE,
1226+
subject_token_type=SUBJECT_TOKEN_TYPE,
1227+
credential_source=self.CREDENTIAL_SOURCE.copy(),
1228+
)
1229+
1230+
assert credentials.info == {
1231+
"type": "external_account",
1232+
"audience": AUDIENCE,
1233+
"subject_token_type": SUBJECT_TOKEN_TYPE,
1234+
"token_url": TOKEN_URL,
1235+
"credential_source": self.CREDENTIAL_SOURCE.copy(),
1236+
"universe_domain": DEFAULT_UNIVERSE_DOMAIN,
1237+
}
1238+
1239+
def test_info_with_default_token_url_with_universe_domain(self):
1240+
credentials = aws.Credentials(
1241+
audience=AUDIENCE,
1242+
subject_token_type=SUBJECT_TOKEN_TYPE,
1243+
credential_source=self.CREDENTIAL_SOURCE.copy(),
1244+
universe_domain="testdomain.org",
1245+
)
1246+
1247+
assert credentials.info == {
1248+
"type": "external_account",
1249+
"audience": AUDIENCE,
1250+
"subject_token_type": SUBJECT_TOKEN_TYPE,
1251+
"token_url": "https://sts.testdomain.org/v1/token",
1252+
"credential_source": self.CREDENTIAL_SOURCE.copy(),
1253+
"universe_domain": "testdomain.org",
1254+
}
1255+
12231256
def test_retrieve_subject_token_missing_region_url(self):
12241257
# When AWS_REGION envvar is not available, region_url is required for
12251258
# determining the current AWS region.

tests/test_identity_pool.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,39 @@ def test_info_with_url_credential_source(self):
782782
"universe_domain": DEFAULT_UNIVERSE_DOMAIN,
783783
}
784784

785+
def test_info_with_default_token_url(self):
786+
credentials = identity_pool.Credentials(
787+
audience=AUDIENCE,
788+
subject_token_type=SUBJECT_TOKEN_TYPE,
789+
credential_source=self.CREDENTIAL_SOURCE_TEXT_URL.copy(),
790+
)
791+
792+
assert credentials.info == {
793+
"type": "external_account",
794+
"audience": AUDIENCE,
795+
"subject_token_type": SUBJECT_TOKEN_TYPE,
796+
"token_url": TOKEN_URL,
797+
"credential_source": self.CREDENTIAL_SOURCE_TEXT_URL,
798+
"universe_domain": DEFAULT_UNIVERSE_DOMAIN,
799+
}
800+
801+
def test_info_with_default_token_url_with_universe_domain(self):
802+
credentials = identity_pool.Credentials(
803+
audience=AUDIENCE,
804+
subject_token_type=SUBJECT_TOKEN_TYPE,
805+
credential_source=self.CREDENTIAL_SOURCE_TEXT_URL.copy(),
806+
universe_domain="testdomain.org",
807+
)
808+
809+
assert credentials.info == {
810+
"type": "external_account",
811+
"audience": AUDIENCE,
812+
"subject_token_type": SUBJECT_TOKEN_TYPE,
813+
"token_url": "https://sts.testdomain.org/v1/token",
814+
"credential_source": self.CREDENTIAL_SOURCE_TEXT_URL,
815+
"universe_domain": "testdomain.org",
816+
}
817+
785818
def test_retrieve_subject_token_missing_subject_token(self, tmpdir):
786819
# Provide empty text file.
787820
empty_file = tmpdir.join("empty.txt")

0 commit comments

Comments
 (0)