Repository navigation
feat(api_core): add shared universe_domain client and host helpers - #19211
Conversation
| 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( |
There was a problem hiding this comment.
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.
bfb90f3 to
168fa15
Compare
| credentials, auth_credentials.AnonymousCredentials | ||
| ): | ||
| return | ||
| compare_domains(client_universe, credentials) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
@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_accountexternal_accountjwtoauth2- 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.
There was a problem hiding this comment.
Sounds good, thanks!
…edentials_universe
| credentials, auth_credentials.AnonymousCredentials | ||
| ): | ||
| return | ||
| compare_domains(client_universe, credentials) |
There was a problem hiding this comment.
Sounds good, thanks!
Problem / Context
Handwritten veneers and composite clients across
google-cloud-pythonrepeatedly implement the same threeuniverse_domainand endpoint-routing checks on top of the low-level primitives ingoogle.api_core.universe:universe_domainfromClientOptions, a dict/mapping,GOOGLE_CLOUD_UNIVERSE_DOMAIN, or"googleapis.com"per AIP-4113.universe_domainwhile exemptingNone, synchronous and asynchronousAnonymousCredentials, and uncachedcompute_engine.Credentials(avoiding synchronous Metadata Server HTTP lookups during client initialization).host:port,dns:,google-c2p:///) resolves togoogleapis.comor*.googleapis.comwhen gating default-universe-only features (such as DirectPath, mTLS endpoint switching, or built-in Cloud Monitoring metrics).Centralizing these helpers in
google.api_core.universeprovides a single canonical implementation for downstream libraries.Solution
google.api_core.universe:resolve_client_universe(client_options=None)for AIP-4113 precedence acrossClientOptions,Mapping,GOOGLE_CLOUD_UNIVERSE_DOMAIN, and"googleapis.com", raisingEmptyUniverseErroron empty strings andTypeErroron invalidclient_optionstypes.validate_credentials_universe(client_universe, credentials)to validate resolved credentials viacompare_domainswhile exemptingNone,google.auth.credentials.AnonymousCredentials,google.auth.aio.credentials.AnonymousCredentials, and uncachedcompute_engine.Credentials.is_googleapis_host(url_or_target)backed by a shared_parse_targetnormalizer built onurllib.parse.urlsplitand 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:universe._parse_targetanduniverse._GOOGLEAPIS_HOST_PATTERNacross_extract_target_host,_is_direct_path_googleapis_target, and_modify_target_for_direct_pathto eliminate duplicate URI-scheme stripping logic while preserving custom DNS authorities (dns://authority/...) untouched when DirectPath is enabled.clean_universe_env(autouse=True) intests/conftest.pyand add comprehensive unit tests intests/unit/test_universe.pyandtests/unit/test_grpc_helpers.pycovering precedence, sync/async credential validation, and URI/target boundary cases at 100% branch coverage.Notes for Reviewers
google-api-coreconstraint predates the next release can mirror these helpers locally with a# TODO(universe-domain):marker and switch to directgoogle.api_core.universeimports once their constraint floor advances.grpc_helpers._is_direct_path_googleapis_targetand_modify_target_for_direct_path,dns://authority/host:porttargets with an explicit custom DNS server authority continue to bypass DirectPathgoogle-c2p:///rewriting so custom DNS resolvers are never silently discarded.