Skip to content

fix(firestore): resolve universe_domain and validate credentials - #19252

Draft
ohmayr wants to merge 3 commits into
mainfrom
smallpr/b-588887079-firestore-universe-domain
Draft

ohmayr wants to merge 3 commits into
mainfrom
smallpr/b-588887079-firestore-universe-domain

Conversation

@ohmayr

@ohmayr ohmayr commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

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

  • Resolve universe_domain in BaseClient.init via google.api_core.universe.determine_domain and expose client.universe_domain.
  • Template non-default endpoints from client_class._DEFAULT_ENDPOINT_TEMPLATE in BaseClient._target_helper when FIRESTORE_EMULATOR_HOST and client_options.api_endpoint are unset.
  • Validate credential universe parity via _validate_credentials_universe in BaseClient.init and before transport.create_channel in _firestore_api_helper (exempting emulator mode, sync and async AnonymousCredentials, uncached compute_engine.Credentials, and non-string credential mocks).
  • Add 18 unit tests in test_base_client.py covering Client and AsyncClient universe resolution, endpoint precedence, empty universe errors, mismatch errors before channel creation, mutated credentials, and exemptions.

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

  • Bumping google-api-core or google-cloud-core minimum versions in setup.py.
  • Changing generated GAPIC client code or re-enabling GAPIC _validate_universe_domain (tracked in b/349488459).
  • Modifying firestore_admin_v1, which is pure GAPIC and already handles universe_domain.

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

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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)

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.

high

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.

Suggested change
universe.compare_domains(client_universe, credentials)
universe.compare_domains(client_universe, cred_universe)
References
  1. Rely on comparison utilities (like 'universe.compare_domains') that safely fallback to a default universe domain when the attribute is missing.

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.

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.

@ohmayr ohmayr changed the title fix(firestore): respect universe_domain and validate credentials fix(firestore): resolve universe_domain and validate credentials Nov 27, 2026
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
@ohmayr
ohmayr force-pushed the smallpr/b-588887079-firestore-universe-domain branch from 3ae9f3d to 20a1ae2 Compare November 27, 2026 18:51
# 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:

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.

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_universe on validate_credentials_universe in 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 of resolve_client_universe from 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.

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.

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.

This branch has not been deployed

No deployments
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