Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces robust parsing and validation of the service account impersonation URL in impersonated_credentials.py. It extracts the target principal and hostname, infers the universe domain from the host, and validates it against explicit universe domains. Additionally, it preserves custom origins and only overrides the IAM endpoint when a non-default URL is used. Extensive unit tests have been added to cover these scenarios. The review feedback points out a potential AttributeError if the source_credentials field is missing from the input dictionary, suggesting an explicit validation check.
| @@ -511,7 +567,36 @@ def from_impersonated_service_account_info(cls, info, scopes=None): | |||
| """ | |||
|
|
|||
| source_credentials_info = info.get("source_credentials") | |||
There was a problem hiding this comment.
If source_credentials is missing from the info dictionary, source_credentials_info will be None. This will lead to a cryptic AttributeError when trying to access .get("universe_domain") or .get("type") on it later in the method. It is highly recommended to validate that source_credentials_info is not None and raise a clear exceptions.InvalidValue exception if it is missing.
| source_credentials_info = info.get("source_credentials") | |
| source_credentials_info = info.get("source_credentials") | |
| if source_credentials_info is None: | |
| raise exceptions.InvalidValue("The 'source_credentials' field is required in the service account info.") |
There was a problem hiding this comment.
Added an explicit Mapping check for source_credentials in commit be24566 so missing or non-mapping values raise InvalidValue instead of AttributeError, along with unit tests.
5a5c9a0 to
0ac36bb
Compare
0ac36bb to
7cbec2a
Compare
7cbec2a to
1ced26c
Compare
1ced26c to
6467757
Compare
6467757 to
fad933e
Compare
Credentials.from_impersonated_service_account_info() ignored a top-level universe_domain field and delegated _universe_domain solely to source_credentials. When source_credentials omitted universe_domain, impersonated credentials defaulted to googleapis.com and sent source tokens to iamcredentials.googleapis.com. Add an optional universe_domain parameter to Credentials.__init__() (validated via _helpers.validate_universe_domain() and compared against source_credentials.universe_domain via _helpers.compare_universe_domains()), forward it in _make_copy(), and propagate top-level universe_domain into a shallow copy of source_credentials_info in from_impersonated_service_account_info() without mutating caller input. b/589828921
Credentials.from_impersonated_service_account_info() sliced the target principal out of service_account_impersonation_url and discarded the URL's host and origin. Impersonated configs targeting a regional, custom-universe, or proxy endpoint therefore fell back to iamcredentials.googleapis.com and could bypass universe consistency checks. Validate service_account_impersonation_url with _parse_impersonation_url() and extract Google IAM Credentials host universes with _extract_universe_from_iam_host(). Reuse _helpers.compare_universe_domains() and _helpers.has_default_universe_host() to enforce host-vs-universe consistency, infer non-default universes when omitted, and pass iam_endpoint_override for non-default URLs while keeping mTLS selection intact for the standard default URL. b/589828921
be24566 to
e9f028f
Compare
Context & Problem
impersonated_credentials.Credentials.from_impersonated_service_account_info()sliced the target principal out ofservice_account_impersonation_urland discarded the URL's host and origin. Impersonated configs targeting a regional, custom-universe, or proxy endpoint therefore fell back toiamcredentials.googleapis.comand could bypass universe consistency checks.Summary of Changes
_parse_impersonation_url()and_extract_universe_from_iam_host()inpackages/google-auth/google/auth/impersonated_credentials.pyto validate HTTPS:generateAccessTokenimpersonation URLs and recognize standard, regional, REP, PSC, and mTLS Google IAM Credentials hostnames.from_impersonated_service_account_info()to enforce host-vs-universe consistency using_helpers.compare_universe_domains()(from chore(auth): add compare_universe_domains helper in _helpers #19220) and_helpers.has_default_universe_host(), infer non-default universes when omitted, and passiam_endpoint_overridefor non-default URLs while keeping mTLS selection intact for the standard default URL.packages/google-auth/tests/test_impersonated_credentials.py.Verification
ruff check,ruff check --select I,ruff format --check, andmypy --config-file=../../mypy.inipassed with zero issues.pytestpassed (496 passed) with 100% statement and branch coverage on bothgoogle/auth/_helpers.pyandgoogle/auth/impersonated_credentials.py.b/589828921