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

Commit 8a247c0

Browse files
feat: add always_use_jwt_access (#59)
... 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 46e7268 commit 8a247c0

File tree

11 files changed

+76
-245
lines changed

11 files changed

+76
-245
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/dataqna/__init__.py

google/cloud/dataqna_v1alpha/services/auto_suggestion_service/transports/base.py

Lines changed: 14 additions & 26 deletions
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.dataqna_v1alpha.types import auto_suggestion_service
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 AutoSuggestionServiceTransport(abc.ABC):
5049
"""Abstract transport class for AutoSuggestionService."""
@@ -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/dataqna_v1alpha/services/auto_suggestion_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def __init__(
219219
scopes=scopes,
220220
quota_project_id=quota_project_id,
221221
client_info=client_info,
222+
always_use_jwt_access=True,
222223
)
223224

224225
if not self._grpc_channel:
@@ -274,14 +275,14 @@ def create_channel(
274275
and ``credentials_file`` are passed.
275276
"""
276277

277-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
278-
279278
return grpc_helpers.create_channel(
280279
host,
281280
credentials=credentials,
282281
credentials_file=credentials_file,
283282
quota_project_id=quota_project_id,
284-
**self_signed_jwt_kwargs,
283+
default_scopes=cls.AUTH_SCOPES,
284+
scopes=scopes,
285+
default_host=cls.DEFAULT_HOST,
285286
**kwargs,
286287
)
287288

google/cloud/dataqna_v1alpha/services/auto_suggestion_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ def create_channel(
148148
aio.Channel: A gRPC AsyncIO channel object.
149149
"""
150150

151-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
152-
153151
return grpc_helpers_async.create_channel(
154152
host,
155153
credentials=credentials,
156154
credentials_file=credentials_file,
157155
quota_project_id=quota_project_id,
158-
**self_signed_jwt_kwargs,
156+
default_scopes=cls.AUTH_SCOPES,
157+
scopes=scopes,
158+
default_host=cls.DEFAULT_HOST,
159159
**kwargs,
160160
)
161161

@@ -265,6 +265,7 @@ def __init__(
265265
scopes=scopes,
266266
quota_project_id=quota_project_id,
267267
client_info=client_info,
268+
always_use_jwt_access=True,
268269
)
269270

270271
if not self._grpc_channel:

google/cloud/dataqna_v1alpha/services/question_service/transports/base.py

Lines changed: 14 additions & 26 deletions
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.dataqna_v1alpha.types import question
2930
from google.cloud.dataqna_v1alpha.types import question as gcd_question
@@ -47,8 +48,6 @@
4748
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4849
_GOOGLE_AUTH_VERSION = None
4950

50-
_API_CORE_VERSION = google.api_core.__version__
51-
5251

5352
class QuestionServiceTransport(abc.ABC):
5453
"""Abstract transport class for QuestionService."""
@@ -66,6 +65,7 @@ def __init__(
6665
scopes: Optional[Sequence[str]] = None,
6766
quota_project_id: Optional[str] = None,
6867
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
68+
always_use_jwt_access: Optional[bool] = False,
6969
**kwargs,
7070
) -> None:
7171
"""Instantiate the transport.
@@ -89,6 +89,8 @@ def __init__(
8989
API requests. If ``None``, then default info will be used.
9090
Generally, you only need to set this if you're developing
9191
your own client library.
92+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
93+
be used for service account credentials.
9294
"""
9395
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9496
if ":" not in host:
@@ -117,13 +119,20 @@ def __init__(
117119
**scopes_kwargs, quota_project_id=quota_project_id
118120
)
119121

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

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

128137
# TODO: Remove this function once google-auth >= 1.25.0 is required
129138
@classmethod
@@ -144,27 +153,6 @@ def _get_scopes_kwargs(
144153

145154
return scopes_kwargs
146155

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

google/cloud/dataqna_v1alpha/services/question_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def __init__(
169169
scopes=scopes,
170170
quota_project_id=quota_project_id,
171171
client_info=client_info,
172+
always_use_jwt_access=True,
172173
)
173174

174175
if not self._grpc_channel:
@@ -224,14 +225,14 @@ def create_channel(
224225
and ``credentials_file`` are passed.
225226
"""
226227

227-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
228-
229228
return grpc_helpers.create_channel(
230229
host,
231230
credentials=credentials,
232231
credentials_file=credentials_file,
233232
quota_project_id=quota_project_id,
234-
**self_signed_jwt_kwargs,
233+
default_scopes=cls.AUTH_SCOPES,
234+
scopes=scopes,
235+
default_host=cls.DEFAULT_HOST,
235236
**kwargs,
236237
)
237238

google/cloud/dataqna_v1alpha/services/question_service/transports/grpc_asyncio.py

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

101-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
102-
103101
return grpc_helpers_async.create_channel(
104102
host,
105103
credentials=credentials,
106104
credentials_file=credentials_file,
107105
quota_project_id=quota_project_id,
108-
**self_signed_jwt_kwargs,
106+
default_scopes=cls.AUTH_SCOPES,
107+
scopes=scopes,
108+
default_host=cls.DEFAULT_HOST,
109109
**kwargs,
110110
)
111111

@@ -215,6 +215,7 @@ def __init__(
215215
scopes=scopes,
216216
quota_project_id=quota_project_id,
217217
client_info=client_info,
218+
always_use_jwt_access=True,
218219
)
219220

220221
if not self._grpc_channel:

setup.py

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

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
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.10.0
1010
packaging==14.3

0 commit comments

Comments
 (0)