Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the _build_iam_endpoint helper function in packages/google-auth/google/auth/iam.py to construct IAM Credentials API URLs, taking into account universe domains and endpoint overrides, alongside comprehensive unit tests in packages/google-auth/tests/test_iam.py. The review feedback correctly identifies a limitation in how endpoint overrides are parsed: the current logic only checks for :generateAccessToken and would produce malformed URLs if other actions (like :signBlob or :generateIdToken) are used in the override. A code suggestion is provided to handle all known IAM actions robustly.
| try: | ||
| parsed = urllib.parse.urlsplit(iam_endpoint_override) | ||
| hostname = parsed.hostname | ||
| except ValueError as exc: | ||
| raise exceptions.InvalidValue( | ||
| f"Invalid iam_endpoint_override: {iam_endpoint_override}" | ||
| ) from exc | ||
|
|
||
| if not parsed.scheme or not hostname: | ||
| raise exceptions.InvalidValue( | ||
| f"Invalid iam_endpoint_override: {iam_endpoint_override}" | ||
| ) |
There was a problem hiding this comment.
Could we tighten the iam_endpoint_override check a bit? Right now it accepts non-HTTPS schemes (like ftp:// or http://), invalid ports (like https://host:abc since parsed.port is not checked), userinfo (@ in netloc), fragments (#), and whitespace/control chars, and non-str inputs raise AttributeError/TypeError instead of InvalidValue. Checking isinstance(..., str), rejecting whitespace/control chars, #, and @, touching parsed.port, and restricting to https would match the rest of google-auth.
| for known_action in _IAM_ACTIONS: | ||
| if parsed.path.endswith(f":{known_action}"): | ||
| path = parsed.path[: -len(known_action)] + action | ||
| break | ||
| else: | ||
| base_path = parsed.path.rstrip("/") | ||
| path = f"{base_path}/v1/projects/-/serviceAccounts/{target_principal}:{action}" | ||
| return urllib.parse.urlunsplit(parsed._replace(path=path)) |
There was a problem hiding this comment.
If iam_endpoint_override has a trailing slash after an action (like ...:generateAccessToken/), endswith misses and the else branch appends /v1/projects/-/serviceAccounts/... a second time. Stripping trailing slashes before the for loop handles that cleanly.
Context
Add a shared IAM Credentials API endpoint builder in
google.auth.iamso callers acrossgoogle-auth(including #19194 and #19219) can share a single URL construction helper.Summary of Changes
_build_iam_endpoint(target_principal, universe_domain, iam_endpoint_override, action)inpackages/google-auth/google/auth/iam.py:universe_domainvia_helpers.validate_universe_domainwheniam_endpoint_overrideis unset.mtls.host prefix strictly toDEFAULT_UNIVERSE_DOMAIN(googleapis.com, case-insensitive) and usesiamcredentials.<universe_domain>for non-default universe domains.generateAccessToken,signBlob,signJwt,generateIdToken, format templates (target_principal="{}"), andiam_endpoint_overrideURLs (rewriting:generateAccessTokensuffixes or appending the standard IAM path while preserving query parameters).exceptions.InvalidValuefor malformed or incompleteiam_endpoint_overrideURLs.See feedback in #19194 (comment)