diff --git a/google/cloud/pubsub_v1/publisher/client.py b/google/cloud/pubsub_v1/publisher/client.py index 3bd21ae23..f1de9f1f4 100644 --- a/google/cloud/pubsub_v1/publisher/client.py +++ b/google/cloud/pubsub_v1/publisher/client.py @@ -130,19 +130,19 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs): target=os.environ.get("PUBSUB_EMULATOR_HOST") ) - # The auto-generated layer's client has mTLS logic to determine the api - # endpoint and the ssl credentials to use. Here we create a client - # and extract the api endpoint and ssl credentials. The api endpoint - # will be used to set `self._target`, and ssl credentials will be - # passed to `grpc_helpers.create_channel` to establish a mTLS channel - # (if ssl credentials is not None). + # The GAPIC client has mTLS logic to determine the api endpoint and the + # ssl credentials to use. Here we create a GAPIC client to help compute the + # api endpoint and ssl credentials. The api endpoint will be used to set + # `self._target`, and ssl credentials will be passed to + # `grpc_helpers.create_channel` to establish a mTLS channel (if ssl + # credentials is not None). client_options = kwargs.get("client_options", None) credentials = kwargs.get("credentials", None) - publisher_client_instance = publisher_client.PublisherClient( + client_for_mtls_info = publisher_client.PublisherClient( credentials=credentials, client_options=client_options ) - self._target = publisher_client_instance._transport._host + self._target = client_for_mtls_info._transport._host # Use a custom channel. # We need this in order to set appropriate default message size and @@ -153,7 +153,7 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs): channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, - ssl_credentials=publisher_client_instance._transport._ssl_channel_credentials, + ssl_credentials=client_for_mtls_info._transport._ssl_channel_credentials, scopes=publisher_client.PublisherClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, diff --git a/google/cloud/pubsub_v1/subscriber/client.py b/google/cloud/pubsub_v1/subscriber/client.py index f0bf43d9b..e33a0e2e6 100644 --- a/google/cloud/pubsub_v1/subscriber/client.py +++ b/google/cloud/pubsub_v1/subscriber/client.py @@ -81,19 +81,19 @@ def __init__(self, **kwargs): target=os.environ.get("PUBSUB_EMULATOR_HOST") ) - # The auto-generated layer's client has mTLS logic to determine the api - # endpoint and the ssl credentials to use. Here we create a client - # and extract the api endpoint and ssl credentials. The api endpoint - # will be used to set `self._target`, and ssl credentials will be - # passed to `grpc_helpers.create_channel` to establish a mTLS channel - # (if ssl credentials is not None). + # The GAPIC client has mTLS logic to determine the api endpoint and the + # ssl credentials to use. Here we create a GAPIC client to help compute the + # api endpoint and ssl credentials. The api endpoint will be used to set + # `self._target`, and ssl credentials will be passed to + # `grpc_helpers.create_channel` to establish a mTLS channel (if ssl + # credentials is not None). client_options = kwargs.get("client_options", None) credentials = kwargs.get("credentials", None) - subscriber_client_instance = subscriber_client.SubscriberClient( + client_for_mtls_info = subscriber_client.SubscriberClient( credentials=credentials, client_options=client_options ) - self._target = subscriber_client_instance._transport._host + self._target = client_for_mtls_info._transport._host # Use a custom channel. # We need this in order to set appropriate default message size and @@ -104,7 +104,7 @@ def __init__(self, **kwargs): channel = grpc_helpers.create_channel( credentials=kwargs.pop("credentials", None), target=self.target, - ssl_credentials=subscriber_client_instance._transport._ssl_channel_credentials, + ssl_credentials=client_for_mtls_info._transport._ssl_channel_credentials, scopes=subscriber_client.SubscriberClient._DEFAULT_SCOPES, options={ "grpc.max_send_message_length": -1, diff --git a/tests/unit/pubsub_v1/publisher/test_publisher_client.py b/tests/unit/pubsub_v1/publisher/test_publisher_client.py index 2e0a761ba..0f661c2fa 100644 --- a/tests/unit/pubsub_v1/publisher/test_publisher_client.py +++ b/tests/unit/pubsub_v1/publisher/test_publisher_client.py @@ -18,6 +18,7 @@ import inspect from google.auth import credentials +import grpc import mock import pytest @@ -104,11 +105,13 @@ def test_init_w_empty_client_options(): def test_init_client_options_pass_through(): + mock_ssl_creds = grpc.ssl_channel_credentials() + def init(self, *args, **kwargs): self.kwargs = kwargs self._transport = mock.Mock() self._transport._host = "testendpoint.google.com" - self._transport._ssl_channel_credentials = None + self._transport._ssl_channel_credentials = mock_ssl_creds with mock.patch.object(publisher_client.PublisherClient, "__init__", init): client = publisher.Client( @@ -122,6 +125,8 @@ def init(self, *args, **kwargs): assert client_options.get("quota_project_id") == "42" assert client_options.get("scopes") == [] assert client_options.get("credentials_file") == "file.json" + assert client.target == "testendpoint.google.com" + assert client.api.transport._ssl_channel_credentials == mock_ssl_creds def test_init_emulator(monkeypatch): diff --git a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py index 167328c41..d56289276 100644 --- a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py +++ b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py @@ -13,6 +13,7 @@ # limitations under the License. from google.auth import credentials +import grpc import mock from google.cloud.pubsub_v1 import subscriber @@ -65,11 +66,13 @@ def test_init_w_empty_client_options(): def test_init_client_options_pass_through(): + mock_ssl_creds = grpc.ssl_channel_credentials() + def init(self, *args, **kwargs): self.kwargs = kwargs self._transport = mock.Mock() self._transport._host = "testendpoint.google.com" - self._transport._ssl_channel_credentials = None + self._transport._ssl_channel_credentials = mock_ssl_creds with mock.patch.object(subscriber_client.SubscriberClient, "__init__", init): client = subscriber.Client( @@ -83,6 +86,8 @@ def init(self, *args, **kwargs): assert client_options.get("quota_project_id") == "42" assert client_options.get("scopes") == [] assert client_options.get("credentials_file") == "file.json" + assert client.target == "testendpoint.google.com" + assert client.api.transport._ssl_channel_credentials == mock_ssl_creds def test_init_emulator(monkeypatch):