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

Commit a7d1670

Browse files
feat: add always_use_jwt_access (#36)
* chore: remove all monolith Bazel deps chore: release gapic-generator-csharp v1.3.7 chore: release gapic-generator-go 0.20.5 chore: release gapic-generator-java 1.0.14 chore: release gapic-generator-php 1.0.1 chore: release gapic-generator-python 0.50.0 chore: update gapic-generator-ruby to the latest commit chore: release gapic-generator-typescript 1.5.0 Committer: @miraleung PiperOrigin-RevId: 380641501 Source-Link: googleapis/googleapis@076f7e9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/27e4c88b4048e5f56508d4e1aa417d60a3380892 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * fix: require google-api-core>=1.26.0 * fix: fix typo in setup.py Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Bu Sun Kim <busunkim@google.com> Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
1 parent 472c9a7 commit a7d1670

7 files changed

Lines changed: 37 additions & 120 deletions

File tree

.coveragerc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
branch = True
33

44
[report]
5-
fail_under = 100
65
show_missing = True
76
omit =
87
google/cloud/domains/__init__.py

google/cloud/domains_v1beta1/services/domains/transports/base.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from google.api_core import retry as retries # type: ignore
2626
from google.api_core import operations_v1 # type: ignore
2727
from google.auth import credentials as ga_credentials # type: ignore
28+
from google.oauth2 import service_account # type: ignore
2829

2930
from google.cloud.domains_v1beta1.types import domains
3031
from google.longrunning import operations_pb2 # type: ignore
@@ -45,8 +46,6 @@
4546
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4647
_GOOGLE_AUTH_VERSION = None
4748

48-
_API_CORE_VERSION = google.api_core.__version__
49-
5049

5150
class DomainsTransport(abc.ABC):
5251
"""Abstract transport class for Domains."""
@@ -64,6 +63,7 @@ def __init__(
6463
scopes: Optional[Sequence[str]] = None,
6564
quota_project_id: Optional[str] = None,
6665
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
66+
always_use_jwt_access: Optional[bool] = False,
6767
**kwargs,
6868
) -> None:
6969
"""Instantiate the transport.
@@ -87,6 +87,8 @@ def __init__(
8787
API requests. If ``None``, then default info will be used.
8888
Generally, you only need to set this if you're developing
8989
your own client library.
90+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91+
be used for service account credentials.
9092
"""
9193
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9294
if ":" not in host:
@@ -115,13 +117,20 @@ def __init__(
115117
**scopes_kwargs, quota_project_id=quota_project_id
116118
)
117119

120+
# If the credentials is service account credentials, then always try to use self signed JWT.
121+
if (
122+
always_use_jwt_access
123+
and isinstance(credentials, service_account.Credentials)
124+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
125+
):
126+
credentials = credentials.with_always_use_jwt_access(True)
127+
118128
# Save the credentials.
119129
self._credentials = credentials
120130

121-
# TODO(busunkim): These two class methods are in the base transport
131+
# TODO(busunkim): This method is in the base transport
122132
# to avoid duplicating code across the transport classes. These functions
123-
# should be deleted once the minimum required versions of google-api-core
124-
# and google-auth are increased.
133+
# should be deleted once the minimum required versions of google-auth is increased.
125134

126135
# TODO: Remove this function once google-auth >= 1.25.0 is required
127136
@classmethod
@@ -142,27 +151,6 @@ def _get_scopes_kwargs(
142151

143152
return scopes_kwargs
144153

145-
# TODO: Remove this function once google-api-core >= 1.26.0 is required
146-
@classmethod
147-
def _get_self_signed_jwt_kwargs(
148-
cls, host: str, scopes: Optional[Sequence[str]]
149-
) -> Dict[str, Union[Optional[Sequence[str]], str]]:
150-
"""Returns kwargs to pass to grpc_helpers.create_channel depending on the google-api-core version"""
151-
152-
self_signed_jwt_kwargs: Dict[str, Union[Optional[Sequence[str]], str]] = {}
153-
154-
if _API_CORE_VERSION and (
155-
packaging.version.parse(_API_CORE_VERSION)
156-
>= packaging.version.parse("1.26.0")
157-
):
158-
self_signed_jwt_kwargs["default_scopes"] = cls.AUTH_SCOPES
159-
self_signed_jwt_kwargs["scopes"] = scopes
160-
self_signed_jwt_kwargs["default_host"] = cls.DEFAULT_HOST
161-
else:
162-
self_signed_jwt_kwargs["scopes"] = scopes or cls.AUTH_SCOPES
163-
164-
return self_signed_jwt_kwargs
165-
166154
def _prep_wrapped_messages(self, client_info):
167155
# Precompute the wrapped methods.
168156
self._wrapped_methods = {

google/cloud/domains_v1beta1/services/domains/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def __init__(
153153
scopes=scopes,
154154
quota_project_id=quota_project_id,
155155
client_info=client_info,
156+
always_use_jwt_access=True,
156157
)
157158

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

211-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
212-
213212
return grpc_helpers.create_channel(
214213
host,
215214
credentials=credentials,
216215
credentials_file=credentials_file,
217216
quota_project_id=quota_project_id,
218-
**self_signed_jwt_kwargs,
217+
default_scopes=cls.AUTH_SCOPES,
218+
scopes=scopes,
219+
default_host=cls.DEFAULT_HOST,
219220
**kwargs,
220221
)
221222

google/cloud/domains_v1beta1/services/domains/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ def create_channel(
8181
aio.Channel: A gRPC AsyncIO channel object.
8282
"""
8383

84-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
85-
8684
return grpc_helpers_async.create_channel(
8785
host,
8886
credentials=credentials,
8987
credentials_file=credentials_file,
9088
quota_project_id=quota_project_id,
91-
**self_signed_jwt_kwargs,
89+
default_scopes=cls.AUTH_SCOPES,
90+
scopes=scopes,
91+
default_host=cls.DEFAULT_HOST,
9292
**kwargs,
9393
)
9494

@@ -199,6 +199,7 @@ def __init__(
199199
scopes=scopes,
200200
quota_project_id=quota_project_id,
201201
client_info=client_info,
202+
always_use_jwt_access=True,
202203
)
203204

204205
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
],
4444
include_package_data=True,
4545
install_requires=(
46-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
46+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4747
"proto-plus >= 1.4.0",
4848
"packaging >= 14.3",
4949
),

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#
2020
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
2121
# Then this file should have foo==1.14.0
22-
google-api-core==1.22.2
22+
google-api-core==1.26.0
2323
proto-plus==1.4.0
2424
packaging==14.3
2525
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 is required through google-api-core

tests/unit/gapic/domains_v1beta1/test_domains.py

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
from google.cloud.domains_v1beta1.services.domains import DomainsClient
3939
from google.cloud.domains_v1beta1.services.domains import pagers
4040
from google.cloud.domains_v1beta1.services.domains import transports
41-
from google.cloud.domains_v1beta1.services.domains.transports.base import (
42-
_API_CORE_VERSION,
43-
)
4441
from google.cloud.domains_v1beta1.services.domains.transports.base import (
4542
_GOOGLE_AUTH_VERSION,
4643
)
@@ -54,8 +51,9 @@
5451
import google.auth
5552

5653

57-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
58-
# - Delete all the api-core and auth "less than" test cases
54+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
55+
# through google-api-core:
56+
# - Delete the auth "less than" test cases
5957
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6058
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6159
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -66,16 +64,6 @@
6664
reason="This test requires google-auth >= 1.25.0",
6765
)
6866

69-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
70-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
71-
reason="This test requires google-api-core < 1.26.0",
72-
)
73-
74-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
75-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
76-
reason="This test requires google-api-core >= 1.26.0",
77-
)
78-
7967

8068
def client_cert_source_callback():
8169
return b"cert bytes", b"key bytes"
@@ -130,6 +118,16 @@ def test_domains_client_from_service_account_info(client_class):
130118
assert client.transport._host == "domains.googleapis.com:443"
131119

132120

121+
@pytest.mark.parametrize("client_class", [DomainsClient, DomainsAsyncClient,])
122+
def test_domains_client_service_account_always_use_jwt(client_class):
123+
with mock.patch.object(
124+
service_account.Credentials, "with_always_use_jwt_access", create=True
125+
) as use_jwt:
126+
creds = service_account.Credentials(None, None, None)
127+
client = client_class(credentials=creds)
128+
use_jwt.assert_called_with(True)
129+
130+
133131
@pytest.mark.parametrize("client_class", [DomainsClient, DomainsAsyncClient,])
134132
def test_domains_client_from_service_account_file(client_class):
135133
creds = ga_credentials.AnonymousCredentials()
@@ -3712,7 +3710,6 @@ def test_domains_transport_auth_adc_old_google_auth(transport_class):
37123710
(transports.DomainsGrpcAsyncIOTransport, grpc_helpers_async),
37133711
],
37143712
)
3715-
@requires_api_core_gte_1_26_0
37163713
def test_domains_transport_create_channel(transport_class, grpc_helpers):
37173714
# If credentials and host are not provided, the transport class should use
37183715
# ADC credentials.
@@ -3741,75 +3738,6 @@ def test_domains_transport_create_channel(transport_class, grpc_helpers):
37413738
)
37423739

37433740

3744-
@pytest.mark.parametrize(
3745-
"transport_class,grpc_helpers",
3746-
[
3747-
(transports.DomainsGrpcTransport, grpc_helpers),
3748-
(transports.DomainsGrpcAsyncIOTransport, grpc_helpers_async),
3749-
],
3750-
)
3751-
@requires_api_core_lt_1_26_0
3752-
def test_domains_transport_create_channel_old_api_core(transport_class, grpc_helpers):
3753-
# If credentials and host are not provided, the transport class should use
3754-
# ADC credentials.
3755-
with mock.patch.object(
3756-
google.auth, "default", autospec=True
3757-
) as adc, mock.patch.object(
3758-
grpc_helpers, "create_channel", autospec=True
3759-
) as create_channel:
3760-
creds = ga_credentials.AnonymousCredentials()
3761-
adc.return_value = (creds, None)
3762-
transport_class(quota_project_id="octopus")
3763-
3764-
create_channel.assert_called_with(
3765-
"domains.googleapis.com:443",
3766-
credentials=creds,
3767-
credentials_file=None,
3768-
quota_project_id="octopus",
3769-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
3770-
ssl_credentials=None,
3771-
options=[
3772-
("grpc.max_send_message_length", -1),
3773-
("grpc.max_receive_message_length", -1),
3774-
],
3775-
)
3776-
3777-
3778-
@pytest.mark.parametrize(
3779-
"transport_class,grpc_helpers",
3780-
[
3781-
(transports.DomainsGrpcTransport, grpc_helpers),
3782-
(transports.DomainsGrpcAsyncIOTransport, grpc_helpers_async),
3783-
],
3784-
)
3785-
@requires_api_core_lt_1_26_0
3786-
def test_domains_transport_create_channel_user_scopes(transport_class, grpc_helpers):
3787-
# If credentials and host are not provided, the transport class should use
3788-
# ADC credentials.
3789-
with mock.patch.object(
3790-
google.auth, "default", autospec=True
3791-
) as adc, mock.patch.object(
3792-
grpc_helpers, "create_channel", autospec=True
3793-
) as create_channel:
3794-
creds = ga_credentials.AnonymousCredentials()
3795-
adc.return_value = (creds, None)
3796-
3797-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3798-
3799-
create_channel.assert_called_with(
3800-
"domains.googleapis.com:443",
3801-
credentials=creds,
3802-
credentials_file=None,
3803-
quota_project_id="octopus",
3804-
scopes=["1", "2"],
3805-
ssl_credentials=None,
3806-
options=[
3807-
("grpc.max_send_message_length", -1),
3808-
("grpc.max_receive_message_length", -1),
3809-
],
3810-
)
3811-
3812-
38133741
@pytest.mark.parametrize(
38143742
"transport_class",
38153743
[transports.DomainsGrpcTransport, transports.DomainsGrpcAsyncIOTransport],

0 commit comments

Comments
 (0)