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

Commit 7d8c3bc

Browse files
feat: add api key support (#291)
* 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 b5b9c89 commit 7d8c3bc

File tree

18 files changed

+1508
-264
lines changed

18 files changed

+1508
-264
lines changed

google/cloud/datacatalog_v1/services/data_catalog/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
@@ -132,6 +132,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
132132

133133
from_service_account_json = from_service_account_file
134134

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

google/cloud/datacatalog_v1/services/data_catalog/client.py

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

347+
@classmethod
348+
def get_mtls_endpoint_and_cert_source(
349+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
350+
):
351+
"""Return the API endpoint and client cert source for mutual TLS.
352+
353+
The client cert source is determined in the following order:
354+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
355+
client cert source is None.
356+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
357+
default client cert source exists, use the default one; otherwise the client cert
358+
source is None.
359+
360+
The API endpoint is determined in the following order:
361+
(1) if `client_options.api_endpoint` if provided, use the provided one.
362+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
363+
default mTLS endpoint; if the environment variabel is "never", use the default API
364+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
365+
use the default API endpoint.
366+
367+
More details can be found at https://google.aip.dev/auth/4114.
368+
369+
Args:
370+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
371+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
372+
in this method.
373+
374+
Returns:
375+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
376+
client cert source to use.
377+
378+
Raises:
379+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
380+
"""
381+
if client_options is None:
382+
client_options = client_options_lib.ClientOptions()
383+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
384+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
385+
if use_client_cert not in ("true", "false"):
386+
raise ValueError(
387+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
388+
)
389+
if use_mtls_endpoint not in ("auto", "never", "always"):
390+
raise MutualTLSChannelError(
391+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
392+
)
393+
394+
# Figure out the client cert source to use.
395+
client_cert_source = None
396+
if use_client_cert == "true":
397+
if client_options.client_cert_source:
398+
client_cert_source = client_options.client_cert_source
399+
elif mtls.has_default_client_cert_source():
400+
client_cert_source = mtls.default_client_cert_source()
401+
402+
# Figure out which api endpoint to use.
403+
if client_options.api_endpoint is not None:
404+
api_endpoint = client_options.api_endpoint
405+
elif use_mtls_endpoint == "always" or (
406+
use_mtls_endpoint == "auto" and client_cert_source
407+
):
408+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
409+
else:
410+
api_endpoint = cls.DEFAULT_ENDPOINT
411+
412+
return api_endpoint, client_cert_source
413+
347414
def __init__(
348415
self,
349416
*,
@@ -394,57 +461,22 @@ def __init__(
394461
if client_options is None:
395462
client_options = client_options_lib.ClientOptions()
396463

397-
# Create SSL credentials for mutual TLS if needed.
398-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
399-
"true",
400-
"false",
401-
):
402-
raise ValueError(
403-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
404-
)
405-
use_client_cert = (
406-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
464+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
465+
client_options
407466
)
408467

409-
client_cert_source_func = None
410-
is_mtls = False
411-
if use_client_cert:
412-
if client_options.client_cert_source:
413-
is_mtls = True
414-
client_cert_source_func = client_options.client_cert_source
415-
else:
416-
is_mtls = mtls.has_default_client_cert_source()
417-
if is_mtls:
418-
client_cert_source_func = mtls.default_client_cert_source()
419-
else:
420-
client_cert_source_func = None
421-
422-
# Figure out which api endpoint to use.
423-
if client_options.api_endpoint is not None:
424-
api_endpoint = client_options.api_endpoint
425-
else:
426-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
427-
if use_mtls_env == "never":
428-
api_endpoint = self.DEFAULT_ENDPOINT
429-
elif use_mtls_env == "always":
430-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
431-
elif use_mtls_env == "auto":
432-
if is_mtls:
433-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
434-
else:
435-
api_endpoint = self.DEFAULT_ENDPOINT
436-
else:
437-
raise MutualTLSChannelError(
438-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
439-
"values: never, auto, always"
440-
)
468+
api_key_value = getattr(client_options, "api_key", None)
469+
if api_key_value and credentials:
470+
raise ValueError(
471+
"client_options.api_key and credentials are mutually exclusive"
472+
)
441473

442474
# Save or instantiate the transport.
443475
# Ordinarily, we provide the transport, but allowing a custom transport
444476
# instance provides an extensibility point for unusual situations.
445477
if isinstance(transport, DataCatalogTransport):
446478
# transport is a DataCatalogTransport instance.
447-
if credentials or client_options.credentials_file:
479+
if credentials or client_options.credentials_file or api_key_value:
448480
raise ValueError(
449481
"When providing a transport instance, "
450482
"provide its credentials directly."
@@ -456,6 +488,15 @@ def __init__(
456488
)
457489
self._transport = transport
458490
else:
491+
import google.auth._default # type: ignore
492+
493+
if api_key_value and hasattr(
494+
google.auth._default, "get_api_key_credentials"
495+
):
496+
credentials = google.auth._default.get_api_key_credentials(
497+
api_key_value
498+
)
499+
459500
Transport = type(self).get_transport_class(transport)
460501
self._transport = Transport(
461502
credentials=credentials,

google/cloud/datacatalog_v1/services/policy_tag_manager/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
@@ -117,6 +117,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
117117

118118
from_service_account_json = from_service_account_file
119119

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

0 commit comments

Comments
 (0)