Skip to content

Commit a53b7cf

Browse files
Revert "Add mTLS support to generator (#359)"
This reverts commit a354629.
1 parent a354629 commit a53b7cf

File tree

6 files changed

+79
-332
lines changed

6 files changed

+79
-332
lines changed

.circleci/config.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ jobs:
253253
ln -s /usr/src/protoc/bin/protoc /usr/local/bin/protoc
254254
- run:
255255
name: Run showcase tests.
256-
command: nox -s showcase_alternative_templates
256+
command: nox -s showcase_alternative_templates
257257
showcase-unit-3.6:
258258
docker:
259259
- image: python:3.6-slim
@@ -263,7 +263,7 @@ jobs:
263263
name: Install system dependencies.
264264
command: |
265265
apt-get update
266-
apt-get install -y curl pandoc unzip git
266+
apt-get install -y curl pandoc unzip
267267
- run:
268268
name: Install protoc 3.7.1.
269269
command: |
@@ -287,7 +287,7 @@ jobs:
287287
name: Install system dependencies.
288288
command: |
289289
apt-get update
290-
apt-get install -y curl pandoc unzip git
290+
apt-get install -y curl pandoc unzip
291291
- run:
292292
name: Install protoc 3.7.1.
293293
command: |
@@ -311,7 +311,7 @@ jobs:
311311
name: Install system dependencies.
312312
command: |
313313
apt-get update
314-
apt-get install -y curl pandoc unzip git
314+
apt-get install -y curl pandoc unzip
315315
- run:
316316
name: Install protoc 3.7.1.
317317
command: |
@@ -335,7 +335,7 @@ jobs:
335335
name: Install system dependencies.
336336
command: |
337337
apt-get update
338-
apt-get install -y curl pandoc unzip git
338+
apt-get install -y curl pandoc unzip
339339
- run:
340340
name: Install protoc 3.7.1.
341341
command: |
@@ -359,7 +359,7 @@ jobs:
359359
name: Install system dependencies.
360360
command: |
361361
apt-get update
362-
apt-get install -y curl pandoc unzip git
362+
apt-get install -y curl pandoc unzip
363363
- run:
364364
name: Install protoc 3.7.1.
365365
command: |
@@ -383,7 +383,7 @@ jobs:
383383
name: Install system dependencies.
384384
command: |
385385
apt-get update
386-
apt-get install -y curl pandoc unzip git
386+
apt-get install -y curl pandoc unzip
387387
- run:
388388
name: Install protoc 3.7.1.
389389
command: |

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

+4-71
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
{% block content %}
44
from collections import OrderedDict
5-
import re
6-
from typing import Callable, Dict, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
5+
from typing import Dict, {% if service.any_server_streaming %}Iterable, {% endif %}{% if service.any_client_streaming %}Iterator, {% endif %}Sequence, Tuple, Type, Union
76
import pkg_resources
87

98
import google.api_core.client_options as ClientOptions # type: ignore
@@ -58,40 +57,7 @@ class {{ service.client_name }}Meta(type):
5857
class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
5958
"""{{ service.meta.doc|rst(width=72, indent=4) }}"""
6059

61-
@staticmethod
62-
def _get_default_mtls_endpoint(api_endpoint):
63-
"""Convert api endpoint to mTLS endpoint.
64-
Convert "*.sandbox.googleapis.com" and "*.googleapis.com" to
65-
"*.mtls.sandbox.googleapis.com" and "*.mtls.googleapis.com" respectively.
66-
Args:
67-
api_endpoint (Optional[str]): the api endpoint to convert.
68-
Returns:
69-
str: converted mTLS api endpoint.
70-
"""
71-
if not api_endpoint:
72-
return api_endpoint
73-
74-
mtls_endpoint_re = re.compile(
75-
r"(?P<name>[^.]+)(?P<mtls>\.mtls)?(?P<sandbox>\.sandbox)?(?P<googledomain>\.googleapis\.com)?"
76-
)
77-
78-
m = mtls_endpoint_re.match(api_endpoint)
79-
name, mtls, sandbox, googledomain = m.groups()
80-
if mtls or not googledomain:
81-
return api_endpoint
82-
83-
if sandbox:
84-
return api_endpoint.replace(
85-
"sandbox.googleapis.com", "mtls.sandbox.googleapis.com"
86-
)
87-
88-
return api_endpoint.replace(".googleapis.com", ".mtls.googleapis.com")
89-
90-
DEFAULT_ENDPOINT = {% if service.host %}'{{ service.host }}'{% else %}None{% endif %}
91-
DEFAULT_MTLS_ENDPOINT = _get_default_mtls_endpoint.__func__( # type: ignore
92-
DEFAULT_ENDPOINT
93-
)
94-
DEFAULT_OPTIONS = ClientOptions.ClientOptions(api_endpoint=DEFAULT_ENDPOINT)
60+
DEFAULT_OPTIONS = ClientOptions.ClientOptions({% if service.host %}api_endpoint='{{ service.host }}'{% endif %})
9561

9662
@classmethod
9763
def from_service_account_file(cls, filename: str, *args, **kwargs):
@@ -140,56 +106,23 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
140106
transport to use. If set to None, a transport is chosen
141107
automatically.
142108
client_options (ClientOptions): Custom options for the client.
143-
(1) The ``api_endpoint`` property can be used to override the
144-
default endpoint provided by the client.
145-
(2) If ``transport`` argument is None, ``client_options`` can be
146-
used to create a mutual TLS transport. If ``api_endpoint`` is
147-
provided and different from the default endpoint, or the
148-
``client_cert_source`` property is provided, mutual TLS
149-
transport will be created if client SSL credentials are found.
150-
Client SSL credentials are obtained from ``client_cert_source``
151-
or application default SSL credentials.
152-
153-
Raises:
154-
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
155-
creation failed for any reason.
156109
"""
157110
if isinstance(client_options, dict):
158111
client_options = ClientOptions.from_dict(client_options)
159112

160-
# Set default api endpoint if not set.
161-
if client_options.api_endpoint is None:
162-
client_options.api_endpoint = self.DEFAULT_ENDPOINT
163-
164113
# Save or instantiate the transport.
165114
# Ordinarily, we provide the transport, but allowing a custom transport
166115
# instance provides an extensibility point for unusual situations.
167116
if isinstance(transport, {{ service.name }}Transport):
168-
# transport is a {{ service.name }}Transport instance.
169117
if credentials:
170118
raise ValueError('When providing a transport instance, '
171119
'provide its credentials directly.')
172120
self._transport = transport
173-
elif transport is not None or (
174-
client_options.api_endpoint == self.DEFAULT_ENDPOINT
175-
and client_options.client_cert_source is None
176-
):
177-
# Don't trigger mTLS.
121+
else:
178122
Transport = type(self).get_transport_class(transport)
179123
self._transport = Transport(
180-
credentials=credentials, host=client_options.api_endpoint
181-
)
182-
else:
183-
# Trigger mTLS. If the user overrides endpoint, use it as the mTLS
184-
# endpoint, otherwise use the default mTLS endpoint.
185-
option_endpoint = client_options.api_endpoint
186-
api_mtls_endpoint = self.DEFAULT_MTLS_ENDPOINT if option_endpoint == self.DEFAULT_ENDPOINT else option_endpoint
187-
188-
self._transport = {{ service.name }}GrpcTransport(
189124
credentials=credentials,
190-
host=client_options.api_endpoint,
191-
api_mtls_endpoint=api_mtls_endpoint,
192-
client_cert_source=client_options.client_cert_source,
125+
host=client_options.api_endpoint{% if service.host %} or '{{ service.host }}'{% endif %},
193126
)
194127

195128
{% for method in service.methods.values() -%}

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

+7-43
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
{% extends '_base.py.j2' %}
22

33
{% block content %}
4-
from typing import Callable, Dict, Tuple
4+
from typing import Callable, Dict
55

66
from google.api_core import grpc_helpers # type: ignore
77
{%- if service.has_lro %}
88
from google.api_core import operations_v1 # type: ignore
99
{%- endif %}
1010
from google.auth import credentials # type: ignore
11-
from google.auth.transport.grpc import SslCredentials # type: ignore
12-
1311

1412
import grpc # type: ignore
1513

@@ -37,9 +35,7 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
3735
def __init__(self, *,
3836
host: str{% if service.host %} = '{{ service.host }}'{% endif %},
3937
credentials: credentials.Credentials = None,
40-
channel: grpc.Channel = None,
41-
api_mtls_endpoint: str = None,
42-
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None) -> None:
38+
channel: grpc.Channel = None) -> None:
4339
"""Instantiate the transport.
4440

4541
Args:
@@ -53,51 +49,19 @@ class {{ service.name }}GrpcTransport({{ service.name }}Transport):
5349
This argument is ignored if ``channel`` is provided.
5450
channel (Optional[grpc.Channel]): A ``Channel`` instance through
5551
which to make calls.
56-
api_mtls_endpoint (Optional[str]): The mutual TLS endpoint. If
57-
provided, it overrides the ``host`` argument and tries to create
58-
a mutual TLS channel with client SSL credentials from
59-
``client_cert_source`` or applicatin default SSL credentials.
60-
client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): A
61-
callback to provide client SSL certificate bytes and private key
62-
bytes, both in PEM format. It is ignored if ``api_mtls_endpoint``
63-
is None.
64-
65-
Raises:
66-
google.auth.exceptions.MutualTlsChannelError: If mutual TLS transport
67-
creation failed for any reason.
6852
"""
53+
# Sanity check: Ensure that channel and credentials are not both
54+
# provided.
6955
if channel:
70-
# Sanity check: Ensure that channel and credentials are not both
71-
# provided.
7256
credentials = False
7357

74-
# If a channel was explicitly provided, set it.
75-
self._grpc_channel = channel
76-
elif api_mtls_endpoint:
77-
host = api_mtls_endpoint if ":" in api_mtls_endpoint else api_mtls_endpoint + ":443"
78-
79-
# Create SSL credentials with client_cert_source or application
80-
# default SSL credentials.
81-
if client_cert_source:
82-
cert, key = client_cert_source()
83-
ssl_credentials = grpc.ssl_channel_credentials(
84-
certificate_chain=cert, private_key=key
85-
)
86-
else:
87-
ssl_credentials = SslCredentials().ssl_credentials
88-
89-
# create a new channel. The provided one is ignored.
90-
self._grpc_channel = grpc_helpers.create_channel(
91-
host,
92-
credentials=credentials,
93-
ssl_credentials=ssl_credentials,
94-
scopes=self.AUTH_SCOPES,
95-
)
96-
9758
# Run the base constructor.
9859
super().__init__(host=host, credentials=credentials)
9960
self._stubs = {} # type: Dict[str, Callable]
10061

62+
# If a channel was explicitly provided, set it.
63+
if channel:
64+
self._grpc_channel = channel
10165

10266
@classmethod
10367
def create_channel(cls,

gapic/templates/setup.py.j2

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ setuptools.setup(
1616
platforms='Posix; MacOS X; Windows',
1717
include_package_data=True,
1818
install_requires=(
19-
'google-auth >= 1.13.1',
2019
'google-api-core >= 1.8.0, < 2.0.0dev',
2120
'googleapis-common-protos >= 1.5.8',
2221
'grpcio >= 1.10.0',

0 commit comments

Comments
 (0)