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

Commit 064f1e0

Browse files
feat: add always_use_jwt_access (#165)
... 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 5dca57c commit 064f1e0

31 files changed

Lines changed: 261 additions & 879 deletions

File tree

.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/monitoring/__init__.py

google/cloud/monitoring_v3/services/alert_policy_service/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.monitoring_v3.types import alert
2930
from google.cloud.monitoring_v3.types import alert_service
@@ -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 AlertPolicyServiceTransport(abc.ABC):
5453
"""Abstract transport class for AlertPolicyService."""
@@ -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/monitoring_v3/services/alert_policy_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def __init__(
159159
scopes=scopes,
160160
quota_project_id=quota_project_id,
161161
client_info=client_info,
162+
always_use_jwt_access=True,
162163
)
163164

164165
if not self._grpc_channel:
@@ -214,14 +215,14 @@ def create_channel(
214215
and ``credentials_file`` are passed.
215216
"""
216217

217-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
218-
219218
return grpc_helpers.create_channel(
220219
host,
221220
credentials=credentials,
222221
credentials_file=credentials_file,
223222
quota_project_id=quota_project_id,
224-
**self_signed_jwt_kwargs,
223+
default_scopes=cls.AUTH_SCOPES,
224+
scopes=scopes,
225+
default_host=cls.DEFAULT_HOST,
225226
**kwargs,
226227
)
227228

google/cloud/monitoring_v3/services/alert_policy_service/transports/grpc_asyncio.py

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

91-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
92-
9391
return grpc_helpers_async.create_channel(
9492
host,
9593
credentials=credentials,
9694
credentials_file=credentials_file,
9795
quota_project_id=quota_project_id,
98-
**self_signed_jwt_kwargs,
96+
default_scopes=cls.AUTH_SCOPES,
97+
scopes=scopes,
98+
default_host=cls.DEFAULT_HOST,
9999
**kwargs,
100100
)
101101

@@ -205,6 +205,7 @@ def __init__(
205205
scopes=scopes,
206206
quota_project_id=quota_project_id,
207207
client_info=client_info,
208+
always_use_jwt_access=True,
208209
)
209210

210211
if not self._grpc_channel:

google/cloud/monitoring_v3/services/group_service/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.monitoring_v3.types import group
2930
from google.cloud.monitoring_v3.types import group as gm_group
@@ -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 GroupServiceTransport(abc.ABC):
5554
"""Abstract transport class for GroupService."""
@@ -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 = {

google/cloud/monitoring_v3/services/group_service/transports/grpc.py

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

168169
if not self._grpc_channel:
@@ -218,14 +219,14 @@ def create_channel(
218219
and ``credentials_file`` are passed.
219220
"""
220221

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

google/cloud/monitoring_v3/services/group_service/transports/grpc_asyncio.py

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

95-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
96-
9795
return grpc_helpers_async.create_channel(
9896
host,
9997
credentials=credentials,
10098
credentials_file=credentials_file,
10199
quota_project_id=quota_project_id,
102-
**self_signed_jwt_kwargs,
100+
default_scopes=cls.AUTH_SCOPES,
101+
scopes=scopes,
102+
default_host=cls.DEFAULT_HOST,
103103
**kwargs,
104104
)
105105

@@ -209,6 +209,7 @@ def __init__(
209209
scopes=scopes,
210210
quota_project_id=quota_project_id,
211211
client_info=client_info,
212+
always_use_jwt_access=True,
212213
)
213214

214215
if not self._grpc_channel:

google/cloud/monitoring_v3/services/metric_service/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.api import metric_pb2 # type: ignore
2930
from google.api import monitored_resource_pb2 # type: ignore
@@ -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 MetricServiceTransport(abc.ABC):
5554
"""Abstract transport class for MetricService."""
@@ -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/monitoring_v3/services/metric_service/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

0 commit comments

Comments
 (0)