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

Commit 13ce7ba

Browse files
feat: add always_use_jwt_access (#9)
... 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 9232675 commit 13ce7ba

File tree

6 files changed

+39
-123
lines changed

6 files changed

+39
-123
lines changed

google/cloud/shell_v1/services/cloud_shell_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.shell_v1.types import cloudshell
3031
from google.longrunning import operations_pb2 # type: ignore
@@ -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 CloudShellServiceTransport(abc.ABC):
5251
"""Abstract transport class for CloudShellService."""
@@ -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/shell_v1/services/cloud_shell_service/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def __init__(
159159
scopes=scopes,
160160
quota_project_id=quota_project_id,
161161
client_info=client_info,
162+
always_use_jwt_access=True,
162163
)
163164

164165
if not self._grpc_channel:
@@ -214,14 +215,14 @@ def create_channel(
214215
and ``credentials_file`` are passed.
215216
"""
216217

217-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
218-
219218
return grpc_helpers.create_channel(
220219
host,
221220
credentials=credentials,
222221
credentials_file=credentials_file,
223222
quota_project_id=quota_project_id,
224-
**self_signed_jwt_kwargs,
223+
default_scopes=cls.AUTH_SCOPES,
224+
scopes=scopes,
225+
default_host=cls.DEFAULT_HOST,
225226
**kwargs,
226227
)
227228

google/cloud/shell_v1/services/cloud_shell_service/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ def create_channel(
8787
aio.Channel: A gRPC AsyncIO channel object.
8888
"""
8989

90-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
91-
9290
return grpc_helpers_async.create_channel(
9391
host,
9492
credentials=credentials,
9593
credentials_file=credentials_file,
9694
quota_project_id=quota_project_id,
97-
**self_signed_jwt_kwargs,
95+
default_scopes=cls.AUTH_SCOPES,
96+
scopes=scopes,
97+
default_host=cls.DEFAULT_HOST,
9898
**kwargs,
9999
)
100100

@@ -205,6 +205,7 @@ def __init__(
205205
scopes=scopes,
206206
quota_project_id=quota_project_id,
207207
client_info=client_info,
208+
always_use_jwt_access=True,
208209
)
209210

210211
if not self._grpc_channel:

setup.py

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

+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.15.0
1010
packaging==14.3
1111
google-auth==1.24.0 # TODO: remove when google-auth>=1.25.0 is transitively required through google-auth

tests/unit/gapic/shell_v1/test_cloud_shell_service.py

+15-89
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
)
4040
from google.cloud.shell_v1.services.cloud_shell_service import CloudShellServiceClient
4141
from google.cloud.shell_v1.services.cloud_shell_service import transports
42-
from google.cloud.shell_v1.services.cloud_shell_service.transports.base import (
43-
_API_CORE_VERSION,
44-
)
4542
from google.cloud.shell_v1.services.cloud_shell_service.transports.base import (
4643
_GOOGLE_AUTH_VERSION,
4744
)
@@ -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_cloud_shell_service_client_from_service_account_info(client_class):
137125
assert client.transport._host == "cloudshell.googleapis.com:443"
138126

139127

128+
@pytest.mark.parametrize(
129+
"client_class", [CloudShellServiceClient, CloudShellServiceAsyncClient,]
130+
)
131+
def test_cloud_shell_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", [CloudShellServiceClient, CloudShellServiceAsyncClient,]
142142
)
@@ -1522,7 +1522,6 @@ def test_cloud_shell_service_transport_auth_adc_old_google_auth(transport_class)
15221522
(transports.CloudShellServiceGrpcAsyncIOTransport, grpc_helpers_async),
15231523
],
15241524
)
1525-
@requires_api_core_gte_1_26_0
15261525
def test_cloud_shell_service_transport_create_channel(transport_class, grpc_helpers):
15271526
# If credentials and host are not provided, the transport class should use
15281527
# ADC credentials.
@@ -1551,79 +1550,6 @@ def test_cloud_shell_service_transport_create_channel(transport_class, grpc_help
15511550
)
15521551

15531552

1554-
@pytest.mark.parametrize(
1555-
"transport_class,grpc_helpers",
1556-
[
1557-
(transports.CloudShellServiceGrpcTransport, grpc_helpers),
1558-
(transports.CloudShellServiceGrpcAsyncIOTransport, grpc_helpers_async),
1559-
],
1560-
)
1561-
@requires_api_core_lt_1_26_0
1562-
def test_cloud_shell_service_transport_create_channel_old_api_core(
1563-
transport_class, grpc_helpers
1564-
):
1565-
# If credentials and host are not provided, the transport class should use
1566-
# ADC credentials.
1567-
with mock.patch.object(
1568-
google.auth, "default", autospec=True
1569-
) as adc, mock.patch.object(
1570-
grpc_helpers, "create_channel", autospec=True
1571-
) as create_channel:
1572-
creds = ga_credentials.AnonymousCredentials()
1573-
adc.return_value = (creds, None)
1574-
transport_class(quota_project_id="octopus")
1575-
1576-
create_channel.assert_called_with(
1577-
"cloudshell.googleapis.com:443",
1578-
credentials=creds,
1579-
credentials_file=None,
1580-
quota_project_id="octopus",
1581-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
1582-
ssl_credentials=None,
1583-
options=[
1584-
("grpc.max_send_message_length", -1),
1585-
("grpc.max_receive_message_length", -1),
1586-
],
1587-
)
1588-
1589-
1590-
@pytest.mark.parametrize(
1591-
"transport_class,grpc_helpers",
1592-
[
1593-
(transports.CloudShellServiceGrpcTransport, grpc_helpers),
1594-
(transports.CloudShellServiceGrpcAsyncIOTransport, grpc_helpers_async),
1595-
],
1596-
)
1597-
@requires_api_core_lt_1_26_0
1598-
def test_cloud_shell_service_transport_create_channel_user_scopes(
1599-
transport_class, grpc_helpers
1600-
):
1601-
# If credentials and host are not provided, the transport class should use
1602-
# ADC credentials.
1603-
with mock.patch.object(
1604-
google.auth, "default", autospec=True
1605-
) as adc, mock.patch.object(
1606-
grpc_helpers, "create_channel", autospec=True
1607-
) as create_channel:
1608-
creds = ga_credentials.AnonymousCredentials()
1609-
adc.return_value = (creds, None)
1610-
1611-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
1612-
1613-
create_channel.assert_called_with(
1614-
"cloudshell.googleapis.com:443",
1615-
credentials=creds,
1616-
credentials_file=None,
1617-
quota_project_id="octopus",
1618-
scopes=["1", "2"],
1619-
ssl_credentials=None,
1620-
options=[
1621-
("grpc.max_send_message_length", -1),
1622-
("grpc.max_receive_message_length", -1),
1623-
],
1624-
)
1625-
1626-
16271553
@pytest.mark.parametrize(
16281554
"transport_class",
16291555
[

0 commit comments

Comments
 (0)