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

Commit 242aa5e

Browse files
feat: add always_use_jwt_access (#138)
... 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 6177388 commit 242aa5e

File tree

11 files changed

+76
-251
lines changed

11 files changed

+76
-251
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/language/__init__.py

google/cloud/language_v1/services/language_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.language_v1.types import language_service
2930

@@ -43,8 +44,6 @@
4344
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4445
_GOOGLE_AUTH_VERSION = None
4546

46-
_API_CORE_VERSION = google.api_core.__version__
47-
4847

4948
class LanguageServiceTransport(abc.ABC):
5049
"""Abstract transport class for LanguageService."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

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

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

google/cloud/language_v1/services/language_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(
150150
scopes=scopes,
151151
quota_project_id=quota_project_id,
152152
client_info=client_info,
153+
always_use_jwt_access=True,
153154
)
154155

155156
if not self._grpc_channel:
@@ -205,14 +206,14 @@ def create_channel(
205206
and ``credentials_file`` are passed.
206207
"""
207208

208-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
209-
210209
return grpc_helpers.create_channel(
211210
host,
212211
credentials=credentials,
213212
credentials_file=credentials_file,
214213
quota_project_id=quota_project_id,
215-
**self_signed_jwt_kwargs,
214+
default_scopes=cls.AUTH_SCOPES,
215+
scopes=scopes,
216+
default_host=cls.DEFAULT_HOST,
216217
**kwargs,
217218
)
218219

google/cloud/language_v1/services/language_service/transports/grpc_asyncio.py

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

82-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
83-
8482
return grpc_helpers_async.create_channel(
8583
host,
8684
credentials=credentials,
8785
credentials_file=credentials_file,
8886
quota_project_id=quota_project_id,
89-
**self_signed_jwt_kwargs,
87+
default_scopes=cls.AUTH_SCOPES,
88+
scopes=scopes,
89+
default_host=cls.DEFAULT_HOST,
9090
**kwargs,
9191
)
9292

@@ -196,6 +196,7 @@ def __init__(
196196
scopes=scopes,
197197
quota_project_id=quota_project_id,
198198
client_info=client_info,
199+
always_use_jwt_access=True,
199200
)
200201

201202
if not self._grpc_channel:

google/cloud/language_v1beta2/services/language_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.language_v1beta2.types import language_service
2930

@@ -43,8 +44,6 @@
4344
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4445
_GOOGLE_AUTH_VERSION = None
4546

46-
_API_CORE_VERSION = google.api_core.__version__
47-
4847

4948
class LanguageServiceTransport(abc.ABC):
5049
"""Abstract transport class for LanguageService."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

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

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

google/cloud/language_v1beta2/services/language_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(
150150
scopes=scopes,
151151
quota_project_id=quota_project_id,
152152
client_info=client_info,
153+
always_use_jwt_access=True,
153154
)
154155

155156
if not self._grpc_channel:
@@ -205,14 +206,14 @@ def create_channel(
205206
and ``credentials_file`` are passed.
206207
"""
207208

208-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
209-
210209
return grpc_helpers.create_channel(
211210
host,
212211
credentials=credentials,
213212
credentials_file=credentials_file,
214213
quota_project_id=quota_project_id,
215-
**self_signed_jwt_kwargs,
214+
default_scopes=cls.AUTH_SCOPES,
215+
scopes=scopes,
216+
default_host=cls.DEFAULT_HOST,
216217
**kwargs,
217218
)
218219

google/cloud/language_v1beta2/services/language_service/transports/grpc_asyncio.py

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

82-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
83-
8482
return grpc_helpers_async.create_channel(
8583
host,
8684
credentials=credentials,
8785
credentials_file=credentials_file,
8886
quota_project_id=quota_project_id,
89-
**self_signed_jwt_kwargs,
87+
default_scopes=cls.AUTH_SCOPES,
88+
scopes=scopes,
89+
default_host=cls.DEFAULT_HOST,
9090
**kwargs,
9191
)
9292

@@ -196,6 +196,7 @@ def __init__(
196196
scopes=scopes,
197197
quota_project_id=quota_project_id,
198198
client_info=client_info,
199+
always_use_jwt_access=True,
199200
)
200201

201202
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# 'Development Status :: 5 - Production/Stable'
3030
release_status = "Development Status :: 5 - Production/Stable"
3131
dependencies = [
32-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
32+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3333
"proto-plus >= 1.10.0",
3434
"packaging >= 14.3",
3535
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Pin the version to the lower bound.
55
# e.g., if setup.py has "google-cloud-foo >= 1.14.0, < 2.0.0dev",
66
# Then this file should have google-cloud-foo==1.14.0
7-
google-api-core==1.22.2
7+
google-api-core==1.26.0
88
proto-plus==1.10.0
99
libcst==0.2.5
1010
packaging==14.3

0 commit comments

Comments
 (0)