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

Commit 6f1f049

Browse files
feat: add always_use_jwt_access (#11)
... 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 61fb8dd commit 6f1f049

File tree

6 files changed

+39
-123
lines changed

6 files changed

+39
-123
lines changed

google/cloud/vpcaccess_v1/services/vpc_access_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.vpcaccess_v1.types import vpc_access
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 VpcAccessServiceTransport(abc.ABC):
5453
"""Abstract transport class for VpcAccessService."""
@@ -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/vpcaccess_v1/services/vpc_access_service/transports/grpc.py

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

159160
if not self._grpc_channel:
@@ -209,14 +210,14 @@ def create_channel(
209210
and ``credentials_file`` are passed.
210211
"""
211212

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

google/cloud/vpcaccess_v1/services/vpc_access_service/transports/grpc_asyncio.py

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

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

@@ -200,6 +200,7 @@ def __init__(
200200
scopes=scopes,
201201
quota_project_id=quota_project_id,
202202
client_info=client_info,
203+
always_use_jwt_access=True,
203204
)
204205

205206
if not self._grpc_channel:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
release_status = "Development Status :: 4 - Beta"
2727
url = "https://github.com/googleapis/python-vpc-access"
2828
dependencies = [
29-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
29+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3030
"proto-plus >= 1.15.0",
3131
"packaging >= 14.3",
3232
]

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.15.0
99
packaging==14.3
1010
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 si transitively required through google-api-core

tests/unit/gapic/vpcaccess_v1/test_vpc_access_service.py

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
from google.cloud.vpcaccess_v1.services.vpc_access_service import VpcAccessServiceClient
4141
from google.cloud.vpcaccess_v1.services.vpc_access_service import pagers
4242
from google.cloud.vpcaccess_v1.services.vpc_access_service import transports
43-
from google.cloud.vpcaccess_v1.services.vpc_access_service.transports.base import (
44-
_API_CORE_VERSION,
45-
)
4643
from google.cloud.vpcaccess_v1.services.vpc_access_service.transports.base import (
4744
_GOOGLE_AUTH_VERSION,
4845
)
@@ -52,8 +49,9 @@
5249
import google.auth
5350

5451

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

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

7866
def client_cert_source_callback():
7967
return b"cert bytes", b"key bytes"
@@ -137,6 +125,18 @@ def test_vpc_access_service_client_from_service_account_info(client_class):
137125
assert client.transport._host == "vpcaccess.googleapis.com:443"
138126

139127

128+
@pytest.mark.parametrize(
129+
"client_class", [VpcAccessServiceClient, VpcAccessServiceAsyncClient,]
130+
)
131+
def test_vpc_access_service_client_service_account_always_use_jwt(client_class):
132+
with mock.patch.object(
133+
service_account.Credentials, "with_always_use_jwt_access", create=True
134+
) as use_jwt:
135+
creds = service_account.Credentials(None, None, None)
136+
client = client_class(credentials=creds)
137+
use_jwt.assert_called_with(True)
138+
139+
140140
@pytest.mark.parametrize(
141141
"client_class", [VpcAccessServiceClient, VpcAccessServiceAsyncClient,]
142142
)
@@ -1719,7 +1719,6 @@ def test_vpc_access_service_transport_auth_adc_old_google_auth(transport_class):
17191719
(transports.VpcAccessServiceGrpcAsyncIOTransport, grpc_helpers_async),
17201720
],
17211721
)
1722-
@requires_api_core_gte_1_26_0
17231722
def test_vpc_access_service_transport_create_channel(transport_class, grpc_helpers):
17241723
# If credentials and host are not provided, the transport class should use
17251724
# ADC credentials.
@@ -1748,79 +1747,6 @@ def test_vpc_access_service_transport_create_channel(transport_class, grpc_helpe
17481747
)
17491748

17501749

1751-
@pytest.mark.parametrize(
1752-
"transport_class,grpc_helpers",
1753-
[
1754-
(transports.VpcAccessServiceGrpcTransport, grpc_helpers),
1755-
(transports.VpcAccessServiceGrpcAsyncIOTransport, grpc_helpers_async),
1756-
],
1757-
)
1758-
@requires_api_core_lt_1_26_0
1759-
def test_vpc_access_service_transport_create_channel_old_api_core(
1760-
transport_class, grpc_helpers
1761-
):
1762-
# If credentials and host are not provided, the transport class should use
1763-
# ADC credentials.
1764-
with mock.patch.object(
1765-
google.auth, "default", autospec=True
1766-
) as adc, mock.patch.object(
1767-
grpc_helpers, "create_channel", autospec=True
1768-
) as create_channel:
1769-
creds = ga_credentials.AnonymousCredentials()
1770-
adc.return_value = (creds, None)
1771-
transport_class(quota_project_id="octopus")
1772-
1773-
create_channel.assert_called_with(
1774-
"vpcaccess.googleapis.com:443",
1775-
credentials=creds,
1776-
credentials_file=None,
1777-
quota_project_id="octopus",
1778-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
1779-
ssl_credentials=None,
1780-
options=[
1781-
("grpc.max_send_message_length", -1),
1782-
("grpc.max_receive_message_length", -1),
1783-
],
1784-
)
1785-
1786-
1787-
@pytest.mark.parametrize(
1788-
"transport_class,grpc_helpers",
1789-
[
1790-
(transports.VpcAccessServiceGrpcTransport, grpc_helpers),
1791-
(transports.VpcAccessServiceGrpcAsyncIOTransport, grpc_helpers_async),
1792-
],
1793-
)
1794-
@requires_api_core_lt_1_26_0
1795-
def test_vpc_access_service_transport_create_channel_user_scopes(
1796-
transport_class, grpc_helpers
1797-
):
1798-
# If credentials and host are not provided, the transport class should use
1799-
# ADC credentials.
1800-
with mock.patch.object(
1801-
google.auth, "default", autospec=True
1802-
) as adc, mock.patch.object(
1803-
grpc_helpers, "create_channel", autospec=True
1804-
) as create_channel:
1805-
creds = ga_credentials.AnonymousCredentials()
1806-
adc.return_value = (creds, None)
1807-
1808-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
1809-
1810-
create_channel.assert_called_with(
1811-
"vpcaccess.googleapis.com:443",
1812-
credentials=creds,
1813-
credentials_file=None,
1814-
quota_project_id="octopus",
1815-
scopes=["1", "2"],
1816-
ssl_credentials=None,
1817-
options=[
1818-
("grpc.max_send_message_length", -1),
1819-
("grpc.max_receive_message_length", -1),
1820-
],
1821-
)
1822-
1823-
18241750
@pytest.mark.parametrize(
18251751
"transport_class",
18261752
[

0 commit comments

Comments
 (0)