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

Commit a88f246

Browse files
feat: add always_use_jwt_access (#68)
... 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 1b77733 commit a88f246

File tree

19 files changed

+142
-475
lines changed

19 files changed

+142
-475
lines changed

.coveragerc

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

google/cloud/workflows/executions_v1/services/executions/transports/base.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.api_core import gapic_v1 # type: ignore
2525
from google.api_core import retry as retries # type: ignore
2626
from google.auth import credentials as ga_credentials # type: ignore
27+
from google.oauth2 import service_account # type: ignore
2728

2829
from google.cloud.workflows.executions_v1.types import executions
2930

@@ -43,8 +44,6 @@
4344
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4445
_GOOGLE_AUTH_VERSION = None
4546

46-
_API_CORE_VERSION = google.api_core.__version__
47-
4847

4948
class ExecutionsTransport(abc.ABC):
5049
"""Abstract transport class for Executions."""
@@ -62,6 +61,7 @@ def __init__(
6261
scopes: Optional[Sequence[str]] = None,
6362
quota_project_id: Optional[str] = None,
6463
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
64+
always_use_jwt_access: Optional[bool] = False,
6565
**kwargs,
6666
) -> None:
6767
"""Instantiate the transport.
@@ -85,6 +85,8 @@ def __init__(
8585
API requests. If ``None``, then default info will be used.
8686
Generally, you only need to set this if you're developing
8787
your own client library.
88+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
89+
be used for service account credentials.
8890
"""
8991
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9092
if ":" not in host:
@@ -113,13 +115,20 @@ def __init__(
113115
**scopes_kwargs, quota_project_id=quota_project_id
114116
)
115117

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

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

124133
# TODO: Remove this function once google-auth >= 1.25.0 is required
125134
@classmethod
@@ -140,27 +149,6 @@ def _get_scopes_kwargs(
140149

141150
return scopes_kwargs
142151

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

google/cloud/workflows/executions_v1/services/executions/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(
150150
scopes=scopes,
151151
quota_project_id=quota_project_id,
152152
client_info=client_info,
153+
always_use_jwt_access=True,
153154
)
154155

155156
if not self._grpc_channel:
@@ -205,14 +206,14 @@ def create_channel(
205206
and ``credentials_file`` are passed.
206207
"""
207208

208-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
209-
210209
return grpc_helpers.create_channel(
211210
host,
212211
credentials=credentials,
213212
credentials_file=credentials_file,
214213
quota_project_id=quota_project_id,
215-
**self_signed_jwt_kwargs,
214+
default_scopes=cls.AUTH_SCOPES,
215+
scopes=scopes,
216+
default_host=cls.DEFAULT_HOST,
216217
**kwargs,
217218
)
218219

google/cloud/workflows/executions_v1/services/executions/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ def create_channel(
7979
aio.Channel: A gRPC AsyncIO channel object.
8080
"""
8181

82-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
83-
8482
return grpc_helpers_async.create_channel(
8583
host,
8684
credentials=credentials,
8785
credentials_file=credentials_file,
8886
quota_project_id=quota_project_id,
89-
**self_signed_jwt_kwargs,
87+
default_scopes=cls.AUTH_SCOPES,
88+
scopes=scopes,
89+
default_host=cls.DEFAULT_HOST,
9090
**kwargs,
9191
)
9292

@@ -196,6 +196,7 @@ def __init__(
196196
scopes=scopes,
197197
quota_project_id=quota_project_id,
198198
client_info=client_info,
199+
always_use_jwt_access=True,
199200
)
200201

201202
if not self._grpc_channel:

google/cloud/workflows/executions_v1beta/services/executions/transports/base.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from google.api_core import gapic_v1 # type: ignore
2525
from google.api_core import retry as retries # type: ignore
2626
from google.auth import credentials as ga_credentials # type: ignore
27+
from google.oauth2 import service_account # type: ignore
2728

2829
from google.cloud.workflows.executions_v1beta.types import executions
2930

@@ -43,8 +44,6 @@
4344
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4445
_GOOGLE_AUTH_VERSION = None
4546

46-
_API_CORE_VERSION = google.api_core.__version__
47-
4847

4948
class ExecutionsTransport(abc.ABC):
5049
"""Abstract transport class for Executions."""
@@ -62,6 +61,7 @@ def __init__(
6261
scopes: Optional[Sequence[str]] = None,
6362
quota_project_id: Optional[str] = None,
6463
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
64+
always_use_jwt_access: Optional[bool] = False,
6565
**kwargs,
6666
) -> None:
6767
"""Instantiate the transport.
@@ -85,6 +85,8 @@ def __init__(
8585
API requests. If ``None``, then default info will be used.
8686
Generally, you only need to set this if you're developing
8787
your own client library.
88+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
89+
be used for service account credentials.
8890
"""
8991
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9092
if ":" not in host:
@@ -113,13 +115,20 @@ def __init__(
113115
**scopes_kwargs, quota_project_id=quota_project_id
114116
)
115117

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

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

124133
# TODO: Remove this function once google-auth >= 1.25.0 is required
125134
@classmethod
@@ -140,27 +149,6 @@ def _get_scopes_kwargs(
140149

141150
return scopes_kwargs
142151

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

google/cloud/workflows/executions_v1beta/services/executions/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def __init__(
151151
scopes=scopes,
152152
quota_project_id=quota_project_id,
153153
client_info=client_info,
154+
always_use_jwt_access=True,
154155
)
155156

156157
if not self._grpc_channel:
@@ -206,14 +207,14 @@ def create_channel(
206207
and ``credentials_file`` are passed.
207208
"""
208209

209-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
210-
211210
return grpc_helpers.create_channel(
212211
host,
213212
credentials=credentials,
214213
credentials_file=credentials_file,
215214
quota_project_id=quota_project_id,
216-
**self_signed_jwt_kwargs,
215+
default_scopes=cls.AUTH_SCOPES,
216+
scopes=scopes,
217+
default_host=cls.DEFAULT_HOST,
217218
**kwargs,
218219
)
219220

google/cloud/workflows/executions_v1beta/services/executions/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def create_channel(
8080
aio.Channel: A gRPC AsyncIO channel object.
8181
"""
8282

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

@@ -197,6 +197,7 @@ def __init__(
197197
scopes=scopes,
198198
quota_project_id=quota_project_id,
199199
client_info=client_info,
200+
always_use_jwt_access=True,
200201
)
201202

202203
if not self._grpc_channel:

google/cloud/workflows_v1/services/workflows/transports/base.py

+14-26
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.workflows_v1.types import workflows
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 WorkflowsTransport(abc.ABC):
5251
"""Abstract transport class for Workflows."""
@@ -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/workflows_v1/services/workflows/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def __init__(
154154
scopes=scopes,
155155
quota_project_id=quota_project_id,
156156
client_info=client_info,
157+
always_use_jwt_access=True,
157158
)
158159

159160
if not self._grpc_channel:
@@ -209,14 +210,14 @@ def create_channel(
209210
and ``credentials_file`` are passed.
210211
"""
211212

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

0 commit comments

Comments
 (0)