10000 feat: add always_use_jwt_access (#88) · googleapis/google-cloud-python@66208b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66208b2

Browse files
feat: add always_use_jwt_access (#88)
... 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: googleapis/googleapis-gen@27e4c88
1 parent 650179d commit 66208b2

File tree

6 files changed

+39
-126
lines changed

6 files changed

+39
-126
lines changed

packages/google-cloud-os-login/google/cloud/oslogin_v1/services/os_login_service/transports/base.py

Lines changed: 14 additions & 26 deletions
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.oslogin_v1 import common # type: ignore
2930
from google.cloud.oslogin_v1.types import oslogin
@@ -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 OsLoginServiceTransport(abc.ABC):
5251
"""Abstract transport class for OsLoginService."""
@@ -67,6 +66,7 @@ def __init__(
6766
scopes: Optional[Sequence[str]] = None,
6867
quota_project_id: Optional[str] = None,
6968
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
69+
always_use_jwt_access: Optional[bool] = False,
7070
**kwargs,
7171
) -> None:
7272
"""Instantiate the transport.
@@ -90,6 +90,8 @@ def __init__(
9090
API requests. If ``None``, then default info will be used.
9191
Generally, you only need to set this if you're developing
9292
your own client library.
93+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
94+
be used for service account credentials.
9395
"""
9496
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9597
if ":" not in host:
@@ -118,13 +120,20 @@ def __init__(
118120
**scopes_kwargs, quota_project_id=quota_project_id
119121
)
120122

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

124-
# TODO(busunkim): These two class methods are in the base transport
134+
# TODO(busunkim): This method is in the base transport
125135
# to avoid duplicating code across the transport classes. These functions
126-
# should be deleted once the minimum required versions of google-api-core
127-
# and google-auth are increased.
136+
# should be deleted once the minimum required versions of google-auth is increased.
128137

129138
# TODO: Remove this function once google-auth >= 1.25.0 is required
130139
@classmethod
@@ -145,27 +154,6 @@ def _get_scopes_kwargs(
145154

146155
return scopes_kwargs
147156

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

packages/google-cloud-os-login/google/cloud/oslogin_v1/services/os_login_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

packages/google-cloud-os-login/google/cloud/oslogin_v1/services/os_login_service/transports/grpc_asyncio.py

Lines changed: 4 additions & 3 deletions
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

@@ -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:

packages/google-cloud-os-login/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# 'Development Status :: 5 - Production/Stable'
3030
release_status = "Development Status :: 5 - Production/Stable"
3131
dependencies = [
32-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
32+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
3333
"proto-plus >= 1.4.0",
3434
"libcst >= 0.2.5",
3535
"packaging >= 14.3",

packages/google-cloud-os-login/testing/constraints-3.6.txt

Lines changed: 1 addition & 1 deletion
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
libcst==0.2.5
1111
packaging==14.3

packages/google-cloud-os-login/tests/unit/gapic/oslogin_v1/test_os_login_service.py

Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
from google.cloud.oslogin_v1.services.os_login_service import OsLoginServiceAsyncClient
3636
from google.cloud.oslogin_v1.services.os_login_service import OsLoginServiceClient
3737
from google.cloud.oslogin_v1.services.os_login_service import transports
38-
from google.cloud.oslogin_v1.services.os_login_service.transports.base import (
39-
_API_CORE_VERSION,
40-
)
4138
from google.cloud.oslogin_v1.services.os_login_service.transports.base import (
4239
_GOOGLE_AUTH_VERSION,
4340
)
@@ -47,8 +44,9 @@
4744
import google.auth
4845

4946

50-
# TODO(busunkim): Once google-api-core >= 1.26.0 is required:
51-
# - Delete all the api-core and auth "less than" test cases
47+
# TODO(busunkim): Once google-auth >= 1.25.0 is required transitively
48+
# through google-api-core:
49+
# - Delete the auth "less than" test cases
5250
# - Delete these pytest markers (Make the "greater than or equal to" tests the default).
5351
requires_google_auth_lt_1_25_0 = pytest.mark.skipif(
5452
packaging.version.parse(_GOOGLE_AUTH_VERSION) >= packaging.version.parse("1.25.0"),
@@ -59,16 +57,6 @@
5957
reason="This test requires google-auth >= 1.25.0",
6058
)
6159

62-
requires_api_core_lt_1_26_0 = pytest.mark.skipif(
63-
packaging.version.parse(_API_CORE_VERSION) >= packaging.version.parse("1.26.0"),
64-
reason="This test requires google-api-core < 1.26.0",
65-
)
66-
67-
requires_api_core_gte_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-
7260

7361
def client_cert_source_callback():
7462
return b"cert bytes", b"key bytes"
@@ -131,6 +119,18 @@ def test_os_login_service_client_from_service_account_info(client_class):
131119
assert client.transport._host == "oslogin.googleapis.com:443"
132120

133121

122+
@pytest.mark.parametrize(
123+
"client_class", [OsLoginServiceClient, OsLoginServiceAsyncClient,]
124+
)
125+
def test_os_login_service_client_service_account_always_use_jwt(client_class):
126+
with mock.patch.object(
127+
service_account.Credentials, "with_always_use_jwt_access", create=True
128+
) as use_jwt:
129+
creds = service_account.Credentials(None, None, None)
130+
client = client_class(credentials=creds)
131+
use_jwt.assert_called_with(True)
132+
133+
134134
@pytest.mark.parametrize(
135135
"client_class", [OsLoginServiceClient, OsLoginServiceAsyncClient,]
136136
)
@@ -2062,7 +2062,6 @@ def test_os_login_service_transport_auth_adc_old_google_auth(transport_class):
20622062
(transports.OsLoginServiceGrpcAsyncIOTransport, grpc_helpers_async),
20632063
],
20642064
)
2065-
@requires_api_core_gte_1_26_0
20662065
def test_os_login_service_transport_create_channel(transport_class, grpc_helpers):
20672066
# If credentials and host are not provided, the transport class should use
20682067
# ADC credentials.
@@ -2094,82 +2093,6 @@ def test_os_login_service_transport_create_channel(transport_class, grpc_helpers
20942093
)
20952094

20962095

2097-
@pytest.mark.parametrize(
2098-
"transport_class,grpc_helpers",
2099-
[
2100-
(transports.OsLoginServiceGrpcTransport, grpc_helpers),
2101-
(transports.OsLoginServiceGrpcAsyncIOTransport, grpc_helpers_async),
2102-
],
2103-
)
2104-
@requires_api_core_lt_1_26_0
2105-
def test_os_login_service_transport_create_channel_old_api_core(
2106-
transport_class, grpc_helpers
2107-
):
2108-
# If credentials and host are not provided, the transport class should use
2109-
# ADC credentials.
2110-
with mock.patch.object(
2111-
google.auth, "default", autospec=True
2112-
) as adc, mock.patch.object(
2113-
grpc_helpers, "create_channel", autospec=True
2114-
) as create_channel:
2115-
creds = ga_credentials.AnonymousCredentials()
2116-
adc.return_value = (creds, None)
2117-
transport_class(quota_project_id="octopus")
2118-
2119-
create_channel.assert_called_with(
2120-
"oslogin.googleapis.com:443",
2121-
credentials=creds,
2122-
credentials_file=None,
2123-
quota_project_id="octopus",
2124-
scopes=(
2125-
"https://www.googleapis.com/auth/cloud-platform",
2126-
"https://www.googleapis.com/auth/compute",
2127-
),
2128-
ssl_credentials=None,
2129-
options=[
2130-
("grpc.max_send_message_length", -1),
2131-
("grpc.max_receive_message_length", -1),
2132-
],
2133-
)
2134-
2135-
2136-
@pytest.mark.parametrize(
2137-
"transport_class,grpc_helpers",
2138-
[
2139-
(transports.OsLoginServiceGrpcTransport, grpc_helpers),
2140-
(transports.OsLoginServiceGrpcAsyncIOTransport, grpc_helpers_async),
2141-
],
2142-
)
2143-
@requires_api_core_lt_1_26_0
2144-
def test_os_login_service_transport_create_channel_user_scopes(
2145-
transport_class, grpc_helpers
2146-
):
2147-
# If credentials and host are not provided, the transport class should use
2148-
# ADC credentials.
2149-
with mock.patch.object(
2150-
google.auth, "default", autospec=True
2151-
) as adc, mock.patch.object(
2152-
grpc_helpers, "create_channel", autospec=True
2153-
) as create_channel:
2154-
creds = ga_credentials.AnonymousCredentials()
2155-
adc.return_value = (creds, None)
2156-
2157-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
2158-
2159-
create_channel.assert_called_with(
2160-
"oslogin.googleapis.com:443",
2161-
credentials=creds,
2162-
credentials_file=None,
2163-
quota_project_id="octopus",
2164-
scopes=["1", "2"],
2165-
ssl_credentials=None,
2166-
options=[
2167-
("grpc.max_send_message_length", -1),
2168-
("grpc.max_receive_message_length", -1),
2169-
],
2170-
)
2171-
2172-
21732096
@pytest.mark.parametrize(
21742097
"transport_class",
21752098
[

0 commit comments

Comments
 (0)
0