GeezerVison is showing github.com — feat: Add trust boundary support for external accounts. (#1809) · googleapis/google-auth-library-python@36ecb1d
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +"""Shared constants.""" | | 2 | + | | 3 | +_SERVICE_ACCOUNT_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/projects/-/serviceAccounts/{service_account_email}/allowedLocations" | | 4 | +_WORKFORCE_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/locations/global/workforcePools/{pool_id}/allowedLocations" | | 5 | +_WORKLOAD_IDENTITY_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT = "https://iamcredentials.{universe_domain}/v1/projects/{project_number}/locations/global/workloadIdentityPools/{pool_id}/allowedLocations" |
Collapse file Copy file name to clipboardExpand all lines: google/auth/external_account.py +62-5Lines changed: 62 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | import json | 37 | 37 | import re | 38 | 38 |
| | 39 | +from google.auth import _constants | 39 | 40 | from google.auth import _helpers | 40 | 41 | from google.auth import credentials | 41 | 42 | from google.auth import exceptions | @@ -81,6 +82,7 @@ class Credentials( | 81 | 82 | credentials.Scoped, | 82 | 83 | credentials.CredentialsWithQuotaProject, | 83 | 84 | credentials.CredentialsWithTokenUri, | | 85 | + credentials.CredentialsWithTrustBoundary, | 84 | 86 | metaclass=abc.ABCMeta, | 85 | 87 | ): | 86 | 88 | """Base class for all external account credentials. | @@ -173,10 +175,7 @@ def __init__( | 173 | 175 | self._scopes = scopes | 174 | 176 | self._default_scopes = default_scopes | 175 | 177 | self._workforce_pool_user_project = workforce_pool_user_project | 176 | | - self._trust_boundary = { | 177 | | - "locations": [], | 178 | | - "encoded_locations": "0x0", | 179 | | - } # expose a placeholder trust boundary value. | | 178 | + self._trust_boundary = trust_boundary | 180 | 179 |
| 181 | 180 | if self._client_id: | 182 | 181 | self._client_auth = utils.ClientAuthentication( | @@ -242,6 +241,7 @@ def _constructor_args(self): | 242 | 241 | "scopes": self._scopes, | 243 | 242 | "default_scopes": self._default_scopes, | 244 | 243 | "universe_domain": self._universe_domain, | | 244 | + "trust_boundary": self._trust_boundary, | 245 | 245 | } | 246 | 246 | if not self.is_workforce_pool: | 247 | 247 | args.pop("workforce_pool_user_project") | @@ -412,8 +412,23 @@ def get_project_id(self, request): | 412 | 412 |
| 413 | 413 | return None | 414 | 414 |
| 415 | | - @_helpers.copy_docstring(credentials.Credentials) | 416 | 415 | def refresh(self, request): | | 416 | + """Refreshes the access token. | | 417 | +
| | 418 | + For impersonated credentials, this method will refresh the underlying | | 419 | + source credentials and the impersonated credentials. For non-impersonated | | 420 | + credentials, it will refresh the access token and the trust boundary. | | 421 | + """ | | 422 | + self._refresh_token(request) | | 423 | + # If we are impersonating, the trust boundary is handled by the | | 424 | + # impersonated credentials object. We need to get it from there. | | 425 | + if self._service_account_impersonation_url: | | 426 | + self._trust_boundary = self._impersonated_credentials._trust_boundary | | 427 | + else: | | 428 | + # Otherwise, refresh the trust boundary for the external account. | | 429 | + self._refresh_trust_boundary(request) | | 430 | + | | 431 | + def _refresh_token(self, request): | 417 | 432 | scopes = self._scopes if self._scopes is not None else self._default_scopes | 418 | 433 |
| 419 | 434 | # Inject client certificate into request. | @@ -463,6 +478,40 @@ def refresh(self, request): | 463 | 478 |
| 464 | 479 | self.expiry = now + lifetime | 465 | 480 |
| | 481 | + def _build_trust_boundary_lookup_url(self): | | 482 | + """Builds and returns the URL for the trust boundary lookup API.""" | | 483 | + url = None | | 484 | + # Try to parse as a workload identity pool. | | 485 | + # Audience format: //iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID | | 486 | + workload_match = re.search( | | 487 | + r"projects/([^/]+)/locations/global/workloadIdentityPools/([^/]+)", | | 488 | + self._audience, | | 489 | + ) | | 490 | + if workload_match: | | 491 | + project_number, pool_id = workload_match.groups() | | 492 | + url = _constants._WORKLOAD_IDENTITY_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT.format( | | 493 | + universe_domain=self._universe_domain, | | 494 | + project_number=project_number, | | 495 | + pool_id=pool_id, | | 496 | + ) | | 497 | + else: | | 498 | + # If that fails, try to parse as a workforce pool. | | 499 | + # Audience format: //iam.googleapis.com/locations/global/workforcePools/POOL_ID/providers/PROVIDER_ID | | 500 | + workforce_match = re.search( | | 501 | + r"locations/[^/]+/workforcePools/([^/]+)", self._audience | | 502 | + ) | | 503 | + if workforce_match: | | 504 | + pool_id = workforce_match.groups()[0] | | 505 | + url = _constants._WORKFORCE_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT.format( | | 506 | + universe_domain=self._universe_domain, pool_id=pool_id | | 507 | + ) | | 508 | + | | 509 | + if url: | | 510 | + return url | | 511 | + else: | | 512 | + # If both fail, the audience format is invalid. | | 513 | + raise exceptions.InvalidValue("Invalid audience format.") | | 514 | + | 466 | 515 | def _make_copy(self): | 467 | 516 | kwargs = self._constructor_args() | 468 | 517 | new_cred = self.__class__(**kwargs) | @@ -489,6 +538,12 @@ def with_universe_domain(self, universe_domain): | 489 | 538 | cred._universe_domain = universe_domain | 490 | 539 | return cred | 491 | 540 |
| | 541 | + @_helpers.copy_docstring(credentials.CredentialsWithTrustBoundary) | | 542 | + def with_trust_boundary(self, trust_boundary): | | 543 | + cred = self._make_copy() | | 544 | + cred._trust_boundary = trust_boundary | | 545 | + return cred | | 546 | + | 492 | 547 | def _should_initialize_impersonated_credentials(self): | 493 | 548 | return ( | 494 | 549 | self._service_account_impersonation_url is not None | @@ -537,6 +592,7 @@ def _initialize_impersonated_credentials(self): | 537 | 592 | lifetime=self._service_account_impersonation_options.get( | 538 | 593 | "token_lifetime_seconds" | 539 | 594 | ), | | 595 | + trust_boundary=self._trust_boundary, | 540 | 596 | ) | 541 | 597 |
| 542 | 598 | def _create_default_metrics_options(self): | @@ -623,6 +679,7 @@ def from_info(cls, info, **kwargs): | 623 | 679 | universe_domain=info.get( | 624 | 680 | "universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN | 625 | 681 | ), | | 682 | + trust_boundary=info.get("trust_boundary"), | 626 | 683 | **kwargs | 627 | 684 | ) | 628 | 685 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/auth/external_account_authorized_user.py +42-12Lines changed: 42 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | import datetime | 37 | 37 | import io | 38 | 38 | import json | | 39 | +import re | 39 | 40 |
| | 41 | +from google.auth import _constants | 40 | 42 | from google.auth import _helpers | 41 | 43 | from google.auth import credentials | 42 | 44 | from google.auth import exceptions | @@ -50,6 +52,7 @@ class Credentials( | 50 | 52 | credentials.CredentialsWithQuotaProject, | 51 | 53 | credentials.ReadOnlyScoped, | 52 | 54 | credentials.CredentialsWithTokenUri, | | 55 | + credentials.CredentialsWithTrustBoundary, | 53 | 56 | ): | 54 | 57 | """Credentials for External Account Authorized Users. | 55 | 58 |
| @@ -83,6 +86,7 @@ def __init__( | 83 | 86 | scopes=None, | 84 | 87 | quota_project_id=None, | 85 | 88 | universe_domain=credentials.DEFAULT_UNIVERSE_DOMAIN, | | 89 | + trust_boundary=None, | 86 | 90 | ): | 87 | 91 | """Instantiates a external account authorized user credentials object. | 88 | 92 |
| @@ -108,6 +112,7 @@ def __init__( | 108 | 112 | create the credentials. | 109 | 113 | universe_domain (Optional[str]): The universe domain. The default value | 110 | 114 | is googleapis.com. | | 115 | + trust_boundary (Mapping[str,str]): A credential trust boundary. | 111 | 116 |
| 112 | 117 | Returns: | 113 | 118 | google.auth.external_account_authorized_user.Credentials: The | @@ -118,7 +123,7 @@ def __init__( | 118 | 123 | self.token = token | 119 | 124 | self.expiry = expiry | 120 | 125 | self._audience = audience | 121 | | - self._refresh_token = refresh_token | | 126 | + self._refresh_token_val = refresh_token | 122 | 127 | self._token_url = token_url | 123 | 128 | self._token_info_url = token_info_url | 124 | 129 | self._client_id = client_id | @@ -128,6 +133,7 @@ def __init__( | 128 | 133 | self._scopes = scopes | 129 | 134 | self._universe_domain = universe_domain or credentials.DEFAULT_UNIVERSE_DOMAIN | 130 | 135 | self._cred_file_path = None | | 136 | + self._trust_boundary = trust_boundary | 131 | 137 |
| 132 | 138 | if not self.valid and not self.can_refresh: | 133 | 139 | raise exceptions.InvalidOperation( | @@ -164,7 +170,7 @@ def info(self): | 164 | 170 | def constructor_args(self): | 165 | 171 | return { | 166 | 172 | "audience": self._audience, | 167 | | - "refresh_token": self._refresh_token, | | 173 | + "refresh_token": self._refresh_token_val, | 168 | 174 | "token_url": self._token_url, | 169 | 175 | "token_info_url": self._token_info_url, | 170 | 176 | "client_id": self._client_id, | @@ -175,6 +181,7 @@ def constructor_args(self): | 175 | 181 | "scopes": self._scopes, | 176 | 182 | "quota_project_id": self._quota_project_id, | 177 | 183 | "universe_domain": self._universe_domain, | | 184 | + "trust_boundary": self._trust_boundary, | 178 | 185 | } | 179 | 186 |
| 180 | 187 | @property | @@ -184,7 +191,7 @@ def scopes(self): | 184 | 191 |
| 185 | 192 | @property | 186 | 193 | def requires_scopes(self): | 187 | | - """ False: OAuth 2.0 credentials have their scopes set when | | 194 | + """False: OAuth 2.0 credentials have their scopes set when | 188 | 195 | the initial token is requested and can not be changed.""" | 189 | 196 | return False | 190 | 197 |
| @@ -201,13 +208,13 @@ def client_secret(self): | 201 | 208 | @property | 202 | 209 | def audience(self): | 203 | 210 | """Optional[str]: The STS audience which contains the resource name for the | 204 | | - workforce pool and the provider identifier in that pool.""" | | 211 | + workforce pool and the provider identifier in that pool.""" | 205 | 212 | return self._audience | 206 | 213 |
| 207 | 214 | @property | 208 | 215 | def refresh_token(self): | 209 | 216 | """Optional[str]: The OAuth 2.0 refresh token.""" | 210 | | - return self._refresh_token | | 217 | + return self._refresh_token_val | 211 | 218 |
| 212 | 219 | @property | 213 | 220 | def token_url(self): | @@ -226,13 +233,18 @@ def revoke_url(self): | 226 | 233 |
| 227 | 234 | @property | 228 | 235 | def is_user(self): | 229 | | - """ True: This credential always represents a user.""" | | 236 | + """True: This credential always represents a user.""" | 230 | 237 | return True | 231 | 238 |
| 232 | 239 | @property | 233 | 240 | def can_refresh(self): | 234 | 241 | return all( | 235 | | - (self._refresh_token, self._token_url, self._client_id, self._client_secret) | | 242 | + ( | | 243 | + self._refresh_token_val, | | 244 | + self._token_url, | | 245 | + self._client_id, | | 246 | + self._client_secret, | | 247 | + ) | 236 | 248 | ) | 237 | 249 |
| 238 | 250 | def get_project_id(self, request=None): | @@ -266,7 +278,7 @@ def to_json(self, strip=None): | 266 | 278 | strip = strip if strip else [] | 267 | 279 | return json.dumps({k: v for (k, v) in self.info.items() if k not in strip}) | 268 | 280 |
| 269 | | - def refresh(self, request): | | 281 | + def _refresh_token(self, request): | 270 | 282 | """Refreshes the access token. | 271 | 283 |
| 272 | 284 | Args: | @@ -285,18 +297,29 @@ def refresh(self, request): | 285 | 297 | ) | 286 | 298 |
| 287 | 299 | now = _helpers.utcnow() | 288 | | - response_data = self._make_sts_request(request) | | 300 | + response_data = self._sts_client.refresh_token(request, self._refresh_token_val) | 289 | 301 |
| 290 | 302 | self.token = response_data.get("access_token") | 291 | 303 |
| 292 | 304 | lifetime = datetime.timedelta(seconds=response_data.get("expires_in")) | 293 | 305 | self.expiry = now + lifetime | 294 | 306 |
| 295 | 307 | if "refresh_token" in response_data: | 296 | | - self._refresh_token = response_data["refresh_token"] | | 308 | + self._refresh_token_val = response_data["refresh_token"] | | 309 | + | | 310 | + def _build_trust_boundary_lookup_url(self): | | 311 | + """Builds and returns the URL for the trust boundary lookup API.""" | | 312 | + # Audience format: //iam.googleapis.com/locations/global/workforcePools/POOL_ID/providers/PROVIDER_ID | | 313 | + match = re.search(r"locations/[^/]+/workforcePools/([^/]+)", self._audience) | | 314 | + | | 315 | + if not match: | | 316 | + raise exceptions.InvalidValue("Invalid workforce pool audience format.") | | 317 | + | | 318 | + pool_id = match.groups()[0] | 297 | 319 |
| 298 | | - def _make_sts_request(self, request): | 299 | | - return self._sts_client.refresh_token(request, self._refresh_token) | | 320 | + return _constants._WORKFORCE_POOL_TRUST_BOUNDARY_LOOKUP_ENDPOINT.format( | | 321 | + universe_domain=self._universe_domain, pool_id=pool_id | | 322 | + ) | 300 | 323 |
| 301 | 324 | @_helpers.copy_docstring(credentials.Credentials) | 302 | 325 | def get_cred_info(self): | @@ -331,6 +354,12 @@ def with_universe_domain(self, universe_domain): | 331 | 354 | cred._universe_domain = universe_domain | 332 | 355 | return cred | 333 | 356 |
| | 357 | + @_helpers.copy_docstring(credentials.CredentialsWithTrustBoundary) | | 358 | + def with_trust_boundary(self, trust_boundary): | | 359 | + cred = self._make_copy() | | 360 | + cred._trust_boundary = trust_boundary | | 361 | + return cred | | 362 | + | 334 | 363 | @classmethod | 335 | 364 | def from_info(cls, info, **kwargs): | 336 | 365 | """Creates a Credentials instance from parsed external account info. | @@ -375,6 +404,7 @@ def from_info(cls, info, **kwargs): | 375 | 404 | universe_domain=info.get( | 376 | 405 | "universe_domain", credentials.DEFAULT_UNIVERSE_DOMAIN | 377 | 406 | ), | | 407 | + trust_boundary=info.get("trust_boundary"), | 378 | 408 | **kwargs | 379 | 409 | ) | 380 | 410 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/auth/impersonated_credentials.py +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -286,7 +286,7 @@ def _refresh_token(self, request): | 286 | 286 | self._source_credentials.token_state == credentials.TokenState.STALE | 287 | 287 | or self._source_credentials.token_state == credentials.TokenState.INVALID | 288 | 288 | ): | 289 | | - self._source_credentials.refresh(request) | | 289 | + self._source_credentials._refresh_token(request) | 290 | 290 |
| 291 | 291 | body = { | 292 | 292 | "delegates": self._delegates, | @@ -526,13 +526,15 @@ def from_impersonated_service_account_info(cls, info, scopes=None): | 526 | 526 | target_principal = impersonation_url[start_index + 1 : end_index] | 527 | 527 | delegates = info.get("delegates") | 528 | 528 | quota_project_id = info.get("quota_project_id") | | 529 | + trust_boundary = info.get("trust_boundary") | 529 | 530 |
| 530 | 531 | return cls( | 531 | 532 | source_credentials, | 532 | 533 | target_principal, | 533 | 534 | scopes, | 534 | 535 | delegates, | 535 | 536 | quota_project_id=quota_project_id, | | 537 | + trust_boundary=trust_boundary, | 536 | 538 | ) | 537 | 539 |
| 538 | 540 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/oauth2/service_account.py +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 73 | 73 | import copy | 74 | 74 | import datetime | 75 | 75 |
| | 76 | +from google.auth import _constants | 76 | 77 | from google.auth import _helpers | 77 | 78 | from google.auth import _service_account_info | 78 | 79 | from google.auth import credentials | | 84 | 85 |
| 85 | 86 | _DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds | 86 | 87 | _GOOGLE_OAUTH2_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token" | 87 | | -_TRUST_BOUNDARY_LOOKUP_ENDPOINT = ( | 88 | | - "https://iamcredentials.{}/v1/projects/-/serviceAccounts/{}/allowedLocations" | 89 | | -) | 90 | 88 |
| 91 | 89 |
| 92 | 90 | class Credentials( | @@ -520,8 +518,9 @@ def _build_trust_boundary_lookup_url(self): | 520 | 518 | raise ValueError( | 521 | 519 | "Service account email is required to build the trust boundary lookup URL." | 522 | 520 | ) | 523 | | - return _TRUST_BOUNDARY_LOOKUP_ENDPOINT.format( | 524 | | - self._universe_domain, self._service_account_email | | 521 | + return _constants._SERVICE_ACCOUNT_TRUST_BOUNDARY_LOOKUP_ENDPOINT.format( | | 522 | + universe_domain=self._universe_domain, | | 523 | + service_account_email=self._service_account_email, | 525 | 524 | ) | 526 | 525 |
| 527 | 526 | @_helpers.copy_docstring(credentials.Signing) | |
|