Skip to content

feat(api_core): add shared universe_domain client and host helpers - #19211

Merged
chalmerlowe merged 4 commits into
mainfrom
feat/api-core-universe-helpers
Dec 1, 2026
Merged

chalmerlowe merged 4 commits into
mainfrom
feat/api-core-universe-helpers

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Context

Handwritten veneers and composite clients across google-cloud-python repeatedly implement the same three universe_domain and endpoint-routing checks on top of the low-level primitives in google.api_core.universe:

  1. Resolving a client's effective universe_domain from ClientOptions, a dict/mapping, GOOGLE_CLOUD_UNIVERSE_DOMAIN, or "googleapis.com" per AIP-4113.
  2. Validating resolved client credentials against the client's universe_domain while exempting None, synchronous and asynchronous AnonymousCredentials, and uncached compute_engine.Credentials (avoiding synchronous Metadata Server HTTP lookups during client initialization).
  3. Checking whether an HTTP(S) URL or gRPC target (host:port, dns:, google-c2p:///) resolves to googleapis.com or *.googleapis.com when gating default-universe-only features (such as DirectPath, mTLS endpoint switching, or built-in Cloud Monitoring metrics).

Centralizing these helpers in google.api_core.universe provides a single canonical implementation for downstream libraries.

Solution

  • google.api_core.universe:
    • Add resolve_client_universe(client_options=None) for AIP-4113 precedence across ClientOptions, Mapping, GOOGLE_CLOUD_UNIVERSE_DOMAIN, and "googleapis.com", raising EmptyUniverseError on empty strings and TypeError on invalid client_options types.
    • Add validate_credentials_universe(client_universe, credentials) to validate resolved credentials via compare_domains while exempting None, google.auth.credentials.AnonymousCredentials, google.auth.aio.credentials.AnonymousCredentials, and uncached compute_engine.Credentials.
    • Add is_googleapis_host(url_or_target) backed by a shared _parse_target normalizer built on urllib.parse.urlsplit and RFC-valid DNS label matching (_GOOGLEAPIS_HOST_PATTERN), rejecting userinfo (@ in authority/netloc) while allowing @ in HTTP(S) URL paths and queries (such as IAM service account endpoints).
  • google.api_core.grpc_helpers:
    • Reuse universe._parse_target and universe._GOOGLEAPIS_HOST_PATTERN across _extract_target_host, _is_direct_path_googleapis_target, and _modify_target_for_direct_path to eliminate duplicate URI-scheme stripping logic while preserving custom DNS authorities (dns://authority/...) untouched when DirectPath is enabled.
  • Tests:
    • Centralize clean_universe_env (autouse=True) in tests/conftest.py and add comprehensive unit tests in tests/unit/test_universe.py and tests/unit/test_grpc_helpers.py covering precedence, sync/async credential validation, and URI/target boundary cases at 100% branch coverage.

Notes for Reviewers

  • Downstream packages whose minimum google-api-core constraint predates the next release can mirror these helpers locally with a # TODO(universe-domain): marker and switch to direct google.api_core.universe imports once their constraint floor advances.
  • In grpc_helpers._is_direct_path_googleapis_target and _modify_target_for_direct_path, dns://authority/host:port targets with an explicit custom DNS server authority continue to bypass DirectPath google-c2p:/// rewriting so custom DNS resolvers are never silently discarded.

from google.api_core._feature_gating_helpers import _strtobool

_DIRECT_PATH_INTERCONNECT_ENV = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT"
_GOOGLEAPIS_HOST_PATTERN = re.compile(

@chalmerlowe chalmerlowe Nov 25, 2026 •

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.

Note

NOTE TO REVIEWERS:
Several items from this file are moved to the universe.py file due to their close association with trying to manage universe_domain issues.

@chalmerlowe
chalmerlowe force-pushed the feat/api-core-universe-helpers branch from bfb90f3 to 168fa15 Compare November 25, 2026 16:54
credentials, auth_credentials.AnonymousCredentials
):
return
compare_domains(client_universe, credentials)

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.

compare_domains sometimes makes a network request to the metadata server to find the credential's universe_domain. My understanding is that this was showing up noisily in logs, so compare_domains was currently removed from use (there's an internal bug I'll send you over chat)

We may want to have this auto-pass for now, with a TODO explaining the situation. Or skip validation for credential types that need to talk to MDS. Or we could leave this level of validation out of scope for now

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.

It looks like google.auth.compute_engine.Credentials is the only one with this problem, so maybe we should just handle that as a special case with a comment here

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.

@daniel-sanche
Great catch! Checked across google-auth and yes, compute_engine.Credentials (credentials.py:249-264) is the one credential class whose .universe_domain property triggers a Metadata Server HTTP lookup when _universe_domain_cached is False.

Went with your middle option in cef786d723c: validate_credentials_universe now skips uncached compute_engine.Credentials (avoiding MDS network requests during client init) while still validating all in-memory credentials:

  • service_account
  • external_account
  • jwt
  • oauth2
  • explicitly configured compute_engine.Credentials

That also gives handwritten veneers (like storage/grpc_client.py, which currently calls raw compare_domains) a single shared MDS-safe helper to call.

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.

Sounds good, thanks!

@chalmerlowe
chalmerlowe marked this pull request as ready for review November 25, 2026 19:14
@chalmerlowe
chalmerlowe requested a review from a team as a code owner November 25, 2026 19:14
credentials, auth_credentials.AnonymousCredentials
):
return
compare_domains(client_universe, credentials)

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.

Sounds good, thanks!

@chalmerlowe
chalmerlowe merged commit be7a791 into main Dec 1, 2026
219 of 280 checks passed
@chalmerlowe
chalmerlowe deleted the feat/api-core-universe-helpers branch December 1, 2026 08:55
@release-please release-please Bot mentioned this pull request Dec 1, 2026
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