Skip to content

Commit e938028

Browse files
feat(google-auth): make _CLOUD_RESOURCE_MANAGER URL universe-domain-aware (#16546)
Replace hardcoded googleapis.com in _CLOUD_RESOURCE_MANAGER with a {universe_domain} placeholder, resolved at credential construction time via self._cloud_resource_manager_url. This mirrors the existing pattern used for _DEFAULT_TOKEN_URL. Add tests verifying the URL is correctly built for both the default (googleapis.com) and custom universe domains, including an end-to-end test through get_project_id. Fixes #16545 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent df07fce commit e938028

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

packages/google-auth/google/auth/external_account.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
# The token exchange requested_token_type. This is always an access_token.
6161
_STS_REQUESTED_TOKEN_TYPE = "urn:ietf:params:oauth:token-type:access_token"
6262
# Cloud resource manager URL used to retrieve project information.
63-
_CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.googleapis.com/v1/projects/"
63+
_CLOUD_RESOURCE_MANAGER = "https://cloudresourcemanager.{universe_domain}/v1/projects/"
6464
# Default Google sts token url.
6565
_DEFAULT_TOKEN_URL = "https://sts.{universe_domain}/v1/token"
6666

@@ -172,6 +172,9 @@ def __init__(
172172
self._token_url = self._token_url.replace(
173173
"{universe_domain}", self._universe_domain
174174
)
175+
self._cloud_resource_manager_url = _CLOUD_RESOURCE_MANAGER.replace(
176+
"{universe_domain}", self._universe_domain
177+
)
175178
self._token_info_url = token_info_url
176179
self._credential_source = credential_source
177180
self._service_account_impersonation_url = service_account_impersonation_url
@@ -404,7 +407,7 @@ def get_project_id(self, request):
404407
project_number = self.project_number or self._workforce_pool_user_project
405408
if project_number and scopes:
406409
headers = {}
407-
url = _CLOUD_RESOURCE_MANAGER + project_number
410+
url = "{}{}".format(self._cloud_resource_manager_url, project_number)
408411
self.before_request(request, "GET", url, headers)
409412
response = request(url=url, method="GET", headers=headers)
410413

packages/google-auth/tests/test_external_account.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,42 @@ def test_get_project_id_cloud_resource_manager_error(self):
21352135
# Only 2 requests to STS and cloud resource manager should be sent.
21362136
assert len(request.call_args_list) == 2
21372137

2138+
def test_cloud_resource_manager_url_with_default_universe_domain(self):
2139+
credentials = self.make_credentials()
2140+
assert credentials._cloud_resource_manager_url == (
2141+
"https://cloudresourcemanager.googleapis.com/v1/projects/"
2142+
)
2143+
2144+
def test_cloud_resource_manager_url_with_custom_universe_domain(self):
2145+
credentials = self.make_credentials(universe_domain="example.com")
2146+
assert credentials._cloud_resource_manager_url == (
2147+
"https://cloudresourcemanager.example.com/v1/projects/"
2148+
)
2149+
2150+
def test_get_project_id_cloud_resource_manager_custom_universe_domain(self):
2151+
custom_universe_domain = "example.com"
2152+
request = self.make_mock_request(
2153+
status=http_client.OK,
2154+
data=self.SUCCESS_RESPONSE.copy(),
2155+
cloud_resource_manager_status=http_client.OK,
2156+
cloud_resource_manager_data=self.CLOUD_RESOURCE_MANAGER_SUCCESS_RESPONSE,
2157+
)
2158+
credentials = self.make_credentials(
2159+
scopes=self.SCOPES,
2160+
universe_domain=custom_universe_domain,
2161+
)
2162+
2163+
project_id = credentials.get_project_id(request)
2164+
2165+
assert project_id == self.PROJECT_ID
2166+
# Verify that the cloud resource manager request used the custom universe domain URL.
2167+
assert len(request.call_args_list) == 2
2168+
crm_request_kwargs = request.call_args_list[1][1]
2169+
expected_url = "https://cloudresourcemanager.{}/v1/projects/{}".format(
2170+
custom_universe_domain, self.PROJECT_NUMBER
2171+
)
2172+
assert crm_request_kwargs["url"] == expected_url
2173+
21382174
def test_refresh_with_existing_impersonated_credentials(self):
21392175
credentials = self.make_credentials(
21402176
service_account_impersonation_url=self.SERVICE_ACCOUNT_IMPERSONATION_URL

0 commit comments

Comments
 (0)