Skip to content

fix(auth): use iam_endpoint_override in sign_bytes and ID token refresh - #19219

Draft
ohmayr wants to merge 3 commits into
mainfrom
fix/auth-impersonated-endpoint-override
Draft

ohmayr wants to merge 3 commits into
mainfrom
fix/auth-impersonated-endpoint-override

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Context & Problem

impersonated_credentials.Credentials.sign_bytes() and IDTokenCredentials.refresh() ignored _iam_endpoint_override and always sent requests to the default iamcredentials.<universe_domain> endpoint, even when iam_endpoint_override was configured (for example, for regional or proxy endpoints).

Summary of Changes

  • Add iam._build_iam_endpoint() in packages/google-auth/google/auth/iam.py to build IAM Credentials API URLs via _IAM_DOMAIN host-prefix templating (preserving mTLS without blind string replacement) and rewrite :generateAccessToken overrides to :signBlob / :generateIdToken when iam_endpoint_override is set.
  • Update _make_iam_token_request(), Credentials.sign_bytes(), and IDTokenCredentials.refresh() in packages/google-auth/google/auth/impersonated_credentials.py to delegate endpoint construction to iam._build_iam_endpoint().
  • Add unit tests in packages/google-auth/tests/test_iam.py and packages/google-auth/tests/test_impersonated_credentials.py covering regional, query-preserving, proxy-prefix, origin-only, mTLS, and invalid schemeless overrides.

Verification

  • ruff check, ruff check --select I, ruff format --check, and mypy --config-file=../../mypy.ini passed with zero issues on all 4 modified files.
  • pytest passed (347 passed) with 100% statement and branch coverage on google/auth/impersonated_credentials.py and all new lines/branches in google/auth/iam.py.

b/589828921

@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 helper function _build_iam_endpoint to construct IAM Credentials API URLs while properly handling and validating iam_endpoint_override values. This helper is integrated into token generation, byte signing, and ID token refresh flows, with comprehensive unit tests added. The review feedback points out a critical issue in _make_iam_token_request where the override bypasses the helper entirely, skipping validation and path formatting, and suggests passing the override directly to _build_iam_endpoint.

Comment on lines +122 to +124
iam_endpoint = iam_endpoint_override or _build_iam_endpoint(
principal, universe_domain, None, "generateAccessToken"
)

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.

high

By using iam_endpoint_override or _build_iam_endpoint(...), the code bypasses _build_iam_endpoint entirely when iam_endpoint_override is provided. This means that if iam_endpoint_override is a base URL (e.g., https://proxy.example.com/iam/) or a schemeless URL, it will not be validated or properly constructed with the required path and action for _make_iam_token_request.

Instead, you should pass iam_endpoint_override directly to _build_iam_endpoint so that it is always validated and formatted correctly.

Suggested change
iam_endpoint = iam_endpoint_override or _build_iam_endpoint(
principal, universe_domain, None, "generateAccessToken"
)
iam_endpoint = _build_iam_endpoint(
principal, universe_domain, iam_endpoint_override, "generateAccessToken"
)

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.

Keeping iam_endpoint_override passed through verbatim in _make_iam_token_request is intentional so existing callers passing custom proxy URLs that do not end in :generateAccessToken keep working as documented. This PR only changes sign_bytes() and IDTokenCredentials.refresh(), which previously ignored iam_endpoint_override altogether.

@ohmayr
ohmayr force-pushed the fix/auth-impersonated-endpoint-override branch from f2ac72c to d44123d Compare November 25, 2026 23:07
@ohmayr ohmayr changed the title fix(auth): honor iam_endpoint_override in sign_bytes and IDToken fix(auth): use iam_endpoint_override in sign_bytes and ID token refresh Nov 25, 2026
Credentials.sign_bytes() and IDTokenCredentials.refresh() ignored
_iam_endpoint_override and always sent requests to the default
iamcredentials.<universe_domain> endpoint, even when
iam_endpoint_override was configured (e.g. for regional or proxy
endpoints).

Add iam._build_iam_endpoint() to construct IAM Credentials API URLs via
_IAM_DOMAIN host-prefix templating (preserving mTLS without blind string
replacement) and rewrite :generateAccessToken overrides to :signBlob /
:generateIdToken when _iam_endpoint_override is set.

b/589828921
_IAM_IDTOKEN_ENDPOINT = _IAM_BASE_URL + ":generateIdToken"


def _build_iam_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 see 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.

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.

Makes sense, I will update _build_iam_endpoint here to only use the mtls. prefix on googleapis.com so it matches #19194 and google-api-core. Whichever PR lands second can drop the duplicate helper and import _build_iam_endpoint directly across google-auth.

…-endpoint-override

# Conflicts:
#	packages/google-auth/tests/test_impersonated_credentials.py
Restrict the mTLS host prefix inside _build_iam_endpoint strictly to
googleapis.com so non-default universes use iamcredentials.<universe>.

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.

2 participants