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

Commit ad969a1

Browse files
feat: add always_use_jwt_access (#79)
... 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 a9c0dea commit ad969a1

15 files changed

Lines changed: 114 additions & 386 deletions

File tree

.coveragerc

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
# -*- coding: utf-8 -*-
2-
#
3-
# Copyright 2020 Google LLC
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# https://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
17-
# Generated by synthtool. DO NOT EDIT!
181
[run]
192
branch = True
203

214
[report]
22-
fail_under = 100
235
show_missing = True
246
omit =
25-
google/cloud/websecurityscanner_v1/*
26-
google/cloud/websecurityscanner_v1beta/__init__.py
27-
google/cloud/websecurityscanner_v1alpha/__init__.py
7+
google/cloud/websecurityscanner/__init__.py
288
exclude_lines =
299
# Re-enable the standard pragma
3010
pragma: NO COVER

google/cloud/websecurityscanner_v1/services/web_security_scanner/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.websecurityscanner_v1.types import finding
2930
from google.cloud.websecurityscanner_v1.types import scan_config
@@ -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 WebSecurityScannerTransport(abc.ABC):
5655
"""Abstract transport class for WebSecurityScanner."""
@@ -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/websecurityscanner_v1/services/web_security_scanner/transports/grpc.py

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

161162
if not self._grpc_channel:
@@ -211,14 +212,14 @@ def create_channel(
211212
and ``credentials_file`` are passed.
212213
"""
213214

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

google/cloud/websecurityscanner_v1/services/web_security_scanner/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

@@ -202,6 +202,7 @@ def __init__(
202202
scopes=scopes,
203203
quota_project_id=quota_project_id,
204204
client_info=client_info,
205+
always_use_jwt_access=True,
205206
)
206207

207208
if not self._grpc_channel:

google/cloud/websecurityscanner_v1alpha/services/web_security_scanner/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.websecurityscanner_v1alpha.types import finding
2930
from google.cloud.websecurityscanner_v1alpha.types import scan_config
@@ -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 WebSecurityScannerTransport(abc.ABC):
5756
"""Abstract transport class for WebSecurityScanner."""
@@ -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/websecurityscanner_v1alpha/services/web_security_scanner/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

google/cloud/websecurityscanner_v1alpha/services/web_security_scanner/transports/grpc_asyncio.py

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

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

@@ -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:

google/cloud/websecurityscanner_v1beta/services/web_security_scanner/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.websecurityscanner_v1beta.types import finding
2930
from google.cloud.websecurityscanner_v1beta.types import scan_config
@@ -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 WebSecurityScannerTransport(abc.ABC):
5756
"""Abstract transport class for WebSecurityScanner."""
@@ -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 = {

0 commit comments

Comments
 (0)