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

Commit e2f7a2c

Browse files
feat: add api key support (#110)
* 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 2cea60a commit e2f7a2c

File tree

6 files changed

+502
-88
lines changed

6 files changed

+502
-88
lines changed

google/cloud/gkehub_v1/services/gke_hub/async_client.py

+37-1
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
@@ -119,6 +119,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
119119

120120
from_service_account_json = from_service_account_file
121121

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

google/cloud/gkehub_v1/services/gke_hub/client.py

+84-43
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,73 @@ def parse_common_location_path(path: str) -> Dict[str, str]:
269269
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
270270
return m.groupdict() if m else {}
271271

272+
@classmethod
273+
def get_mtls_endpoint_and_cert_source(
274+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
275+
):
276+
"""Return the API endpoint and client cert source for mutual TLS.
277+
278+
The client cert source is determined in the following order:
279+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
280+
client cert source is None.
281+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
282+
default client cert source exists, use the default one; otherwise the client cert
283+
source is None.
284+
285+
The API endpoint is determined in the following order:
286+
(1) if `client_options.api_endpoint` if provided, use the provided one.
287+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
288+
default mTLS endpoint; if the environment variabel is "never", use the default API
289+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
290+
use the default API endpoint.
291+
292+
More details can be found at https://google.aip.dev/auth/4114.
293+
294+
Args:
295+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
296+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
297+
in this method.
298+
299+
Returns:
300+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
301+
client cert source to use.
302+
303+
Raises:
304+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
305+
"""
306+
if client_options is None:
307+
client_options = client_options_lib.ClientOptions()
308+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
309+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
310+
if use_client_cert not in ("true", "false"):
311+
raise ValueError(
312+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
313+
)
314+
if use_mtls_endpoint not in ("auto", "never", "always"):
315+
raise MutualTLSChannelError(
316+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
317+
)
318+
319+
# Figure out the client cert source to use.
320+
client_cert_source = None
321+
if use_client_cert == "true":
322+
if client_options.client_cert_source:
323+
client_cert_source = client_options.client_cert_source
324+
elif mtls.has_default_client_cert_source():
325+
client_cert_source = mtls.default_client_cert_source()
326+
327+
# Figure out which api endpoint to use.
328+
if client_options.api_endpoint is not None:
329+
api_endpoint = client_options.api_endpoint
330+
elif use_mtls_endpoint == "always" or (
331+
use_mtls_endpoint == "auto" and client_cert_source
332+
):
333+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
334+
else:
335+
api_endpoint = cls.DEFAULT_ENDPOINT
336+
337+
return api_endpoint, client_cert_source
338+
272339
def __init__(
273340
self,
274341
*,
@@ -319,57 +386,22 @@ def __init__(
319386
if client_options is None:
320387
client_options = client_options_lib.ClientOptions()
321388

322-
# Create SSL credentials for mutual TLS if needed.
323-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
324-
"true",
325-
"false",
326-
):
327-
raise ValueError(
328-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
329-
)
330-
use_client_cert = (
331-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
389+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
390+
client_options
332391
)
333392

334-
client_cert_source_func = None
335-
is_mtls = False
336-
if use_client_cert:
337-
if client_options.client_cert_source:
338-
is_mtls = True
339-
client_cert_source_func = client_options.client_cert_source
340-
else:
341-
is_mtls = mtls.has_default_client_cert_source()
342-
if is_mtls:
343-
client_cert_source_func = mtls.default_client_cert_source()
344-
else:
345-
client_cert_source_func = None
346-
347-
# Figure out which api endpoint to use.
348-
if client_options.api_endpoint is not None:
349-
api_endpoint = client_options.api_endpoint
350-
else:
351-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
352-
if use_mtls_env == "never":
353-
api_endpoint = self.DEFAULT_ENDPOINT
354-
elif use_mtls_env == "always":
355-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
356-
elif use_mtls_env == "auto":
357-
if is_mtls:
358-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
359-
else:
360-
api_endpoint = self.DEFAULT_ENDPOINT
361-
else:
362-
raise MutualTLSChannelError(
363-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
364-
"values: never, auto, always"
365-
)
393+
api_key_value = getattr(client_options, "api_key", None)
394+
if api_key_value and credentials:
395+
raise ValueError(
396+
"client_options.api_key and credentials are mutually exclusive"
397+
)
366398

367399
# Save or instantiate the transport.
368400
# Ordinarily, we provide the transport, but allowing a custom transport
369401
# instance provides an extensibility point for unusual situations.
370402
if isinstance(transport, GkeHubTransport):
371403
# transport is a GkeHubTransport instance.
372-
if credentials or client_options.credentials_file:
404+
if credentials or client_options.credentials_file or api_key_value:
373405
raise ValueError(
374406
"When providing a transport instance, "
375407
"provide its credentials directly."
@@ -381,6 +413,15 @@ def __init__(
381413
)
382414
self._transport = transport
383415
else:
416+
import google.auth._default # type: ignore
417+
418+
if api_key_value and hasattr(
419+
google.auth._default, "get_api_key_credentials"
420+
):
421+
credentials = google.auth._default.get_api_key_credentials(
422+
api_key_value
423+
)
424+
384425
Transport = type(self).get_transport_class(transport)
385426
self._transport = Transport(
386427
credentials=credentials,

google/cloud/gkehub_v1beta1/services/gke_hub_membership_service/async_client.py

+37-1
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
@@ -127,6 +127,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
127127

128128
from_service_account_json = from_service_account_file
129129

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

0 commit comments

Comments
 (0)