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

Commit cd10c37

Browse files
feat: add always_use_jwt_access (#155)
... 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 23132d6 commit cd10c37

File tree

11 files changed

+72
-245
lines changed

11 files changed

+72
-245
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/texttospeech/__init__.py

google/cloud/texttospeech_v1/services/text_to_speech/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.texttospeech_v1.types import cloud_tts
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 TextToSpeechTransport(abc.ABC):
5251
"""Abstract transport class for TextToSpeech."""
@@ -64,6 +63,7 @@ def __init__(
6463
scopes: Optional[Sequence[str]] = None,
6564
quota_project_id: Optional[str] = None,
6665
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
66+
always_use_jwt_access: Optional[bool] = False,
6767
**kwargs,
6868
) -> None:
6969
"""Instantiate the transport.
@@ -87,6 +87,8 @@ def __init__(
8787
API requests. If ``None``, then default info will be used.
8888
Generally, you only need to set this if you're developing
8989
your own client library.
90+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91+
be used for service account credentials.
9092
"""
9193
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9294
if ":" not in host:
@@ -115,13 +117,20 @@ def __init__(
115117
**scopes_kwargs, quota_project_id=quota_project_id
116118
)
117119

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

121-
# TODO(busunkim): These two class methods are in the base transport
131+
# TODO(busunkim): This method is in the base transport
122132
# to avoid duplicating code across the transport classes. These functions
123-
# should be deleted once the minimum required versions of google-api-core
124-
# and google-auth are increased.
133+
# should be deleted once the minimum required versions of google-auth is increased.
125134

126135
# TODO: Remove this function once google-auth >= 1.25.0 is required
127136
@classmethod
@@ -142,27 +151,6 @@ def _get_scopes_kwargs(
142151

143152
return scopes_kwargs
144153

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

google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc.py

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

154155
if not self._grpc_channel:
@@ -204,14 +205,14 @@ def create_channel(
204205
and ``credentials_file`` are passed.
205206
"""
206207

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

google/cloud/texttospeech_v1/services/text_to_speech/transports/grpc_asyncio.py

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

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

@@ -195,6 +195,7 @@ def __init__(
195195
scopes=scopes,
196196
quota_project_id=quota_project_id,
197197
client_info=client_info,
198+
always_use_jwt_access=True,
198199
)
199200

200201
if not self._grpc_channel:

google/cloud/texttospeech_v1beta1/services/text_to_speech/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.texttospeech_v1beta1.types import cloud_tts
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 TextToSpeechTransport(abc.ABC):
5251
"""Abstract transport class for TextToSpeech."""
@@ -64,6 +63,7 @@ def __init__(
6463
scopes: Optional[Sequence[str]] = None,
6564
quota_project_id: Optional[str] = None,
6665
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
66+
always_use_jwt_access: Optional[bool] = False,
6767
**kwargs,
6868
) -> None:
6969
"""Instantiate the transport.
@@ -87,6 +87,8 @@ def __init__(
8787
API requests. If ``None``, then default info will be used.
8888
Generally, you only need to set this if you're developing
8989
your own client library.
90+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
91+
be used for service account credentials.
9092
"""
9193
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9294
if ":" not in host:
@@ -115,13 +117,20 @@ def __init__(
115117
**scopes_kwargs, quota_project_id=quota_project_id
116118
)
117119

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

121-
# TODO(busunkim): These two class methods are in the base transport
131+
# TODO(busunkim): This method is in the base transport
122132
# to avoid duplicating code across the transport classes. These functions
123-
# should be deleted once the minimum required versions of google-api-core
124-
# and google-auth are increased.
133+
# should be deleted once the minimum required versions of google-auth is increased.
125134

126135
# TODO: Remove this function once google-auth >= 1.25.0 is required
127136
@classmethod
@@ -142,27 +151,6 @@ def _get_scopes_kwargs(
142151

143152
return scopes_kwargs
144153

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

google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc.py

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

154155
if not self._grpc_channel:
@@ -204,14 +205,14 @@ def create_channel(
204205
and ``credentials_file`` are passed.
205206
"""
206207

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

google/cloud/texttospeech_v1beta1/services/text_to_speech/transports/grpc_asyncio.py

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

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

@@ -195,6 +195,7 @@ def __init__(
195195
scopes=scopes,
196196
quota_project_id=quota_project_id,
197197
client_info=client_info,
198+
always_use_jwt_access=True,
198199
)
199200

200201
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
# 'Development Status :: 5 - Production/Stable'
4343
release_status = "Development Status :: 5 - Production/Stable"
4444
dependencies = [
45-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
45+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4646
"proto-plus >= 1.4.0",
4747
"packaging >= 14.3",
4848
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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
1111
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 is transitively required through google-api-core

0 commit comments

Comments
 (0)