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