Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 74c9531

Browse files
feat: add api key support (#327)
* chore: upgrade gapic-generator-java, gax-java and gapic-generator-python PiperOrigin-RevId: 423842556 Source-Link: googleapis/googleapis@a616ca0 Source-Link: https://github.com/googleapis/googleapis-gen/commit/29b938c58c1e51d019f2ee539d55dc0a3c86a905 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMjliOTM4YzU4YzFlNTFkMDE5ZjJlZTUzOWQ1NWRjMGEzYzg2YTkwNSJ9 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 051fbfa commit 74c9531

File tree

12 files changed

+990
-176
lines changed

12 files changed

+990
-176
lines changed

google/cloud/automl_v1/services/auto_ml/async_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections import OrderedDict
1717
import functools
1818
import re
19-
from typing import Dict, Sequence, Tuple, Type, Union
19+
from typing import Dict, Optional, Sequence, Tuple, Type, Union
2020
import pkg_resources
2121

2222
from google.api_core.client_options import ClientOptions
@@ -137,6 +137,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
137137

138138
from_service_account_json = from_service_account_file
139139

140+
@classmethod
141+
def get_mtls_endpoint_and_cert_source(
142+
cls, client_options: Optional[ClientOptions] = None
143+
):
144+
"""Return the API endpoint and client cert source for mutual TLS.
145+
146+
The client cert source is determined in the following order:
147+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
148+
client cert source is None.
149+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
150+
default client cert source exists, use the default one; otherwise the client cert
151+
source is None.
152+
153+
The API endpoint is determined in the following order:
154+
(1) if `client_options.api_endpoint` if provided, use the provided one.
155+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
156+
default mTLS endpoint; if the environment variabel is "never", use the default API
157+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
158+
use the default API endpoint.
159+
160+
More details can be found at https://google.aip.dev/auth/4114.
161+
162+
Args:
163+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
164+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
165+
in this method.
166+
167+
Returns:
168+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
169+
client cert source to use.
170+
171+
Raises:
172+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
173+
"""
174+
return AutoMlClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
175+
140176
@property
141177
def transport(self) -> AutoMlTransport:
142178
"""Returns the transport used by the client instance.

google/cloud/automl_v1/services/auto_ml/client.py

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
325325
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
326326
return m.groupdict() if m else {}
327327

328+
@classmethod
329+
def get_mtls_endpoint_and_cert_source(
330+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
331+
):
332+
"""Return the API endpoint and client cert source for mutual TLS.
333+
334+
The client cert source is determined in the following order:
335+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
336+
client cert source is None.
337+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
338+
default client cert source exists, use the default one; otherwise the client cert
339+
source is None.
340+
341+
The API endpoint is determined in the following order:
342+
(1) if `client_options.api_endpoint` if provided, use the provided one.
343+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
344+
default mTLS endpoint; if the environment variabel is "never", use the default API
345+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
346+
use the default API endpoint.
347+
348+
More details can be found at https://google.aip.dev/auth/4114.
349+
350+
Args:
351+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
352+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
353+
in this method.
354+
355+
Returns:
356+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
357+
client cert source to use.
358+
359+
Raises:
360+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
361+
"""
362+
if client_options is None:
363+
client_options = client_options_lib.ClientOptions()
364+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
365+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
366+
if use_client_cert not in ("true", "false"):
367+
raise ValueError(
368+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
369+
)
370+
if use_mtls_endpoint not in ("auto", "never", "always"):
371+
raise MutualTLSChannelError(
372+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
373+
)
374+
375+
# Figure out the client cert source to use.
376+
client_cert_source = None
377+
if use_client_cert == "true":
378+
if client_options.client_cert_source:
379+
client_cert_source = client_options.client_cert_source
380+
elif mtls.has_default_client_cert_source():
381+
client_cert_source = mtls.default_client_cert_source()
382+
383+
# Figure out which api endpoint to use.
384+
if client_options.api_endpoint is not None:
385+
api_endpoint = client_options.api_endpoint
386+
elif use_mtls_endpoint == "always" or (
387+
use_mtls_endpoint == "auto" and client_cert_source
388+
):
389+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
390+
else:
391+
api_endpoint = cls.DEFAULT_ENDPOINT
392+
393+
return api_endpoint, client_cert_source
394+
328395
def __init__(
329396
self,
330397
*,
@@ -375,57 +442,22 @@ def __init__(
375442
if client_options is None:
376443
client_options = client_options_lib.ClientOptions()
377444

378-
# Create SSL credentials for mutual TLS if needed.
379-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
380-
"true",
381-
"false",
382-
):
383-
raise ValueError(
384-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
385-
)
386-
use_client_cert = (
387-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
445+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
446+
client_options
388447
)
389448

390-
client_cert_source_func = None
391-
is_mtls = False
392-
if use_client_cert:
393-
if client_options.client_cert_source:
394-
is_mtls = True
395-
client_cert_source_func = client_options.client_cert_source
396-
else:
397-
is_mtls = mtls.has_default_client_cert_source()
398-
if is_mtls:
399-
client_cert_source_func = mtls.default_client_cert_source()
400-
else:
401-
client_cert_source_func = None
402-
403-
# Figure out which api endpoint to use.
404-
if client_options.api_endpoint is not None:
405-
api_endpoint = client_options.api_endpoint
406-
else:
407-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
408-
if use_mtls_env == "never":
409-
api_endpoint = self.DEFAULT_ENDPOINT
410-
elif use_mtls_env == "always":
411-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
412-
elif use_mtls_env == "auto":
413-
if is_mtls:
414-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
415-
else:
416-
api_endpoint = self.DEFAULT_ENDPOINT
417-
else:
418-
raise MutualTLSChannelError(
419-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
420-
"values: never, auto, always"
421-
)
449+
api_key_value = getattr(client_options, "api_key", None)
450+
if api_key_value and credentials:
451+
raise ValueError(
452+
"client_options.api_key and credentials are mutually exclusive"
453+
)
422454

423455
# Save or instantiate the transport.
424456
# Ordinarily, we provide the transport, but allowing a custom transport
425457
# instance provides an extensibility point for unusual situations.
426458
if isinstance(transport, AutoMlTransport):
427459
# transport is a AutoMlTransport instance.
428-
if credentials or client_options.credentials_file:
460+
if credentials or client_options.credentials_file or api_key_value:
429461
raise ValueError(
430462
"When providing a transport instance, "
431463
"provide its credentials directly."
@@ -437,6 +469,15 @@ def __init__(
437469
)
438470
self._transport = transport
439471
else:
472+
import google.auth._default # type: ignore
473+
474+
if api_key_value and hasattr(
475+
google.auth._default, "get_api_key_credentials"
476+
):
477+
credentials = google.auth._default.get_api_key_credentials(
478+
api_key_value
479+
)
480+
440481
Transport = type(self).get_transport_class(transport)
441482
self._transport = Transport(
442483
credentials=credentials,

google/cloud/automl_v1/services/prediction_service/async_client.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections import OrderedDict
1717
import functools
1818
import re
19-
from typing import Dict, Sequence, Tuple, Type, Union
19+
from typing import Dict, Optional, Sequence, Tuple, Type, Union
2020
import pkg_resources
2121

2222
from google.api_core.client_options import ClientOptions
@@ -115,6 +115,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
115115

116116
from_service_account_json = from_service_account_file
117117

118+
@classmethod
119+
def get_mtls_endpoint_and_cert_source(
120+
cls, client_options: Optional[ClientOptions] = None
121+
):
122+
"""Return the API endpoint and client cert source for mutual TLS.
123+
124+
The client cert source is determined in the following order:
125+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
126+
client cert source is None.
127+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
128+
default client cert source exists, use the default one; otherwise the client cert
129+
source is None.
130+
131+
The API endpoint is determined in the following order:
132+
(1) if `client_options.api_endpoint` if provided, use the provided one.
133+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
134+
default mTLS endpoint; if the environment variabel is "never", use the default API
135+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
136+
use the default API endpoint.
137+
138+
More details can be found at https://google.aip.dev/auth/4114.
139+
140+
Args:
141+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
142+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
143+
in this method.
144+
145+
Returns:
146+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
147+
client cert source to use.
148+
149+
Raises:
150+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
151+
"""
152+
return PredictionServiceClient.get_mtls_endpoint_and_cert_source(client_options) # type: ignore
153+
118154
@property
119155
def transport(self) -> PredictionServiceTransport:
120156
"""Returns the transport used by the client instance.

0 commit comments

Comments
 (0)