Skip to content

Commit e4a24e9

Browse files
feat: add api key support (#91)
* 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 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent a872af7 commit e4a24e9

File tree

3 files changed

+249
-44
lines changed

3 files changed

+249
-44
lines changed

packages/google-cloud-service-management/google/cloud/servicemanagement_v1/services/service_manager/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
import warnings
2222

@@ -134,6 +134,42 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
134134

135135
from_service_account_json = from_service_account_file
136136

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

packages/google-cloud-service-management/google/cloud/servicemanagement_v1/services/service_manager/client.py

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

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

301-
# Create SSL credentials for mutual TLS if needed.
302-
if os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") not in (
303-
"true",
304-
"false",
305-
):
306-
raise ValueError(
307-
"Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`"
308-
)
309-
use_client_cert = (
310-
os.getenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false") == "true"
368+
api_endpoint, client_cert_source_func = self.get_mtls_endpoint_and_cert_source(
369+
client_options
311370
)
312371

313-
client_cert_source_func = None
314-
is_mtls = False
315-
if use_client_cert:
316-
if client_options.client_cert_source:
317-
is_mtls = True
318-
client_cert_source_func = client_options.client_cert_source
319-
else:
320-
is_mtls = mtls.has_default_client_cert_source()
321-
if is_mtls:
322-
client_cert_source_func = mtls.default_client_cert_source()
323-
else:
324-
client_cert_source_func = None
325-
326-
# Figure out which api endpoint to use.
327-
if client_options.api_endpoint is not None:
328-
api_endpoint = client_options.api_endpoint
329-
else:
330-
use_mtls_env = os.getenv("GOOGLE_API_USE_MTLS_ENDPOINT", "auto")
331-
if use_mtls_env == "never":
332-
api_endpoint = self.DEFAULT_ENDPOINT
333-
elif use_mtls_env == "always":
334-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
335-
elif use_mtls_env == "auto":
336-
if is_mtls:
337-
api_endpoint = self.DEFAULT_MTLS_ENDPOINT
338-
else:
339-
api_endpoint = self.DEFAULT_ENDPOINT
340-
else:
341-
raise MutualTLSChannelError(
342-
"Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted "
343-
"values: never, auto, always"
344-
)
372+
api_key_value = getattr(client_options, "api_key", None)
373+
if api_key_value and credentials:
374+
raise ValueError(
375+
"client_options.api_key and credentials are mutually exclusive"
376+
)
345377

346378
# Save or instantiate the transport.
347379
# Ordinarily, we provide the transport, but allowing a custom transport
348380
# instance provides an extensibility point for unusual situations.
349381
if isinstance(transport, ServiceManagerTransport):
350382
# transport is a ServiceManagerTransport instance.
351-
if credentials or client_options.credentials_file:
383+
if credentials or client_options.credentials_file or api_key_value:
352384
raise ValueError(
353385
"When providing a transport instance, "
354386
"provide its credentials directly."
@@ -360,6 +392,15 @@ def __init__(
360392
)
361393
self._transport = transport
362394
else:
395+
import google.auth._default # type: ignore
396+
397+
if api_key_value and hasattr(
398+
google.auth._default, "get_api_key_credentials"
399+
):
400+
credentials = google.auth._default.get_api_key_credentials(
401+
api_key_value
402+
)
403+
363404
Transport = type(self).get_transport_class(transport)
364405
self._transport = Transport(
365406
credentials=credentials,

packages/google-cloud-service-management/tests/unit/gapic/servicemanagement_v1/test_service_manager.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,87 @@ def test_service_manager_client_mtls_env_auto(
431431
)
432432

433433

434+
@pytest.mark.parametrize(
435+
"client_class", [ServiceManagerClient, ServiceManagerAsyncClient]
436+
)
437+
@mock.patch.object(
438+
ServiceManagerClient,
439+
"DEFAULT_ENDPOINT",
440+
modify_default_endpoint(ServiceManagerClient),
441+
)
442+
@mock.patch.object(
443+
ServiceManagerAsyncClient,
444+
"DEFAULT_ENDPOINT",
445+
modify_default_endpoint(ServiceManagerAsyncClient),
446+
)
447+
def test_service_manager_client_get_mtls_endpoint_and_cert_source(client_class):
448+
mock_client_cert_source = mock.Mock()
449+
450+
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
451+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
452+
mock_api_endpoint = "foo"
453+
options = client_options.ClientOptions(
454+
client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint
455+
)
456+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(
457+
options
458+
)
459+
assert api_endpoint == mock_api_endpoint
460+
assert cert_source == mock_client_cert_source
461+
462+
# Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
463+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
464+
mock_client_cert_source = mock.Mock()
465+
mock_api_endpoint = "foo"
466+
options = client_options.ClientOptions(
467+
client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint
468+
)
469+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(
470+
options
471+
)
472+
assert api_endpoint == mock_api_endpoint
473+
assert cert_source is None
474+
475+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
476+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
477+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
478+
assert api_endpoint == client_class.DEFAULT_ENDPOINT
479+
assert cert_source is None
480+
481+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
482+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
483+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
484+
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
485+
assert cert_source is None
486+
487+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
488+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
489+
with mock.patch(
490+
"google.auth.transport.mtls.has_default_client_cert_source",
491+
return_value=False,
492+
):
493+
api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
494+
assert api_endpoint == client_class.DEFAULT_ENDPOINT
495+
assert cert_source is None
496+
497+
# Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
498+
with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
499+
with mock.patch(
500+
"google.auth.transport.mtls.has_default_client_cert_source",
501+
return_value=True,
502+
):
503+
with mock.patch(
504+
"google.auth.transport.mtls.default_client_cert_source",
505+
return_value=mock_client_cert_source,
506+
):
507+
(
508+
api_endpoint,
509+
cert_source,
510+
) = client_class.get_mtls_endpoint_and_cert_source()
511+
assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
512+
assert cert_source == mock_client_cert_source
513+
514+
434515
@pytest.mark.parametrize(
435516
"client_class,transport_class,transport_name",
436517
[
@@ -3542,6 +3623,23 @@ def test_credentials_transport_error():
35423623
transport=transport,
35433624
)
35443625

3626+
# It is an error to provide an api_key and a transport instance.
3627+
transport = transports.ServiceManagerGrpcTransport(
3628+
credentials=ga_credentials.AnonymousCredentials(),
3629+
)
3630+
options = client_options.ClientOptions()
3631+
options.api_key = "api_key"
3632+
with pytest.raises(ValueError):
3633+
client = ServiceManagerClient(client_options=options, transport=transport,)
3634+
3635+
# It is an error to provide an api_key and a credential.
3636+
options = mock.Mock()
3637+
options.api_key = "api_key"
3638+
with pytest.raises(ValueError):
3639+
client = ServiceManagerClient(
3640+
client_options=options, credentials=ga_credentials.AnonymousCredentials()
3641+
)
3642+
35453643
# It is an error to provide scopes and a transport instance.
35463644
transport = transports.ServiceManagerGrpcTransport(
35473645
credentials=ga_credentials.AnonymousCredentials(),
@@ -4142,3 +4240,33 @@ def test_client_ctx():
41424240
with client:
41434241
pass
41444242
close.assert_called()
4243+
4244+
4245+
@pytest.mark.parametrize(
4246+
"client_class,transport_class",
4247+
[
4248+
(ServiceManagerClient, transports.ServiceManagerGrpcTransport),
4249+
(ServiceManagerAsyncClient, transports.ServiceManagerGrpcAsyncIOTransport),
4250+
],
4251+
)
4252+
def test_api_key_credentials(client_class, transport_class):
4253+
with mock.patch.object(
4254+
google.auth._default, "get_api_key_credentials", create=True
4255+
) as get_api_key_credentials:
4256+
mock_cred = mock.Mock()
4257+
get_api_key_credentials.return_value = mock_cred
4258+
options = client_options.ClientOptions()
4259+
options.api_key = "api_key"
4260+
with mock.patch.object(transport_class, "__init__") as patched:
4261+
patched.return_value = None
4262+
client = client_class(client_options=options)
4263+
patched.assert_called_once_with(
4264+
credentials=mock_cred,
4265+
credentials_file=None,
4266+
host=client.DEFAULT_ENDPOINT,
4267+
scopes=None,
4268+
client_cert_source_for_mtls=None,
4269+
quota_project_id=None,
4270+
client_info=transports.base.DEFAULT_CLIENT_INFO,
4271+
always_use_jwt_access=True,
4272+
)

0 commit comments

Comments
 (0)