Repository navigation
Conversation
There was a problem hiding this comment.
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.
| iam_endpoint = iam_endpoint_override or _build_iam_endpoint( | ||
| principal, universe_domain, None, "generateAccessToken" | ||
| ) |
There was a problem hiding this comment.
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.
| 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" | |
| ) |
There was a problem hiding this comment.
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.
f2ac72c to
d44123d
Compare
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
d44123d to
68da057
Compare
| _IAM_IDTOKEN_ENDPOINT = _IAM_BASE_URL + ":generateIdToken" | ||
|
|
||
|
|
||
| def _build_iam_endpoint( |
There was a problem hiding this comment.
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_endpointso it coversgenerateAccessToken,signBlob,generateIdToken, andiam_endpoint_override). - Scope the
mtls.host prefix strictly togoogleapis.com(matching#19194andgoogle.api_core.universe.get_api_endpoint), rather than prependingmtls.to non-default universes. - Import that single helper directly across
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.
There was a problem hiding this comment.
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>.
Context & Problem
impersonated_credentials.Credentials.sign_bytes()andIDTokenCredentials.refresh()ignored_iam_endpoint_overrideand always sent requests to the defaultiamcredentials.<universe_domain>endpoint, even wheniam_endpoint_overridewas configured (for example, for regional or proxy endpoints).Summary of Changes
iam._build_iam_endpoint()inpackages/google-auth/google/auth/iam.pyto build IAM Credentials API URLs via_IAM_DOMAINhost-prefix templating (preserving mTLS without blind string replacement) and rewrite:generateAccessTokenoverrides to:signBlob/:generateIdTokenwheniam_endpoint_overrideis set._make_iam_token_request(),Credentials.sign_bytes(), andIDTokenCredentials.refresh()inpackages/google-auth/google/auth/impersonated_credentials.pyto delegate endpoint construction toiam._build_iam_endpoint().packages/google-auth/tests/test_iam.pyandpackages/google-auth/tests/test_impersonated_credentials.pycovering regional, query-preserving, proxy-prefix, origin-only, mTLS, and invalid schemeless overrides.Verification
ruff check,ruff check --select I,ruff format --check, andmypy --config-file=../../mypy.inipassed with zero issues on all 4 modified files.pytestpassed (347 passed) with 100% statement and branch coverage ongoogle/auth/impersonated_credentials.pyand all new lines/branches ingoogle/auth/iam.py.b/589828921