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

Commit 9b3584d

Browse files
feat: add always_use_jwt_access (#70)
* chore: remove all monolith Bazel deps chore: release gapic-generator-csharp v1.3.7 chore: release gapic-generator-go 0.20.5 chore: release gapic-generator-java 1.0.14 chore: release gapic-generator-php 1.0.1 chore: release gapic-generator-python 0.50.0 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 * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/master/packages/owl-bot/README.md * fix: require google-api-core>=1.26.0 * fix: fix typo in setup.py Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Bu Sun Kim <busunkim@google.com> Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
1 parent 559816a commit 9b3584d

File tree

11 files changed

+82
-257
lines changed

11 files changed

+82
-257
lines changed

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

google/cloud/security/privateca_v1/services/certificate_authority_service/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.security.privateca_v1.types import resources
3031
from google.cloud.security.privateca_v1.types import service
@@ -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 CertificateAuthorityServiceTransport(abc.ABC):
5554
"""Abstract transport class for CertificateAuthorityService."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

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

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

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

google/cloud/security/privateca_v1/services/certificate_authority_service/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

google/cloud/security/privateca_v1/services/certificate_authority_service/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:

google/cloud/security/privateca_v1beta1/services/certificate_authority_service/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.security.privateca_v1beta1.types import resources
3031
from google.cloud.security.privateca_v1beta1.types import service
@@ -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 CertificateAuthorityServiceTransport(abc.ABC):
5554
"""Abstract transport class for CertificateAuthorityService."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

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

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

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

google/cloud/security/privateca_v1beta1/services/certificate_authority_service/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

google/cloud/security/privateca_v1beta1/services/certificate_authority_service/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:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
platforms="Posix; MacOS X; Windows",
4242
include_package_data=True,
4343
install_requires=(
44-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
44+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4545
"proto-plus >= 1.4.0",
4646
"packaging >= 14.3",
4747
),

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
#
66
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
77
# Then this file should have foo==1.14.0
8-
google-api-core==1.22.2
8+
google-api-core==1.26.0
99
proto-plus==1.4.0
1010
packaging==14.3

0 commit comments

Comments
 (0)