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