Repository navigation
fix(storage): support universe_domain in gRPC clients - #19160
daniel-sanche wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces universe domain support and validation for both synchronous and asynchronous gRPC clients in the Google Cloud Storage library. It refactors configuration resolution into shared helper functions and ensures DirectPath is disabled for non-default universes. The review feedback highlights a potential issue in _validate_universe_domain where un-specced mock.Mock() credentials used in user tests could cause unexpected failures because they dynamically return truthy Mock objects for the universe_domain attribute. A defensive check is suggested to ensure the retrieved universe domain is a string before validating it.
| def _validate_universe_domain(universe_domain: str, credentials) -> None: | ||
| """Raises UniverseMismatchError unless the credentials belong to ``universe_domain``. | ||
| Credentials that do not expose a universe domain are treated as | ||
| ``googleapis.com``, so they are rejected in any other universe. | ||
| """ | ||
| if credentials is not None and not isinstance( | ||
| credentials, auth_credentials.AnonymousCredentials | ||
| ): | ||
| universe.compare_domains(universe_domain, credentials) |
There was a problem hiding this comment.
When users write unit tests for their applications, they often use mock.Mock() to mock credentials. Since mock.Mock dynamically creates attributes on access, getattr(credentials, "universe_domain", None) will return a truthy Mock object instead of None or a string. This causes universe.compare_domains to fail or raise unexpected errors because it expects a string or None.
To prevent breaking user tests that use un-specced mocks, we should defensively check if the retrieved universe_domain is a string before calling compare_domains.
| def _validate_universe_domain(universe_domain: str, credentials) -> None: | |
| """Raises UniverseMismatchError unless the credentials belong to ``universe_domain``. | |
| Credentials that do not expose a universe domain are treated as | |
| ``googleapis.com``, so they are rejected in any other universe. | |
| """ | |
| if credentials is not None and not isinstance( | |
| credentials, auth_credentials.AnonymousCredentials | |
| ): | |
| universe.compare_domains(universe_domain, credentials) | |
| def _validate_universe_domain(universe_domain: str, credentials) -> None: | |
| """Raises UniverseMismatchError unless the credentials belong to 'universe_domain'. | |
| Credentials that do not expose a universe domain are treated as | |
| 'googleapis.com', so they are rejected in any other universe. | |
| """ | |
| if credentials is not None and not isinstance( | |
| credentials, auth_credentials.AnonymousCredentials | |
| ): | |
| creds_universe = getattr(credentials, "universe_domain", None) | |
| if creds_universe is not None and not isinstance(creds_universe, str): | |
| return | |
| universe.compare_domains(universe_domain, credentials) |
Summary
Updates
GrpcClientandAsyncGrpcClientto honoruniverse_domain(viaclient_optionsorGOOGLE_CLOUD_UNIVERSE_DOMAIN) and disable DirectPath outside the defaultgoogleapis.comuniverse.Changes
_resolve_grpc_configand_validate_universe_domainhelpers, shared between sync and async client init methods. These helpers will avoid direct path on non-default universes