Skip to content

Commit 0f1a5de

Browse files
feat: add always_use_jwt_access (#381)
... 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: googleapis/googleapis-gen@27e4c88
1 parent 43154da commit 0f1a5de

File tree

14 files changed

+112
-369
lines changed

14 files changed

+112
-369
lines changed

google/cloud/spanner_admin_database_v1/services/database_admin/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.spanner_admin_database_v1.types import backup
3031
from google.cloud.spanner_admin_database_v1.types import backup as gsad_backup
@@ -52,8 +53,6 @@
5253
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5354
_GOOGLE_AUTH_VERSION = None
5455

55-
_API_CORE_VERSION = google.api_core.__version__
56-
5756

5857
class DatabaseAdminTransport(abc.ABC):
5958
"""Abstract transport class for DatabaseAdmin."""
@@ -74,6 +73,7 @@ def __init__(
7473
scopes: Optional[Sequence[str]] = None,
7574
quota_project_id: Optional[str] = None,
7675
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
76+
always_use_jwt_access: Optional[bool] = False,
7777
**kwargs,
7878
) -> None:
7979
"""Instantiate the transport.
@@ -97,6 +97,8 @@ def __init__(
9797
API requests. If ``None``, then default info will be used.
9898
Generally, you only need to set this if you're developing
9999
your own client library.
100+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
101+
be used for service account credentials.
100102
"""
101103
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
102104
if ":" not in host:
@@ -125,13 +127,20 @@ def __init__(
125127
**scopes_kwargs, quota_project_id=quota_project_id
126128
)
127129

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

131-
# TODO(busunkim): These two class methods are in the base transport
141+
# TODO(busunkim): This method is in the base transport
132142
# to avoid duplicating code across the transport classes. These functions
133-
# should be deleted once the minimum required versions of google-api-core
134-
# and google-auth are increased.
143+
# should be deleted once the minimum required versions of google-auth is increased.
135144

136145
# TODO: Remove this function once google-auth >= 1.25.0 is required
137146
@classmethod
@@ -152,27 +161,6 @@ def _get_scopes_kwargs(
152161

153162
return scopes_kwargs
154163

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

google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def __init__(
162162
scopes=scopes,
163163
quota_project_id=quota_project_id,
164164
client_info=client_info,
165+
always_use_jwt_access=True,
165166
)
166167

167168
if not self._grpc_channel:
@@ -217,14 +218,14 @@ def create_channel(
217218
and ``credentials_file`` are passed.
218219
"""
219220

220-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
221-
222221
return grpc_helpers.create_channel(
223222
host,
224223
credentials=credentials,
225224
credentials_file=credentials_file,
226225
quota_project_id=quota_project_id,
227-
**self_signed_jwt_kwargs,
226+
default_scopes=cls.AUTH_SCOPES,
227+
scopes=scopes,
228+
default_host=cls.DEFAULT_HOST,
228229
**kwargs,
229230
)
230231

google/cloud/spanner_admin_database_v1/services/database_admin/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ def create_channel(
9090
aio.Channel: A gRPC AsyncIO channel object.
9191
"""
9292

93-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
94-
9593
return grpc_helpers_async.create_channel(
9694
host,
9795
credentials=credentials,
9896
credentials_file=credentials_file,
9997
quota_project_id=quota_project_id,
100-
**self_signed_jwt_kwargs,
98+
default_scopes=cls.AUTH_SCOPES,
99+
scopes=scopes,
100+
default_host=cls.DEFAULT_HOST,
101101
**kwargs,
102102
)
103103

@@ -208,6 +208,7 @@ def __init__(
208208
scopes=scopes,
209209
quota_project_id=quota_project_id,
210210
client_info=client_info,
211+
always_use_jwt_access=True,
211212
)
212213

213214
if not self._grpc_channel:

google/cloud/spanner_admin_instance_v1/services/instance_admin/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.spanner_admin_instance_v1.types import spanner_instance_admin
3031
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -50,8 +51,6 @@
5051
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5152
_GOOGLE_AUTH_VERSION = None
5253

53-
_API_CORE_VERSION = google.api_core.__version__
54-
5554

5655
class InstanceAdminTransport(abc.ABC):
5756
"""Abstract transport class for InstanceAdmin."""
@@ -72,6 +71,7 @@ def __init__(
7271
scopes: Optional[Sequence[str]] = None,
7372
quota_project_id: Optional[str] = None,
7473
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
74+
always_use_jwt_access: Optional[bool] = False,
7575
**kwargs,
7676
) -> None:
7777
"""Instantiate the transport.
@@ -95,6 +95,8 @@ def __init__(
9595
API requests. If ``None``, then default info will be used.
9696
Generally, you only need to set this if you're developing
9797
your own client library.
98+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
99+
be used for service account credentials.
98100
"""
99101
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
100102
if ":" not in host:
@@ -123,13 +125,20 @@ def __init__(
123125
**scopes_kwargs, quota_project_id=quota_project_id
124126
)
125127

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

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

134143
# TODO: Remove this function once google-auth >= 1.25.0 is required
135144
@classmethod
@@ -150,27 +159,6 @@ def _get_scopes_kwargs(
150159

151160
return scopes_kwargs
152161

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

google/cloud/spanner_admin_instance_v1/services/instance_admin/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

google/cloud/spanner_admin_instance_v1/services/instance_admin/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ def create_channel(
103103
aio.Channel: A gRPC AsyncIO channel object.
104104
"""
105105

106-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
107-
108106
return grpc_helpers_async.create_channel(
109107
host,
110108
credentials=credentials,
111109
credentials_file=credentials_file,
112110
quota_project_id=quota_project_id,
113-
**self_signed_jwt_kwargs,
111+
default_scopes=cls.AUTH_SCOPES,
112+
scopes=scopes,
113+
default_host=cls.DEFAULT_HOST,
114114
**kwargs,
115115
)
116116

@@ -221,6 +221,7 @@ def __init__(
221221
scopes=scopes,
222222
quota_project_id=quota_project_id,
223223
client_info=client_info,
224+
always_use_jwt_access=True,
224225
)
225226

226227
if not self._grpc_channel:

google/cloud/spanner_v1/services/spanner/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.spanner_v1.types import commit_response
2930
from google.cloud.spanner_v1.types import result_set
@@ -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 SpannerTransport(abc.ABC):
5453
"""Abstract transport class for Spanner."""
@@ -69,6 +68,7 @@ def __init__(
6968
scopes: Optional[Sequence[str]] = None,
7069
quota_project_id: Optional[str] = None,
7170
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
71+
always_use_jwt_access: Optional[bool] = False,
7272
**kwargs,
7373
) -> None:
7474
"""Instantiate the transport.
@@ -92,6 +92,8 @@ def __init__(
9292
API requests. If ``None``, then default info will be used.
9393
Generally, you only need to set this if you're developing
9494
your own client library.
95+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
96+
be used for service account credentials.
9597
"""
9698
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9799
if ":" not in host:
@@ -120,13 +122,20 @@ def __init__(
120122
**scopes_kwargs, quota_project_id=quota_project_id
121123
)
122124

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

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

131140
# TODO: Remove this function once google-auth >= 1.25.0 is required
132141
@classmethod
@@ -147,27 +156,6 @@ def _get_scopes_kwargs(
147156

148157
return scopes_kwargs
149158

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

google/cloud/spanner_v1/services/spanner/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

0 commit comments

Comments
 (0)