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

Commit 223cac0

Browse files
feat: add always_use_jwt_access (#88)
... 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 2e2f58c commit 223cac0

File tree

11 files changed

+72
-243
lines changed

11 files changed

+72
-243
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/redis/__init__.py

google/cloud/redis_v1/services/cloud_redis/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.redis_v1.types import cloud_redis
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 CloudRedisTransport(abc.ABC):
5251
"""Abstract transport class for CloudRedis."""
@@ -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/redis_v1/services/cloud_redis/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def __init__(
172172
scopes=scopes,
173173
quota_project_id=quota_project_id,
174174
client_info=client_info,
175+
always_use_jwt_access=True,
175176
)
176177

177178
if not self._grpc_channel:
@@ -227,14 +228,14 @@ def create_channel(
227228
and ``credentials_file`` are passed.
228229
"""
229230

230-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
231-
232231
return grpc_helpers.create_channel(
233232
host,
234233
credentials=credentials,
235234
credentials_file=credentials_file,
236235
quota_project_id=quota_project_id,
237-
**self_signed_jwt_kwargs,
236+
default_scopes=cls.AUTH_SCOPES,
237+
scopes=scopes,
238+
default_host=cls.DEFAULT_HOST,
238239
**kwargs,
239240
)
240241

google/cloud/redis_v1/services/cloud_redis/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def create_channel(
100100
aio.Channel: A gRPC AsyncIO channel object.
101101
"""
102102

103-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
104-
105103
return grpc_helpers_async.create_channel(
106104
host,
107105
credentials=credentials,
108106
credentials_file=credentials_file,
109107
quota_project_id=quota_project_id,
110-
**self_signed_jwt_kwargs,
108+
default_scopes=cls.AUTH_SCOPES,
109+
scopes=scopes,
110+
default_host=cls.DEFAULT_HOST,
111111
**kwargs,
112112
)
113113

@@ -218,6 +218,7 @@ def __init__(
218218
scopes=scopes,
219219
quota_project_id=quota_project_id,
220220
client_info=client_info,
221+
always_use_jwt_access=True,
221222
)
222223

223224
if not self._grpc_channel:

google/cloud/redis_v1beta1/services/cloud_redis/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.redis_v1beta1.types import cloud_redis
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 CloudRedisTransport(abc.ABC):
5251
"""Abstract transport class for CloudRedis."""
@@ -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/redis_v1beta1/services/cloud_redis/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def __init__(
172172
scopes=scopes,
173173
quota_project_id=quota_project_id,
174174
client_info=client_info,
175+
always_use_jwt_access=True,
175176
)
176177

177178
if not self._grpc_channel:
@@ -227,14 +228,14 @@ def create_channel(
227228
and ``credentials_file`` are passed.
228229
"""
229230

230-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
231-
232231
return grpc_helpers.create_channel(
233232
host,
234233
credentials=credentials,
235234
credentials_file=credentials_file,
236235
quota_project_id=quota_project_id,
237-
**self_signed_jwt_kwargs,
236+
default_scopes=cls.AUTH_SCOPES,
237+
scopes=scopes,
238+
default_host=cls.DEFAULT_HOST,
238239
**kwargs,
239240
)
240241

google/cloud/redis_v1beta1/services/cloud_redis/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def create_channel(
100100
aio.Channel: A gRPC AsyncIO channel object.
101101
"""
102102

103-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
104-
105103
return grpc_helpers_async.create_channel(
106104
host,
107105
credentials=credentials,
108106
credentials_file=credentials_file,
109107
quota_project_id=quota_project_id,
110-
**self_signed_jwt_kwargs,
108+
default_scopes=cls.AUTH_SCOPES,
109+
scopes=scopes,
110+
default_host=cls.DEFAULT_HOST,
111111
**kwargs,
112112
)
113113

@@ -218,6 +218,7 @@ def __init__(
218218
scopes=scopes,
219219
quota_project_id=quota_project_id,
220220
client_info=client_info,
221+
always_use_jwt_access=True,
221222
)
222223

223224
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# 'Development Status :: 5 - Production/Stable'
3030
release_status = "Development Status :: 5 - Production/Stable"
3131
dependencies = [
32-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
32+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3333
"proto-plus >= 1.4.0",
3434
"packaging >= 14.3",
3535
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
77
# Then this file should have foo==1.14.0
8-
google-api-core==1.22.2
8+
google-api-core==1.26.0
99
proto-plus==1.4.0
1010
libcst==0.2.5
1111
packaging==14.3

0 commit comments

Comments
 (0)