Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 59a44bc

Browse files
feat: add client_cert_source_for_mtls argument to transports (#107)
* changes without context autosynth cannot find the source of changes triggered by earlier changes in this repository, or by version upgrades to tools such as linters. * chore: update Go generator, rules_go, and protobuf PiperOrigin-RevId: 352816749 Source-Author: Google APIs <noreply@google.com> Source-Date: Wed Jan 20 10:06:23 2021 -0800 Source-Repo: googleapis/googleapis Source-Sha: ceaaf31b3d13badab7cf9d3b570f5639db5593d9 Source-Link: googleapis/googleapis@ceaaf31 * chore: upgrade gapic-generator-python to 0.40.5 PiperOrigin-RevId: 354996675 Source-Author: Google APIs <noreply@google.com> Source-Date: Mon Feb 1 12:11:49 2021 -0800 Source-Repo: googleapis/googleapis Source-Sha: 20712b8fe95001b312f62c6c5f33e3e3ec92cfaf Source-Link: googleapis/googleapis@20712b8 * feat: Add Pub/Sub endpoints for Cloud Channel API. PiperOrigin-RevId: 355059873 Source-Author: Google APIs <noreply@google.com> Source-Date: Mon Feb 1 17:13:22 2021 -0800 Source-Repo: googleapis/googleapis Source-Sha: 6ef9eaea379fc1cc0355e06a5a20b594543ee693 Source-Link: googleapis/googleapis@6ef9eae * revert flakybot changes Co-authored-by: Tim Swast <swast@google.com>
1 parent 00a5f47 commit 59a44bc

File tree

18 files changed

+593
-397
lines changed

18 files changed

+593
-397
lines changed

google/cloud/datacatalog_v1/services/data_catalog/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,17 @@ def __init__(
367367
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
368368
)
369369

370-
ssl_credentials = None
370+
client_cert_source_func = None
371371
is_mtls = False
372372
if use_client_cert:
373373
if client_options.client_cert_source:
374-
import grpc # type: ignore
375-
376-
cert, key = client_options.client_cert_source()
377-
ssl_credentials = grpc.ssl_channel_credentials(
378-
certificate_chain=cert, private_key=key
379-
)
380374
is_mtls = True
375+
client_cert_source_func = client_options.client_cert_source
381376
else:
382-
creds = SslCredentials()
383-
is_mtls = creds.is_mtls
384-
ssl_credentials = creds.ssl_credentials if is_mtls else None
377+
is_mtls = mtls.has_default_client_cert_source()
378+
client_cert_source_func = (
379+
mtls.default_client_cert_source() if is_mtls else None
380+
)
385381

386382
# Figure out which api endpoint to use.
387383
if client_options.api_endpoint is not None:
@@ -424,7 +420,7 @@ def __init__(
424420
credentials_file=client_options.credentials_file,
425421
host=api_endpoint,
426422
scopes=client_options.scopes,
427-
ssl_channel_credentials=ssl_credentials,
423+
client_cert_source_for_mtls=client_cert_source_func,
428424
quota_project_id=client_options.quota_project_id,
429425
client_info=client_info,
430426
)

google/cloud/datacatalog_v1/services/data_catalog/transports/grpc.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(
6262
api_mtls_endpoint: str = None,
6363
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
6464
ssl_channel_credentials: grpc.ChannelCredentials = None,
65+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
6566
quota_project_id: Optional[str] = None,
6667
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
6768
) -> None:
@@ -92,6 +93,10 @@ def __init__(
9293
``api_mtls_endpoint`` is None.
9394
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
9495
for grpc channel. It is ignored if ``channel`` is provided.
96+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
97+
A callback to provide client certificate bytes and private key bytes,
98+
both in PEM format. It is used to configure mutual TLS channel. It is
99+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
95100
quota_project_id (Optional[str]): An optional project to use for billing
96101
and quota.
97102
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -108,6 +113,11 @@ def __init__(
108113
"""
109114
self._ssl_channel_credentials = ssl_channel_credentials
110115

116+
if api_mtls_endpoint:
117+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
118+
if client_cert_source:
119+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
120+
111121
if channel:
112122
# Sanity check: Ensure that channel and credentials are not both
113123
# provided.
@@ -117,11 +127,6 @@ def __init__(
117127
self._grpc_channel = channel
118128
self._ssl_channel_credentials = None
119129
elif api_mtls_endpoint:
120-
warnings.warn(
121-
"api_mtls_endpoint and client_cert_source are deprecated",
122-
DeprecationWarning,
123-
)
124-
125130
host = (
126131
api_mtls_endpoint
127132
if ":" in api_mtls_endpoint
@@ -165,12 +170,18 @@ def __init__(
165170
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
166171
)
167172

173+
if client_cert_source_for_mtls and not ssl_channel_credentials:
174+
cert, key = client_cert_source_for_mtls()
175+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
176+
certificate_chain=cert, private_key=key
177+
)
178+
168179
# create a new channel. The provided one is ignored.
169180
self._grpc_channel = type(self).create_channel(
170181
host,
171182
credentials=credentials,
172183
credentials_file=credentials_file,
173-
ssl_credentials=ssl_channel_credentials,
184+
ssl_credentials=self._ssl_channel_credentials,
174185
scopes=scopes or self.AUTH_SCOPES,
175186
quota_project_id=quota_project_id,
176187
options=[

google/cloud/datacatalog_v1/services/data_catalog/transports/grpc_asyncio.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(
106106
api_mtls_endpoint: str = None,
107107
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
108108
ssl_channel_credentials: grpc.ChannelCredentials = None,
109+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
109110
quota_project_id=None,
110111
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
111112
) -> None:
@@ -137,6 +138,10 @@ def __init__(
137138
``api_mtls_endpoint`` is None.
138139
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
139140
for grpc channel. It is ignored if ``channel`` is provided.
141+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
142+
A callback to provide client certificate bytes and private key bytes,
143+
both in PEM format. It is used to configure mutual TLS channel. It is
144+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
140145
quota_project_id (Optional[str]): An optional project to use for billing
141146
and quota.
142147
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -153,6 +158,11 @@ def __init__(
153158
"""
154159
self._ssl_channel_credentials = ssl_channel_credentials
155160

161+
if api_mtls_endpoint:
162+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
163+
if client_cert_source:
164+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
165+
156166
if channel:
157167
# Sanity check: Ensure that channel and credentials are not both
158168
# provided.
@@ -162,11 +172,6 @@ def __init__(
162172
self._grpc_channel = channel
163173
self._ssl_channel_credentials = None
164174
elif api_mtls_endpoint:
165-
warnings.warn(
166-
"api_mtls_endpoint and client_cert_source are deprecated",
167-
DeprecationWarning,
168-
)
169-
170175
host = (
171176
api_mtls_endpoint
172177
if ":" in api_mtls_endpoint
@@ -210,12 +215,18 @@ def __init__(
210215
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
211216
)
212217

218+
if client_cert_source_for_mtls and not ssl_channel_credentials:
219+
cert, key = client_cert_source_for_mtls()
220+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
221+
certificate_chain=cert, private_key=key
222+
)
223+
213224
# create a new channel. The provided one is ignored.
214225
self._grpc_channel = type(self).create_channel(
215226
host,
216227
credentials=credentials,
217228
credentials_file=credentials_file,
218-
ssl_credentials=ssl_channel_credentials,
229+
ssl_credentials=self._ssl_channel_credentials,
219230
scopes=scopes or self.AUTH_SCOPES,
220231
quota_project_id=quota_project_id,
221232
options=[

google/cloud/datacatalog_v1beta1/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"CreateTagTemplateFieldRequest",
104104
"CreateTagTemplateRequest",
105105
"CreateTaxonomyRequest",
106+
"DataCatalogClient",
106107
"DeleteEntryGroupRequest",
107108
"DeleteEntryRequest",
108109
"DeletePolicyTagRequest",
@@ -140,7 +141,6 @@
140141
"LookupEntryRequest",
141142
"PolicyTag",
142143
"PolicyTagManagerClient",
143-
"PolicyTagManagerSerializationClient",
144144
"RenameTagTemplateFieldRequest",
145145
"Schema",
146146
"SearchCatalogRequest",
@@ -165,5 +165,5 @@
165165
"UpdateTagTemplateRequest",
166166
"UpdateTaxonomyRequest",
167167
"ViewSpec",
168-
"DataCatalogClient",
168+
"PolicyTagManagerSerializationClient",
169169
)

google/cloud/datacatalog_v1beta1/services/data_catalog/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,17 @@ def __init__(
367367
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
368368
)
369369

370-
ssl_credentials = None
370+
client_cert_source_func = None
371371
is_mtls = False
372372
if use_client_cert:
373373
if client_options.client_cert_source:
374-
import grpc # type: ignore
375-
376-
cert, key = client_options.client_cert_source()
377-
ssl_credentials = grpc.ssl_channel_credentials(
378-
certificate_chain=cert, private_key=key
379-
)
380374
is_mtls = True
375+
client_cert_source_func = client_options.client_cert_source
381376
else:
382-
creds = SslCredentials()
383-
is_mtls = creds.is_mtls
384-
ssl_credentials = creds.ssl_credentials if is_mtls else None
377+
is_mtls = mtls.has_default_client_cert_source()
378+
client_cert_source_func = (
379+
mtls.default_client_cert_source() if is_mtls else None
380+
)
385381

386382
# Figure out which api endpoint to use.
387383
if client_options.api_endpoint is not None:
@@ -424,7 +420,7 @@ def __init__(
424420
credentials_file=client_options.credentials_file,
425421
host=api_endpoint,
426422
scopes=client_options.scopes,
427-
ssl_channel_credentials=ssl_credentials,
423+
client_cert_source_for_mtls=client_cert_source_func,
428424
quota_project_id=client_options.quota_project_id,
429425
client_info=client_info,
430426
)

google/cloud/datacatalog_v1beta1/services/data_catalog/transports/grpc.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(
6262
api_mtls_endpoint: str = None,
6363
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
6464
ssl_channel_credentials: grpc.ChannelCredentials = None,
65+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
6566
quota_project_id: Optional[str] = None,
6667
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
6768
) -> None:
@@ -92,6 +93,10 @@ def __init__(
9293
``api_mtls_endpoint`` is None.
9394
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
9495
for grpc channel. It is ignored if ``channel`` is provided.
96+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
97+
A callback to provide client certificate bytes and private key bytes,
98+
both in PEM format. It is used to configure mutual TLS channel. It is
99+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
95100
quota_project_id (Optional[str]): An optional project to use for billing
96101
and quota.
97102
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -108,6 +113,11 @@ def __init__(
108113
"""
109114
self._ssl_channel_credentials = ssl_channel_credentials
110115

116+
if api_mtls_endpoint:
117+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
118+
if client_cert_source:
119+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
120+
111121
if channel:
112122
# Sanity check: Ensure that channel and credentials are not both
113123
# provided.
@@ -117,11 +127,6 @@ def __init__(
117127
self._grpc_channel = channel
118128
self._ssl_channel_credentials = None
119129
elif api_mtls_endpoint:
120-
warnings.warn(
121-
"api_mtls_endpoint and client_cert_source are deprecated",
122-
DeprecationWarning,
123-
)
124-
125130
host = (
126131
api_mtls_endpoint
127132
if ":" in api_mtls_endpoint
@@ -165,12 +170,18 @@ def __init__(
165170
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
166171
)
167172

173+
if client_cert_source_for_mtls and not ssl_channel_credentials:
174+
cert, key = client_cert_source_for_mtls()
175+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
176+
certificate_chain=cert, private_key=key
177+
)
178+
168179
# create a new channel. The provided one is ignored.
169180
self._grpc_channel = type(self).create_channel(
170181
host,
171182
credentials=credentials,
172183
credentials_file=credentials_file,
173-
ssl_credentials=ssl_channel_credentials,
184+
ssl_credentials=self._ssl_channel_credentials,
174185
scopes=scopes or self.AUTH_SCOPES,
175186
quota_project_id=quota_project_id,
176187
options=[

google/cloud/datacatalog_v1beta1/services/data_catalog/transports/grpc_asyncio.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(
106106
api_mtls_endpoint: str = None,
107107
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
108108
ssl_channel_credentials: grpc.ChannelCredentials = None,
109+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
109110
quota_project_id=None,
110111
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
111112
) -> None:
@@ -137,6 +138,10 @@ def __init__(
137138
``api_mtls_endpoint`` is None.
138139
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
139140
for grpc channel. It is ignored if ``channel`` is provided.
141+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
142+
A callback to provide client certificate bytes and private key bytes,
143+
both in PEM format. It is used to configure mutual TLS channel. It is
144+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
140145
quota_project_id (Optional[str]): An optional project to use for billing
141146
and quota.
142147
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -153,6 +158,11 @@ def __init__(
153158
"""
154159
self._ssl_channel_credentials = ssl_channel_credentials
155160

161+
if api_mtls_endpoint:
162+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
163+
if client_cert_source:
164+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
165+
156166
if channel:
157167
# Sanity check: Ensure that channel and credentials are not both
158168
# provided.
@@ -162,11 +172,6 @@ def __init__(
162172
self._grpc_channel = channel
163173
self._ssl_channel_credentials = None
164174
elif api_mtls_endpoint:
165-
warnings.warn(
166-
"api_mtls_endpoint and client_cert_source are deprecated",
167-
DeprecationWarning,
168-
)
169-
170175
host = (
171176
api_mtls_endpoint
172177
if ":" in api_mtls_endpoint
@@ -210,12 +215,18 @@ def __init__(
210215
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
211216
)
212217

218+
if client_cert_source_for_mtls and not ssl_channel_credentials:
219+
cert, key = client_cert_source_for_mtls()
220+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
221+
certificate_chain=cert, private_key=key
222+
)
223+
213224
# create a new channel. The provided one is ignored.
214225
self._grpc_channel = type(self).create_channel(
215226
host,
216227
credentials=credentials,
217228
credentials_file=credentials_file,
218-
ssl_credentials=ssl_channel_credentials,
229+
ssl_credentials=self._ssl_channel_credentials,
219230
scopes=scopes or self.AUTH_SCOPES,
220231
quota_project_id=quota_project_id,
221232
options=[

google/cloud/datacatalog_v1beta1/services/policy_tag_manager/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,17 @@ def __init__(
310310
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
311311
)
312312

313-
ssl_credentials = None
313+
client_cert_source_func = None
314314
is_mtls = False
315315
if use_client_cert:
316316
if client_options.client_cert_source:
317-
import grpc # type: ignore
318-
319-
cert, key = client_options.client_cert_source()
320-
ssl_credentials = grpc.ssl_channel_credentials(
321-
certificate_chain=cert, private_key=key
322-
)
323317
is_mtls = True
318+
client_cert_source_func = client_options.client_cert_source
324319
else:
325-
creds = SslCredentials()
326-
is_mtls = creds.is_mtls
327-
ssl_credentials = creds.ssl_credentials if is_mtls else None
320+
is_mtls = mtls.has_default_client_cert_source()
321+
client_cert_source_func = (
322+
mtls.default_client_cert_source() if is_mtls else None
323+
)
328324

329325
# Figure out which api endpoint to use.
330326
if client_options.api_endpoint is not None:
@@ -367,7 +363,7 @@ def __init__(
367363
credentials_file=client_options.credentials_file,
368364
host=api_endpoint,
369365
scopes=client_options.scopes,
370-
ssl_channel_credentials=ssl_credentials,
366+
client_cert_source_for_mtls=client_cert_source_func,
371367
quota_project_id=client_options.quota_project_id,
372368
client_info=client_info,
373369
)

0 commit comments

Comments
 (0)