@@ -243,6 +243,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
243
243
m = re .match (r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$" , path )
244
244
return m .groupdict () if m else {}
245
245
246
+ @classmethod
247
+ def get_mtls_endpoint_and_cert_source (
248
+ cls , client_options : Optional [client_options_lib .ClientOptions ] = None
249
+ ):
250
+ """Return the API endpoint and client cert source for mutual TLS.
251
+
252
+ The client cert source is determined in the following order:
253
+ (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
254
+ client cert source is None.
255
+ (2) if `client_options.client_cert_source` is provided, use the provided one; if the
256
+ default client cert source exists, use the default one; otherwise the client cert
257
+ source is None.
258
+
259
+ The API endpoint is determined in the following order:
260
+ (1) if `client_options.api_endpoint` if provided, use the provided one.
261
+ (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
262
+ default mTLS endpoint; if the environment variabel is "never", use the default API
263
+ endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
264
+ use the default API endpoint.
265
+
266
+ More details can be found at https://google.aip.dev/auth/4114.
267
+
268
+ Args:
269
+ client_options (google.api_core.client_options.ClientOptions): Custom options for the
270
+ client. Only the `api_endpoint` and `client_cert_source` properties may be used
271
+ in this method.
272
+
273
+ Returns:
274
+ Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
275
+ client cert source to use.
276
+
277
+ Raises:
278
+ google.auth.exceptions.MutualTLSChannelError: If any errors happen.
279
+ """
280
+ if client_options is None :
281
+ client_options = client_options_lib .ClientOptions ()
282
+ use_client_cert = os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
283
+ use_mtls_endpoint = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
284
+ if use_client_cert not in ("true" , "false" ):
285
+ raise ValueError (
286
+ "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
287
+ )
288
+ if use_mtls_endpoint not in ("auto" , "never" , "always" ):
289
+ raise MutualTLSChannelError (
290
+ "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
291
+ )
292
+
293
+ # Figure out the client cert source to use.
294
+ client_cert_source = None
295
+ if use_client_cert == "true" :
296
+ if client_options .client_cert_source :
297
+ client_cert_source = client_options .client_cert_source
298
+ elif mtls .has_default_client_cert_source ():
299
+ client_cert_source = mtls .default_client_cert_source ()
300
+
301
+ # Figure out which api endpoint to use.
302
+ if client_options .api_endpoint is not None :
303
+ api_endpoint = client_options .api_endpoint
304
+ elif use_mtls_endpoint == "always" or (
305
+ use_mtls_endpoint == "auto" and client_cert_source
306
+ ):
307
+ api_endpoint = cls .DEFAULT_MTLS_ENDPOINT
308
+ else :
309
+ api_endpoint = cls .DEFAULT_ENDPOINT
310
+
311
+ return api_endpoint , client_cert_source
312
+
246
313
def __init__ (
247
314
self ,
248
315
* ,
@@ -293,57 +360,22 @@ def __init__(
293
360
if client_options is None :
294
361
client_options = client_options_lib .ClientOptions ()
295
362
296
- # Create SSL credentials for mutual TLS if needed.
297
- if os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) not in (
298
- "true" ,
299
- "false" ,
300
- ):
301
- raise ValueError (
302
- "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
303
- )
304
- use_client_cert = (
305
- os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) == "true"
363
+ api_endpoint , client_cert_source_func = self .get_mtls_endpoint_and_cert_source (
364
+ client_options
306
365
)
307
366
308
- client_cert_source_func = None
309
- is_mtls = False
310
- if use_client_cert :
311
- if client_options .client_cert_source :
312
- is_mtls = True
313
- client_cert_source_func = client_options .client_cert_source
314
- else :
315
- is_mtls = mtls .has_default_client_cert_source ()
316
- if is_mtls :
317
- client_cert_source_func = mtls .default_client_cert_source ()
318
- else :
319
- client_cert_source_func = None
320
-
321
- # Figure out which api endpoint to use.
322
- if client_options .api_endpoint is not None :
323
- api_endpoint = client_options .api_endpoint
324
- else :
325
- use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
326
- if use_mtls_env == "never" :
327
- api_endpoint = self .DEFAULT_ENDPOINT
328
- elif use_mtls_env == "always" :
329
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
330
- elif use_mtls_env == "auto" :
331
- if is_mtls :
332
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
333
- else :
334
- api_endpoint = self .DEFAULT_ENDPOINT
335
- else :
336
- raise MutualTLSChannelError (
337
- "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
338
- "values: never, auto, always"
339
- )
367
+ api_key_value = getattr (client_options , "api_key" , None )
368
+ if api_key_value and credentials :
369
+ raise ValueError (
370
+ "client_options.api_key and credentials are mutually exclusive"
371
+ )
340
372
341
373
# Save or instantiate the transport.
342
374
# Ordinarily, we provide the transport, but allowing a custom transport
343
375
# instance provides an extensibility point for unusual situations.
344
376
if isinstance (transport , ManagedNotebookServiceTransport ):
345
377
# transport is a ManagedNotebookServiceTransport instance.
346
- if credentials or client_options .credentials_file :
378
+ if credentials or client_options .credentials_file or api_key_value :
347
379
raise ValueError (
348
380
"When providing a transport instance, "
349
381
"provide its credentials directly."
@@ -355,6 +387,15 @@ def __init__(
355
387
)
356
388
self ._transport = transport
357
389
else :
390
+ import google .auth ._default # type: ignore
391
+
392
+ if api_key_value and hasattr (
393
+ google .auth ._default , "get_api_key_credentials"
394
+ ):
395
+ credentials = google .auth ._default .get_api_key_credentials (
396
+ api_key_value
397
+ )
398
+
358
399
Transport = type (self ).get_transport_class (transport )
359
400
self ._transport = Transport (
360
401
credentials = credentials ,
0 commit comments