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