Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates Credentials and IDTokenCredentials to support the CredentialsWithUniverseDomain interface, normalizing empty or null universe domains to the default domain and preserving explicit caller-provided universe domains. It also ensures cached self-signed JWT credentials are cloned during copies and cleared appropriately when scopes, JWT access, or universe domains change. The review feedback highlights a potential bug where with_subject does not clear _jwt_credentials, leading to stale credentials. Additionally, the reviewer suggests refactoring IDTokenCredentials.with_universe_domain to correctly reset _use_iam_endpoint to False when transitioning back to the default universe domain, along with a corresponding update to the unit tests.
| self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN | ||
|
|
||
| if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN: | ||
| # Check the normalized universe domain so None or "" falls back to the |
There was a problem hiding this comment.
I believe "" raises EmptyUniverseError instead of falling back to default
| cred = self._make_copy() | ||
| cred._universe_domain = universe_domain | ||
| if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN: | ||
| cred._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN |
There was a problem hiding this comment.
Should we really fall back to the default domain if empty here? We want to fall back in some cases, but I think when explicitly calling with_universe_domain(), we can enforce that an actual string is passed, to make sure there's no mistake
That's what we ended up doing in https://github.com/googleapis/google-cloud-python/pull/19133/changes. But I guess that logic should be more centralized
| if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN: | ||
| cred._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN | ||
| if cred._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN: | ||
| cred._always_use_jwt_access = True |
There was a problem hiding this comment.
If you go back and forth:
cred.with_universe_domain(NON_DEFAULT).with_universe_domain(DEFAULT_UNIVERSE_DOMAIN)
won't _always_use_jwt_access get stuck on?
| return cred | ||
|
|
||
| @_helpers.copy_docstring(credentials.CredentialsWithUniverseDomain) | ||
| def with_universe_domain(self, universe_domain): |
There was a problem hiding this comment.
I think we need to clear the RAB state here too:
google-cloud-python/packages/google-auth/google/auth/jwt.py
Lines 619 to 622 in fef3fc6
Maybe there's a better way to structure this so it can be easily shared?
| # letting a missing, null, or empty JSON value overwrite it. | ||
| if "universe_domain" not in kwargs: | ||
| kwargs["universe_domain"] = ( | ||
| info.get("universe_domain") or credentials.DEFAULT_UNIVERSE_DOMAIN |
There was a problem hiding this comment.
Are you sure we should set the default here?
There was a concern about being able to tell the difference between a "default because of fallback" from "default because it was set", so we can make the right decisions later. By setting it before the constructor here, that would be hard to re-construct later
| # Rebuild the IAM generateIdToken URL for the updated universe domain. | ||
| cred._iam_id_token_endpoint = iam._IAM_IDTOKEN_ENDPOINT.replace( | ||
| "googleapis.com", cred._universe_domain | ||
| ) |
There was a problem hiding this comment.
blind string replacement like this seems risky. Can we make iam._IAM_IDTOKEN_ENDPOINT into a template string, and force callers to populate the universe domain?
Right now, universes that contain "googleapis.com" as a substring would break here. Maybe that's not super realistic, but I think using a template here would be generally safer anyway
| _GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" | ||
|
|
||
|
|
||
| class Credentials( |
There was a problem hiding this comment.
Does async need to be handled here too?
| if "universe_domain" not in kwargs: | ||
| kwargs["universe_domain"] = ( | ||
| info.get("universe_domain") or credentials.DEFAULT_UNIVERSE_DOMAIN | ||
| ) |
There was a problem hiding this comment.
I left this comment elsewhere, but I don't think we should populate credentials.DEFAULT_UNIVERSE_DOMAIN at this level. cls already handles this
Fix
universe_domainhandling and copy behavior ingoogle.oauth2.service_account:universe_domainkwargs in factory constructors: Check"universe_domain" not in kwargsinCredentials._from_signer_and_info()andIDTokenCredentials._from_signer_and_info()so an explicituniverse_domainargument passed by the caller is not overwritten by a missing,None, or empty"universe_domain"entry in the service account info dictionary.universe_domainbefore checking against the default: Compare normalizedself._universe_domain(rather than the raw parameter) inCredentials.__init__()andCredentials.with_universe_domain()soNoneor""defaults togoogleapis.comwithout forcing_always_use_jwt_access = True._jwt_credentialsvia.with_claims()inCredentials._make_copy()so audience-bound copies (e.g.with_quota_project()) retain their self-signed JWT configuration without sharing mutable token state. Clear cached_jwt_credentialsinwith_scopes(),with_universe_domain(),with_subject(), andwith_always_use_jwt_access(False)so stale JWT state is not reused.CredentialsWithUniverseDomain: AddCredentialsWithUniverseDomainto the base classes of bothCredentialsandIDTokenCredentials, and addIDTokenCredentials.with_universe_domain()(updating_iam_id_token_endpointand_use_iam_endpointfor both non-default and default universe domains) to match other credential types.