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

Commit 0d6bebd

Browse files
feat: add api key support (#468)
* 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 7e01f1d commit 0d6bebd

File tree

90 files changed

+7398
-1318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+7398
-1318
lines changed

google/cloud/dialogflow_v2/services/agents/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
@@ -102,6 +102,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
102102

103103
from_service_account_json = from_service_account_file
104104

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

google/cloud/dialogflow_v2/services/agents/client.py

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

236+
@classmethod
237+
def get_mtls_endpoint_and_cert_source(
238+
cls, client_options: Optional[client_options_lib.ClientOptions] = None
239+
):
240+
"""Return the API endpoint and client cert source for mutual TLS.
241+
242+
The client cert source is determined in the following order:
243+
(1) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not "true", the
244+
client cert source is None.
245+
(2) if `client_options.client_cert_source` is provided, use the provided one; if the
246+
default client cert source exists, use the default one; otherwise the client cert
247+
source is None.
248+
249+
The API endpoint is determined in the following order:
250+
(1) if `client_options.api_endpoint` if provided, use the provided one.
251+
(2) if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is "always", use the
252+
default mTLS endpoint; if the environment variabel is "never", use the default API
253+
endpoint; otherwise if client cert source exists, use the default mTLS endpoint, otherwise
254+
use the default API endpoint.
255+
256+
More details can be found at https://google.aip.dev/auth/4114.
257+
258+
Args:
259+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
260+
client. Only the `api_endpoint` and `client_cert_source` properties may be used
261+
in this method.
262+
263+
Returns:
264+
Tuple[str, Callable[[], Tuple[bytes, bytes]]]: returns the API endpoint and the
265+
client cert source to use.
266+
267+
Raises:
268+
google.auth.exceptions.MutualTLSChannelError: If any errors happen.
269+
"""
270+
if client_options is None:
271+
client_options = client_options_lib.ClientOptions()
272+
use_client_cert = os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
273+
use_mtls_endpoint = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
274+
if use_client_cert not in ("true", "false"):
275+
raise ValueError(
276+
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
277+
)
278+
if use_mtls_endpoint not in ("auto", "never", "always"):
279+
raise MutualTLSChannelError(
280+
"Environment variable `GOOGLE_API_USE_MTLS_ENDPOINT` must be `never`, `auto` or `always`"
281+
)
282+
283+
# Figure out the client cert source to use.
284+
client_cert_source = None
285+
if use_client_cert == "true":
286+
if client_options.client_cert_source:
287+
client_cert_source = client_options.client_cert_source
288+
elif mtls.has_default_client_cert_source():
289+
client_cert_source = mtls.default_client_cert_source()
290+
291+
# Figure out which api endpoint to use.
292+
if client_options.api_endpoint is not None:
293+
api_endpoint = client_options.api_endpoint
294+
elif use_mtls_endpoint == "always" or (
295+
use_mtls_endpoint == "auto" and client_cert_source
296+
):
297+
api_endpoint = cls.DEFAULT_MTLS_ENDPOINT
298+
else:
299+
api_endpoint = cls.DEFAULT_ENDPOINT
300+
301+
return api_endpoint, client_cert_source
302+
236303
def __init__(
237304
self,
238305
*,
@@ -283,57 +350,22 @@ def __init__(
283350
if client_options is None:
284351
client_options = client_options_lib.ClientOptions()
285352

286-
# Create SSL credentials for mutual TLS if needed.
287-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
288-
"true",
289-
"false",
290-
):
291-
raise ValueError(
292-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
293-
)
294-
use_client_cert = (
295-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
353+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
354+
client_options
296355
)
297356

298-
client_cert_source_func = None
299-
is_mtls = False
300-
if use_client_cert:
301-
if client_options.client_cert_source:
302-
is_mtls = True
303-
client_cert_source_func = client_options.client_cert_source
304-
else:
305-
is_mtls = mtls.has_default_client_cert_source()
306-
if is_mtls:
307-
client_cert_source_func = mtls.default_client_cert_source()
308-
else:
309-
client_cert_source_func = None
310-
311-
# Figure out which api endpoint to use.
312-
if client_options.api_endpoint is not None:
313-
api_endpoint = client_options.api_endpoint
314-
else:
315-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
316-
if use_mtls_env == "never":
317-
api_endpoint = self.DEFAULT_ENDPOINT
318-
elif use_mtls_env == "always":
319-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
320-
elif use_mtls_env == "auto":
321-
if is_mtls:
322-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
323-
else:
324-
api_endpoint = self.DEFAULT_ENDPOINT
325-
else:
326-
raise MutualTLSChannelError(
327-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
328-
"values: never, auto, always"
329-
)
357+
api_key_value = getattr(client_options, "api_key", None)
358+
if api_key_value and credentials:
359+
raise ValueError(
360+
"client_options.api_key and credentials are mutually exclusive"
361+
)
330362

331363
# Save or instantiate the transport.
332364
# Ordinarily, we provide the transport, but allowing a custom transport
333365
# instance provides an extensibility point for unusual situations.
334366
if isinstance(transport, AgentsTransport):
335367
# transport is a AgentsTransport instance.
336-
if credentials or client_options.credentials_file:
368+
if credentials or client_options.credentials_file or api_key_value:
337369
raise ValueError(
338370
"When providing a transport instance, "
339371
"provide its credentials directly."
@@ -345,6 +377,15 @@ def __init__(
345377
)
346378
self._transport = transport
347379
else:
380+
import google.auth._default # type: ignore
381+
382+
if api_key_value and hasattr(
383+
google.auth._default, "get_api_key_credentials"
384+
):
385+
credentials = google.auth._default.get_api_key_credentials(
386+
api_key_value
387+
)
388+
348389
Transport = type(self).get_transport_class(transport)
349390
self._transport = Transport(
350391
credentials=credentials,

google/cloud/dialogflow_v2/services/answer_records/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
@@ -112,6 +112,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
112112

113113
from_service_account_json = from_service_account_file
114114

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

0 commit comments

Comments
 (0)