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

Commit d43c40e

Browse files
feat: add always_use_jwt_access (#62)
... 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 f1285fc commit d43c40e

File tree

7 files changed

+39
-124
lines changed

7 files changed

+39
-124
lines changed

.coveragerc

-1
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/video/transcoder/__init__.py

google/cloud/video/transcoder_v1beta1/services/transcoder_service/transports/base.py

+14-26
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.video.transcoder_v1beta1.types import resources
2930
from google.cloud.video.transcoder_v1beta1.types import services
@@ -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 TranscoderServiceTransport(abc.ABC):
5453
"""Abstract transport class for TranscoderService."""
@@ -66,6 +65,7 @@ def __init__(
6665
scopes: Optional[Sequence[str]] = None,
6766
quota_project_id: Optional[str] = None,
6867
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
68+
always_use_jwt_access: Optional[bool] = False,
6969
**kwargs,
7070
) -> None:
7171
"""Instantiate the transport.
@@ -89,6 +89,8 @@ def __init__(
8989
API requests. If ``None``, then default info will be used.
9090
Generally, you only need to set this if you're developing
9191
your own client library.
92+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
93+
be used for service account credentials.
9294
"""
9395
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9496
if ":" not in host:
@@ -117,13 +119,20 @@ def __init__(
117119
**scopes_kwargs, quota_project_id=quota_project_id
118120
)
119121

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

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

128137
# TODO: Remove this function once google-auth >= 1.25.0 is required
129138
@classmethod
@@ -144,27 +153,6 @@ def _get_scopes_kwargs(
144153

145154
return scopes_kwargs
146155

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

google/cloud/video/transcoder_v1beta1/services/transcoder_service/transports/grpc.py

+4-3
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/video/transcoder_v1beta1/services/transcoder_service/transports/grpc_asyncio.py

+4-3
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:

setup.py

+1-1
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

+1-1
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 si transitively required through google-api-core

tests/unit/gapic/transcoder_v1beta1/test_transcoder_service.py

+15-89
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
)
4040
from google.cloud.video.transcoder_v1beta1.services.transcoder_service import pagers
4141
from google.cloud.video.transcoder_v1beta1.services.transcoder_service import transports
42-
from google.cloud.video.transcoder_v1beta1.services.transcoder_service.transports.base import (
43-
_API_CORE_VERSION,
44-
)
4542
from google.cloud.video.transcoder_v1beta1.services.transcoder_service.transports.base import (
4643
_GOOGLE_AUTH_VERSION,
4744
)
@@ -53,8 +50,9 @@
5350
import google.auth
5451

5552

56-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
57-
# - Delete all the api-core and auth "less than" test cases
53+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
54+
# through google-api-core:
55+
# - Delete the auth "less than" test cases
5856
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
5957
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6058
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -65,16 +63,6 @@
6563
reason="This test requires google-auth >= 1.25.0",
6664
)
6765

68-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
69-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
70-
reason="This test requires google-api-core < 1.26.0",
71-
)
72-
73-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
74-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
75-
reason="This test requires google-api-core >= 1.26.0",
76-
)
77-
7866

7967
def client_cert_source_callback():
8068
return b"cert bytes", b"key bytes"
@@ -138,6 +126,18 @@ def test_transcoder_service_client_from_service_account_info(client_class):
138126
assert client.transport._host == "transcoder.googleapis.com:443"
139127

140128

129+
@pytest.mark.parametrize(
130+
"client_class", [TranscoderServiceClient, TranscoderServiceAsyncClient,]
131+
)
132+
def test_transcoder_service_client_service_account_always_use_jwt(client_class):
133+
with mock.patch.object(
134+
service_account.Credentials, "with_always_use_jwt_access", create=True
135+
) as use_jwt:
136+
creds = service_account.Credentials(None, None, None)
137+
client = client_class(credentials=creds)
138+
use_jwt.assert_called_with(True)
139+
140+
141141
@pytest.mark.parametrize(
142142
"client_class", [TranscoderServiceClient, TranscoderServiceAsyncClient,]
143143
)
@@ -2693,7 +2693,6 @@ def test_transcoder_service_transport_auth_adc_old_google_auth(transport_class):
26932693
(transports.TranscoderServiceGrpcAsyncIOTransport, grpc_helpers_async),
26942694
],
26952695
)
2696-
@requires_api_core_gte_1_26_0
26972696
def test_transcoder_service_transport_create_channel(transport_class, grpc_helpers):
26982697
# If credentials and host are not provided, the transport class should use
26992698
# ADC credentials.
@@ -2722,79 +2721,6 @@ def test_transcoder_service_transport_create_channel(transport_class, grpc_helpe
27222721
)
27232722

27242723

2725-
@pytest.mark.parametrize(
2726-
"transport_class,grpc_helpers",
2727-
[
2728-
(transports.TranscoderServiceGrpcTransport, grpc_helpers),
2729-
(transports.TranscoderServiceGrpcAsyncIOTransport, grpc_helpers_async),
2730-
],
2731-
)
2732-
@requires_api_core_lt_1_26_0
2733-
def test_transcoder_service_transport_create_channel_old_api_core(
2734-
transport_class, grpc_helpers
2735-
):
2736-
# If credentials and host are not provided, the transport class should use
2737-
# ADC credentials.
2738-
with mock.patch.object(
2739-
google.auth, "default", autospec=True
2740-
) as adc, mock.patch.object(
2741-
grpc_helpers, "create_channel", autospec=True
2742-
) as create_channel:
2743-
creds = ga_credentials.AnonymousCredentials()
2744-
adc.return_value = (creds, None)
2745-
transport_class(quota_project_id="octopus")
2746-
2747-
create_channel.assert_called_with(
2748-
"transcoder.googleapis.com:443",
2749-
credentials=creds,
2750-
credentials_file=None,
2751-
quota_project_id="octopus",
2752-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
2753-
ssl_credentials=None,
2754-
options=[
2755-
("grpc.max_send_message_length", -1),
2756-
("grpc.max_receive_message_length", -1),
2757-
],
2758-
)
2759-
2760-
2761-
@pytest.mark.parametrize(
2762-
"transport_class,grpc_helpers",
2763-
[
2764-
(transports.TranscoderServiceGrpcTransport, grpc_helpers),
2765-
(transports.TranscoderServiceGrpcAsyncIOTransport, grpc_helpers_async),
2766-
],
2767-
)
2768-
@requires_api_core_lt_1_26_0
2769-
def test_transcoder_service_transport_create_channel_user_scopes(
2770-
transport_class, grpc_helpers
2771-
):
2772-
# If credentials and host are not provided, the transport class should use
2773-
# ADC credentials.
2774-
with mock.patch.object(
2775-
google.auth, "default", autospec=True
2776-
) as adc, mock.patch.object(
2777-
grpc_helpers, "create_channel", autospec=True
2778-
) as create_channel:
2779-
creds = ga_credentials.AnonymousCredentials()
2780-
adc.return_value = (creds, None)
2781-
2782-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
2783-
2784-
create_channel.assert_called_with(
2785-
"transcoder.googleapis.com:443",
2786-
credentials=creds,
2787-
credentials_file=None,
2788-
quota_project_id="octopus",
2789-
scopes=["1", "2"],
2790-
ssl_credentials=None,
2791-
options=[
2792-
("grpc.max_send_message_length", -1),
2793-
("grpc.max_receive_message_length", -1),
2794-
],
2795-
)
2796-
2797-
27982724
@pytest.mark.parametrize(
27992725
"transport_class",
28002726
[

0 commit comments

Comments
 (0)