Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces universe domain validation to the Firestore base client, ensuring that client credentials match the resolved universe domain. It adds the _validate_credentials_universe helper, updates client initialization and API helpers to perform validation, and adjusts endpoint targeting for non-default universes. Comprehensive unit tests have also been added. However, a critical bug was identified in _validate_credentials_universe where universe.compare_domains is called with the credentials object instead of the resolved cred_universe string, which will lead to an AttributeError at runtime.
| cred_universe = getattr(credentials, "universe_domain", universe.DEFAULT_UNIVERSE) | ||
| if not isinstance(cred_universe, str): | ||
| return | ||
| universe.compare_domains(client_universe, credentials) |
There was a problem hiding this comment.
In _validate_credentials_universe, universe.compare_domains is called with credentials (the credentials object) instead of cred_universe (the resolved universe domain string).
This will cause an AttributeError at runtime when using real credentials because the credentials object does not have string methods like lower() or strip(). The unit tests did not catch this because they use a mock credentials object where calling strip().lower() returns another mock, which then compared unequal to the expected string and raised UniverseMismatchError by coincidence.
| universe.compare_domains(client_universe, credentials) | |
| universe.compare_domains(client_universe, cred_universe) |
References
- Rely on comparison utilities (like 'universe.compare_domains') that safely fallback to a default universe domain when the attribute is missing.
There was a problem hiding this comment.
google.api_core.universe.compare_domains(client_universe, credentials) takes the credentials object (reading credentials.universe_domain via getattr) and raises UniverseMismatchError on mismatch. Passing the cred_universe string would break mismatch detection because getattr on a str always falls back to DEFAULT_UNIVERSE, so the current call is intentional.
Resolve universe_domain on BaseClient from client_options or GOOGLE_CLOUD_UNIVERSE_DOMAIN, format the default endpoint template for non-default universes, and validate credentials against the resolved universe before opening a gRPC channel. Fixes: b/588887079
3ae9f3d to
20a1ae2
Compare
| # TODO(universe-domain): Replace with | ||
| # google.api_core.universe.validate_credentials_universe once minimum | ||
| # google-api-core version exceeds 2.44.0. | ||
| def _validate_credentials_universe(client_universe: str, credentials: Any) -> None: |
There was a problem hiding this comment.
SYNC WARNING:
I noticed a couple of sync gaps between #19252 and the proposed shared helpers in catalog.py (cl/1029785586). I recommend the following actions:
- Mirror
_validate_credentials_universeonvalidate_credentials_universein catalog.py (cl/1029785586), which also exempts google.auth.aio.credentials.AnonymousCredentials (important for Firestore's AsyncClient) alongside sync AnonymousCredentials and uncached compute_engine.Credentials. - Replace the inline universe resolution in
BaseClient.__init__with a copy ofresolve_client_universefrom catalog.py (cl/1029785586) and a # TODO(universe-domain): comment so universe resolution and EmptyUniverseError/TypeError handling stay consistent across clients. - Because google-cloud-firestore pins google-api-core==2.28.0 in testing/constraints-3.10.txt, keep both as local # TODO(universe-domain): copies until the floor exceeds 2.44.0.
I know that you have better context on this than I do, so if any of these suggestions are wrong, please leave a comment here. Gemini will pick them up and learn from them during the next sync sweep.
There was a problem hiding this comment.
Updated both helpers in 6d75f72: added aio AnonymousCredentials exemption (guarded for google-auth 2.14.1) and replaced the inline resolution with a local _resolve_client_universe copy marked with TODO(universe-domain) until the google-api-core floor passes 2.44.0. Also added unit tests covering both helpers.
Context & Problem
Both synchronous Client and asynchronous AsyncClient in google-cloud-firestore ignored client_options.universe_domain and GOOGLE_CLOUD_UNIVERSE_DOMAIN, hardcoding firestore.googleapis.com and opening a gRPC channel without validating credential universe parity.
Summary of Changes
Why This Approach
Resolving universe_domain and validating credentials inside BaseClient fixes both sync Client and AsyncClient (including _to_sync_copy) in one place while keeping google-cloud-firestore's google-api-core >= 2.28.0 lower bound. We considered waiting for google.api_core.universe.validate_credentials_universe (> 2.44.0) or google-cloud-core (#19231), but mirroring _validate_credentials_universe locally with a TODO(universe-domain) marker matches sibling PR #19251 and works immediately without bumping dependency floors.
Intentionally Out of Scope
Verification
pytest packages/google-cloud-firestore/tests/unit/v1/test_base_client.py test_client.py test_async_client.py on Python 3.14 with google-api-core==2.28.0 (166 passed, including 18 new universe_domain tests); nox -s lint and nox -s mypy-3.14 clean
Fixes: b/588887079