Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ def process_regional_access_boundary_info(self, regional_access_boundary_info):
# Perform the atomic swap of the state object.
self._data = updated_data

def clear_data(self):
"""Clears cached Regional Access Boundary location data."""
with self._update_lock:
self._data = _RegionalAccessBoundaryData(
encoded_locations=None,
expiry=None,
cooldown_expiry=None,
cooldown_duration=DEFAULT_REGIONAL_ACCESS_BOUNDARY_COOLDOWN,
)


class _RegionalAccessBoundaryRefreshThread(threading.Thread):
"""Thread for background refreshing of the Regional Access Boundary."""
Expand Down
13 changes: 13 additions & 0 deletions packages/google-auth/google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,19 @@ def _copy_regional_access_boundary_manager(self, target):
self._rab_manager._use_blocking_regional_access_boundary_lookup
)

def _update_regional_access_boundary_for_universe(
self, target_credentials, universe_domain
):
"""Copies RAB state for the default universe domain or clears it otherwise."""
# RAB state is only valid for the default universe domain, so only copy
# cached RAB state/config when targeting the default universe domain.
if universe_domain == DEFAULT_UNIVERSE_DOMAIN:
self._copy_regional_access_boundary_manager(target_credentials)
else:
rab_manager = getattr(target_credentials, "_rab_manager", None)
if rab_manager is not None:
rab_manager.clear_data()

def _set_regional_access_boundary(self, initial_boundary):
"""Applies the regional_access_boundary provided via the initial_boundary on these
credentials. This is intended for internal use only as an invalid
Expand Down
21 changes: 21 additions & 0 deletions packages/google-auth/google/auth/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@
_IAM_SIGN_ENDPOINT = _IAM_BASE_URL + ":signBlob"
_IAM_SIGNJWT_ENDPOINT = _IAM_BASE_URL + ":signJwt"
_IAM_IDTOKEN_ENDPOINT = _IAM_BASE_URL + ":generateIdToken"
# Template for non-default universe domains; double braces escape the service
# account email placeholder so .format(universe_domain=...) leaves `{}` intact
# for downstream email formatting.
_IAM_IDTOKEN_ENDPOINT_TEMPLATE = (
"https://iamcredentials.{universe_domain}/v1/projects/-/"
"serviceAccounts/{{}}:generateIdToken"
)


def _get_iam_idtoken_endpoint(

@chalmerlowe chalmerlowe Nov 30, 2026 •

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.

SYNC WARNING:

I noticed a sync issue between #19194 (_get_iam_idtoken_endpoint) and #19219 (_build_iam_endpoint), which both add IAM Credentials API URL builders in packages/google-auth/google/auth/iam.py. And which seem to be trying to build out:

f"https://{host_prefix}{universe_domain}/v1/projects/-/"
f"serviceAccounts/{target_principal}:{action}"

I recommend the following actions:

  • Combine these two functions into a single helper in google/auth/iam.py (using _build_iam_endpoint so it covers generateAccessToken, signBlob, generateIdToken, and iam_endpoint_override).
  • Scope the mtls. host prefix strictly to googleapis.com (matching #19194 and google.api_core.universe.get_api_endpoint), rather than prepending mtls. to non-default universes.
  • Import that single helper directly across google-auth in whichever PR lands second (no # TODO(universe-domain): copy needed since both live in google-auth).

I know that you have better context on this than I do, so if any of these suggestions are wrong, please leave a comment here. Gemini will pick them up and learn from them during the next sync sweep.

universe_domain: str = credentials.DEFAULT_UNIVERSE_DOMAIN,
) -> str:
"""Returns the IAM generateIdToken endpoint template for ``universe_domain``.

Uses ``_IAM_IDTOKEN_ENDPOINT`` (which respects mTLS configuration) for the
default universe domain, and formats ``_IAM_IDTOKEN_ENDPOINT_TEMPLATE`` for
non-default universe domains.
"""
if universe_domain == credentials.DEFAULT_UNIVERSE_DOMAIN:
return _IAM_IDTOKEN_ENDPOINT
return _IAM_IDTOKEN_ENDPOINT_TEMPLATE.format(universe_domain=universe_domain)


class Signer(crypt.Signer):
Expand Down
23 changes: 9 additions & 14 deletions packages/google-auth/google/auth/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,15 @@ def from_signing_credentials(cls, credentials, audience, **kwargs):
# Regional Access Boundary (RAB) only applies to the default universe
# domain. Reuse the source credentials' cached RAB state only when the
# new credentials stay in the default universe; otherwise start with
# empty RAB state so a googleapis.com boundary is never sent to another
# universe.
if (
isinstance(
credentials,
google.auth.credentials.CredentialsWithRegionalAccessBoundary,
)
and jwt_creds.universe_domain
== google.auth.credentials.DEFAULT_UNIVERSE_DOMAIN
# empty RAB state so a default-universe boundary is never sent to
# another universe.
if isinstance(
credentials,
google.auth.credentials.CredentialsWithRegionalAccessBoundary,
):
credentials._copy_regional_access_boundary_manager(jwt_creds)
credentials._update_regional_access_boundary_for_universe(
jwt_creds, jwt_creds.universe_domain
)

return jwt_creds

Expand Down Expand Up @@ -605,10 +603,7 @@ def with_universe_domain(self, universe_domain):
quota_project_id=self._quota_project_id,
universe_domain=universe_domain,
)
# 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)
self._update_regional_access_boundary_for_universe(cred, cred.universe_domain)
return cred

def _make_jwt(self):
Expand Down
84 changes: 66 additions & 18 deletions packages/google-auth/google/oauth2/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Credentials(
credentials.Scoped,
credentials.CredentialsWithQuotaProject,
credentials.CredentialsWithTokenUri,
credentials.CredentialsWithUniverseDomain,
credentials.CredentialsWithRegionalAccessBoundary,
):
"""Service account credentials
Expand Down Expand Up @@ -193,10 +194,15 @@ def __init__(
self._project_id = project_id
self._quota_project_id = quota_project_id
self._token_uri = token_uri
self._explicit_always_use_jwt_access = bool(always_use_jwt_access)
self._always_use_jwt_access = always_use_jwt_access
if universe_domain is not None:
_helpers.validate_universe_domain(universe_domain)
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN

if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
# Check the normalized universe domain so None falls back to the
# default universe domain without forcing self-signed JWT access.
if self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
self._always_use_jwt_access = True

self._jwt_credentials = None
Expand Down Expand Up @@ -224,14 +230,16 @@ def _from_signer_and_info(cls, signer, info, **kwargs):
Raises:
ValueError: If the info is not in the expected format.
"""
# Respect an explicit universe_domain kwarg from the caller; otherwise
# forward from info whenever present and not None so empty strings still
# fail validation while omitted/null values let cls() apply its default.
if "universe_domain" not in kwargs and info.get("universe_domain") is not None:
kwargs["universe_domain"] = info["universe_domain"]
return cls(
signer,
service_account_email=info["client_email"],
token_uri=info["token_uri"],
project_id=info.get("project_id"),
universe_domain=info.get(
"universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN
),
trust_boundary=info.get("trust_boundary"),
**kwargs,
)
Expand Down Expand Up @@ -293,7 +301,7 @@ def requires_scopes(self):
"""
return True if not self._scopes else False

def _make_copy(self):
def _make_copy(self, *, _copy_rab=True):
cred = self.__class__(
self._signer,
service_account_email=self._service_account_email,
Expand All @@ -304,19 +312,28 @@ def _make_copy(self):
project_id=self._project_id,
quota_project_id=self._quota_project_id,
additional_claims=self._additional_claims.copy(),
always_use_jwt_access=self._always_use_jwt_access,
always_use_jwt_access=getattr(
self, "_explicit_always_use_jwt_access", self._always_use_jwt_access
),
universe_domain=self._universe_domain,
trust_boundary=self._trust_boundary,
)
cred._cred_file_path = self._cred_file_path
self._copy_regional_access_boundary_manager(cred)
# Clone cached self-signed JWT credentials so audience-bound copies keep
# their audience without sharing mutable token state with the original.
if self._jwt_credentials is not None:
cred._jwt_credentials = self._jwt_credentials.with_claims()
Comment thread
parthea marked this conversation as resolved.
Resolved
if _copy_rab:
self._copy_regional_access_boundary_manager(cred)
return cred

@_helpers.copy_docstring(credentials.Scoped)
def with_scopes(self, scopes, default_scopes=None):
cred = self._make_copy()
cred._scopes = scopes
cred._default_scopes = default_scopes
# Clear cached JWT credentials so subsequent refreshes reflect the new scopes.
cred._jwt_credentials = None
return cred

def with_always_use_jwt_access(self, always_use_jwt_access):
Expand All @@ -340,15 +357,26 @@ def with_always_use_jwt_access(self, always_use_jwt_access):
raise exceptions.InvalidValue(
"always_use_jwt_access should be True for non-default universe domain"
)
cred._explicit_always_use_jwt_access = bool(always_use_jwt_access)
cred._always_use_jwt_access = always_use_jwt_access
if not always_use_jwt_access:
# Drop cached self-signed JWT credentials when opting out of JWT access.
cred._jwt_credentials = None
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?

Copy link
Copy Markdown
Contributor Author

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:

Fixed in fa569a5

Maybe there's a better way to structure this so it can be easily shared?

Fixed in 5b5e06e

cred = self._make_copy()
_helpers.validate_universe_domain(universe_domain)
cred = self._make_copy(_copy_rab=False)
cred._universe_domain = universe_domain
if universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
cred._always_use_jwt_access = True
cred._always_use_jwt_access = (
getattr(cred, "_explicit_always_use_jwt_access", False)
or cred._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN
)
# Clear cached JWT credentials since audience/universe metadata may change.
cred._jwt_credentials = None
# RAB state is only valid for the default universe domain.
self._update_regional_access_boundary_for_universe(cred, cred._universe_domain)
return cred

def with_subject(self, subject):
Expand All @@ -363,6 +391,9 @@ def with_subject(self, subject):
"""
cred = self._make_copy()
cred._subject = subject
# Clear cached JWT credentials since subject delegation does not use
# self-signed JWTs.
cred._jwt_credentials = None
return cred

def with_claims(self, additional_claims):
Expand All @@ -381,6 +412,8 @@ def with_claims(self, additional_claims):
new_additional_claims.update(additional_claims or {})
cred = self._make_copy()
cred._additional_claims = new_additional_claims
# Clear cached JWT credentials so they regenerate with the new claims
cred._jwt_credentials = None
return cred

@_helpers.copy_docstring(credentials.CredentialsWithQuotaProject)
Expand Down Expand Up @@ -560,6 +593,7 @@ class IDTokenCredentials(
credentials.Signing,
credentials.CredentialsWithQuotaProject,
credentials.CredentialsWithTokenUri,
credentials.CredentialsWithUniverseDomain,
):
"""Open ID Connect ID Token-based service account credentials.

Expand Down Expand Up @@ -645,14 +679,12 @@ def __init__(
self._quota_project_id = quota_project_id
self._use_iam_endpoint = False

if not universe_domain:
self._universe_domain = credentials.DEFAULT_UNIVERSE_DOMAIN
else:
self._universe_domain = universe_domain
self._iam_id_token_endpoint = iam._IAM_IDTOKEN_ENDPOINT.replace(
"googleapis.com", self._universe_domain
if universe_domain is not None:
_helpers.validate_universe_domain(universe_domain)
self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN
self._iam_id_token_endpoint = iam._get_iam_idtoken_endpoint(
self._universe_domain
)

if self._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN:
self._use_iam_endpoint = True

Expand All @@ -679,7 +711,10 @@ def _from_signer_and_info(cls, signer, info, **kwargs):
"""
kwargs.setdefault("service_account_email", info["client_email"])
kwargs.setdefault("token_uri", info["token_uri"])
if "universe_domain" in info:
# Respect an explicit universe_domain kwarg from the caller; otherwise
# forward from info whenever present and not None so empty strings still
# fail validation while omitted/null values let cls() apply its default.
if "universe_domain" not in kwargs and info.get("universe_domain") is not None:
kwargs["universe_domain"] = info["universe_domain"]
return cls(signer, **kwargs)

Expand Down Expand Up @@ -791,6 +826,19 @@ def with_token_uri(self, token_uri):
cred._token_uri = token_uri
return cred

@_helpers.copy_docstring(credentials.CredentialsWithUniverseDomain)
def with_universe_domain(self, universe_domain):
_helpers.validate_universe_domain(universe_domain)
cred = self._make_copy()
cred._universe_domain = universe_domain
cred._iam_id_token_endpoint = iam._get_iam_idtoken_endpoint(
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 922b93d

cred._use_iam_endpoint = (
cred._universe_domain != credentials.DEFAULT_UNIVERSE_DOMAIN
)
return cred

def _make_authorization_grant_assertion(self):
"""Create the OAuth 2.0 assertion.

Expand Down
Loading
Loading