Skip to content

Commit ee3a6c4

Browse files
feat: add api key support (#497)
* chore: upgrade gapic-generator-java, gax-java and gapic-generator-python PiperOrigin-RevId: 423842556 Source-Link: googleapis/googleapis@a616ca0 Source-Link: googleapis/googleapis-gen@29b938c Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMjliOTM4YzU4YzFlNTFkMDE5ZjJlZTUzOWQ1NWRjMGEzYzg2YTkwNSJ9 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 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 30071d9 commit ee3a6c4

File tree

9 files changed

+760
-132
lines changed

9 files changed

+760
-132
lines changed

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/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
@@ -131,6 +131,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
131131

132132
from_service_account_json = from_service_account_file
133133

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

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/client.py

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

302+
@classmethod
303+
def get_mtls_endpoint_and_cert_source(
304+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
305+
):
306+
"""Return the API endpoint and client cert source for mutual TLS.
307+
308+
The client cert source is determined in the following order:
309+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
310+
client cert source is None.
311+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
312+
default client cert source exists, use the default one; otherwise the client cert
313+
source is None.
314+
315+
The API endpoint is determined in the following order:
316+
(1) if `client_options.api_endpoint` if provided, use the provided one.
317+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
318+
default mTLS endpoint; if the environment variabel is "never", use the default API
319+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
320+
use the default API endpoint.
321+
322+
More details can be found at https://google.aip.dev/auth/4114.
323+
324+
Args:
325+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
326+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
327+
in this method.
328+
329+
Returns:
330+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
331+
client cert source to use.
332+
333+
Raises:
334+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
335+
"""
336+
if client_options is None:
337+
client_options = client_options_lib.ClientOptions()
338+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
339+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
340+
if use_client_cert not in ("true", "false"):
341+
raise ValueError(
342+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
343+
)
344+
if use_mtls_endpoint not in ("auto", "never", "always"):
345+
raise MutualTLSChannelError(
346+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
347+
)
348+
349+
# Figure out the client cert source to use.
350+
client_cert_source = None
351+
if use_client_cert == "true":
352+
if client_options.client_cert_source:
353+
client_cert_source = client_options.client_cert_source
354+
elif mtls.has_default_client_cert_source():
355+
client_cert_source = mtls.default_client_cert_source()
356+
357+
# Figure out which api endpoint to use.
358+
if client_options.api_endpoint is not None:
359+
api_endpoint = client_options.api_endpoint
360+
elif use_mtls_endpoint == "always" or (
361+
use_mtls_endpoint == "auto" and client_cert_source
362+
):
363+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
364+
else:
365+
api_endpoint = cls.DEFAULT_ENDPOINT
366+
367+
return api_endpoint, client_cert_source
368+
302369
def __init__(
303370
self,
304371
*,
@@ -349,57 +416,22 @@ def __init__(
349416
if client_options is None:
350417
client_options = client_options_lib.ClientOptions()
351418

352-
# Create SSL credentials for mutual TLS if needed.
353-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
354-
"true",
355-
"false",
356-
):
357-
raise ValueError(
358-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
359-
)
360-
use_client_cert = (
361-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
419+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
420+
client_options
362421
)
363422

364-
client_cert_source_func = None
365-
is_mtls = False
366-
if use_client_cert:
367-
if client_options.client_cert_source:
368-
is_mtls = True
369-
client_cert_source_func = client_options.client_cert_source
370-
else:
371-
is_mtls = mtls.has_default_client_cert_source()
372-
if is_mtls:
373-
client_cert_source_func = mtls.default_client_cert_source()
374-
else:
375-
client_cert_source_func = None
376-
377-
# Figure out which api endpoint to use.
378-
if client_options.api_endpoint is not None:
379-
api_endpoint = client_options.api_endpoint
380-
else:
381-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
382-
if use_mtls_env == "never":
383-
api_endpoint = self.DEFAULT_ENDPOINT
384-
elif use_mtls_env == "always":
385-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
386-
elif use_mtls_env == "auto":
387-
if is_mtls:
388-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
389-
else:
390-
api_endpoint = self.DEFAULT_ENDPOINT
391-
else:
392-
raise MutualTLSChannelError(
393-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
394-
"values: never, auto, always"
395-
)
423+
api_key_value = getattr(client_options, "api_key", None)
424+
if api_key_value and credentials:
425+
raise ValueError(
426+
"client_options.api_key and credentials are mutually exclusive"
427+
)
396428

397429
# Save or instantiate the transport.
398430
# Ordinarily, we provide the transport, but allowing a custom transport
399431
# instance provides an extensibility point for unusual situations.
400432
if isinstance(transport, BigtableInstanceAdminTransport):
401433
# transport is a BigtableInstanceAdminTransport instance.
402-
if credentials or client_options.credentials_file:
434+
if credentials or client_options.credentials_file or api_key_value:
403435
raise ValueError(
404436
"When providing a transport instance, "
405437
"provide its credentials directly."
@@ -411,6 +443,15 @@ def __init__(
411443
)
412444
self._transport = transport
413445
else:
446+
import google.auth._default # type: ignore
447+
448+
if api_key_value and hasattr(
449+
google.auth._default, "get_api_key_credentials"
450+
):
451+
credentials = google.auth._default.get_api_key_credentials(
452+
api_key_value
453+
)
454+
414455
Transport = type(self).get_transport_class(transport)
415456
self._transport = Transport(
416457
credentials=credentials,

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/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
@@ -133,6 +133,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
133133

134134
from_service_account_json = from_service_account_file
135135

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

0 commit comments

Comments
 (0)