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

Commit 3ffe025

Browse files
feat: add always_use_jwt_access (#38)
... 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 e9e5993 commit 3ffe025

File tree

7 files changed

+39
-124
lines changed

7 files changed

+39
-124
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/apigateway/__init__.py

google/cloud/apigateway_v1/services/api_gateway_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.apigateway_v1.types import apigateway
3031
from google.longrunning import operations_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 ApiGatewayServiceTransport(abc.ABC):
5453
"""Abstract transport class for ApiGatewayService."""
@@ -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/apigateway_v1/services/api_gateway_service/transports/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def __init__(
153153
scopes=scopes,
154154
quota_project_id=quota_project_id,
155155
client_info=client_info,
156+
always_use_jwt_access=True,
156157
)
157158

158159
if not self._grpc_channel:
@@ -208,14 +209,14 @@ def create_channel(
208209
and ``credentials_file`` are passed.
209210
"""
210211

211-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
212-
213212
return grpc_helpers.create_channel(
214213
host,
215214
credentials=credentials,
216215
credentials_file=credentials_file,
217216
quota_project_id=quota_project_id,
218-
**self_signed_jwt_kwargs,
217+
default_scopes=cls.AUTH_SCOPES,
218+
scopes=scopes,
219+
default_host=cls.DEFAULT_HOST,
219220
**kwargs,
220221
)
221222

google/cloud/apigateway_v1/services/api_gateway_service/transports/grpc_asyncio.py

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

84-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
85-
8684
return grpc_helpers_async.create_channel(
8785
host,
8886
credentials=credentials,
8987
credentials_file=credentials_file,
9088
quota_project_id=quota_project_id,
91-
**self_signed_jwt_kwargs,
89+
default_scopes=cls.AUTH_SCOPES,
90+
scopes=scopes,
91+
default_host=cls.DEFAULT_HOST,
9292
**kwargs,
9393
)
9494

@@ -199,6 +199,7 @@ def __init__(
199199
scopes=scopes,
200200
quota_project_id=quota_project_id,
201201
client_info=client_info,
202+
always_use_jwt_access=True,
202203
)
203204

204205
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
release_status = "Development Status :: 5 - Production/Stable"
2626
url = "https://github.com/googleapis/python-api-gateway"
2727
dependencies = [
28-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
28+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
2929
"proto-plus >= 1.4.0",
3030
"packaging >= 14.3",
3131
]

testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
#
2020
# e.g., if setup.py has "foo >= 1.14.0, < 2.0.0dev",
2121
# Then this file should have foo==1.14.0
22-
google-api-core==1.22.2
22+
google-api-core==1.26.0
2323
proto-plus==1.4.0
2424
packaging==14.3

tests/unit/gapic/apigateway_v1/test_api_gateway_service.py

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
)
4343
from google.cloud.apigateway_v1.services.api_gateway_service import pagers
4444
from google.cloud.apigateway_v1.services.api_gateway_service import transports
45-
from google.cloud.apigateway_v1.services.api_gateway_service.transports.base import (
46-
_API_CORE_VERSION,
47-
)
4845
from google.cloud.apigateway_v1.services.api_gateway_service.transports.base import (
4946
_GOOGLE_AUTH_VERSION,
5047
)
@@ -56,8 +53,9 @@
5653
import google.auth
5754

5855

59-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
60-
# - Delete all the api-core and auth "less than" test cases
56+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
57+
# through google-api-core:
58+
# - Delete the auth "less than" test cases
6159
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
6260
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
6361
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -68,16 +66,6 @@
6866
reason="This test requires google-auth >= 1.25.0",
6967
)
7068

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

8270
def client_cert_source_callback():
8371
return b"cert bytes", b"key bytes"
@@ -141,6 +129,18 @@ def test_api_gateway_service_client_from_service_account_info(client_class):
141129
assert client.transport._host == "apigateway.googleapis.com:443"
142130

143131

132+
@pytest.mark.parametrize(
133+
"client_class", [ApiGatewayServiceClient, ApiGatewayServiceAsyncClient,]
134+
)
135+
def test_api_gateway_service_client_service_account_always_use_jwt(client_class):
136+
with mock.patch.object(
137+
service_account.Credentials, "with_always_use_jwt_access", create=True
138+
) as use_jwt:
139+
creds = service_account.Credentials(None, None, None)
140+
client = client_class(credentials=creds)
141+
use_jwt.assert_called_with(True)
142+
143+
144144
@pytest.mark.parametrize(
145145
"client_class", [ApiGatewayServiceClient, ApiGatewayServiceAsyncClient,]
146146
)
@@ -4356,7 +4356,6 @@ def test_api_gateway_service_transport_auth_adc_old_google_auth(transport_class)
43564356
(transports.ApiGatewayServiceGrpcAsyncIOTransport, grpc_helpers_async),
43574357
],
43584358
)
4359-
@requires_api_core_gte_1_26_0
43604359
def test_api_gateway_service_transport_create_channel(transport_class, grpc_helpers):
43614360
# If credentials and host are not provided, the transport class should use
43624361
# ADC credentials.
@@ -4385,79 +4384,6 @@ def test_api_gateway_service_transport_create_channel(transport_class, grpc_help
43854384
)
43864385

43874386

4388-
@pytest.mark.parametrize(
4389-
"transport_class,grpc_helpers",
4390-
[
4391-
(transports.ApiGatewayServiceGrpcTransport, grpc_helpers),
4392-
(transports.ApiGatewayServiceGrpcAsyncIOTransport, grpc_helpers_async),
4393-
],
4394-
)
4395-
@requires_api_core_lt_1_26_0
4396-
def test_api_gateway_service_transport_create_channel_old_api_core(
4397-
transport_class, grpc_helpers
4398-
):
4399-
# If credentials and host are not provided, the transport class should use
4400-
# ADC credentials.
4401-
with mock.patch.object(
4402-
google.auth, "default", autospec=True
4403-
) as adc, mock.patch.object(
4404-
grpc_helpers, "create_channel", autospec=True
4405-
) as create_channel:
4406-
creds = ga_credentials.AnonymousCredentials()
4407-
adc.return_value = (creds, None)
4408-
transport_class(quota_project_id="octopus")
4409-
4410-
create_channel.assert_called_with(
4411-
"apigateway.googleapis.com:443",
4412-
credentials=creds,
4413-
credentials_file=None,
4414-
quota_project_id="octopus",
4415-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
4416-
ssl_credentials=None,
4417-
options=[
4418-
("grpc.max_send_message_length", -1),
4419-
("grpc.max_receive_message_length", -1),
4420-
],
4421-
)
4422-
4423-
4424-
@pytest.mark.parametrize(
4425-
"transport_class,grpc_helpers",
4426-
[
4427-
(transports.ApiGatewayServiceGrpcTransport, grpc_helpers),
4428-
(transports.ApiGatewayServiceGrpcAsyncIOTransport, grpc_helpers_async),
4429-
],
4430-
)
4431-
@requires_api_core_lt_1_26_0
4432-
def test_api_gateway_service_transport_create_channel_user_scopes(
4433-
transport_class, grpc_helpers
4434-
):
4435-
# If credentials and host are not provided, the transport class should use
4436-
# ADC credentials.
4437-
with mock.patch.object(
4438-
google.auth, "default", autospec=True
4439-
) as adc, mock.patch.object(
4440-
grpc_helpers, "create_channel", autospec=True
4441-
) as create_channel:
4442-
creds = ga_credentials.AnonymousCredentials()
4443-
adc.return_value = (creds, None)
4444-
4445-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
4446-
4447-
create_channel.assert_called_with(
4448-
"apigateway.googleapis.com:443",
4449-
credentials=creds,
4450-
credentials_file=None,
4451-
quota_project_id="octopus",
4452-
scopes=["1", "2"],
4453-
ssl_credentials=None,
4454-
options=[
4455-
("grpc.max_send_message_length", -1),
4456-
("grpc.max_receive_message_length", -1),
4457-
],
4458-
)
4459-
4460-
44614387
@pytest.mark.parametrize(
44624388
"transport_class",
44634389
[

0 commit comments

Comments
 (0)