Repository navigation
fix(auth): preserve universe_domain and state in service_account creds #19194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bad53ca
6da80c8
d2b6e63
26f9bb5
ecc966b
61b1ee9
7928eab
fa569a5
89b8d8b
5b5e06e
08aae82
922b93d
bb28bdb
66d06e7
57b5f73
82901e9
ed4ba4b
1d35469
a12460a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -101,6 +101,7 @@ class Credentials( | |||||||||
| credentials.Scoped, | ||||||||||
| credentials.CredentialsWithQuotaProject, | ||||||||||
| credentials.CredentialsWithTokenUri, | ||||||||||
| credentials.CredentialsWithUniverseDomain, | ||||||||||
| credentials.CredentialsWithRegionalAccessBoundary, | ||||||||||
| ): | ||||||||||
| """Service account credentials | ||||||||||
|
|
@@ -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 | ||||||||||
|
|
@@ -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, | ||||||||||
| ) | ||||||||||
|
|
@@ -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, | ||||||||||
|
|
@@ -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() | ||||||||||
|
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): | ||||||||||
|
|
@@ -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): | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| 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): | ||||||||||
|
|
@@ -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): | ||||||||||
|
|
@@ -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) | ||||||||||
|
|
@@ -560,6 +593,7 @@ class IDTokenCredentials( | |||||||||
| credentials.Signing, | ||||||||||
| credentials.CredentialsWithQuotaProject, | ||||||||||
| credentials.CredentialsWithTokenUri, | ||||||||||
| credentials.CredentialsWithUniverseDomain, | ||||||||||
| ): | ||||||||||
| """Open ID Connect ID Token-based service account credentials. | ||||||||||
|
|
||||||||||
|
|
@@ -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 | ||||||||||
|
|
||||||||||
|
|
@@ -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) | ||||||||||
|
|
||||||||||
|
|
@@ -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 | ||||||||||
| ) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||||||
|
|
||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 inpackages/google-auth/google/auth/iam.py. And which seem to be trying to build out:I recommend the following actions:
google/auth/iam.py(using_build_iam_endpointso it coversgenerateAccessToken,signBlob,generateIdToken, andiam_endpoint_override).mtls.host prefix strictly togoogleapis.com(matching#19194andgoogle.api_core.universe.get_api_endpoint), rather than prependingmtls.to non-default universes.google-authin whichever PR lands second (no# TODO(universe-domain):copy needed since both live ingoogle-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.