Repository navigation
Conversation
When universe_domain is omitted in a credential config, constructor call, or Compute Engine metadata response (404 or empty), google-auth previously filled in "googleapis.com" in _universe_domain. That made an omitted universe look identical to an explicitly configured default universe, so Regional Access Boundary (RAB) lookups could send tokens for other universes to iamcredentials.googleapis.com. Store None in _universe_domain when universe_domain is omitted while keeping the public universe_domain property returning "googleapis.com" for backwards compatibility. Only allow RAB lookups when _universe_domain is explicitly "googleapis.com" (after resolving lazy metadata on Compute Engine). b/588990592
There was a problem hiding this comment.
Code Review
This pull request refactors how the universe_domain is initialized and stored across various credential classes, changing the default internal state of _universe_domain from "googleapis.com" to None when omitted. This distinction is used to skip Regional Access Boundary (RAB) lookups unless the universe domain is explicitly configured. The reviewer identified three critical issues in external_account.py, external_account_authorized_user.py, and oauth2/credentials.py where using the public self.universe_domain property during serialization would write out the default "googleapis.com" instead of preserving the omitted None state, which would incorrectly enable RAB lookups upon deserialization.
| service_account_impersonation=config_info.pop( | ||
| "service_account_impersonation_options", None | ||
| ), | ||
| universe_domain=self.universe_domain, |
There was a problem hiding this comment.
Using self.universe_domain here will serialize the default "googleapis.com" even if the universe domain was originally omitted (i.e., self._universe_domain is None). When this serialized info is later loaded back via from_info, the recreated credential will have an explicit "googleapis.com" universe domain, which will enable Regional Access Boundary (RAB) lookup. To preserve the omitted state across serialization and deserialization, serialize self._universe_domain instead.
| universe_domain=self.universe_domain, | |
| universe_domain=self._universe_domain, |
There was a problem hiding this comment.
Fixed in 44719c8 — removed the universe_domain=self.universe_domain override so info preserves self._universe_domain (omitting universe_domain when unset) and from_info(creds.info) round-trips do not re-enable RAB.
| config_info.update(type=_EXTERNAL_ACCOUNT_AUTHORIZED_USER_JSON_TYPE) | ||
| config_info.update( | ||
| type=_EXTERNAL_ACCOUNT_AUTHORIZED_USER_JSON_TYPE, | ||
| universe_domain=self.universe_domain, |
There was a problem hiding this comment.
Using self.universe_domain here will serialize the default "googleapis.com" even if the universe domain was originally omitted (i.e., self._universe_domain is None). When this serialized info is later loaded back via from_info, the recreated credential will have an explicit "googleapis.com" universe domain, which will enable Regional Access Boundary (RAB) lookup. To preserve the omitted state across serialization and deserialization, serialize self._universe_domain instead.
| universe_domain=self.universe_domain, | |
| universe_domain=self._universe_domain, |
There was a problem hiding this comment.
Fixed in 44719c8 — removed the universe_domain=self.universe_domain override so info and to_json() preserve self._universe_domain across from_info() round-trips.
| "scopes": self.scopes, | ||
| "rapt_token": self.rapt_token, | ||
| "universe_domain": self._universe_domain, | ||
| "universe_domain": self.universe_domain, |
There was a problem hiding this comment.
Using self.universe_domain here will serialize the default "googleapis.com" even if the universe domain was originally omitted (i.e., self._universe_domain is None). When this serialized JSON is later loaded back, the recreated credential will have an explicit "googleapis.com" universe domain, which will enable Regional Access Boundary (RAB) lookup. To preserve the omitted state across serialization and deserialization, serialize self._universe_domain instead.
| "universe_domain": self.universe_domain, | |
| "universe_domain": self._universe_domain, |
There was a problem hiding this comment.
Kept self.universe_domain intentionally here for backwards compatibility. google-auth-oauthlib / gcloud auth application-default login builds oauth2.credentials.Credentials without passing universe_domain and saves ADC via to_json(), expecting "universe_domain": "googleapis.com" in the output. User OAuth2 credentials also only support refreshing in googleapis.com (_perform_refresh_token raises RefreshError for other universes), so keeping self.universe_domain here avoids breaking ADC serialization without opening a non-default refresh path.
Stop writing self.universe_domain ("googleapis.com") into
external_account.Credentials.info and
external_account_authorized_user.Credentials.info when universe_domain
was omitted. Otherwise, round-tripping through from_info(creds.info)
would turn an omitted universe_domain into an explicit "googleapis.com"
and enable Regional Access Boundary (RAB) lookups.
Keep self.universe_domain in oauth2.credentials.Credentials.to_json()
so existing authorized-user ADC serialization (which does not support
refreshing outside googleapis.com) remains backwards-compatible.
b/588990592
|
Clean design storing self._universe_domain = None when omitted while keeping the public creds.universe_domain property returning "googleapis.com"—that keeps client-side google.api_core.universe.compare_domains checks (and validate_credentials_universe in #19211) working unchanged without needing any mock-specific branches in production code. Two quick coordination notes across our in-flight google-auth PRs: downscoped.Credentials (google/auth/downscoped.py): Right now downscoped.Credentials.init sets self._universe_domain = universe_domain or None without inheriting from source_credentials. Once this lands, I can reuse _get_explicit_universe_domain(source_credentials) in the downscoped follow-up so custom-universe source_credentials propagate to downscoped.Credentials the same way impersonated_credentials and jwt.from_signing_credentials do here. Merge order heads-up: service_account.py (IDTokenCredentials._from_signer_and_info) overlaps slightly with #19194 and external_account.py overlaps with #19200, so worth coordinating merge order across those three. |
|
Thanks @chalmerlowe! Sounds good on reusing On merge order: #19208 and #19200 merge cleanly with no conflicts in either order. For #19194, the only overlap is in |
daniel-sanche
left a comment
There was a problem hiding this comment.
Mostly LGTM, just a small suggestion and a couple optional nits. I also added @nbayati to take a look at the RAB side of things though
| if universe_domain and universe_domain != DEFAULT_UNIVERSE_DOMAIN: | ||
| return universe_domain | ||
| if isinstance(creds, Credentials): | ||
| return getattr(creds, "_universe_domain", None) |
There was a problem hiding this comment.
Can we do this, to avoid empty strings?
| return getattr(creds, "_universe_domain", None) | |
| return getattr(creds, "_universe_domain", None) or None |
| def _get_explicit_universe_domain(creds): | ||
| """Returns the explicitly configured universe domain from creds, or None.""" | ||
| # Read public universe_domain first so lazy credentials (such as Compute | ||
| # Engine) resolve metadata before we inspect _universe_domain. |
There was a problem hiding this comment.
nit: I feel like this could use some more elaboration. I missed at first that universe_domain and _universe_domain were different
Maybe something like:
# Unlike `_universe_domain` (which is None when omitted), the public
# `universe_domain` property always falls back to DEFAULT_UNIVERSE_DOMAIN.
# Read `universe_domain` first so lazy credentials (such as Compute Engine)
# resolve metadata and any non-default universe is returned directly; then
# check `_universe_domain` to distinguish an explicit default universe from
# an omitted one.
| # inspect _universe_domain. | ||
| if self.universe_domain != DEFAULT_UNIVERSE_DOMAIN: | ||
| return False | ||
| if getattr(self, "_universe_domain", None) != DEFAULT_UNIVERSE_DOMAIN: |
There was a problem hiding this comment.
nit: It feels a little risky to have an under-documented, private attribute doing such an important job here. Subclasses could implement their own version of universe_domain, and not realize they're silently skipping RAB lookups
Maybe we should have a separate thin self._get_explicit_universe_domain(), or self._is_explicit_default_universe() method, to better document this?
Context & Problem
When
universe_domainis omitted in a credential config, constructor call, or Compute Engine metadata response (404or empty),google-authpreviously filled in"googleapis.com"inself._universe_domain. Because of that backfill,_is_regional_access_boundary_lookup_required()could not tell an omitted universe apart from a credential explicitly configured forgoogleapis.com.Summary of Changes
self._universe_domain = Nonewhenuniverse_domainis omitted across baseCredentialsand its subclasses (compute_engine,service_account,jwt,external_account,external_account_authorized_user,impersonated_credentials,downscoped,oauth2.credentials), while keeping the publicuniverse_domainproperty returning"googleapis.com"so existing callers don't break.external_account.Credentials.infoandexternal_account_authorized_user.Credentials.info/to_json()omitting"universe_domain"when unset sofrom_info()round-trips do not re-enable RAB._metadata._get_explicit_universe_domain(request)returningNoneon404or an empty response, and use it incompute_engine.Credentials.universe_domain(leaving_metadata.get_universe_domain(request)unchanged).CredentialsWithRegionalAccessBoundary._is_regional_access_boundary_lookup_required()to requireself._universe_domain == "googleapis.com"after readingself.universe_domain(so Compute Engine still resolves metadata lazily first)._get_explicit_universe_domain()incredentials.pyso wrapper credentials (impersonated_credentialsandjwt.from_signing_credentials) preserveNoneinstead of turning it into"googleapis.com".universe_domain, and add tests for omitted configs, Compute Engine metadata404/empty vs.200, credential copies,info/to_json()round-trips, wrapper credentials, and unpickled credentials without_universe_domain.What This PR Does Not Change
_universe_domainfield instead of a second_universe_domain_if_setfield: Callers likegcloudwritecreds._universe_domain = ...directly, so keeping one attribute avoids two fields getting out of sync. The one trade-off is that credentials pickled ongoogle-auth2.27–2.63 already have"_universe_domain": "googleapis.com"in their saved state (pre-2.27 pickled credentials without_universe_domainstill skip RAB).oauth2.credentials.Credentials.to_json(): Keeps serializingself.universe_domain("googleapis.com") for backwards compatibility withgcloud auth application-default loginADC dumps (user OAuth2 credentials only support refreshing ingoogleapis.com).with_universe_domain()and empty-string validation: Constructor fallback for""is left as-is to stay backwards-compatible with existing tests (such astest_jwt.py::test_constructor_empty_universe_domain), and centralizingwith_universe_domain()validation is left separate to coordinate with fix(auth): preserve universe_domain and state in service_account creds #19194.external_account.Credentials.get_project_id(): Handled separately in fix(auth): guard default-universe get_project_id by endpoint host #19200.Verification
pytest tests tests_async(2,389 passed, 1 skipped) +pytest tests/test__cloud_sdk.py(13 passed)pytest --cov=google.auth --cov=google.oauth2 --cov-branch(100% coverage on modified lines)ruff check,ruff format --check, andmypyon all modified filesb/588990592