@@ -282,6 +282,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
282
282
m = re .match (r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$" , path )
283
283
return m .groupdict () if m else {}
284
284
285
+ @classmethod
286
+ def get_mtls_endpoint_and_cert_source (
287
+ cls , client_options : Optional [client_options_lib .ClientOptions ] = None
288
+ ):
289
+ """Return the API endpoint and client cert source for mutual TLS.
290
+
291
+ The client cert source is determined in the following order:
292
+ (1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
293
+ client cert source is None.
294
+ (2) if `client_options.client_cert_source` is provided, use the provided one; if the
295
+ default client cert source exists, use the default one; otherwise the client cert
296
+ source is None.
297
+
298
+ The API endpoint is determined in the following order:
299
+ (1) if `client_options.api_endpoint` if provided, use the provided one.
300
+ (2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
301
+ default mTLS endpoint; if the environment variabel is "never", use the default API
302
+ endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
303
+ use the default API endpoint.
304
+
305
+ More details can be found at https://google.aip.dev/auth/4114.
306
+
307
+ Args:
308
+ client_options (google.api_core.client_options.ClientOptions): Custom options for the
309
+ client. Only the `api_endpoint` and `client_cert_source` properties may be used
310
+ in this method.
311
+
312
+ Returns:
313
+ Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
314
+ client cert source to use.
315
+
316
+ Raises:
317
+ google.auth.exceptions.MutualTLSChannelError: If any errors happen.
318
+ """
319
+ if client_options is None :
320
+ client_options = client_options_lib .ClientOptions ()
321
+ use_client_cert = os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
322
+ use_mtls_endpoint = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
323
+ if use_client_cert not in ("true" , "false" ):
324
+ raise ValueError (
325
+ "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
326
+ )
327
+ if use_mtls_endpoint not in ("auto" , "never" , "always" ):
328
+ raise MutualTLSChannelError (
329
+ "Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
330
+ )
331
+
332
+ # Figure out the client cert source to use.
333
+ client_cert_source = None
334
+ if use_client_cert == "true" :
335
+ if client_options .client_cert_source :
336
+ client_cert_source = client_options .client_cert_source
337
+ elif mtls .has_default_client_cert_source ():
338
+ client_cert_source = mtls .default_client_cert_source ()
339
+
340
+ # Figure out which api endpoint to use.
341
+ if client_options .api_endpoint is not None :
342
+ api_endpoint = client_options .api_endpoint
343
+ elif use_mtls_endpoint == "always" or (
344
+ use_mtls_endpoint == "auto" and client_cert_source
345
+ ):
346
+ api_endpoint = cls .DEFAULT_MTLS_ENDPOINT
347
+ else :
348
+ api_endpoint = cls .DEFAULT_ENDPOINT
349
+
350
+ return api_endpoint , client_cert_source
351
+
285
352
def __init__ (
286
353
self ,
287
354
* ,
@@ -332,57 +399,22 @@ def __init__(
332
399
if client_options is None :
333
400
client_options = client_options_lib .ClientOptions ()
334
401
335
- # Create SSL credentials for mutual TLS if needed.
336
- if os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) not in (
337
- "true" ,
338
- "false" ,
339
- ):
340
- raise ValueError (
341
- "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
342
- )
343
- use_client_cert = (
344
- os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ) == "true"
402
+ api_endpoint , client_cert_source_func = self .get_mtls_endpoint_and_cert_source (
403
+ client_options
345
404
)
346
405
347
- client_cert_source_func = None
348
- is_mtls = False
349
- if use_client_cert :
350
- if client_options .client_cert_source :
351
- is_mtls = True
352
- client_cert_source_func = client_options .client_cert_source
353
- else :
354
- is_mtls = mtls .has_default_client_cert_source ()
355
- if is_mtls :
356
- client_cert_source_func = mtls .default_client_cert_source ()
357
- else :
358
- client_cert_source_func = None
359
-
360
- # Figure out which api endpoint to use.
361
- if client_options .api_endpoint is not None :
362
- api_endpoint = client_options .api_endpoint
363
- else :
364
- use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
365
- if use_mtls_env == "never" :
366
- api_endpoint = self .DEFAULT_ENDPOINT
367
- elif use_mtls_env == "always" :
368
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
369
- elif use_mtls_env == "auto" :
370
- if is_mtls :
371
- api_endpoint = self .DEFAULT_MTLS_ENDPOINT
372
- else :
373
- api_endpoint = self .DEFAULT_ENDPOINT
374
- else :
375
- raise MutualTLSChannelError (
376
- "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
377
- "values: never, auto, always"
378
- )
406
+ api_key_value = getattr (client_options , "api_key" , None )
407
+ if api_key_value and credentials :
408
+ raise ValueError (
409
+ "client_options.api_key and credentials are mutually exclusive"
410
+ )
379
411
380
412
# Save or instantiate the transport.
381
413
# Ordinarily, we provide the transport, but allowing a custom transport
382
414
# instance provides an extensibility point for unusual situations.
383
415
if isinstance (transport , CloudFilestoreManagerTransport ):
384
416
# transport is a CloudFilestoreManagerTransport instance.
385
- if credentials or client_options .credentials_file :
417
+ if credentials or client_options .credentials_file or api_key_value :
386
418
raise ValueError (
387
419
"When providing a transport instance, "
388
420
"provide its credentials directly."
@@ -394,6 +426,15 @@ def __init__(
394
426
)
395
427
self ._transport = transport
396
428
else :
429
+ import google .auth ._default # type: ignore
430
+
431
+ if api_key_value and hasattr (
432
+ google .auth ._default , "get_api_key_credentials"
433
+ ):
434
+ credentials = google .auth ._default .get_api_key_credentials (
435
+ api_key_value
436
+ )
437
+
397
438
Transport = type (self ).get_transport_class (transport )
398
439
self ._transport = Transport (
399
440
credentials = credentials ,
0 commit comments