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

Commit 5ed6734

Browse files
feat: add always_use_jwt_access (#11)
... 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 5e3aece commit 5ed6734

6 files changed

Lines changed: 37 additions & 117 deletions

File tree

google/cloud/tpu_v1/services/tpu/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.tpu_v1.types import cloud_tpu
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 TpuTransport(abc.ABC):
5251
"""Abstract transport class for Tpu."""
@@ -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/tpu_v1/services/tpu/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/tpu_v1/services/tpu/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
@@ -26,7 +26,7 @@
2626
release_status = "Development Status :: 4 - Beta"
2727
url = "https://github.com/googleapis/python-tpu"
2828
dependencies = [
29-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
29+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3030
"proto-plus >= 1.15.0",
3131
"packaging >= 14.3",
3232
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.22.2
7+
google-api-core==1.26.0
88
proto-plus==1.15.0
99
packaging==14.3
1010
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 si transitively required through google-api-core

tests/unit/gapic/tpu_v1/test_tpu.py

Lines changed: 13 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from google.cloud.tpu_v1.services.tpu import TpuClient
3939
from google.cloud.tpu_v1.services.tpu import pagers
4040
from google.cloud.tpu_v1.services.tpu import transports
41-
from google.cloud.tpu_v1.services.tpu.transports.base import _API_CORE_VERSION
4241
from google.cloud.tpu_v1.services.tpu.transports.base import _GOOGLE_AUTH_VERSION
4342
from google.cloud.tpu_v1.types import cloud_tpu
4443
from google.longrunning import operations_pb2
@@ -47,8 +46,9 @@
4746
import google.auth
4847

4948

50-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
51-
# - Delete all the api-core and auth "less than" test cases
49+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
50+
# through google-api-core:
51+
# - Delete the auth "less than" test cases
5252
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
5353
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
5454
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -59,16 +59,6 @@
5959
reason="This test requires google-auth >= 1.25.0",
6060
)
6161

62-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
63-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
64-
reason="This test requires google-api-core < 1.26.0",
65-
)
66-
67-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
68-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
69-
reason="This test requires google-api-core >= 1.26.0",
70-
)
71-
7262

7363
def client_cert_source_callback():
7464
return b"cert bytes", b"key bytes"
@@ -120,6 +110,16 @@ def test_tpu_client_from_service_account_info(client_class):
120110
assert client.transport._host == "tpu.googleapis.com:443"
121111

122112

113+
@pytest.mark.parametrize("client_class", [TpuClient, TpuAsyncClient,])
114+
def test_tpu_client_service_account_always_use_jwt(client_class):
115+
with mock.patch.object(
116+
service_account.Credentials, "with_always_use_jwt_access", create=True
117+
) as use_jwt:
118+
creds = service_account.Credentials(None, None, None)
119+
client = client_class(credentials=creds)
120+
use_jwt.assert_called_with(True)
121+
122+
123123
@pytest.mark.parametrize("client_class", [TpuClient, TpuAsyncClient,])
124124
def test_tpu_client_from_service_account_file(client_class):
125125
creds = ga_credentials.AnonymousCredentials()
@@ -3202,7 +3202,6 @@ def test_tpu_transport_auth_adc_old_google_auth(transport_class):
32023202
(transports.TpuGrpcAsyncIOTransport, grpc_helpers_async),
32033203
],
32043204
)
3205-
@requires_api_core_gte_1_26_0
32063205
def test_tpu_transport_create_channel(transport_class, grpc_helpers):
32073206
# If credentials and host are not provided, the transport class should use
32083207
# ADC credentials.
@@ -3231,75 +3230,6 @@ def test_tpu_transport_create_channel(transport_class, grpc_helpers):
32313230
)
32323231

32333232

3234-
@pytest.mark.parametrize(
3235-
"transport_class,grpc_helpers",
3236-
[
3237-
(transports.TpuGrpcTransport, grpc_helpers),
3238-
(transports.TpuGrpcAsyncIOTransport, grpc_helpers_async),
3239-
],
3240-
)
3241-
@requires_api_core_lt_1_26_0
3242-
def test_tpu_transport_create_channel_old_api_core(transport_class, grpc_helpers):
3243-
# If credentials and host are not provided, the transport class should use
3244-
# ADC credentials.
3245-
with mock.patch.object(
3246-
google.auth, "default", autospec=True
3247-
) as adc, mock.patch.object(
3248-
grpc_helpers, "create_channel", autospec=True
3249-
) as create_channel:
3250-
creds = ga_credentials.AnonymousCredentials()
3251-
adc.return_value = (creds, None)
3252-
transport_class(quota_project_id="octopus")
3253-
3254-
create_channel.assert_called_with(
3255-
"tpu.googleapis.com:443",
3256-
credentials=creds,
3257-
credentials_file=None,
3258-
quota_project_id="octopus",
3259-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
3260-
ssl_credentials=None,
3261-
options=[
3262-
("grpc.max_send_message_length", -1),
3263-
("grpc.max_receive_message_length", -1),
3264-
],
3265-
)
3266-
3267-
3268-
@pytest.mark.parametrize(
3269-
"transport_class,grpc_helpers",
3270-
[
3271-
(transports.TpuGrpcTransport, grpc_helpers),
3272-
(transports.TpuGrpcAsyncIOTransport, grpc_helpers_async),
3273-
],
3274-
)
3275-
@requires_api_core_lt_1_26_0
3276-
def test_tpu_transport_create_channel_user_scopes(transport_class, grpc_helpers):
3277-
# If credentials and host are not provided, the transport class should use
3278-
# ADC credentials.
3279-
with mock.patch.object(
3280-
google.auth, "default", autospec=True
3281-
) as adc, mock.patch.object(
3282-
grpc_helpers, "create_channel", autospec=True
3283-
) as create_channel:
3284-
creds = ga_credentials.AnonymousCredentials()
3285-
adc.return_value = (creds, None)
3286-
3287-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3288-
3289-
create_channel.assert_called_with(
3290-
"tpu.googleapis.com:443",
3291-
credentials=creds,
3292-
credentials_file=None,
3293-
quota_project_id="octopus",
3294-
scopes=["1", "2"],
3295-
ssl_credentials=None,
3296-
options=[
3297-
("grpc.max_send_message_length", -1),
3298-
("grpc.max_receive_message_length", -1),
3299-
],
3300-
)
3301-
3302-
33033233
@pytest.mark.parametrize(
33043234
"transport_class", [transports.TpuGrpcTransport, transports.TpuGrpcAsyncIOTransport]
33053235
)

0 commit comments

Comments
 (0)