From 756f3afa1e15abf41814661c22497e51f3b0e874 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 20 Nov 2026 13:30:12 -0700 Subject: [PATCH 1/2] fix(storage): support universe_domain in gRPC clients --- .../storage/asyncio/async_grpc_client.py | 76 ++++---- .../google/cloud/storage/grpc_client.py | 132 ++++++++++---- .../unit/asyncio/test_async_grpc_client.py | 172 +++++++++++++++++- .../unit/asyncio/test_rapid_observability.py | 4 +- .../tests/unit/test_grpc_client.py | 156 +++++++++++++++- 5 files changed, 456 insertions(+), 84 deletions(-) diff --git a/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py b/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py index e113943857a8..0e11993482b7 100644 --- a/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py +++ b/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py @@ -14,6 +14,7 @@ """An async client for interacting with Google Cloud Storage using the gRPC API.""" +import google.auth import grpc from google.auth import credentials as auth_credentials @@ -26,9 +27,9 @@ from google.cloud.storage._helpers import create_trace_span_helper from google.cloud.storage.asyncio import _utils from google.cloud.storage.grpc_client import ( - _DEFAULT_HOST, - _resolve_direct_path_interconnect, - _rewrite_host_for_interconnect, + _resolve_grpc_config, + _resolve_universe_domain, + _validate_universe_domain, ) @@ -118,6 +119,7 @@ def __init__( raise ValueError( "Either client_options or `client_option.api_endpoint` is None. Please provide api_endpoint when `AnonymousCredentials` is used " ) + self._universe_domain = _resolve_universe_domain(client_options) self._grpc_client = self._create_anonymous_client( client_options, credentials ) @@ -132,18 +134,19 @@ def __init__( if agent_version not in client_info.user_agent: client_info.user_agent += f" {agent_version} " - use_dp_interconnect = _resolve_direct_path_interconnect( - attempt_direct_path_xds_over_interconnect - ) - self._grpc_client = self._create_async_grpc_client( credentials=credentials, client_info=client_info, client_options=client_options, attempt_direct_path=attempt_direct_path, - attempt_direct_path_xds_over_interconnect=use_dp_interconnect, + attempt_direct_path_xds_over_interconnect=attempt_direct_path_xds_over_interconnect, ) + @property + def universe_domain(self) -> str: + """The universe domain used by the client instance.""" + return self._universe_domain + @property def metrics_enabled(self) -> bool: """(Experimental) Returns ``True`` if OpenTelemetry metrics recording is active for this client.""" @@ -177,40 +180,39 @@ def _create_async_grpc_client( attempt_direct_path=True, attempt_direct_path_xds_over_interconnect=False, ): + ( + self._universe_domain, + host, + quota_project_id, + attempt_direct_path, + use_dp_interconnect, + ) = _resolve_grpc_config( + client_options, + attempt_direct_path, + attempt_direct_path_xds_over_interconnect, + ) transport_cls = storage_v2.StorageAsyncClient.get_transport_class( "grpc_asyncio" ) + if credentials is None: + credentials, _ = google.auth.default( + default_scopes=transport_cls.AUTH_SCOPES + ) + _validate_universe_domain(self._universe_domain, credentials) primary_user_agent = client_info.to_user_agent() - - host = _DEFAULT_HOST - quota_project_id = None - if isinstance(client_options, dict): - host = client_options.get("api_endpoint") or _DEFAULT_HOST - quota_project_id = client_options.get("quota_project_id") - elif client_options: - host = getattr(client_options, "api_endpoint", None) or _DEFAULT_HOST - quota_project_id = getattr(client_options, "quota_project_id", None) - - if attempt_direct_path_xds_over_interconnect: - host = _rewrite_host_for_interconnect(host) - channel = transport_cls.create_channel( - host=host, - quota_project_id=quota_project_id, - attempt_direct_path=True, - attempt_direct_path_xds_over_interconnect=True, - credentials=credentials, - options=(("grpc.primary_user_agent", primary_user_agent),), - ) - else: - channel = transport_cls.create_channel( - host=host, - quota_project_id=quota_project_id, - attempt_direct_path=attempt_direct_path, - credentials=credentials, - options=(("grpc.primary_user_agent", primary_user_agent),), - ) - transport = transport_cls(channel=channel) + channel_kwargs = { + "host": host, + "quota_project_id": quota_project_id, + "attempt_direct_path": attempt_direct_path, + "credentials": credentials, + "options": (("grpc.primary_user_agent", primary_user_agent),), + } + if use_dp_interconnect: + channel_kwargs["attempt_direct_path_xds_over_interconnect"] = True + + channel = transport_cls.create_channel(**channel_kwargs) + transport = transport_cls(host=host, channel=channel) return storage_v2.StorageAsyncClient( transport=transport, diff --git a/packages/google-cloud-storage/google/cloud/storage/grpc_client.py b/packages/google-cloud-storage/google/cloud/storage/grpc_client.py index 79bcb4f0d8b1..4b2bb02459ac 100644 --- a/packages/google-cloud-storage/google/cloud/storage/grpc_client.py +++ b/packages/google-cloud-storage/google/cloud/storage/grpc_client.py @@ -16,12 +16,16 @@ import os +from google.api_core import universe +from google.auth import credentials as auth_credentials from google.cloud.client import ClientWithProject from google.cloud import _storage_v2 as storage_v2 +from google.cloud._storage_v2._compat import get_universe_domain from google.cloud.storage import _opentelemetry_metrics _marker = object() +_DEFAULT_UNIVERSE_DOMAIN = universe.DEFAULT_UNIVERSE _DEFAULT_HOST = "storage.googleapis.com" _DEFAULT_HOST_DIRECT_PATH = "storage-direct.googleapis.com" _DIRECT_PATH_INTERCONNECT_ENV = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT" @@ -60,6 +64,66 @@ def _rewrite_host_for_interconnect( return host +def _get_option(client_options, name): + """Reads an option from a dict, a ``ClientOptions`` instance, or ``None``.""" + if isinstance(client_options, dict): + return client_options.get(name) + return getattr(client_options, name, None) + + +def _resolve_universe_domain(client_options) -> str: + """Resolves the universe domain with the helper the generated client uses.""" + return get_universe_domain( + _get_option(client_options, "universe_domain"), + os.getenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN"), + default_universe=_DEFAULT_UNIVERSE_DOMAIN, + ) + + +def _resolve_grpc_config( + client_options, + attempt_direct_path, + attempt_direct_path_xds_over_interconnect: bool, +): + """Resolves universe domain, target host, quota project, and DirectPath flags. + + DirectPath (including DirectPath over Interconnect) is only attempted in the + default universe. + """ + universe_domain = _resolve_universe_domain(client_options) + use_dp_interconnect = _resolve_direct_path_interconnect( + attempt_direct_path_xds_over_interconnect + ) + if universe_domain.lower() != _DEFAULT_UNIVERSE_DOMAIN.lower(): + attempt_direct_path = False + use_dp_interconnect = False + + host = _get_option(client_options, "api_endpoint") or f"storage.{universe_domain}" + if use_dp_interconnect: + host = _rewrite_host_for_interconnect(host) + attempt_direct_path = True + + return ( + universe_domain, + host, + _get_option(client_options, "quota_project_id"), + attempt_direct_path, + use_dp_interconnect, + ) + + +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) + + class GrpcClient(ClientWithProject): """A client for interacting with Google Cloud Storage using the gRPC API. @@ -151,18 +215,24 @@ def __init__( elif api_key: client_options.api_key = api_key - use_dp_interconnect = _resolve_direct_path_interconnect( - attempt_direct_path_xds_over_interconnect + effective_credentials = ( + credentials + if credentials is not None + else getattr(self, "_credentials", None) ) - self._grpc_client = self._create_gapic_client( - credentials=credentials, + credentials=effective_credentials, client_info=client_info, client_options=client_options, attempt_direct_path=attempt_direct_path, - attempt_direct_path_xds_over_interconnect=use_dp_interconnect, + attempt_direct_path_xds_over_interconnect=attempt_direct_path_xds_over_interconnect, ) + @property + def universe_domain(self) -> str: + """The universe domain used by the client instance.""" + return self._universe_domain + @property def metrics_enabled(self) -> bool: """(Experimental) Returns ``True`` if OpenTelemetry metrics recording is active for this client.""" @@ -182,36 +252,34 @@ def _create_gapic_client( attempt_direct_path_xds_over_interconnect=False, ): """Creates and configures the low-level GAPIC `storage_v2` client.""" - transport_cls = storage_v2.StorageClient.get_transport_class("grpc") - - if attempt_direct_path_xds_over_interconnect: - host = _DEFAULT_HOST - quota_project_id = None - if isinstance(client_options, dict): - host = client_options.get("api_endpoint") or _DEFAULT_HOST - quota_project_id = client_options.get("quota_project_id") - elif client_options is not None: - host = getattr(client_options, "api_endpoint", None) or _DEFAULT_HOST - quota_project_id = getattr(client_options, "quota_project_id", None) - host = _rewrite_host_for_interconnect(host) - channel_kwargs = { - "host": host, - "credentials": credentials, - "attempt_direct_path": True, - "attempt_direct_path_xds_over_interconnect": True, - } - if quota_project_id is not None: - channel_kwargs["quota_project_id"] = quota_project_id - channel = transport_cls.create_channel(**channel_kwargs) - else: - channel = transport_cls.create_channel( - attempt_direct_path=attempt_direct_path - ) + ( + self._universe_domain, + host, + quota_project_id, + attempt_direct_path, + use_dp_interconnect, + ) = _resolve_grpc_config( + client_options, + attempt_direct_path, + attempt_direct_path_xds_over_interconnect, + ) + _validate_universe_domain(self._universe_domain, credentials) - transport = transport_cls(credentials=credentials, channel=channel) + transport_cls = storage_v2.StorageClient.get_transport_class("grpc") + channel_kwargs = { + "host": host, + "credentials": credentials, + "attempt_direct_path": attempt_direct_path, + } + if use_dp_interconnect: + channel_kwargs["attempt_direct_path_xds_over_interconnect"] = True + if quota_project_id is not None: + channel_kwargs["quota_project_id"] = quota_project_id + + channel = transport_cls.create_channel(**channel_kwargs) + transport = transport_cls(host=host, channel=channel) return storage_v2.StorageClient( - credentials=credentials, transport=transport, client_info=client_info, client_options=client_options, diff --git a/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py b/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py index 0b96a92eb055..dee6bde15893 100644 --- a/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py +++ b/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py @@ -24,9 +24,20 @@ from google.cloud.storage.asyncio import async_grpc_client +@pytest.fixture(autouse=True) +def _hermetic_env(monkeypatch): + """Keeps the runner's environment from changing host or DirectPath resolution.""" + monkeypatch.delenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", raising=False) + monkeypatch.delenv( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", raising=False + ) + + def _make_credentials(spec=None): if spec is None: - return mock.Mock(spec=auth_credentials.Credentials) + return mock.Mock( + spec=auth_credentials.Credentials, universe_domain="googleapis.com" + ) return mock.Mock(spec=spec) @@ -60,7 +71,9 @@ def test_constructor_default_options(self, mock_async_storage_client): options=expected_options, ) mock_channel = mock_transport_cls.create_channel.return_value - mock_transport_cls.assert_called_once_with(channel=mock_channel) + mock_transport_cls.assert_called_once_with( + host="storage.googleapis.com", channel=mock_channel + ) mock_transport = mock_transport_cls.return_value assert kwargs["transport"] is mock_transport assert kwargs["client_options"] is None @@ -147,7 +160,9 @@ def test_constructor_disables_directpath(self, mock_async_storage_client): options=expected_options, ) mock_channel = mock_transport_cls.create_channel.return_value - mock_transport_cls.assert_called_once_with(channel=mock_channel) + mock_transport_cls.assert_called_once_with( + host="storage.googleapis.com", channel=mock_channel + ) @mock.patch("google.cloud._storage_v2.StorageAsyncClient") def test_metrics_properties(self, mock_async_storage_client): @@ -291,7 +306,9 @@ def test_grpc_client_property(self, mock_grpc_gapic_client): credentials=mock_creds, options=expected_options, ) - mock_transport_cls.assert_called_once_with(channel=channel_sentinel) + mock_transport_cls.assert_called_once_with( + host="storage.googleapis.com", channel=channel_sentinel + ) mock_grpc_gapic_client.assert_called_once_with( transport=mock_transport_instance, client_info=client_info, @@ -648,3 +665,150 @@ def test_async_grpc_client_interconnect_env_override( ): with pytest.raises(ValueError): async_grpc_client.AsyncGrpcClient(credentials=mock_creds) + + @pytest.mark.parametrize( + "env,opts,expected_host", + [ + ( + {"GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT": "true"}, + {"universe_domain": "example.com"}, + "storage.example.com", + ), + ( + {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}, + None, + "storage.example.com", + ), + ( + {}, + client_options.ClientOptions( + universe_domain="example.com", api_endpoint="custom.example.com:443" + ), + "custom.example.com:443", + ), + ], + ) + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") + def test_universe_domain_routing_and_direct_path_disabled( + self, mock_async_storage_client, env, opts, expected_host + ): + mock_transport_cls = mock.MagicMock() + mock_async_storage_client.get_transport_class.return_value = mock_transport_cls + mock_creds = _make_credentials() + mock_creds.universe_domain = "example.com" + + with mock.patch.dict("os.environ", env): + client = async_grpc_client.AsyncGrpcClient( + credentials=mock_creds, + client_options=opts, + attempt_direct_path=True, + attempt_direct_path_xds_over_interconnect=True, + ) + + assert client.universe_domain == "example.com" + primary_ua = mock_async_storage_client.call_args.kwargs[ + "client_info" + ].to_user_agent() + mock_transport_cls.create_channel.assert_called_once_with( + host=expected_host, + quota_project_id=None, + attempt_direct_path=False, + credentials=mock_creds, + options=(("grpc.primary_user_agent", primary_ua),), + ) + mock_transport_cls.assert_called_once_with( + host=expected_host, + channel=mock_transport_cls.create_channel.return_value, + ) + + @mock.patch("google.auth.default") + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") + def test_universe_domain_validation_errors( + self, mock_async_storage_client, mock_auth_default + ): + mock_transport_cls = mock.MagicMock() + mock_async_storage_client.get_transport_class.return_value = mock_transport_cls + other_creds = _make_credentials() + other_creds.universe_domain = "example.com" + mock_auth_default.return_value = (other_creds, "test-project") + + with pytest.raises(ValueError, match="example.com"): + async_grpc_client.AsyncGrpcClient(credentials=other_creds) + with pytest.raises(ValueError, match="example.com"): + async_grpc_client.AsyncGrpcClient() + with pytest.raises( + ValueError, match="Universe Domain cannot be an empty string" + ): + async_grpc_client.AsyncGrpcClient( + credentials=other_creds, + client_options=client_options.ClientOptions(universe_domain=""), + ) + mock_auth_default.assert_called_once_with( + default_scopes=mock_transport_cls.AUTH_SCOPES + ) + + # Credentials that expose no usable universe domain must not be accepted + # in a non-default universe. + missing_universe_creds = _make_credentials() + del missing_universe_creds.universe_domain + none_universe_creds = _make_credentials() + none_universe_creds.universe_domain = None + for creds in (missing_universe_creds, none_universe_creds): + with pytest.raises(ValueError, match="does not match"): + async_grpc_client.AsyncGrpcClient( + credentials=creds, + client_options={"universe_domain": "example.com"}, + ) + mock_transport_cls.create_channel.assert_not_called() + + @pytest.mark.parametrize( + "env,universe_opt,expected", + [ + ({}, "example.com", "example.com"), + ({"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}, None, "example.com"), + ({}, None, "googleapis.com"), + ], + ) + @mock.patch("google.cloud._storage_v2.StorageAsyncClient") + @mock.patch( + "google.cloud.storage.asyncio.async_grpc_client.grpc.aio.insecure_channel" + ) + def test_anonymous_credentials_universe_domain( + self, + mock_insecure_channel, + mock_async_storage_client, + env, + universe_opt, + expected, + ): + with mock.patch.dict("os.environ", env): + client = async_grpc_client.AsyncGrpcClient( + client_options=client_options.ClientOptions( + api_endpoint="localhost:8888", universe_domain=universe_opt + ), + credentials=AnonymousCredentials(), + ) + + assert client.universe_domain == expected + mock_insecure_channel.assert_called_once_with("localhost:8888") + + @mock.patch("google.api_core.grpc_helpers_async.create_channel") + def test_real_gapic_client_integration_non_default_universe( + self, mock_create_channel + ): + import grpc.aio + + mock_channel = mock.MagicMock(spec=grpc.aio.Channel) + mock_channel._unary_unary_interceptors = [] + mock_create_channel.return_value = mock_channel + mock_creds = _make_credentials() + mock_creds.universe_domain = "example.com" + client = async_grpc_client.AsyncGrpcClient( + credentials=mock_creds, + client_options=client_options.ClientOptions(universe_domain="example.com"), + ) + assert client.universe_domain == "example.com" + assert client.grpc_client.universe_domain == "example.com" + assert client.grpc_client.api_endpoint == "storage.example.com:443" + assert mock_create_channel.call_args.args[0] == "storage.example.com" + assert mock_create_channel.call_args.kwargs["attempt_direct_path"] is False diff --git a/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py b/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py index 0d0dfcd57a99..b41428728249 100644 --- a/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py +++ b/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py @@ -89,7 +89,7 @@ def mock_client(): """Create an AsyncGrpcClient with a mocked underlying GAPIC client.""" with mock.patch("google.cloud._storage_v2.StorageAsyncClient"): client = async_grpc_client.AsyncGrpcClient( - credentials=mock.Mock(), + credentials=mock.Mock(universe_domain="googleapis.com"), ) client._grpc_client = mock.AsyncMock() # Pre-populate ACO bucket metadata cache for Rapid Storage bucket verification @@ -961,7 +961,7 @@ async def test_async_grpc_client_tracing_coexists_with_metrics_and_interconnect_ ), ): client = async_grpc_client.AsyncGrpcClient( - credentials=mock.Mock(), + credentials=mock.Mock(universe_domain="googleapis.com"), attempt_direct_path_xds_over_interconnect=True, enable_metrics=True, enable_debug_metrics=True, diff --git a/packages/google-cloud-storage/tests/unit/test_grpc_client.py b/packages/google-cloud-storage/tests/unit/test_grpc_client.py index 5e3789549696..374bfdc130c4 100644 --- a/packages/google-cloud-storage/tests/unit/test_grpc_client.py +++ b/packages/google-cloud-storage/tests/unit/test_grpc_client.py @@ -15,15 +15,27 @@ import unittest from unittest import mock +import pytest from google.api_core import client_options as client_options_lib from google.auth import credentials as auth_credentials from google.cloud.storage import grpc_client +@pytest.fixture(autouse=True) +def _hermetic_env(monkeypatch): + """Keeps the runner's environment from changing host or DirectPath resolution.""" + monkeypatch.delenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", raising=False) + monkeypatch.delenv( + "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT", raising=False + ) + + def _make_credentials(spec=None): if spec is None: - return mock.Mock(spec=auth_credentials.Credentials) + return mock.Mock( + spec=auth_credentials.Credentials, universe_domain="googleapis.com" + ) return mock.Mock(spec=spec) @@ -54,16 +66,17 @@ def test_constructor_defaults_and_options( project="test-project", credentials=mock_creds ) - # 2. Assert DirectPath is ON by default. + # 2. Assert DirectPath is ON by default and endpoint/credentials are forwarded. mock_storage_client.get_transport_class.assert_called_once_with("grpc") mock_transport_cls.create_channel.assert_called_once_with( - attempt_direct_path=True + host="test.endpoint", + credentials=mock_creds, + attempt_direct_path=True, ) # 3. Assert the GAPIC client was created with the correct options. mock_transport = mock_transport_cls.return_value mock_storage_client.assert_called_once_with( - credentials=mock_creds, transport=mock_transport, client_info=mock_client_info, client_options=client_options_dict, @@ -90,7 +103,9 @@ def test_constructor_disables_direct_path( ) mock_transport_cls.create_channel.assert_called_once_with( - attempt_direct_path=False + host="storage.googleapis.com", + credentials=mock_creds, + attempt_direct_path=False, ) @mock.patch("google.cloud.storage.grpc_client.ClientWithProject") @@ -115,7 +130,6 @@ def test_constructor_initialize_with_api_key( # that contains the api_key. mock_transport = mock_transport_cls.return_value mock_storage_client.assert_called_once_with( - credentials=mock_creds, transport=mock_transport, client_info=None, client_options={"api_key": "test-api-key"}, @@ -159,7 +173,6 @@ def test_constructor_with_api_key_and_client_options( ) mock_storage_client.assert_called_once_with( - credentials=mock_creds, transport=mock_transport, client_info=None, client_options=client_options_obj, @@ -192,7 +205,6 @@ def test_constructor_with_api_key_and_dict_options( "api_key": "new-test-key", } mock_storage_client.assert_called_once_with( - credentials=mock_creds, transport=mock_transport, client_info=None, client_options=expected_options, @@ -314,7 +326,9 @@ def test_grpc_client_attempt_direct_path_xds_over_interconnect_env_override( ) mock_transport_cls.create_channel.assert_called_once_with( - attempt_direct_path=True + host="storage.googleapis.com", + credentials=mock_creds, + attempt_direct_path=True, ) with mock.patch.dict( @@ -327,6 +341,130 @@ def test_grpc_client_attempt_direct_path_xds_over_interconnect_env_override( credentials=mock_creds, ) + @mock.patch("google.cloud._storage_v2.StorageClient") + def test_universe_domain_routing_and_direct_path_disabled( + self, mock_storage_client + ): + other_creds = _make_credentials() + other_creds.universe_domain = "example.com" + anon_creds = auth_credentials.AnonymousCredentials() + cases = [ + ( + {"GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT": "true"}, + other_creds, + {"universe_domain": "example.com", "quota_project_id": "qp"}, + "storage.example.com", + {"quota_project_id": "qp"}, + ), + ( + {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "example.com"}, + other_creds, + None, + "storage.example.com", + {}, + ), + ( + {}, + other_creds, + client_options_lib.ClientOptions( + universe_domain="example.com", api_endpoint="custom.example.com:443" + ), + "custom.example.com:443", + {}, + ), + ( + {}, + anon_creds, + {"universe_domain": "example.com", "api_endpoint": "localhost:8888"}, + "localhost:8888", + {}, + ), + ] + for env, creds, opts, expected_host, extra_kw in cases: + mock_transport_cls = mock.MagicMock() + mock_storage_client.get_transport_class.return_value = mock_transport_cls + with ( + self.subTest(expected_host=expected_host, opts=opts), + mock.patch.dict("os.environ", env, clear=False), + ): + client = grpc_client.GrpcClient( + project="test-project", + credentials=creds, + client_options=opts, + attempt_direct_path=True, + attempt_direct_path_xds_over_interconnect=True, + ) + self.assertEqual(client.universe_domain, "example.com") + mock_transport_cls.create_channel.assert_called_once_with( + host=expected_host, + credentials=creds, + attempt_direct_path=False, + **extra_kw, + ) + mock_transport_cls.assert_called_once_with( + host=expected_host, + channel=mock_transport_cls.create_channel.return_value, + ) + + @mock.patch("google.auth.default") + @mock.patch("google.cloud._storage_v2.StorageClient") + def test_universe_domain_validation_errors( + self, mock_storage_client, mock_auth_default + ): + mock_transport_cls = mock.MagicMock() + mock_storage_client.get_transport_class.return_value = mock_transport_cls + other_creds = _make_credentials() + other_creds.universe_domain = "example.com" + mock_auth_default.return_value = (other_creds, "test-project") + + with self.assertRaisesRegex(ValueError, "example.com"): + grpc_client.GrpcClient(project="test-project", credentials=other_creds) + with self.assertRaisesRegex(ValueError, "example.com"): + grpc_client.GrpcClient(project="test-project") + with self.assertRaisesRegex( + ValueError, "Universe Domain cannot be an empty string" + ): + grpc_client.GrpcClient( + project="test-project", + credentials=other_creds, + client_options={"universe_domain": ""}, + ) + + # Credentials that expose no usable universe domain must not be accepted + # in a non-default universe. + missing_universe_creds = _make_credentials() + del missing_universe_creds.universe_domain + none_universe_creds = _make_credentials() + none_universe_creds.universe_domain = None + for creds in (missing_universe_creds, none_universe_creds): + with self.assertRaisesRegex(ValueError, "does not match"): + grpc_client.GrpcClient( + project="test-project", + credentials=creds, + client_options={"universe_domain": "example.com"}, + ) + mock_transport_cls.create_channel.assert_not_called() + + @mock.patch("google.api_core.grpc_helpers.create_channel") + def test_real_gapic_client_integration_non_default_universe( + self, mock_create_channel + ): + import grpc + + mock_create_channel.return_value = mock.MagicMock(spec=grpc.Channel) + mock_creds = _make_credentials() + mock_creds.universe_domain = "example.com" + client = grpc_client.GrpcClient( + project="test-project", + credentials=mock_creds, + client_options={"universe_domain": "example.com"}, + ) + self.assertEqual(client.universe_domain, "example.com") + self.assertEqual(client.grpc_client.universe_domain, "example.com") + self.assertEqual(client.grpc_client.api_endpoint, "storage.example.com:443") + self.assertEqual(mock_create_channel.call_args.args[0], "storage.example.com") + self.assertFalse(mock_create_channel.call_args.kwargs["attempt_direct_path"]) + def test_rewrite_host_for_interconnect_delimiters(self): cases = [ ("storage.googleapis.com", "storage-direct.googleapis.com"), From 41708cc3df791c82b65f8a42380b5bf0af1b9f46 Mon Sep 17 00:00:00 2001 From: Daniel Sanche Date: Fri, 20 Nov 2026 17:57:31 -0700 Subject: [PATCH 2/2] removed universe validation --- .../storage/asyncio/async_grpc_client.py | 8 +---- .../google/cloud/storage/grpc_client.py | 22 ++---------- .../unit/asyncio/test_async_grpc_client.py | 36 +++---------------- .../unit/asyncio/test_rapid_observability.py | 4 +-- .../tests/unit/test_grpc_client.py | 32 ++--------------- 5 files changed, 12 insertions(+), 90 deletions(-) diff --git a/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py b/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py index 0e11993482b7..12a3d8ff722f 100644 --- a/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py +++ b/packages/google-cloud-storage/google/cloud/storage/asyncio/async_grpc_client.py @@ -14,7 +14,6 @@ """An async client for interacting with Google Cloud Storage using the gRPC API.""" -import google.auth import grpc from google.auth import credentials as auth_credentials @@ -29,7 +28,6 @@ from google.cloud.storage.grpc_client import ( _resolve_grpc_config, _resolve_universe_domain, - _validate_universe_domain, ) @@ -134,6 +132,7 @@ def __init__( if agent_version not in client_info.user_agent: client_info.user_agent += f" {agent_version} " + self._universe_domain = _resolve_universe_domain(client_options) self._grpc_client = self._create_async_grpc_client( credentials=credentials, client_info=client_info, @@ -194,11 +193,6 @@ def _create_async_grpc_client( transport_cls = storage_v2.StorageAsyncClient.get_transport_class( "grpc_asyncio" ) - if credentials is None: - credentials, _ = google.auth.default( - default_scopes=transport_cls.AUTH_SCOPES - ) - _validate_universe_domain(self._universe_domain, credentials) primary_user_agent = client_info.to_user_agent() channel_kwargs = { diff --git a/packages/google-cloud-storage/google/cloud/storage/grpc_client.py b/packages/google-cloud-storage/google/cloud/storage/grpc_client.py index 4b2bb02459ac..d5c0e302415b 100644 --- a/packages/google-cloud-storage/google/cloud/storage/grpc_client.py +++ b/packages/google-cloud-storage/google/cloud/storage/grpc_client.py @@ -17,7 +17,6 @@ import os from google.api_core import universe -from google.auth import credentials as auth_credentials from google.cloud.client import ClientWithProject from google.cloud import _storage_v2 as storage_v2 @@ -112,18 +111,6 @@ def _resolve_grpc_config( ) -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) - - class GrpcClient(ClientWithProject): """A client for interacting with Google Cloud Storage using the gRPC API. @@ -215,13 +202,9 @@ def __init__( elif api_key: client_options.api_key = api_key - effective_credentials = ( - credentials - if credentials is not None - else getattr(self, "_credentials", None) - ) + self._universe_domain = _resolve_universe_domain(client_options) self._grpc_client = self._create_gapic_client( - credentials=effective_credentials, + credentials=credentials, client_info=client_info, client_options=client_options, attempt_direct_path=attempt_direct_path, @@ -263,7 +246,6 @@ def _create_gapic_client( attempt_direct_path, attempt_direct_path_xds_over_interconnect, ) - _validate_universe_domain(self._universe_domain, credentials) transport_cls = storage_v2.StorageClient.get_transport_class("grpc") channel_kwargs = { diff --git a/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py b/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py index dee6bde15893..242c26243af7 100644 --- a/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py +++ b/packages/google-cloud-storage/tests/unit/asyncio/test_async_grpc_client.py @@ -35,9 +35,7 @@ def _hermetic_env(monkeypatch): def _make_credentials(spec=None): if spec is None: - return mock.Mock( - spec=auth_credentials.Credentials, universe_domain="googleapis.com" - ) + return mock.Mock(spec=auth_credentials.Credentials) return mock.Mock(spec=spec) @@ -721,44 +719,18 @@ def test_universe_domain_routing_and_direct_path_disabled( channel=mock_transport_cls.create_channel.return_value, ) - @mock.patch("google.auth.default") @mock.patch("google.cloud._storage_v2.StorageAsyncClient") - def test_universe_domain_validation_errors( - self, mock_async_storage_client, mock_auth_default - ): + def test_empty_universe_domain_error(self, mock_async_storage_client): mock_transport_cls = mock.MagicMock() mock_async_storage_client.get_transport_class.return_value = mock_transport_cls - other_creds = _make_credentials() - other_creds.universe_domain = "example.com" - mock_auth_default.return_value = (other_creds, "test-project") - - with pytest.raises(ValueError, match="example.com"): - async_grpc_client.AsyncGrpcClient(credentials=other_creds) - with pytest.raises(ValueError, match="example.com"): - async_grpc_client.AsyncGrpcClient() + with pytest.raises( ValueError, match="Universe Domain cannot be an empty string" ): async_grpc_client.AsyncGrpcClient( - credentials=other_creds, + credentials=_make_credentials(), client_options=client_options.ClientOptions(universe_domain=""), ) - mock_auth_default.assert_called_once_with( - default_scopes=mock_transport_cls.AUTH_SCOPES - ) - - # Credentials that expose no usable universe domain must not be accepted - # in a non-default universe. - missing_universe_creds = _make_credentials() - del missing_universe_creds.universe_domain - none_universe_creds = _make_credentials() - none_universe_creds.universe_domain = None - for creds in (missing_universe_creds, none_universe_creds): - with pytest.raises(ValueError, match="does not match"): - async_grpc_client.AsyncGrpcClient( - credentials=creds, - client_options={"universe_domain": "example.com"}, - ) mock_transport_cls.create_channel.assert_not_called() @pytest.mark.parametrize( diff --git a/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py b/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py index b41428728249..0d0dfcd57a99 100644 --- a/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py +++ b/packages/google-cloud-storage/tests/unit/asyncio/test_rapid_observability.py @@ -89,7 +89,7 @@ def mock_client(): """Create an AsyncGrpcClient with a mocked underlying GAPIC client.""" with mock.patch("google.cloud._storage_v2.StorageAsyncClient"): client = async_grpc_client.AsyncGrpcClient( - credentials=mock.Mock(universe_domain="googleapis.com"), + credentials=mock.Mock(), ) client._grpc_client = mock.AsyncMock() # Pre-populate ACO bucket metadata cache for Rapid Storage bucket verification @@ -961,7 +961,7 @@ async def test_async_grpc_client_tracing_coexists_with_metrics_and_interconnect_ ), ): client = async_grpc_client.AsyncGrpcClient( - credentials=mock.Mock(universe_domain="googleapis.com"), + credentials=mock.Mock(), attempt_direct_path_xds_over_interconnect=True, enable_metrics=True, enable_debug_metrics=True, diff --git a/packages/google-cloud-storage/tests/unit/test_grpc_client.py b/packages/google-cloud-storage/tests/unit/test_grpc_client.py index 374bfdc130c4..bb8203df7810 100644 --- a/packages/google-cloud-storage/tests/unit/test_grpc_client.py +++ b/packages/google-cloud-storage/tests/unit/test_grpc_client.py @@ -33,9 +33,7 @@ def _hermetic_env(monkeypatch): def _make_credentials(spec=None): if spec is None: - return mock.Mock( - spec=auth_credentials.Credentials, universe_domain="googleapis.com" - ) + return mock.Mock(spec=auth_credentials.Credentials) return mock.Mock(spec=spec) @@ -406,43 +404,19 @@ def test_universe_domain_routing_and_direct_path_disabled( channel=mock_transport_cls.create_channel.return_value, ) - @mock.patch("google.auth.default") @mock.patch("google.cloud._storage_v2.StorageClient") - def test_universe_domain_validation_errors( - self, mock_storage_client, mock_auth_default - ): + def test_empty_universe_domain_error(self, mock_storage_client): mock_transport_cls = mock.MagicMock() mock_storage_client.get_transport_class.return_value = mock_transport_cls - other_creds = _make_credentials() - other_creds.universe_domain = "example.com" - mock_auth_default.return_value = (other_creds, "test-project") - with self.assertRaisesRegex(ValueError, "example.com"): - grpc_client.GrpcClient(project="test-project", credentials=other_creds) - with self.assertRaisesRegex(ValueError, "example.com"): - grpc_client.GrpcClient(project="test-project") with self.assertRaisesRegex( ValueError, "Universe Domain cannot be an empty string" ): grpc_client.GrpcClient( project="test-project", - credentials=other_creds, + credentials=_make_credentials(), client_options={"universe_domain": ""}, ) - - # Credentials that expose no usable universe domain must not be accepted - # in a non-default universe. - missing_universe_creds = _make_credentials() - del missing_universe_creds.universe_domain - none_universe_creds = _make_credentials() - none_universe_creds.universe_domain = None - for creds in (missing_universe_creds, none_universe_creds): - with self.assertRaisesRegex(ValueError, "does not match"): - grpc_client.GrpcClient( - project="test-project", - credentials=creds, - client_options={"universe_domain": "example.com"}, - ) mock_transport_cls.create_channel.assert_not_called() @mock.patch("google.api_core.grpc_helpers.create_channel")