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

Commit d2005b7

Browse files
feat: add always_use_jwt_access (#77)
... 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 8b2ac6d commit d2005b7

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

google/cloud/functions_v1/services/cloud_functions_service/transports/base.py

+14-26
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.functions_v1.types import functions
3031
from google.iam.v1 import iam_policy_pb2 # type: ignore
@@ -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 CloudFunctionsServiceTransport(abc.ABC):
5453
"""Abstract transport class for CloudFunctionsService."""
@@ -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/functions_v1/services/cloud_functions_service/transports/grpc.py

+4-3
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/functions_v1/services/cloud_functions_service/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ def create_channel(
8383
aio.Channel: A gRPC AsyncIO channel object.
8484
"""
8585

86-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
87-
8886
return grpc_helpers_async.create_channel(
8987
host,
9088
credentials=credentials,
9189
credentials_file=credentials_file,
9290
quota_project_id=quota_project_id,
93-
**self_signed_jwt_kwargs,
91+
default_scopes=cls.AUTH_SCOPES,
92+
scopes=scopes,
93+
default_host=cls.DEFAULT_HOST,
9494
**kwargs,
9595
)
9696

@@ -201,6 +201,7 @@ def __init__(
201201
scopes=scopes,
202202
quota_project_id=quota_project_id,
203203
client_info=client_info,
204+
always_use_jwt_access=True,
204205
)
205206

206207
if not self._grpc_channel:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
platforms="Posix; MacOS X; Windows",
4646
include_package_data=True,
4747
install_requires=(
48-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
48+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4949
"proto-plus >= 1.10.0",
5050
"packaging >= 14.3",
5151
"grpc-google-iam-v1 >= 0.12.3, < 0.13dev",

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.10.0
1010
grpc-google-iam-v1==0.12.3
1111
packaging==14.3

tests/unit/gapic/functions_v1/test_cloud_functions_service.py

+15-89
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
)
4343
from google.cloud.functions_v1.services.cloud_functions_service import pagers
4444
from google.cloud.functions_v1.services.cloud_functions_service import transports
45-
from google.cloud.functions_v1.services.cloud_functions_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.functions_v1.services.cloud_functions_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -62,8 +59,9 @@
6259
import google.auth
6360

6461

65-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
66-
# - Delete all the api-core and auth "less than" test cases
62+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
63+
# through google-api-core:
64+
# - Delete the auth "less than" test cases
6765
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6866
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6967
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -74,16 +72,6 @@
7472
reason="This test requires google-auth >= 1.25.0",
7573
)
7674

77-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
78-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
79-
reason="This test requires google-api-core < 1.26.0",
80-
)
81-
82-
requires_api_core_gte_1_26_0 = pytest.mark.skipif(
83-
packaging.version.parse(_API_CORE_VERSION) < packaging.version.parse("1.26.0"),
84-
reason="This test requires google-api-core >= 1.26.0",
85-
)
86-
8775

8876
def client_cert_source_callback():
8977
return b"cert bytes", b"key bytes"
@@ -147,6 +135,18 @@ def test_cloud_functions_service_client_from_service_account_info(client_class):
147135
assert client.transport._host == "cloudfunctions.googleapis.com:443"
148136

149137

138+
@pytest.mark.parametrize(
139+
"client_class", [CloudFunctionsServiceClient, CloudFunctionsServiceAsyncClient,]
140+
)
141+
def test_cloud_functions_service_client_service_account_always_use_jwt(client_class):
142+
with mock.patch.object(
143+
service_account.Credentials, "with_always_use_jwt_access", create=True
144+
) as use_jwt:
145+
creds = service_account.Credentials(None, None, None)
146+
client = client_class(credentials=creds)
147+
use_jwt.assert_called_with(True)
148+
149+
150150
@pytest.mark.parametrize(
151151
"client_class", [CloudFunctionsServiceClient, CloudFunctionsServiceAsyncClient,]
152152
)
@@ -2955,7 +2955,6 @@ def test_cloud_functions_service_transport_auth_adc_old_google_auth(transport_cl
29552955
(transports.CloudFunctionsServiceGrpcAsyncIOTransport, grpc_helpers_async),
29562956
],
29572957
)
2958-
@requires_api_core_gte_1_26_0
29592958
def test_cloud_functions_service_transport_create_channel(
29602959
transport_class, grpc_helpers
29612960
):
@@ -2986,79 +2985,6 @@ def test_cloud_functions_service_transport_create_channel(
29862985
)
29872986

29882987

2989-
@pytest.mark.parametrize(
2990-
"transport_class,grpc_helpers",
2991-
[
2992-
(transports.CloudFunctionsServiceGrpcTransport, grpc_helpers),
2993-
(transports.CloudFunctionsServiceGrpcAsyncIOTransport, grpc_helpers_async),
2994-
],
2995-
)
2996-
@requires_api_core_lt_1_26_0
2997-
def test_cloud_functions_service_transport_create_channel_old_api_core(
2998-
transport_class, grpc_helpers
2999-
):
3000-
# If credentials and host are not provided, the transport class should use
3001-
# ADC credentials.
3002-
with mock.patch.object(
3003-
google.auth, "default", autospec=True
3004-
) as adc, mock.patch.object(
3005-
grpc_helpers, "create_channel", autospec=True
3006-
) as create_channel:
3007-
creds = ga_credentials.AnonymousCredentials()
3008-
adc.return_value = (creds, None)
3009-
transport_class(quota_project_id="octopus")
3010-
3011-
create_channel.assert_called_with(
3012-
"cloudfunctions.googleapis.com:443",
3013-
credentials=creds,
3014-
credentials_file=None,
3015-
quota_project_id="octopus",
3016-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
3017-
ssl_credentials=None,
3018-
options=[
3019-
("grpc.max_send_message_length", -1),
3020-
("grpc.max_receive_message_length", -1),
3021-
],
3022-
)
3023-
3024-
3025-
@pytest.mark.parametrize(
3026-
"transport_class,grpc_helpers",
3027-
[
3028-
(transports.CloudFunctionsServiceGrpcTransport, grpc_helpers),
3029-
(transports.CloudFunctionsServiceGrpcAsyncIOTransport, grpc_helpers_async),
3030-
],
3031-
)
3032-
@requires_api_core_lt_1_26_0
3033-
def test_cloud_functions_service_transport_create_channel_user_scopes(
3034-
transport_class, grpc_helpers
3035-
):
3036-
# If credentials and host are not provided, the transport class should use
3037-
# ADC credentials.
3038-
with mock.patch.object(
3039-
google.auth, "default", autospec=True
3040-
) as adc, mock.patch.object(
3041-
grpc_helpers, "create_channel", autospec=True
3042-
) as create_channel:
3043-
creds = ga_credentials.AnonymousCredentials()
3044-
adc.return_value = (creds, None)
3045-
3046-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
3047-
3048-
create_channel.assert_called_with(
3049-
"cloudfunctions.googleapis.com:443",
3050-
credentials=creds,
3051-
credentials_file=None,
3052-
quota_project_id="octopus",
3053-
scopes=["1", "2"],
3054-
ssl_credentials=None,
3055-
options=[
3056-
("grpc.max_send_message_length", -1),
3057-
("grpc.max_receive_message_length", -1),
3058-
],
3059-
)
3060-
3061-
30622988
@pytest.mark.parametrize(
30632989
"transport_class",
30642990
[

0 commit comments

Comments
 (0)