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

Commit dec3204

Browse files
feat: add always_use_jwt_access (#48)
... 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
1 parent 39a0694 commit dec3204

File tree

7 files changed

+37
-124
lines changed

7 files changed

+37
-124
lines changed

.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/networkconnectivity/__init__.py

google/cloud/networkconnectivity_v1alpha1/services/hub_service/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.networkconnectivity_v1alpha1.types import hub
3031
from google.cloud.networkconnectivity_v1alpha1.types import hub as gcn_hub
@@ -48,8 +49,6 @@
4849
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4950
_GOOGLE_AUTH_VERSION = None
5051

51-
_API_CORE_VERSION = google.api_core.__version__
52-
5352

5453
class HubServiceTransport(abc.ABC):
5554
"""Abstract transport class for HubService."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

123+
# If the credentials is service account credentials, then always try to use self signed JWT.
124+
if (
125+
always_use_jwt_access
126+
and isinstance(credentials, service_account.Credentials)
127+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
128+
):
129+
credentials = credentials.with_always_use_jwt_access(True)
130+
121131
# Save the credentials.
122132
self._credentials = credentials
123133

124-
# TODO(busunkim): These two class methods are in the base transport
134+
# TODO(busunkim): This method is in the base transport
125135
# to avoid duplicating code across the transport classes. These functions
126-
# should be deleted once the minimum required versions of google-api-core
127-
# and google-auth are increased.
136+
# should be deleted once the minimum required versions of google-auth is increased.
128137

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

google/cloud/networkconnectivity_v1alpha1/services/hub_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def __init__(
156156
scopes=scopes,
157157
quota_project_id=quota_project_id,
158158
client_info=client_info,
159+
always_use_jwt_access=True,
159160
)
160161

161162
if not self._grpc_channel:
@@ -211,14 +212,14 @@ def create_channel(
211212
and ``credentials_file`` are passed.
212213
"""
213214

214-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
215-
216215
return grpc_helpers.create_channel(
217216
host,
218217
credentials=credentials,
219218
credentials_file=credentials_file,
220219
quota_project_id=quota_project_id,
221-
**self_signed_jwt_kwargs,
220+
default_scopes=cls.AUTH_SCOPES,
221+
scopes=scopes,
222+
default_host=cls.DEFAULT_HOST,
222223
**kwargs,
223224
)
224225

google/cloud/networkconnectivity_v1alpha1/services/hub_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ def create_channel(
8484
aio.Channel: A gRPC AsyncIO channel object.
8585
"""
8686

87-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
88-
8987
return grpc_helpers_async.create_channel(
9088
host,
9189
credentials=credentials,
9290
credentials_file=credentials_file,
9391
quota_project_id=quota_project_id,
94-
**self_signed_jwt_kwargs,
92+
default_scopes=cls.AUTH_SCOPES,
93+
scopes=scopes,
94+
default_host=cls.DEFAULT_HOST,
9595
**kwargs,
9696
)
9797

@@ -202,6 +202,7 @@ def __init__(
202202
scopes=scopes,
203203
quota_project_id=quota_project_id,
204204
client_info=client_info,
205+
always_use_jwt_access=True,
205206
)
206207

207208
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
platforms="Posix; MacOS X; Windows",
4545
include_package_data=True,
4646
install_requires=(
47-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
47+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4848
"proto-plus >= 1.4.0",
4949
"packaging >= 14.3",
5050
),

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
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

tests/unit/gapic/networkconnectivity_v1alpha1/test_hub_service.py

Lines changed: 13 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
)
4343
from google.cloud.networkconnectivity_v1alpha1.services.hub_service import pagers
4444
from google.cloud.networkconnectivity_v1alpha1.services.hub_service import transports
45-
from google.cloud.networkconnectivity_v1alpha1.services.hub_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.networkconnectivity_v1alpha1.services.hub_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -58,8 +55,9 @@
5855
import google.auth
5956

6057

61-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
62-
# - Delete all the api-core and auth "less than" test cases
58+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
59+
# through google-api-core:
60+
# - Delete the auth "less than" test cases
6361
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6462
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6563
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -70,16 +68,6 @@
7068
reason="This test requires google-auth >= 1.25.0",
7169
)
7270

73-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
74-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
75-
reason="This test requires google-api-core < 1.26.0",
76-
)
77-
78-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
79-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
80-
reason="This test requires google-api-core >= 1.26.0",
81-
)
82-
8371

8472
def client_cert_source_callback():
8573
return b"cert bytes", b"key bytes"
@@ -137,6 +125,16 @@ def test_hub_service_client_from_service_account_info(client_class):
137125
assert client.transport._host == "networkconnectivity.googleapis.com:443"
138126

139127

128+
@pytest.mark.parametrize("client_class", [HubServiceClient, HubServiceAsyncClient,])
129+
def test_hub_service_client_service_account_always_use_jwt(client_class):
130+
with mock.patch.object(
131+
service_account.Credentials, "with_always_use_jwt_access", create=True
132+
) as use_jwt:
133+
creds = service_account.Credentials(None, None, None)
134+
client = client_class(credentials=creds)
135+
use_jwt.assert_called_with(True)
136+
137+
140138
@pytest.mark.parametrize("client_class", [HubServiceClient, HubServiceAsyncClient,])
141139
def test_hub_service_client_from_service_account_file(client_class):
142140
creds = ga_credentials.AnonymousCredentials()
@@ -2956,7 +2954,6 @@ def test_hub_service_transport_auth_adc_old_google_auth(transport_class):
29562954
(transports.HubServiceGrpcAsyncIOTransport, grpc_helpers_async),
29572955
],
29582956
)
2959-
@requires_api_core_gte_1_26_0
29602957
def test_hub_service_transport_create_channel(transport_class, grpc_helpers):
29612958
# If credentials and host are not provided, the transport class should use
29622959
# ADC credentials.
@@ -2985,79 +2982,6 @@ def test_hub_service_transport_create_channel(transport_class, grpc_helpers):
29852982
)
29862983

29872984

2988-
@pytest.mark.parametrize(
2989-
"transport_class,grpc_helpers",
2990-
[
2991-
(transports.HubServiceGrpcTransport, grpc_helpers),
2992-
(transports.HubServiceGrpcAsyncIOTransport, grpc_helpers_async),
2993-
],
2994-
)
2995-
@requires_api_core_lt_1_26_0
2996-
def test_hub_service_transport_create_channel_old_api_core(
2997-
transport_class, grpc_helpers
2998-
):
2999-
# If credentials and host are not provided, the transport class should use
3000-
# ADC credentials.
3001-
with mock.patch.object(
3002-
google.auth, "default", autospec=True
3003-
) as adc, mock.patch.object(
3004-
grpc_helpers, "create_channel", autospec=True
3005-
) as create_channel:
3006-
creds = ga_credentials.AnonymousCredentials()
3007-
adc.return_value = (creds, None)
3008-
transport_class(quota_project_id="octopus")
3009-
3010-
create_channel.assert_called_with(
3011-
"networkconnectivity.googleapis.com:443",
3012-
credentials=creds,
3013-
credentials_file=None,
3014-
quota_project_id="octopus",
3015-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
3016-
ssl_credentials=None,
3017-
options=[
3018-
("grpc.max_send_message_length", -1),
3019-
("grpc.max_receive_message_length", -1),
3020-
],
3021-
)
3022-
3023-
3024-
@pytest.mark.parametrize(
3025-
"transport_class,grpc_helpers",
3026-
[
3027-
(transports.HubServiceGrpcTransport, grpc_helpers),
3028-
(transports.HubServiceGrpcAsyncIOTransport, grpc_helpers_async),
3029-
],
3030-
)
3031-
@requires_api_core_lt_1_26_0
3032-
def test_hub_service_transport_create_channel_user_scopes(
3033-
transport_class, grpc_helpers
3034-
):
3035-
# If credentials and host are not provided, the transport class should use
3036-
# ADC credentials.
3037-
with mock.patch.object(
3038-
google.auth, "default", autospec=True
3039-
) as adc, mock.patch.object(
3040-
grpc_helpers, "create_channel", autospec=True
3041-
) as create_channel:
3042-
creds = ga_credentials.AnonymousCredentials()
3043-
adc.return_value = (creds, None)
3044-
3045-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3046-
3047-
create_channel.assert_called_with(
3048-
"networkconnectivity.googleapis.com:443",
3049-
credentials=creds,
3050-
credentials_file=None,
3051-
quota_project_id="octopus",
3052-
scopes=["1", "2"],
3053-
ssl_credentials=None,
3054-
options=[
3055-
("grpc.max_send_message_length", -1),
3056-
("grpc.max_receive_message_length", -1),
3057-
],
3058-
)
3059-
3060-
30612985
@pytest.mark.parametrize(
30622986
"transport_class",
30632987
[transports.HubServiceGrpcTransport, transports.HubServiceGrpcAsyncIOTransport],

0 commit comments

Comments
 (0)