Skip to content

fix(auth): derive AIP-4111 JWT audience from URL for unscoped service_account credentials - #19280

Open
parthea wants to merge 1 commit into
fix/auth-sa-non-gdu-refreshfrom
fix/auth-sa-url-jwt-audience
Open

parthea wants to merge 1 commit into
fix/auth-sa-non-gdu-refreshfrom
fix/auth-sa-url-jwt-audience

Conversation

@parthea

@parthea parthea commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add _derive_self_signed_jwt_audience(url) on service_account.Credentials to derive an AIP-4111 audience (https://{hostname}/) from the outbound request URL when always_use_jwt_access=True and no scopes, default scopes, subject, or explicit JWT audience (e.g., default_host) are configured. Handles port stripping, hostname normalization, IPv6 bracket notation, and returns None on relative or malformed URLs.
  • Store URL-derived self-signed JWTs in a bounded, lock-protected per-audience LRUCache (_url_jwt_credentials_cache) without mutating self.token or self._jwt_credentials, keeping shared credentials thread- and async-safe across multiple hosts.
  • Add __getstate__ / __setstate__ on service_account.Credentials to exclude the lock and cache during pickling and reinitialize them on unpickle.
  • Override before_request() on service_account.Credentials (applying CRED_TYPE_SA_JWT metric header) and _service_account_async.Credentials to mint and apply per-audience JWTs on demand.
  • Update _SyncCredentialsAdapter.before_request() in google/auth/aio/transport/sessions.py to skip _refresh_shared() when the wrapped credentials derive a URL-based self-signed JWT, avoiding a premature refresh() call without the request URL.

Stacked on #19279.

@parthea
parthea requested review from a team as code owners November 30, 2026 21:29

@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 introduces a mechanism to derive AIP-4111 self-signed JWT audiences from outbound request URLs for unscoped service account credentials in non-default universe domains. It implements a thread-safe, async-safe per-audience LRU cache to store these JWTs, preventing memory bloat and redundant RSA signing, and updates both synchronous and asynchronous credentials as well as various transport layers. The review feedback highlights a missing CRED_TYPE_SA_JWT metric header in the asynchronous before_request implementation, which should be added to match the synchronous counterpart's telemetry behavior.

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.

1 participant