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

Commit e4b39f0

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 07b3890 commit e4b39f0

File tree

15 files changed

+1276
-220
lines changed

15 files changed

+1276
-220
lines changed

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/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
@@ -129,6 +129,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
129129

130130
from_service_account_json = from_service_account_file
131131

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

google/cloud/binaryauthorization_v1/services/binauthz_management_service_v1/client.py

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

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

312-
# Create SSL credentials for mutual TLS if needed.
313-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
314-
"true",
315-
"false",
316-
):
317-
raise ValueError(
318-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
319-
)
320-
use_client_cert = (
321-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
379+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
380+
client_options
322381
)
323382

324-
client_cert_source_func = None
325-
is_mtls = False
326-
if use_client_cert:
327-
if client_options.client_cert_source:
328-
is_mtls = True
329-
client_cert_source_func = client_options.client_cert_source
330-
else:
331-
is_mtls = mtls.has_default_client_cert_source()
332-
if is_mtls:
333-
client_cert_source_func = mtls.default_client_cert_source()
334-
else:
335-
client_cert_source_func = None
336-
337-
# Figure out which api endpoint to use.
338-
if client_options.api_endpoint is not None:
339-
api_endpoint = client_options.api_endpoint
340-
else:
341-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
342-
if use_mtls_env == "never":
343-
api_endpoint = self.DEFAULT_ENDPOINT
344-
elif use_mtls_env == "always":
345-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
346-
elif use_mtls_env == "auto":
347-
if is_mtls:
348-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
349-
else:
350-
api_endpoint = self.DEFAULT_ENDPOINT
351-
else:
352-
raise MutualTLSChannelError(
353-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
354-
"values: never, auto, always"
355-
)
383+
api_key_value = getattr(client_options, "api_key", None)
384+
if api_key_value and credentials:
385+
raise ValueError(
386+
"client_options.api_key and credentials are mutually exclusive"
387+
)
356388

357389
# Save or instantiate the transport.
358390
# Ordinarily, we provide the transport, but allowing a custom transport
359391
# instance provides an extensibility point for unusual situations.
360392
if isinstance(transport, BinauthzManagementServiceV1Transport):
361393
# transport is a BinauthzManagementServiceV1Transport instance.
362-
if credentials or client_options.credentials_file:
394+
if credentials or client_options.credentials_file or api_key_value:
363395
raise ValueError(
364396
"When providing a transport instance, "
365397
"provide its credentials directly."
@@ -371,6 +403,15 @@ def __init__(
371403
)
372404
self._transport = transport
373405
else:
406+
import google.auth._default # type: ignore
407+
408+
if api_key_value and hasattr(
409+
google.auth._default, "get_api_key_credentials"
410+
):
411+
credentials = google.auth._default.get_api_key_credentials(
412+
api_key_value
413+
)
414+
374415
Transport = type(self).get_transport_class(transport)
375416
self._transport = Transport(
376417
credentials=credentials,

google/cloud/binaryauthorization_v1/services/system_policy_v1/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
@@ -107,6 +107,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
107107

108108
from_service_account_json = from_service_account_file
109109

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

0 commit comments

Comments
 (0)