Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 072850d

Browse files
feat: add client_cert_source_for_mtls argument to transports (#135)
This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/3b4457c8-4080-407a-9a6d-4a48ddcea154/targets - [ ] To automatically regenerate this PR, check this box. PiperOrigin-RevId: 354996675 Source-Link: googleapis/googleapis@20712b8 PiperOrigin-RevId: 352816749 Source-Link: googleapis/googleapis@ceaaf31
1 parent bc94422 commit 072850d

13 files changed

Lines changed: 454 additions & 305 deletions

File tree

google/cloud/bigquery_storage_v1/services/big_query_read/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,17 @@ def __init__(
322322
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
323323
)
324324

325-
ssl_credentials = None
325+
client_cert_source_func = None
326326
is_mtls = False
327327
if use_client_cert:
328328
if client_options.client_cert_source:
329-
import grpc # type: ignore
330-
331-
cert, key = client_options.client_cert_source()
332-
ssl_credentials = grpc.ssl_channel_credentials(
333-
certificate_chain=cert, private_key=key
334-
)
335329
is_mtls = True
330+
client_cert_source_func = client_options.client_cert_source
336331
else:
337-
creds = SslCredentials()
338-
is_mtls = creds.is_mtls
339-
ssl_credentials = creds.ssl_credentials if is_mtls else None
332+
is_mtls = mtls.has_default_client_cert_source()
333+
client_cert_source_func = (
334+
mtls.default_client_cert_source() if is_mtls else None
335+
)
340336

341337
# Figure out which api endpoint to use.
342338
if client_options.api_endpoint is not None:
@@ -379,7 +375,7 @@ def __init__(
379375
credentials_file=client_options.credentials_file,
380376
host=api_endpoint,
381377
scopes=client_options.scopes,
382-
ssl_channel_credentials=ssl_credentials,
378+
client_cert_source_for_mtls=client_cert_source_func,
383379
quota_project_id=client_options.quota_project_id,
384380
client_info=client_info,
385381
)

google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(
5959
api_mtls_endpoint: str = None,
6060
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
6161
ssl_channel_credentials: grpc.ChannelCredentials = None,
62+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
6263
quota_project_id: Optional[str] = None,
6364
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
6465
) -> None:
@@ -89,6 +90,10 @@ def __init__(
8990
``api_mtls_endpoint`` is None.
9091
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
9192
for grpc channel. It is ignored if ``channel`` is provided.
93+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
94+
A callback to provide client certificate bytes and private key bytes,
95+
both in PEM format. It is used to configure mutual TLS channel. It is
96+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
9297
quota_project_id (Optional[str]): An optional project to use for billing
9398
and quota.
9499
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -105,6 +110,11 @@ def __init__(
105110
"""
106111
self._ssl_channel_credentials = ssl_channel_credentials
107112

113+
if api_mtls_endpoint:
114+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
115+
if client_cert_source:
116+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
117+
108118
if channel:
109119
# Sanity check: Ensure that channel and credentials are not both
110120
# provided.
@@ -114,11 +124,6 @@ def __init__(
114124
self._grpc_channel = channel
115125
self._ssl_channel_credentials = None
116126
elif api_mtls_endpoint:
117-
warnings.warn(
118-
"api_mtls_endpoint and client_cert_source are deprecated",
119-
DeprecationWarning,
120-
)
121-
122127
host = (
123128
api_mtls_endpoint
124129
if ":" in api_mtls_endpoint
@@ -162,12 +167,18 @@ def __init__(
162167
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
163168
)
164169

170+
if client_cert_source_for_mtls and not ssl_channel_credentials:
171+
cert, key = client_cert_source_for_mtls()
172+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
173+
certificate_chain=cert, private_key=key
174+
)
175+
165176
# create a new channel. The provided one is ignored.
166177
self._grpc_channel = type(self).create_channel(
167178
host,
168179
credentials=credentials,
169180
credentials_file=credentials_file,
170-
ssl_credentials=ssl_channel_credentials,
181+
ssl_credentials=self._ssl_channel_credentials,
171182
scopes=scopes or self.AUTH_SCOPES,
172183
quota_project_id=quota_project_id,
173184
options=[

google/cloud/bigquery_storage_v1/services/big_query_read/transports/grpc_asyncio.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def __init__(
103103
api_mtls_endpoint: str = None,
104104
client_cert_source: Callable[[], Tuple[bytes, bytes]] = None,
105105
ssl_channel_credentials: grpc.ChannelCredentials = None,
106+
client_cert_source_for_mtls: Callable[[], Tuple[bytes, bytes]] = None,
106107
quota_project_id=None,
107108
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
108109
) -> None:
@@ -134,6 +135,10 @@ def __init__(
134135
``api_mtls_endpoint`` is None.
135136
ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials
136137
for grpc channel. It is ignored if ``channel`` is provided.
138+
client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]):
139+
A callback to provide client certificate bytes and private key bytes,
140+
both in PEM format. It is used to configure mutual TLS channel. It is
141+
ignored if ``channel`` or ``ssl_channel_credentials`` is provided.
137142
quota_project_id (Optional[str]): An optional project to use for billing
138143
and quota.
139144
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
@@ -150,6 +155,11 @@ def __init__(
150155
"""
151156
self._ssl_channel_credentials = ssl_channel_credentials
152157

158+
if api_mtls_endpoint:
159+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
160+
if client_cert_source:
161+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
162+
153163
if channel:
154164
# Sanity check: Ensure that channel and credentials are not both
155165
# provided.
@@ -159,11 +169,6 @@ def __init__(
159169
self._grpc_channel = channel
160170
self._ssl_channel_credentials = None
161171
elif api_mtls_endpoint:
162-
warnings.warn(
163-
"api_mtls_endpoint and client_cert_source are deprecated",
164-
DeprecationWarning,
165-
)
166-
167172
host = (
168173
api_mtls_endpoint
169174
if ":" in api_mtls_endpoint
@@ -207,12 +212,18 @@ def __init__(
207212
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
208213
)
209214

215+
if client_cert_source_for_mtls and not ssl_channel_credentials:
216+
cert, key = client_cert_source_for_mtls()
217+
self._ssl_channel_credentials = grpc.ssl_channel_credentials(
218+
certificate_chain=cert, private_key=key
219+
)
220+
210221
# create a new channel. The provided one is ignored.
211222
self._grpc_channel = type(self).create_channel(
212223
host,
213224
credentials=credentials,
214225
credentials_file=credentials_file,
215-
ssl_credentials=ssl_channel_credentials,
226+
ssl_credentials=self._ssl_channel_credentials,
216227
scopes=scopes or self.AUTH_SCOPES,
217228
quota_project_id=quota_project_id,
218229
options=[

google/cloud/bigquery_storage_v1beta2/services/big_query_read/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,21 +324,17 @@ def __init__(
324324
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
325325
)
326326

327-
ssl_credentials = None
327+
client_cert_source_func = None
328328
is_mtls = False
329329
if use_client_cert:
330330
if client_options.client_cert_source:
331-
import grpc # type: ignore
332-
333-
cert, key = client_options.client_cert_source()
334-
ssl_credentials = grpc.ssl_channel_credentials(
335-
certificate_chain=cert, private_key=key
336-
)
337331
is_mtls = True
332+
client_cert_source_func = client_options.client_cert_source
338333
else:
339-
creds = SslCredentials()
340-
is_mtls = creds.is_mtls
341-
ssl_credentials = creds.ssl_credentials if is_mtls else None
334+
is_mtls = mtls.has_default_client_cert_source()
335+
client_cert_source_func = (
336+
mtls.default_client_cert_source() if is_mtls else None
337+
)
342338

343339
# Figure out which api endpoint to use.
344340
if client_options.api_endpoint is not None:
@@ -381,7 +377,7 @@ def __init__(
381377
credentials_file=client_options.credentials_file,
382378
host=api_endpoint,
383379
scopes=client_options.scopes,
384-
ssl_channel_credentials=ssl_credentials,
380+
client_cert_source_for_mtls=client_cert_source_func,
385381
quota_project_id=client_options.quota_project_id,
386382
client_info=client_info,
387383
)

google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc.py

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

115+
if api_mtls_endpoint:
116+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
117+
if client_cert_source:
118+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
119+
110120
if channel:
111121
# Sanity check: Ensure that channel and credentials are not both
112122
# provided.
@@ -116,11 +126,6 @@ def __init__(
116126
self._grpc_channel = channel
117127
self._ssl_channel_credentials = None
118128
elif api_mtls_endpoint:
119-
warnings.warn(
120-
"api_mtls_endpoint and client_cert_source are deprecated",
121-
DeprecationWarning,
122-
)
123-
124129
host = (
125130
api_mtls_endpoint
126131
if ":" in api_mtls_endpoint
@@ -164,12 +169,18 @@ def __init__(
164169
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
165170
)
166171

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

google/cloud/bigquery_storage_v1beta2/services/big_query_read/transports/grpc_asyncio.py

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

160+
if api_mtls_endpoint:
161+
warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning)
162+
if client_cert_source:
163+
warnings.warn("client_cert_source is deprecated", DeprecationWarning)
164+
155165
if channel:
156166
# Sanity check: Ensure that channel and credentials are not both
157167
# provided.
@@ -161,11 +171,6 @@ def __init__(
161171
self._grpc_channel = channel
162172
self._ssl_channel_credentials = None
163173
elif api_mtls_endpoint:
164-
warnings.warn(
165-
"api_mtls_endpoint and client_cert_source are deprecated",
166-
DeprecationWarning,
167-
)
168-
169174
host = (
170175
api_mtls_endpoint
171176
if ":" in api_mtls_endpoint
@@ -209,12 +214,18 @@ def __init__(
209214
scopes=self.AUTH_SCOPES, quota_project_id=quota_project_id
210215
)
211216

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

google/cloud/bigquery_storage_v1beta2/services/big_query_write/client.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,21 +314,17 @@ def __init__(
314314
util.strtobool(os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false"))
315315
)
316316

317-
ssl_credentials = None
317+
client_cert_source_func = None
318318
is_mtls = False
319319
if use_client_cert:
320320
if client_options.client_cert_source:
321-
import grpc # type: ignore
322-
323-
cert, key = client_options.client_cert_source()
324-
ssl_credentials = grpc.ssl_channel_credentials(
325-
certificate_chain=cert, private_key=key
326-
)
327321
is_mtls = True
322+
client_cert_source_func = client_options.client_cert_source
328323
else:
329-
creds = SslCredentials()
330-
is_mtls = creds.is_mtls
331-
ssl_credentials = creds.ssl_credentials if is_mtls else None
324+
is_mtls = mtls.has_default_client_cert_source()
325+
client_cert_source_func = (
326+
mtls.default_client_cert_source() if is_mtls else None
327+
)
332328

333329
# Figure out which api endpoint to use.
334330
if client_options.api_endpoint is not None:
@@ -371,7 +367,7 @@ def __init__(
371367
credentials_file=client_options.credentials_file,
372368
host=api_endpoint,
373369
scopes=client_options.scopes,
374-
ssl_channel_credentials=ssl_credentials,
370+
client_cert_source_for_mtls=client_cert_source_func,
375371
quota_project_id=client_options.quota_project_id,
376372
client_info=client_info,
377373
)

0 commit comments

Comments
 (0)