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

Commit 7da3fc7

Browse files
feat: add always_use_jwt_access (#175)
... 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 5a0c42b commit 7da3fc7

18 files changed

Lines changed: 146 additions & 476 deletions

File tree

google/cloud/automl_v1/services/auto_ml/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.automl_v1.types import annotation_spec
3031
from google.cloud.automl_v1.types import dataset
@@ -51,8 +52,6 @@
5152
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5253
_GOOGLE_AUTH_VERSION = None
5354

54-
_API_CORE_VERSION = google.api_core.__version__
55-
5655

5756
class AutoMlTransport(abc.ABC):
5857
"""Abstract transport class for AutoMl."""
@@ -70,6 +69,7 @@ def __init__(
7069
scopes: Optional[Sequence[str]] = None,
7170
quota_project_id: Optional[str] = None,
7271
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
72+
always_use_jwt_access: Optional[bool] = False,
7373
**kwargs,
7474
) -> None:
7575
"""Instantiate the transport.
@@ -93,6 +93,8 @@ def __init__(
9393
API requests. If ``None``, then default info will be used.
9494
Generally, you only need to set this if you're developing
9595
your own client library.
96+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
97+
be used for service account credentials.
9698
"""
9799
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
98100
if ":" not in host:
@@ -121,13 +123,20 @@ def __init__(
121123
**scopes_kwargs, quota_project_id=quota_project_id
122124
)
123125

126+
# If the credentials is service account credentials, then always try to use self signed JWT.
127+
if (
128+
always_use_jwt_access
129+
and isinstance(credentials, service_account.Credentials)
130+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
131+
):
132+
credentials = credentials.with_always_use_jwt_access(True)
133+
124134
# Save the credentials.
125135
self._credentials = credentials
126136

127-
# TODO(busunkim): These two class methods are in the base transport
137+
# TODO(busunkim): This method is in the base transport
128138
# to avoid duplicating code across the transport classes. These functions
129-
# should be deleted once the minimum required versions of google-api-core
130-
# and google-auth are increased.
139+
# should be deleted once the minimum required versions of google-auth is increased.
131140

132141
# TODO: Remove this function once google-auth >= 1.25.0 is required
133142
@classmethod
@@ -148,27 +157,6 @@ def _get_scopes_kwargs(
148157

149158
return scopes_kwargs
150159

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

google/cloud/automl_v1/services/auto_ml/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/automl_v1/services/auto_ml/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/automl_v1/services/prediction_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.automl_v1.types import prediction_service
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 PredictionServiceTransport(abc.ABC):
5251
"""Abstract transport class for PredictionService."""
@@ -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/automl_v1/services/prediction_service/transports/grpc.py

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

160161
if not self._grpc_channel:
@@ -210,14 +211,14 @@ def create_channel(
210211
and ``credentials_file`` are passed.
211212
"""
212213

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

google/cloud/automl_v1/services/prediction_service/transports/grpc_asyncio.py

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

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

@@ -201,6 +201,7 @@ def __init__(
201201
scopes=scopes,
202202
quota_project_id=quota_project_id,
203203
client_info=client_info,
204+
always_use_jwt_access=True,
204205
)
205206

206207
if not self._grpc_channel:

google/cloud/automl_v1beta1/services/auto_ml/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.automl_v1beta1.types import annotation_spec
3031
from google.cloud.automl_v1beta1.types import column_spec
@@ -54,8 +55,6 @@
5455
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5556
_GOOGLE_AUTH_VERSION = None
5657

57-
_API_CORE_VERSION = google.api_core.__version__
58-
5958

6059
class AutoMlTransport(abc.ABC):
6160
"""Abstract transport class for AutoMl."""
@@ -73,6 +72,7 @@ def __init__(
7372
scopes: Optional[Sequence[str]] = None,
7473
quota_project_id: Optional[str] = None,
7574
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
75+
always_use_jwt_access: Optional[bool] = False,
7676
**kwargs,
7777
) -> None:
7878
"""Instantiate the transport.
@@ -96,6 +96,8 @@ def __init__(
9696
API requests. If ``None``, then default info will be used.
9797
Generally, you only need to set this if you're developing
9898
your own client library.
99+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
100+
be used for service account credentials.
99101
"""
100102
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
101103
if ":" not in host:
@@ -124,13 +126,20 @@ def __init__(
124126
**scopes_kwargs, quota_project_id=quota_project_id
125127
)
126128

129+
# If the credentials is service account credentials, then always try to use self signed JWT.
130+
if (
131+
always_use_jwt_access
132+
and isinstance(credentials, service_account.Credentials)
133+
and hasattr(service_account.Credentials, "with_always_use_jwt_access")
134+
):
135+
credentials = credentials.with_always_use_jwt_access(True)
136+
127137
# Save the credentials.
128138
self._credentials = credentials
129139

130-
# TODO(busunkim): These two class methods are in the base transport
140+
# TODO(busunkim): This method is in the base transport
131141
# to avoid duplicating code across the transport classes. These functions
132-
# should be deleted once the minimum required versions of google-api-core
133-
# and google-auth are increased.
142+
# should be deleted once the minimum required versions of google-auth is increased.
134143

135144
# TODO: Remove this function once google-auth >= 1.25.0 is required
136145
@classmethod
@@ -151,27 +160,6 @@ def _get_scopes_kwargs(
151160

152161
return scopes_kwargs
153162

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

google/cloud/automl_v1beta1/services/auto_ml/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def __init__(
175175
scopes=scopes,
176176
quota_project_id=quota_project_id,
177177
client_info=client_info,
178+
always_use_jwt_access=True,
178179
)
179180

180181
if not self._grpc_channel:
@@ -230,14 +231,14 @@ def create_channel(
230231
and ``credentials_file`` are passed.
231232
"""
232233

233-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
234-
235234
return grpc_helpers.create_channel(
236235
host,
237236
credentials=credentials,
238237
credentials_file=credentials_file,
239238
quota_project_id=quota_project_id,
240-
**self_signed_jwt_kwargs,
239+
default_scopes=cls.AUTH_SCOPES,
240+
scopes=scopes,
241+
default_host=cls.DEFAULT_HOST,
241242
**kwargs,
242243
)
243244

0 commit comments

Comments
 (0)