Skip to content

fix(auth): honor universe_domain and URL host in impersonated creds - #19215

Closed
ohmayr wants to merge 1 commit into
refactor/auth-validate-universe-domainfrom
fix/auth-impersonated-universe-domain
Closed

ohmayr wants to merge 1 commit into
refactor/auth-validate-universe-domainfrom
fix/auth-impersonated-universe-domain

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Context & Problem

impersonated_credentials.Credentials.from_impersonated_service_account_info() ignored a top-level universe_domain and only sliced the service account email out of service_account_impersonation_url, discarding the URL's host and origin. As a result, impersonated configs for non-default universes, regional endpoints, or custom endpoints fell back to iamcredentials.googleapis.com on refresh, sign_bytes(), and IDTokenCredentials.refresh(). In addition, Credentials.sign_bytes() and IDTokenCredentials.refresh() ignored _iam_endpoint_override even when explicitly set.

Summary of Changes

  • Update Credentials.from_impersonated_service_account_info() to:
    • Validate source_credentials is a mapping and parse/validate service_account_impersonation_url (HTTPS only, valid host/port, no userinfo/fragments/whitespace/control chars, valid :generateAccessToken path).
    • Validate explicit top-level and nested universe_domain values using _helpers.validate_universe_domain() from fix(auth): centralize validate_universe_domain in _helpers #19216, checking them against each other and against service_account_impersonation_url (supporting global, regional, REP, PSC, mTLS, and custom proxy hosts, while rejecting conflicting universes and any *.googleapis.com host on a non-default universe).
    • Infer non-default universes from the impersonation URL host when universe_domain is omitted, while leaving omitted default-universe configs unset (None) to stay compatible with fix(auth): require explicit googleapis.com for RAB lookups #19208.
    • Propagate the resolved universe_domain onto a shallow copy of source_credentials_info (without mutating the caller's dict).
    • Preserve iam_endpoint_override=impersonation_url for regional, non-default-universe, explicit mTLS, and custom-proxy URLs, while leaving it None for the exact standard default https://iamcredentials.googleapis.com/... URL so mTLS endpoint selection remains intact.
  • Add optional universe_domain=None to Credentials.__init__() (validated via _helpers.validate_universe_domain() when provided and checked against source_credentials.universe_domain) and forward it in _make_copy().
  • Add _build_iam_endpoint() (using iam._IAM_DOMAIN host-prefix templating instead of .replace("googleapis.com", ...)) and use it in _make_iam_token_request(), Credentials.sign_bytes(), and IDTokenCredentials.refresh() so _iam_endpoint_override and mTLS prefixes are respected across all three IAM calls.
  • Add unit tests in test_impersonated_credentials.py covering universe validation, URL parsing/inference, proxy/regional/REP/PSC/mTLS endpoints, validation errors, sign_bytes(), and IDTokenCredentials.refresh().

What This PR Does Not Change

  • IDTokenCredentials.__init__() is left unchanged (tracked separately).

Verification

  • pytest --cov=google.auth.impersonated_credentials --cov=google.auth._helpers --cov-branch tests/test_impersonated_credentials.py tests/test__helpers.py tests/test__default.py tests/oauth2/test_id_token.py tests/test_external_account.py tests/test_iam.py: 477 passed, 100% statement and 100% branch coverage on impersonated_credentials.py and _helpers.py.
  • ruff check, ruff check --select I, ruff format --check, and repo-config mypy: clean.

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 robust validation and resolution logic for the universe_domain in impersonated credentials. It adds a new helper function validate_universe_domain in _helpers.py and several utility functions in impersonated_credentials.py to parse impersonation URLs and construct IAM endpoints correctly. The Credentials class and its factory method from_impersonated_service_account_info have been updated to enforce matching universe domains between source and impersonated credentials, and to handle custom or regional endpoints safely. Comprehensive unit tests have been added to verify these changes. I have no feedback to provide as there are no review comments.

@ohmayr

ohmayr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Opened #19216 to centralize validate_universe_domain() in google/auth/_helpers.py (and migrate jwt.py) separately so #19194 and this PR don't overlap on _helpers.py. Once #19216 merges, I'll rebase this PR onto main, drop the duplicate _helpers.py / test__helpers.py changes here, and update the impersonated_credentials.py call sites to guard if ... is not None: before calling _helpers.validate_universe_domain().

@ohmayr
ohmayr force-pushed the fix/auth-impersonated-universe-domain branch from b375b7f to b5ac372 Compare November 25, 2026 22:37
@ohmayr
ohmayr changed the base branch from main to refactor/auth-validate-universe-domain November 25, 2026 22:38
@ohmayr
ohmayr added this pull request to stack #19217 November 25, 2026 22:38
Credentials.from_impersonated_service_account_info() ignored top-level
universe_domain and discarded the host from
service_account_impersonation_url, causing non-default universe and
regional configs to fall back to iamcredentials.googleapis.com.
Credentials.sign_bytes() and IDTokenCredentials.refresh() also ignored
_iam_endpoint_override.

Validate explicit universe_domain values with
_helpers.validate_universe_domain(), parse and validate
service_account_impersonation_url (rejecting conflicting universes,
non-HTTPS URLs, malformed hosts, and default-universe endpoints on
non-default universes), infer non-default universes from the
impersonation host when omitted, propagate the resolved universe into
source credentials without mutating caller input, keep
iam_endpoint_override for non-default URLs (preserving mTLS selection
for the standard default URL), and honor _iam_endpoint_override in
sign_bytes() and IDTokenCredentials.refresh().

Fixes: b/589828921
@ohmayr
ohmayr force-pushed the fix/auth-impersonated-universe-domain branch from b5ac372 to a8ac188 Compare November 25, 2026 22:42
@ohmayr
ohmayr force-pushed the refactor/auth-validate-universe-domain branch from cc4e503 to 7df3e30 Compare November 25, 2026 22:42
@ohmayr ohmayr closed this Nov 25, 2026
@ohmayr
ohmayr deleted the fix/auth-impersonated-universe-domain branch November 25, 2026 23:01
@ohmayr
ohmayr removed this pull request from stack #19217 November 25, 2026 23:04
ohmayr added a commit that referenced this pull request Nov 25, 2026
## Context & Problem
`google/auth/jwt.py` currently defines a module-private
`_validate_universe_domain()` helper, and other credential modules
(`service_account` in #19194, `impersonated_credentials` in #19215, and
future `universe_domain` work) need the same validation. Moving it into
`google.auth._helpers` avoids duplicating validation across credential
classes and keeps error behavior consistent.

## Summary of Changes
- Add `_helpers.validate_universe_domain(universe_domain)` in
`packages/google-auth/google/auth/_helpers.py`:
- Validates against a strict ASCII domain allowlist
(`[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*` via `re.fullmatch`), raising
`exceptions.InvalidValue` (which subclasses `ValueError`) on
non-strings, empty strings, whitespace, control characters, URI/format
delimiters, Unicode homographs, and malformed dot structures (leading,
trailing, or consecutive dots).
- Keeps existing synthetic test placeholders (such as
`fake_universe_domain`, `dummy_universe.com`, and single-label domains)
working.
- Callers where `None` means "omitted" simply guard `if universe_domain
is not None: _helpers.validate_universe_domain(universe_domain)`.
- Remove local `_validate_universe_domain()` from
`packages/google-auth/google/auth/jwt.py` and update
`Credentials.with_universe_domain()` and
`OnDemandCredentials.with_universe_domain()` (inherited by `_jwt_async`)
to call `_helpers.validate_universe_domain(universe_domain)`.
- Add unit tests in `packages/google-auth/tests/test__helpers.py` and
expand the `with_universe_domain` invalid-input tests in
`packages/google-auth/tests/test_jwt.py`.

## What This PR Does Not Change
- `jwt.Credentials` and `jwt.OnDemandCredentials` constructors and
loaders are left unchanged here to keep this PR scoped to centralizing
the existing validator without changing constructor fallback semantics.

## Verification
- `pytest tests/test__helpers.py tests/test_jwt.py
tests_async/test_jwt_async.py`: `263 passed`, 100% statement and branch
coverage on `_helpers.py`
- `ruff check`, `ruff format --check`, and repo-config `mypy` (`google/`
+ `tests/`): clean
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