Skip to content

fix(auth): preserve universe_domain and state in service_account creds - #19194

Open
parthea wants to merge 3 commits into
mainfrom
fix/auth-sa-universe-domain
Open

parthea wants to merge 3 commits into
mainfrom
fix/auth-sa-universe-domain

Conversation

@parthea

@parthea parthea commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fix universe_domain handling and copy behavior in google.oauth2.service_account:

  • Preserve explicit universe_domain kwargs in factory constructors: Check "universe_domain" not in kwargs in Credentials._from_signer_and_info() and IDTokenCredentials._from_signer_and_info() so an explicit universe_domain argument passed by the caller is not overwritten by a missing, None, or empty "universe_domain" entry in the service account info dictionary.
  • Normalize universe_domain before checking against the default: Compare normalized self._universe_domain (rather than the raw parameter) in Credentials.__init__() and Credentials.with_universe_domain() so None or "" defaults to googleapis.com without forcing _always_use_jwt_access = True.
  • Preserve self-signed JWT credentials across copies: Clone _jwt_credentials via .with_claims() in Credentials._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_credentials in with_scopes(), with_universe_domain(), with_subject(), and with_always_use_jwt_access(False) so stale JWT state is not reused.
  • Implement CredentialsWithUniverseDomain: Add CredentialsWithUniverseDomain to the base classes of both Credentials and IDTokenCredentials, and add IDTokenCredentials.with_universe_domain() (updating _iam_id_token_endpoint and _use_iam_endpoint for both non-default and default universe domains) to match other credential types.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@parthea
parthea marked this pull request as ready for review November 24, 2026 16:23
@parthea
parthea requested review from a team as code owners November 24, 2026 16:23
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to clear the RAB state here too:

# RAB state is only valid for the default universe domain, so don't
# carry cached RAB data over when switching to another universe.
if cred.universe_domain == google.auth.credentials.DEFAULT_UNIVERSE_DOMAIN:
self._copy_regional_access_boundary_manager(cred)

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this comment elsewhere, but I don't think we should populate credentials.DEFAULT_UNIVERSE_DOMAIN at this level. cls already handles this

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants