Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit bff7763

Browse files
feat: add always_use_jwt_access (#166)
... 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 778af83 commit bff7763

File tree

34 files changed

+298
-994
lines changed

34 files changed

+298
-994
lines changed

google/cloud/vision_v1/services/image_annotator/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.vision_v1.types import image_annotator
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 ImageAnnotatorTransport(abc.ABC):
5251
"""Abstract transport class for ImageAnnotator."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

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

124-
# TODO(busunkim): These two class methods are in the base transport
134+
# TODO(busunkim): This method is in the base transport
125135
# to avoid duplicating code across the transport classes. These functions
126-
# should be deleted once the minimum required versions of google-api-core
127-
# and google-auth are increased.
136+
# should be deleted once the minimum required versions of google-auth is increased.
128137

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

google/cloud/vision_v1/services/image_annotator/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/vision_v1/services/image_annotator/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/vision_v1/services/product_search/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.vision_v1.types import product_search_service
3031
from google.longrunning import operations_pb2 # type: ignore
@@ -46,8 +47,6 @@
4647
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4748
_GOOGLE_AUTH_VERSION = None
4849

49-
_API_CORE_VERSION = google.api_core.__version__
50-
5150

5251
class ProductSearchTransport(abc.ABC):
5352
"""Abstract transport class for ProductSearch."""
@@ -68,6 +67,7 @@ def __init__(
6867
scopes: Optional[Sequence[str]] = None,
6968
quota_project_id: Optional[str] = None,
7069
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
70+
always_use_jwt_access: Optional[bool] = False,
7171
**kwargs,
7272
) -> None:
7373
"""Instantiate the transport.
@@ -91,6 +91,8 @@ def __init__(
9191
API requests. If ``None``, then default info will be used.
9292
Generally, you only need to set this if you're developing
9393
your own client library.
94+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
95+
be used for service account credentials.
9496
"""
9597
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9698
if ":" not in host:
@@ -119,13 +121,20 @@ def __init__(
119121
**scopes_kwargs, quota_project_id=quota_project_id
120122
)
121123

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

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

130139
# TODO: Remove this function once google-auth >= 1.25.0 is required
131140
@classmethod
@@ -146,27 +155,6 @@ def _get_scopes_kwargs(
146155

147156
return scopes_kwargs
148157

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

google/cloud/vision_v1/services/product_search/transports/grpc.py

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

175176
if not self._grpc_channel:
@@ -225,14 +226,14 @@ def create_channel(
225226
and ``credentials_file`` are passed.
226227
"""
227228

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

google/cloud/vision_v1/services/product_search/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

@@ -216,6 +216,7 @@ def __init__(
216216
scopes=scopes,
217217
quota_project_id=quota_project_id,
218218
client_info=client_info,
219+
always_use_jwt_access=True,
219220
)
220221

221222
if not self._grpc_channel:

google/cloud/vision_v1p1beta1/services/image_annotator/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.vision_v1p1beta1.types import image_annotator
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 ImageAnnotatorTransport(abc.ABC):
5049
"""Abstract transport class for ImageAnnotator."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

122-
# TODO(busunkim): These two class methods are in the base transport
132+
# TODO(busunkim): This method is in the base transport
123133
# to avoid duplicating code across the transport classes. These functions
124-
# should be deleted once the minimum required versions of google-api-core
125-
# and google-auth are increased.
134+
# should be deleted once the minimum required versions of google-auth is increased.
126135

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

google/cloud/vision_v1p1beta1/services/image_annotator/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def __init__(
152152
scopes=scopes,
153153
quota_project_id=quota_project_id,
154154
client_info=client_info,
155+
always_use_jwt_access=True,
155156
)
156157

157158
if not self._grpc_channel:
@@ -207,14 +208,14 @@ def create_channel(
207208
and ``credentials_file`` are passed.
208209
"""
209210

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

0 commit comments

Comments
 (0)