@@ -330,6 +330,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
330
330
m = re .match (r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$" , path )
331
331
return m .groupdict () if m else {}
332
332
333
+ @classmethod
334
+ def get_mtls_endpoint_and_cert_source (
335
+ cls , client_options : Optional [client_options_lib .ClientOptions ] = None
336
+ ):
337
+ """Return the API endpoint and client cert source for mutual TLS.
338
+
339
+ The client cert source is determined in the following order:
340
+ (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
341
+ client cert source is None.
342
+ (2) if `client_options.client_cert_source` is provided, use the provided one; if the
343
+ default client cert source exists, use the default one; otherwise the client cert
344
+ source is None.
345
+
346
+ The API endpoint is determined in the following order:
347
+ (1) if `client_options.api_endpoint` if provided, use the provided one.
348
+ (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
349
+ default mTLS endpoint; if the environment variabel is "never", use the default API
350
+ endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
351
+ use the default API endpoint.
352
+
353
+ More details can be found at https://google.aip.dev/auth/4114.
354
+
355
+ Args:
356
+ client_options (google.api_core.client_options.ClientOptions): Custom options for the
357
+ client. Only the `api_endpoint` and `client_cert_source` properties may be used
358
+ in this method.
359
+
360
+ Returns:
361
+ Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
362
+ client cert source to use.
363
+
364
+ Raises:
365
+ google.auth.exceptions.MutualTLSChannelError: If any errors happen.
366
+ """
367
+ if client_options is None :
368
+ client_options = client_options_lib .ClientOptions ()
369
+ use_client_cert = os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
370
+ use_mtls_endpoint = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
371
+ if use_client_cert not in ("true" , "false" ):
372
+ raise ValueError (
373
+ "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
374
+ )
375
+ if use_mtls_endpoint not in ("auto" , "never" , "always" ):
376
+ raise MutualTLSChannelError (
377
+ "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
378
+ )
379
+
380
+ # Figure out the client cert source to use.
381
+ client_cert_source = None
382
+ if use_client_cert == "true" :
383
+ if client_options .client_cert_source :
384
+ client_cert_source = client_options .client_cert_source
385
+ elif mtls .has_default_client_cert_source ():
386
+ client_cert_source = mtls .default_client_cert_source ()
387
+
388
+ # Figure out which api endpoint to use.
389
+ if client_options .api_endpoint is not None :
390
+ api_endpoint = client_options .api_endpoint
391
+ elif use_mtls_endpoint == "always" or (
392
+ use_mtls_endpoint == "auto" and client_cert_source
393
+ ):
394
+ api_endpoint = cls .DEFAULT_MTLS_ENDPOINT
395
+ else :
396
+ api_endpoint = cls .DEFAULT_ENDPOINT
397
+
398
+ return api_endpoint , client_cert_source
399
+
333
400
def __init__ (
334
401
self ,
335
402
* ,
@@ -380,57 +447,22 @@ def __init__(
380
447
if client_options is None :
381
448
client_options = client_options_lib .ClientOptions ()
382
449
383
- # Create SSL credentials for mutual TLS if needed.
384
- if os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) not in (
385
- "true" ,
386
- "false" ,
387
- ):
388
- raise ValueError (
389
- "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
390
- )
391
- use_client_cert = (
392
- os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) == "true"
450
+ api_endpoint , client_cert_source_func = self .get_mtls_endpoint_and_cert_source (
451
+ client_options
393
452
)
394
453
395
- client_cert_source_func = None
396
- is_mtls = False
397
- if use_client_cert :
398
- if client_options .client_cert_source :
399
- is_mtls = True
400
- client_cert_source_func = client_options .client_cert_source
401
- else :
402
- is_mtls = mtls .has_default_client_cert_source ()
403
- if is_mtls :
404
- client_cert_source_func = mtls .default_client_cert_source ()
405
- else :
406
- client_cert_source_func = None
407
-
408
- # Figure out which api endpoint to use.
409
- if client_options .api_endpoint is not None :
410
- api_endpoint = client_options .api_endpoint
411
- else :
412
- use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
413
- if use_mtls_env == "never" :
414
- api_endpoint = self .DEFAULT_ENDPOINT
415
- elif use_mtls_env == "always" :
416
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
417
- elif use_mtls_env == "auto" :
418
- if is_mtls :
419
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
420
- else :
421
- api_endpoint = self .DEFAULT_ENDPOINT
422
- else :
423
- raise MutualTLSChannelError (
424
- "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
425
- "values: never, auto, always"
426
- )
454
F987
+ api_key_value = getattr (client_options , "api_key" , None )
455
+ if api_key_value and credentials :
456
+ raise ValueError (
457
+ "client_options.api_key and credentials are mutually exclusive"
458
+ )
427
459
428
460
# Save or instantiate the transport.
429
461
# Ordinarily, we provide the transport, but allowing a custom transport
430
462
# instance provides an extensibility point for unusual situations.
431
463
if isinstance (transport , DlpServiceTransport ):
432
464
# transport is a DlpServiceTransport instance.
433
- if credentials or client_options .credentials_file :
465
+ if credentials or client_options .credentials_file or api_key_value :
434
466
raise ValueError (
435
467
"When providing a transport instance, "
436
468
"provide its credentials directly."
@@ -442,6 +474,15 @@ def __init__(
442
474
)
443
475
self ._transport = transport
444
476
else :
477
+ import google .auth ._default # type: ignore
478
+
479
+ if api_key_value and hasattr (
480
+ google .auth ._default , "get_api_key_credentials"
481
+ ):
482
+ credentials = google .auth ._default .get_api_key_credentials (
483
+ api_key_value
484
+ )
485
+
445
486
Transport = type (self ).get_transport_class (transport )
446
487
self ._transport = Transport (
447
488
credentials = credentials ,
0 commit comments