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

Commit a3938e3

Browse files
chore: upgrade gapic-generator-java, gax-java and gapic-generator-python (#159)
* 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 eca73af commit a3938e3

File tree

3 files changed

+252
-44
lines changed

3 files changed

+252
-44
lines changed

google/cloud/bigquery_connection_v1/services/connection_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
@@ -110,6 +110,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
110110

111111
from_service_account_json = from_service_account_file
112112

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

google/cloud/bigquery_connection_v1/services/connection_service/client.py

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

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

293-
# Create SSL credentials for mutual TLS if needed.
294-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
295-
"true",
296-
"false",
297-
):
298-
raise ValueError(
299-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
300-
)
301-
use_client_cert = (
302-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
360+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
361+
client_options
303362
)
304363

305-
client_cert_source_func = None
306-
is_mtls = False
307-
if use_client_cert:
308-
if client_options.client_cert_source:
309-
is_mtls = True
310-
client_cert_source_func = client_options.client_cert_source
311-
else:
312-
is_mtls = mtls.has_default_client_cert_source()
313-
if is_mtls:
314-
client_cert_source_func = mtls.default_client_cert_source()
315-
else:
316-
client_cert_source_func = None
317-
318-
# Figure out which api endpoint to use.
319-
if client_options.api_endpoint is not None:
320-
api_endpoint = client_options.api_endpoint
321-
else:
322-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
323-
if use_mtls_env == "never":
324-
api_endpoint = self.DEFAULT_ENDPOINT
325-
elif use_mtls_env == "always":
326-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
327-
elif use_mtls_env == "auto":
328-
if is_mtls:
329-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
330-
else:
331-
api_endpoint = self.DEFAULT_ENDPOINT
332-
else:
333-
raise MutualTLSChannelError(
334-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
335-
"values: never, auto, always"
336-
)
364+
api_key_value = getattr(client_options, "api_key", None)
365+
if api_key_value and credentials:
366+
raise ValueError(
367+
"client_options.api_key and credentials are mutually exclusive"
368+
)
337369

338370
# Save or instantiate the transport.
339371
# Ordinarily, we provide the transport, but allowing a custom transport
340372
# instance provides an extensibility point for unusual situations.
341373
if isinstance(transport, ConnectionServiceTransport):
342374
# transport is a ConnectionServiceTransport instance.
343-
if credentials or client_options.credentials_file:
375+
if credentials or client_options.credentials_file or api_key_value:
344376
raise ValueError(
345377
"When providing a transport instance, "
346378
"provide its credentials directly."
@@ -352,6 +384,15 @@ def __init__(
352384
)
353385
self._transport = transport
354386
else:
387+
import google.auth._default # type: ignore
388+
389+
if api_key_value and hasattr(
390+
google.auth._default, "get_api_key_credentials"
391+
):
392+
credentials = google.auth._default.get_api_key_credentials(
393+
api_key_value
394+
)
395+
355396
Transport = type(self).get_transport_class(transport)
356397
self._transport = Transport(
357398
credentials=credentials,

tests/unit/gapic/bigquery_connection_v1/test_connection_service.py

+131
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,87 @@ def test_connection_service_client_mtls_env_auto(
416416
)
417417

418418

419+
@pytest.mark.parametrize(
420+
"client_class", [ConnectionServiceClient, ConnectionServiceAsyncClient]
421+
)
422+
@mock.patch.object(
423+
ConnectionServiceClient,
424+
"DEFAULT_ENDPOINT",
425+
modify_default_endpoint(ConnectionServiceClient),
426+
)
427+
@mock.patch.object(
428+
ConnectionServiceAsyncClient,
429+
"DEFAULT_ENDPOINT",
430+
modify_default_endpoint(ConnectionServiceAsyncClient),
431+
)
432+
def test_connection_service_client_get_mtls_endpoint_and_cert_source(client_class):
433+
mock_client_cert_source = mock.Mock()
434+
435+
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
436+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
437+
mock_api_endpoint = "foo"
438+
options = client_options.ClientOptions(
439+
client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint
440+
)
441+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(
442+
options
443+
)
444+
assert api_endpoint == mock_api_endpoint
445+
assert cert_source == mock_client_cert_source
446+
447+
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
448+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
449+
mock_client_cert_source = mock.Mock()
450+
mock_api_endpoint = "foo"
451+
options = client_options.ClientOptions(
452+
client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint
453+
)
454+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(
455+
options
456+
)
457+
assert api_endpoint == mock_api_endpoint
458+
assert cert_source is None
459+
460+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
461+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
462+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
463+
assert api_endpoint == client_class.DEFAULT_ENDPOINT
464+
assert cert_source is None
465+
466+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
467+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
468+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
469+
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
470+
assert cert_source is None
471+
472+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
473+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
474+
with mock.patch(
475+
"google.auth.transport.mtls.has_default_client_cert_source",
476+
return_value=False,
477+
):
478+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
479+
assert api_endpoint == client_class.DEFAULT_ENDPOINT
480+
assert cert_source is None
481+
482+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
483+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
484+
with mock.patch(
485+
"google.auth.transport.mtls.has_default_client_cert_source",
486+
return_value=True,
487+
):
488+
with mock.patch(
489+
"google.auth.transport.mtls.default_client_cert_source",
490+
return_value=mock_client_cert_source,
491+
):
492+
(
493+
api_endpoint,
494+
cert_source,
495+
) = client_class.get_mtls_endpoint_and_cert_source()
496+
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
497+
assert cert_source == mock_client_cert_source
498+
499+
419500
@pytest.mark.parametrize(
420501
"client_class,transport_class,transport_name",
421502
[
@@ -2523,6 +2604,23 @@ def test_credentials_transport_error():
25232604
transport=transport,
25242605
)
25252606

2607+
# It is an error to provide an api_key and a transport instance.
2608+
transport = transports.ConnectionServiceGrpcTransport(
2609+
credentials=ga_credentials.AnonymousCredentials(),
2610+
)
2611+
options = client_options.ClientOptions()
2612+
options.api_key = "api_key"
2613+
with pytest.raises(ValueError):
2614+
client = ConnectionServiceClient(client_options=options, transport=transport,)
2615+
2616+
# It is an error to provide an api_key and a credential.
2617+
options = mock.Mock()
2618+
options.api_key = "api_key"
2619+
with pytest.raises(ValueError):
2620+
client = ConnectionServiceClient(
2621+
client_options=options, credentials=ga_credentials.AnonymousCredentials()
2622+
)
2623+
25262624
# It is an error to provide scopes and a transport instance.
25272625
transport = transports.ConnectionServiceGrpcTransport(
25282626
credentials=ga_credentials.AnonymousCredentials(),
@@ -3101,3 +3199,36 @@ def test_client_ctx():
31013199
with client:
31023200
pass
31033201
close.assert_called()
3202+
3203+
3204+
@pytest.mark.parametrize(
3205+
"client_class,transport_class",
3206+
[
3207+
(ConnectionServiceClient, transports.ConnectionServiceGrpcTransport),
3208+
(
3209+
ConnectionServiceAsyncClient,
3210+
transports.ConnectionServiceGrpcAsyncIOTransport,
3211+
),
3212+
],
3213+
)
3214+
def test_api_key_credentials(client_class, transport_class):
3215+
with mock.patch.object(
3216+
google.auth._default, "get_api_key_credentials", create=True
3217+
) as get_api_key_credentials:
3218+
mock_cred = mock.Mock()
3219+
get_api_key_credentials.return_value = mock_cred
3220+
options = client_options.ClientOptions()
3221+
options.api_key = "api_key"
3222+
with mock.patch.object(transport_class, "__init__") as patched:
3223+
patched.return_value = None
3224+
client = client_class(client_options=options)
3225+
patched.assert_called_once_with(
3226+
credentials=mock_cred,
3227+
credentials_file=None,
3228+
host=client.DEFAULT_ENDPOINT,
3229+
scopes=None,
3230+
client_cert_source_for_mtls=None,
3231+
quota_project_id=None,
3232+
client_info=transports.base.DEFAULT_CLIENT_INFO,
3233+
always_use_jwt_access=True,
3234+
)

0 commit comments

Comments
 (0)