Skip to content

Commit a301512

Browse files
feat: add always_use_jwt_access (#40)
... 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 aebac75 commit a301512

File tree

35 files changed

+288
-987
lines changed

35 files changed

+288
-987
lines changed

packages/google-cloud-appengine-admin/.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/appengine_admin/__init__.py

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/applications/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.appengine_admin_v1.types import appengine
3031
from google.cloud.appengine_admin_v1.types import application
@@ -48,8 +49,6 @@
4849
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4950
_GOOGLE_AUTH_VERSION = None
5051

51-
_API_CORE_VERSION = google.api_core.__version__
52-
5352

5453
class ApplicationsTransport(abc.ABC):
5554
"""Abstract transport class for Applications."""
@@ -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-appengine-admin/google/cloud/appengine_admin_v1/services/applications/transports/grpc.py

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

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

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

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/applications/transports/grpc_asyncio.py

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

84-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
85-
8684
return grpc_helpers_async.create_channel(
8785
host,
8886
credentials=credentials,
8987
credentials_file=credentials_file,
9088
quota_project_id=quota_project_id,
91-
**self_signed_jwt_kwargs,
89+
default_scopes=cls.AUTH_SCOPES,
90+
scopes=scopes,
91+
default_host=cls.DEFAULT_HOST,
9292
**kwargs,
9393
)
9494

@@ -199,6 +199,7 @@ def __init__(
199199
scopes=scopes,
200200
quota_project_id=quota_project_id,
201201
client_info=client_info,
202+
always_use_jwt_access=True,
202203
)
203204

204205
if not self._grpc_channel:

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/authorized_certificates/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.appengine_admin_v1.types import appengine
2930
from google.cloud.appengine_admin_v1.types import certificate
@@ -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 AuthorizedCertificatesTransport(abc.ABC):
5453
"""Abstract transport class for AuthorizedCertificates."""
@@ -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 = {

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/authorized_certificates/transports/grpc.py

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

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

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

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/authorized_certificates/transports/grpc_asyncio.py

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

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

@@ -199,6 +199,7 @@ def __init__(
199199
scopes=scopes,
200200
quota_project_id=quota_project_id,
201201
client_info=client_info,
202+
always_use_jwt_access=True,
202203
)
203204

204205
if not self._grpc_channel:

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/authorized_domains/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.appengine_admin_v1.types import appengine
2930

@@ -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 AuthorizedDomainsTransport(abc.ABC):
5251
"""Abstract transport class for AuthorizedDomains."""
@@ -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 = {

packages/google-cloud-appengine-admin/google/cloud/appengine_admin_v1/services/authorized_domains/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

0 commit comments

Comments
 (0)