Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds universe domain support and validation to the Google Cloud NDB client, resolving the universe domain from client options or environment variables and templating the API endpoint. A critical issue was identified in the _validate_credentials_universe helper function, where universe.compare_domains is incorrectly called with a Credentials object instead of a domain string, and its return value is not checked to raise a UniverseMismatchError on mismatch.
| 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.
There are two critical issues with this line:\n\n1. Incorrect Argument: universe.compare_domains expects two domain strings, but credentials (a Credentials object) is passed instead of cred_universe. This will raise an AttributeError at runtime (e.g., 'Credentials' object has no attribute 'strip') when real credentials are used.\n2. Ignored Return Value: universe.compare_domains returns a boolean and does not raise an exception on mismatch. The return value must be checked, and universe.UniverseMismatchError should be raised if it returns False.
if not universe.compare_domains(client_universe, cred_universe):\n raise universe.UniverseMismatchError(\n f"The client universe {client_universe} does not match the credentials universe {cred_universe}."\n )There was a problem hiding this comment.
google.api_core.universe.compare_domains(client_universe, credentials) actually takes the credentials object (it reads credentials.universe_domain internally via getattr) and raises UniverseMismatchError directly on mismatch rather than returning False. Passing the cred_universe string instead would break mismatch detection since getattr on a str would always fall back to DEFAULT_UNIVERSE, so the current call is intentional.
Resolve universe_domain via google.api_core.universe.determine_domain, template the default Datastore host as datastore.<universe_domain>, and validate credential universe parity before opening a secure gRPC channel (AIP-4113). Fixes: b/589768754
7d3f485 to
6f851f4
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, credentials): |
There was a problem hiding this comment.
SYNC WARNING:
I noticed two sync gaps between #19251 and the proposed shared helpers in catalog.py (cl/1029785586). I recommend the following actions:
- Update
_validate_credentials_universeto mirrorvalidate_credentials_universeincatalog.py(cl/1029785586), specifically usingnot getattr(credentials, "_universe_domain_cached", False)and exemptinggoogle.auth.aio.credentials.AnonymousCredentials. - Replace the inline universe resolution in
Client.__init__with a copy ofresolve_client_universefromcatalog.py(cl/1029785586) and a# TODO(universe-domain):comment.
Why: Checking getattr(..., "_universe_domain_cached", None) is False misses older google-auth versions where _universe_domain_cached is absent, and resolve_client_universe adds consistent EmptyUniverseError handling for empty/whitespace values. Keep both as local # TODO(universe-domain): copies until testing/constraints-3.10.txt exceeds google-api-core 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 55a8eb2 to mirror validate_credentials_universe (using not getattr(..., "_universe_domain_cached", False) and exempting aio AnonymousCredentials) and resolve_client_universe with TODO(universe-domain) comments and unit tests. Kept the non-string cred_universe guard for existing ndb mock credentials and .strip() for google-api-core 2.28.0 compatibility.
Context & Problem
google.cloud.ndb.Client ignored client_options.universe_domain and GOOGLE_CLOUD_UNIVERSE_DOMAIN, hardcoding datastore.googleapis.com and opening a gRPC channel without validating credential universe parity (AIP-4113).
Summary of Changes
Why This Approach
Resolving universe_domain and validating credentials directly inside ndb.Client.init before make_secure_channel prevents cross-universe token leakage while preserving google-cloud-ndb's google-api-core >= 2.28.0 lower bound. We considered waiting for google.api_core.universe.validate_credentials_universe (> 2.44.0) or relying on google-cloud-core, but mirroring _validate_credentials_universe locally with a TODO(universe-domain) marker matches sibling PRs (#19192, #19231) and works immediately without bumping dependency floors.
Intentionally Out of Scope
Verification
pytest packages/google-cloud-ndb/tests/unit/ on Python 3.14 with google-api-core==2.28.0 (1851 passed, including 16 new universe_domain tests in test_client.py)
Fixes: b/589768754