Skip to content
This repository was archived by the owner on Mar 15, 2025. It is now read-only.

Commit 6acf334

Browse files
feat: add always_use_jwt_access (#59)
... 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 2111d9d commit 6acf334

File tree

7 files changed

+37
-122
lines changed

7 files changed

+37
-122
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/orgpolicy/__init__.py

google/cloud/orgpolicy_v2/services/org_policy/transports/base.py

+14-26
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.orgpolicy_v2.types import orgpolicy
2930
from google.protobuf import empty_pb2 # type: ignore
@@ -46,8 +47,6 @@
4647
except pkg_resources.DistributionNotFound: # pragma: NO COVER
4748
_GOOGLE_AUTH_VERSION = None
4849

49-
_API_CORE_VERSION = google.api_core.__version__
50-
5150

5251
class OrgPolicyTransport(abc.ABC):
5352
"""Abstract transport class for OrgPolicy."""
@@ -65,6 +64,7 @@ def __init__(
6564
scopes: Optional[Sequence[str]] = None,
6665
quota_project_id: Optional[str] = None,
6766
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
67+
always_use_jwt_access: Optional[bool] = False,
6868
**kwargs,
6969
) -> None:
7070
"""Instantiate the transport.
@@ -88,6 +88,8 @@ def __init__(
8888
API requests. If ``None``, then default info will be used.
8989
Generally, you only need to set this if you're developing
9090
your own client library.
91+
always_use_jwt_access (Optional[bool]): Whether self signed JWT should
92+
be used for service account credentials.
9193
"""
9294
# Save the hostname. Default to port 443 (HTTPS) if none is specified.
9395
if ":" not in host:
@@ -116,13 +118,20 @@ def __init__(
116118
**scopes_kwargs, quota_project_id=quota_project_id
117119
)
118120

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

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

127136
# TODO: Remove this function once google-auth >= 1.25.0 is required
128137
@classmethod
@@ -143,27 +152,6 @@ def _get_scopes_kwargs(
143152

144153
return scopes_kwargs
145154

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

google/cloud/orgpolicy_v2/services/org_policy/transports/grpc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def __init__(
171171
scopes=scopes,
172172
quota_project_id=quota_project_id,
173173
client_info=client_info,
174+
always_use_jwt_access=True,
174175
)
175176

176177
if not self._grpc_channel:
@@ -226,14 +227,14 @@ def create_channel(
226227
and ``credentials_file`` are passed.
227228
"""
228229

229-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
230-
231230
return grpc_helpers.create_channel(
232231
host,
233232
credentials=credentials,
234233
credentials_file=credentials_file,
235234
quota_project_id=quota_project_id,
236-
**self_signed_jwt_kwargs,
235+
default_scopes=cls.AUTH_SCOPES,
236+
scopes=scopes,
237+
default_host=cls.DEFAULT_HOST,
237238
**kwargs,
238239
)
239240

google/cloud/orgpolicy_v2/services/org_policy/transports/grpc_asyncio.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def create_channel(
100100
aio.Channel: A gRPC AsyncIO channel object.
101101
"""
102102

103-
self_signed_jwt_kwargs = cls._get_self_signed_jwt_kwargs(host, scopes)
104-
105103
return grpc_helpers_async.create_channel(
106104
host,
107105
credentials=credentials,
108106
credentials_file=credentials_file,
109107
quota_project_id=quota_project_id,
110-
**self_signed_jwt_kwargs,
108+
default_scopes=cls.AUTH_SCOPES,
109+
scopes=scopes,
110+
default_host=cls.DEFAULT_HOST,
111111
**kwargs,
112112
)
113113

@@ -217,6 +217,7 @@ def __init__(
217217
scopes=scopes,
218218
quota_project_id=quota_project_id,
219219
client_info=client_info,
220+
always_use_jwt_access=True,
220221
)
221222

222223
if not self._grpc_channel:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
platforms="Posix; MacOS X; Windows",
4242
include_package_data=True,
4343
install_requires=(
44-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
44+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
4545
"proto-plus >= 1.10.0",
4646
"packaging >= 14.3",
4747
),

testing/constraints-3.6.txt

+1-1
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.10.0
2424
packaging==14.3

tests/unit/gapic/orgpolicy_v2/test_org_policy.py

+13-87
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
from google.cloud.orgpolicy_v2.services.org_policy import OrgPolicyClient
3636
from google.cloud.orgpolicy_v2.services.org_policy import pagers
3737
from google.cloud.orgpolicy_v2.services.org_policy import transports
38-
from google.cloud.orgpolicy_v2.services.org_policy.transports.base import (
39-
_API_CORE_VERSION,
40-
)
4138
from google.cloud.orgpolicy_v2.services.org_policy.transports.base import (
4239
_GOOGLE_AUTH_VERSION,
4340
)
@@ -49,8 +46,9 @@
4946
import google.auth
5047

5148

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

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

7563
def client_cert_source_callback():
7664
return b"cert bytes", b"key bytes"
@@ -126,6 +114,16 @@ def test_org_policy_client_from_service_account_info(client_class):
126114
assert client.transport._host == "orgpolicy.googleapis.com:443"
127115

128116

117+
@pytest.mark.parametrize("client_class", [OrgPolicyClient, OrgPolicyAsyncClient,])
118+
def test_org_policy_client_service_account_always_use_jwt(client_class):
119+
with mock.patch.object(
120+
service_account.Credentials, "with_always_use_jwt_access", create=True
121+
) as use_jwt:
122+
creds = service_account.Credentials(None, None, None)
123+
client = client_class(credentials=creds)
124+
use_jwt.assert_called_with(True)
125+
126+
129127
@pytest.mark.parametrize("client_class", [OrgPolicyClient, OrgPolicyAsyncClient,])
130128
def test_org_policy_client_from_service_account_file(client_class):
131129
creds = ga_credentials.AnonymousCredentials()
@@ -2321,7 +2319,6 @@ def test_org_policy_transport_auth_adc_old_google_auth(transport_class):
23212319
(transports.OrgPolicyGrpcAsyncIOTransport, grpc_helpers_async),
23222320
],
23232321
)
2324-
@requires_api_core_gte_1_26_0
23252322
def test_org_policy_transport_create_channel(transport_class, grpc_helpers):
23262323
# If credentials and host are not provided, the transport class should use
23272324
# ADC credentials.
@@ -2350,77 +2347,6 @@ def test_org_policy_transport_create_channel(transport_class, grpc_helpers):
23502347
)
23512348

23522349

2353-
@pytest.mark.parametrize(
2354-
"transport_class,grpc_helpers",
2355-
[
2356-
(transports.OrgPolicyGrpcTransport, grpc_helpers),
2357-
(transports.OrgPolicyGrpcAsyncIOTransport, grpc_helpers_async),
2358-
],
2359-
)
2360-
@requires_api_core_lt_1_26_0
2361-
def test_org_policy_transport_create_channel_old_api_core(
2362-
transport_class, grpc_helpers
2363-
):
2364-
# If credentials and host are not provided, the transport class should use
2365-
# ADC credentials.
2366-
with mock.patch.object(
2367-
google.auth, "default", autospec=True
2368-
) as adc, mock.patch.object(
2369-
grpc_helpers, "create_channel", autospec=True
2370-
) as create_channel:
2371-
creds = ga_credentials.AnonymousCredentials()
2372-
adc.return_value = (creds, None)
2373-
transport_class(quota_project_id="octopus")
2374-
2375-
create_channel.assert_called_with(
2376-
"orgpolicy.googleapis.com:443",
2377-
credentials=creds,
2378-
credentials_file=None,
2379-
quota_project_id="octopus",
2380-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
2381-
ssl_credentials=None,
2382-
options=[
2383-
("grpc.max_send_message_length", -1),
2384-
("grpc.max_receive_message_length", -1),
2385-
],
2386-
)
2387-
2388-
2389-
@pytest.mark.parametrize(
2390-
"transport_class,grpc_helpers",
2391-
[
2392-
(transports.OrgPolicyGrpcTransport, grpc_helpers),
2393-
(transports.OrgPolicyGrpcAsyncIOTransport, grpc_helpers_async),
2394-
],
2395-
)
2396-
@requires_api_core_lt_1_26_0
2397-
def test_org_policy_transport_create_channel_user_scopes(transport_class, grpc_helpers):
2398-
# If credentials and host are not provided, the transport class should use
2399-
# ADC credentials.
2400-
with mock.patch.object(
2401-
google.auth, "default", autospec=True
2402-
) as adc, mock.patch.object(
2403-
grpc_helpers, "create_channel", autospec=True
2404-
) as create_channel:
2405-
creds = ga_credentials.AnonymousCredentials()
2406-
adc.return_value = (creds, None)
2407-
2408-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
2409-
2410-
create_channel.assert_called_with(
2411-
"orgpolicy.googleapis.com:443",
2412-
credentials=creds,
2413-
credentials_file=None,
2414-
quota_project_id="octopus",
2415-
scopes=["1", "2"],
2416-
ssl_credentials=None,
2417-
options=[
2418-
("grpc.max_send_message_length", -1),
2419-
("grpc.max_receive_message_length", -1),
2420-
],
2421-
)
2422-
2423-
24242350
@pytest.mark.parametrize(
24252351
"transport_class",
24262352
[transports.OrgPolicyGrpcTransport, transports.OrgPolicyGrpcAsyncIOTransport],

0 commit comments

Comments
 (0)