Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 451ac23

Browse files
committed
chore: cleanup deprecated code for mtls endpoint
1 parent b5098fb commit 451ac23

36 files changed

Lines changed: 0 additions & 1898 deletions

File tree

gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -118,40 +118,6 @@ class {{ service.async_client_name }}:
118118

119119
from_service_account_json = from_service_account_file
120120

121-
@classmethod
122-
def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[ClientOptions] = None):
123-
"""Return the API endpoint and client cert source for mutual TLS.
124-
125-
The client cert source is determined in the following order:
126-
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
127-
client cert source is None.
128-
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
129-
default client cert source exists, use the default one; otherwise the client cert
130-
source is None.
131-
132-
The API endpoint is determined in the following order:
133-
(1) if `client_options.api_endpoint` if provided, use the provided one.
134-
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
135-
default mTLS endpoint; if the environment variable is "never", use the default API
136-
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
137-
use the default API endpoint.
138-
139-
More details can be found at https://google.aip.dev/auth/4114.
140-
141-
Args:
142-
client_options (google.api_core.client_options.ClientOptions): Custom options for the
143-
client. Only the `api_endpoint` and `client_cert_source` properties may be used
144-
in this method.
145-
146-
Returns:
147-
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
148-
client cert source to use.
149-
150-
Raises:
151-
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
152-
"""
153-
return {{ service.client_name }}.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
154-
155121
@property
156122
def transport(self) -> {{ service.name }}Transport:
157123
"""Returns the transport used by the client instance.

gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -260,68 +260,6 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
260260

261261
{% endfor %}{# common resources #}
262262

263-
@classmethod
264-
def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_options_lib.ClientOptions] = None):
265-
"""Deprecated. Return the API endpoint and client cert source for mutual TLS.
266-
267-
The client cert source is determined in the following order:
268-
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
269-
client cert source is None.
270-
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
271-
default client cert source exists, use the default one; otherwise the client cert
272-
source is None.
273-
274-
The API endpoint is determined in the following order:
275-
(1) if `client_options.api_endpoint` if provided, use the provided one.
276-
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
277-
default mTLS endpoint; if the environment variable is "never", use the default API
278-
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
279-
use the default API endpoint.
280-
281-
More details can be found at https://google.aip.dev/auth/4114.
282-
283-
Args:
284-
client_options (google.api_core.client_options.ClientOptions): Custom options for the
285-
client. Only the `api_endpoint` and `client_cert_source` properties may be used
286-
in this method.
287-
288-
Returns:
289-
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
290-
client cert source to use.
291-
292-
Raises:
293-
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
294-
"""
295-
296-
warnings.warn("get_mtls_endpoint_and_cert_source is deprecated. Use the api_endpoint property instead.",
297-
DeprecationWarning)
298-
if client_options is None:
299-
client_options = client_options_lib.ClientOptions()
300-
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
301-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
302-
if use_client_cert not in ("true", "false"):
303-
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
304-
if use_mtls_endpoint not in ("auto", "never", "always"):
305-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
306-
307-
# Figure out the client cert source to use.
308-
client_cert_source = None
309-
if use_client_cert == "true":
310-
if client_options.client_cert_source:
311-
client_cert_source = client_options.client_cert_source
312-
elif mtls.has_default_client_cert_source():
313-
client_cert_source = mtls.default_client_cert_source()
314-
315-
# Figure out which api endpoint to use.
316-
if client_options.api_endpoint is not None:
317-
api_endpoint = client_options.api_endpoint
318-
elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source):
319-
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
320-
else:
321-
api_endpoint = cls.DEFAULT_ENDPOINT
322-
323-
return api_endpoint, client_cert_source
324-
325263
@staticmethod
326264
def _read_environment_variables():
327265
"""Returns the environment variables used by the client.

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -639,71 +639,7 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp
639639
{{ service.client_name }}
640640
{% endif %}
641641
])
642-
@mock.patch.object({{ service.client_name }}, "DEFAULT_ENDPOINT", modify_default_endpoint({{ service.client_name }}))
643-
{% if 'grpc' in opts.transport %}
644-
@mock.patch.object({{ service.async_client_name }}, "DEFAULT_ENDPOINT", modify_default_endpoint({{ service.async_client_name }}))
645-
{% endif %}
646-
def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(client_class):
647-
mock_client_cert_source = mock.Mock()
648-
649-
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
650-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
651-
mock_api_endpoint = "foo"
652-
options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
653-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
654-
assert api_endpoint == mock_api_endpoint
655-
assert cert_source == mock_client_cert_source
656-
657-
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
658-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
659-
mock_client_cert_source = mock.Mock()
660-
mock_api_endpoint = "foo"
661-
options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
662-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
663-
assert api_endpoint == mock_api_endpoint
664-
assert cert_source is None
665-
666-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
667-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
668-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
669-
assert api_endpoint == client_class.DEFAULT_ENDPOINT
670-
assert cert_source is None
671-
672-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
673-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
674-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
675-
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
676-
assert cert_source is None
677-
678-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
679-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
680-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
681-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
682-
assert api_endpoint == client_class.DEFAULT_ENDPOINT
683-
assert cert_source is None
684-
685-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
686-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
687-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
688-
with mock.patch('google.auth.transport.mtls.default_client_cert_source', return_value=mock_client_cert_source):
689-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
690-
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
691-
assert cert_source == mock_client_cert_source
692-
693-
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
694-
# unsupported value.
695-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
696-
with pytest.raises(MutualTLSChannelError) as excinfo:
697-
client_class.get_mtls_endpoint_and_cert_source()
698-
699-
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
700-
701-
# Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value.
702-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
703-
with pytest.raises(ValueError) as excinfo:
704-
client_class.get_mtls_endpoint_and_cert_source()
705642

706-
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
707643

708644
@pytest.mark.parametrize("client_class", [
709645
{% if 'grpc' in opts.transport %}

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -126,40 +126,6 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
126126

127127
from_service_account_json = from_service_account_file
128128

129-
@classmethod
130-
def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[ClientOptions] = None):
131-
"""Return the API endpoint and client cert source for mutual TLS.
132-
133-
The client cert source is determined in the following order:
134-
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
135-
client cert source is None.
136-
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
137-
default client cert source exists, use the default one; otherwise the client cert
138-
source is None.
139-
140-
The API endpoint is determined in the following order:
141-
(1) if `client_options.api_endpoint` if provided, use the provided one.
142-
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
143-
default mTLS endpoint; if the environment variable is "never", use the default API
144-
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
145-
use the default API endpoint.
146-
147-
More details can be found at https://google.aip.dev/auth/4114.
148-
149-
Args:
150-
client_options (google.api_core.client_options.ClientOptions): Custom options for the
151-
client. Only the `api_endpoint` and `client_cert_source` properties may be used
152-
in this method.
153-
154-
Returns:
155-
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
156-
client cert source to use.
157-
158-
Raises:
159-
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
160-
"""
161-
return AssetServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
162-
163129
@property
164130
def transport(self) -> AssetServiceTransport:
165131
"""Returns the transport used by the client instance.

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/client.py

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -319,68 +319,6 @@ def parse_common_location_path(path: str) -> Dict[str,str]:
319319
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
320320
return m.groupdict() if m else {}
321321

322-
@classmethod
323-
def get_mtls_endpoint_and_cert_source(cls, client_options: Optional[client_options_lib.ClientOptions] = None):
324-
"""Deprecated. Return the API endpoint and client cert source for mutual TLS.
325-
326-
The client cert source is determined in the following order:
327-
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
328-
client cert source is None.
329-
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
330-
default client cert source exists, use the default one; otherwise the client cert
331-
source is None.
332-
333-
The API endpoint is determined in the following order:
334-
(1) if `client_options.api_endpoint` if provided, use the provided one.
335-
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
336-
default mTLS endpoint; if the environment variable is "never", use the default API
337-
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
338-
use the default API endpoint.
339-
340-
More details can be found at https://google.aip.dev/auth/4114.
341-
342-
Args:
343-
client_options (google.api_core.client_options.ClientOptions): Custom options for the
344-
client. Only the `api_endpoint` and `client_cert_source` properties may be used
345-
in this method.
346-
347-
Returns:
348-
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
349-
client cert source to use.
350-
351-
Raises:
352-
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
353-
"""
354-
355-
warnings.warn("get_mtls_endpoint_and_cert_source is deprecated. Use the api_endpoint property instead.",
356-
DeprecationWarning)
357-
if client_options is None:
358-
client_options = client_options_lib.ClientOptions()
359-
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
360-
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
361-
if use_client_cert not in ("true", "false"):
362-
raise ValueError("Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`")
363-
if use_mtls_endpoint not in ("auto", "never", "always"):
364-
raise MutualTLSChannelError("Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`")
365-
366-
# Figure out the client cert source to use.
367-
client_cert_source = None
368-
if use_client_cert == "true":
369-
if client_options.client_cert_source:
370-
client_cert_source = client_options.client_cert_source
371-
elif mtls.has_default_client_cert_source():
372-
client_cert_source = mtls.default_client_cert_source()
373-
374-
# Figure out which api endpoint to use.
375-
if client_options.api_endpoint is not None:
376-
api_endpoint = client_options.api_endpoint
377-
elif use_mtls_endpoint == "always" or (use_mtls_endpoint == "auto" and client_cert_source):
378-
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
379-
else:
380-
api_endpoint = cls.DEFAULT_ENDPOINT
381-
382-
return api_endpoint, client_cert_source
383-
384322
@staticmethod
385323
def _read_environment_variables():
386324
"""Returns the environment variables used by the client.

tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -526,69 +526,7 @@ def test_asset_service_client_mtls_env_auto(client_class, transport_class, trans
526526
@pytest.mark.parametrize("client_class", [
527527
AssetServiceClient, AssetServiceAsyncClient
528528
])
529-
@mock.patch.object(AssetServiceClient, "DEFAULT_ENDPOINT", modify_default_endpoint(AssetServiceClient))
530-
@mock.patch.object(AssetServiceAsyncClient, "DEFAULT_ENDPOINT", modify_default_endpoint(AssetServiceAsyncClient))
531-
def test_asset_service_client_get_mtls_endpoint_and_cert_source(client_class):
532-
mock_client_cert_source = mock.Mock()
533-
534-
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
535-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
536-
mock_api_endpoint = "foo"
537-
options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
538-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
539-
assert api_endpoint == mock_api_endpoint
540-
assert cert_source == mock_client_cert_source
541-
542-
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
543-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
544-
mock_client_cert_source = mock.Mock()
545-
mock_api_endpoint = "foo"
546-
options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
547-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
548-
assert api_endpoint == mock_api_endpoint
549-
assert cert_source is None
550-
551-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
552-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
553-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
554-
assert api_endpoint == client_class.DEFAULT_ENDPOINT
555-
assert cert_source is None
556-
557-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
558-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
559-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
560-
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
561-
assert cert_source is None
562-
563-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
564-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
565-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
566-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
567-
assert api_endpoint == client_class.DEFAULT_ENDPOINT
568-
assert cert_source is None
569-
570-
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
571-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
572-
with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
573-
with mock.patch('google.auth.transport.mtls.default_client_cert_source', return_value=mock_client_cert_source):
574-
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
575-
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
576-
assert cert_source == mock_client_cert_source
577-
578-
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
579-
# unsupported value.
580-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "Unsupported"}):
581-
with pytest.raises(MutualTLSChannelError) as excinfo:
582-
client_class.get_mtls_endpoint_and_cert_source()
583-
584-
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
585-
586-
# Check the case GOOGLE_API_USE_CLIENT_CERTIFICATE has unsupported value.
587-
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
588-
with pytest.raises(ValueError) as excinfo:
589-
client_class.get_mtls_endpoint_and_cert_source()
590529

591-
assert str(excinfo.value) == "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
592530

593531
@pytest.mark.parametrize("client_class", [
594532
AssetServiceClient, AssetServiceAsyncClient

0 commit comments

Comments
 (0)