Skip to content

Commit 93ef477

Browse files
feat: add always_use_jwt_access (#73)
... 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 c71a9a6 commit 93ef477

File tree

26 files changed

+214
-732
lines changed

26 files changed

+214
-732
lines changed

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/folders/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.resourcemanager_v3.types import folders
3031
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -49,8 +50,6 @@
4950
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5051
_GOOGLE_AUTH_VERSION = None
5152

52-
_API_CORE_VERSION = google.api_core.__version__
53-
5453

5554
class FoldersTransport(abc.ABC):
5655
"""Abstract transport class for Folders."""
@@ -71,6 +70,7 @@ def __init__(
7170
scopes: Optional[Sequence[str]] = None,
7271
quota_project_id: Optional[str] = None,
7372
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
73+
always_use_jwt_access: Optional[bool] = False,
7474
**kwargs,
7575
) -> None:
7676
"""Instantiate the transport.
@@ -94,6 +94,8 @@ def __init__(
9494
API requests. If ``None``, then default info will be used.
9595
Generally, you only need to set this if you're developing
9696
your own client library.
97+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
98+
be used for service account credentials.
9799
"""
98100
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
99101
if ":" not in host:
@@ -122,13 +124,20 @@ def __init__(
122124
**scopes_kwargs, quota_project_id=quota_project_id
123125
)
124126

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

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

133142
# TODO: Remove this function once google-auth >= 1.25.0 is required
134143
@classmethod
@@ -149,27 +158,6 @@ def _get_scopes_kwargs(
149158

150159
return scopes_kwargs
151160

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

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/folders/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def __init__(
157157
scopes=scopes,
158158
quota_project_id=quota_project_id,
159159
client_info=client_info,
160+
always_use_jwt_access=True,
160161
)
161162

162163
if not self._grpc_channel:
@@ -212,14 +213,14 @@ def create_channel(
212213
and ``credentials_file`` are passed.
213214
"""
214215

215-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
216-
217216
return grpc_helpers.create_channel(
218217
host,
219218
credentials=credentials,
220219
credentials_file=credentials_file,
221220
quota_project_id=quota_project_id,
222-
**self_signed_jwt_kwargs,
221+
default_scopes=cls.AUTH_SCOPES,
222+
scopes=scopes,
223+
default_host=cls.DEFAULT_HOST,
223224
**kwargs,
224225
)
225226

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/folders/transports/grpc_asyncio.py

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

88-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
89-
9088
return grpc_helpers_async.create_channel(
9189
host,
9290
credentials=credentials,
9391
credentials_file=credentials_file,
9492
quota_project_id=quota_project_id,
95-
**self_signed_jwt_kwargs,
93+
default_scopes=cls.AUTH_SCOPES,
94+
scopes=scopes,
95+
default_host=cls.DEFAULT_HOST,
9696
**kwargs,
9797
)
9898

@@ -203,6 +203,7 @@ def __init__(
203203
scopes=scopes,
204204
quota_project_id=quota_project_id,
205205
client_info=client_info,
206+
always_use_jwt_access=True,
206207
)
207208

208209
if not self._grpc_channel:

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/organizations/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.resourcemanager_v3.types import organizations
2930
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -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 OrganizationsTransport(abc.ABC):
5453
"""Abstract transport class for Organizations."""
@@ -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 = {

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/organizations/transports/grpc.py

Lines changed: 4 additions & 3 deletions
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

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/organizations/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
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:

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/projects/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.resourcemanager_v3.types import projects
3031
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -49,8 +50,6 @@
4950
except pkg_resources.DistributionNotFound: # pragma: NO COVER
5051
_GOOGLE_AUTH_VERSION = None
5152

52-
_API_CORE_VERSION = google.api_core.__version__
53-
5453

5554
class ProjectsTransport(abc.ABC):
5655
"""Abstract transport class for Projects."""
@@ -71,6 +70,7 @@ def __init__(
7170
scopes: Optional[Sequence[str]] = None,
7271
quota_project_id: Optional[str] = None,
7372
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
73+
always_use_jwt_access: Optional[bool] = False,
7474
**kwargs,
7575
) -> None:
7676
"""Instantiate the transport.
@@ -94,6 +94,8 @@ def __init__(
9494
API requests. If ``None``, then default info will be used.
9595
Generally, you only need to set this if you're developing
9696
your own client library.
97+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
98+
be used for service account credentials.
9799
"""
98100
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
99101
if ":" not in host:
@@ -122,13 +124,20 @@ def __init__(
122124
**scopes_kwargs, quota_project_id=quota_project_id
123125
)
124126

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

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

133142
# TODO: Remove this function once google-auth >= 1.25.0 is required
134143
@classmethod
@@ -149,27 +158,6 @@ def _get_scopes_kwargs(
149158

150159
return scopes_kwargs
151160

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

packages/google-cloud-resource-manager/google/cloud/resourcemanager_v3/services/projects/transports/grpc.py

Lines changed: 4 additions & 3 deletions
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)