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

Commit 63a3c9a

Browse files
feat: add always_use_jwt_access (#48)
... 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 fd4062d commit 63a3c9a

File tree

6 files changed

+45
-129
lines changed

6 files changed

+45
-129
lines changed

google/cloud/binaryauthorization_v1beta1/services/binauthz_management_service_v1_beta1/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.binaryauthorization_v1beta1.types import resources
2930
from google.cloud.binaryauthorization_v1beta1.types import service
@@ -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 BinauthzManagementServiceV1Beta1Transport(abc.ABC):
5453
"""Abstract transport class for BinauthzManagementServiceV1Beta1."""
@@ -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/binaryauthorization_v1beta1/services/binauthz_management_service_v1_beta1/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/binaryauthorization_v1beta1/services/binauthz_management_service_v1_beta1/transports/grpc_asyncio.py

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

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

@@ -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
@@ -46,7 +46,7 @@
4646
platforms="Posix; MacOS X; Windows",
4747
include_package_data=True,
4848
install_requires=(
49-
"google-api-core[grpc] >= 1.22.2, < 2.0.0dev",
49+
"google-api-core[grpc] >= 1.26.0, <2.0.0dev",
5050
"proto-plus >= 1.4.0",
5151
"packaging >= 14.3",
5252
),

testing/constraints-3.6.txt

+1-1
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.4.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/binaryauthorization_v1beta1/test_binauthz_management_service_v1_beta1.py

+21-95
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
from google.cloud.binaryauthorization_v1beta1.services.binauthz_management_service_v1_beta1 import (
4444
transports,
4545
)
46-
from google.cloud.binaryauthorization_v1beta1.services.binauthz_management_service_v1_beta1.transports.base import (
47-
_API_CORE_VERSION,
48-
)
4946
from google.cloud.binaryauthorization_v1beta1.services.binauthz_management_service_v1_beta1.transports.base import (
5047
_GOOGLE_AUTH_VERSION,
5148
)
@@ -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"
@@ -155,6 +143,24 @@ def test_binauthz_management_service_v1_beta1_client_from_service_account_info(
155143
assert client.transport._host == "binaryauthorization.googleapis.com:443"
156144

157145

146+
@pytest.mark.parametrize(
147+
"client_class",
148+
[
149+
BinauthzManagementServiceV1Beta1Client,
150+
BinauthzManagementServiceV1Beta1AsyncClient,
151+
],
152+
)
153+
def test_binauthz_management_service_v1_beta1_client_service_account_always_use_jwt(
154+
client_class,
155+
):
156+
with mock.patch.object(
157+
service_account.Credentials, "with_always_use_jwt_access", create=True
158+
) as use_jwt:
159+
creds = service_account.Credentials(None, None, None)
160+
client = client_class(credentials=creds)
161+
use_jwt.assert_called_with(True)
162+
163+
158164
@pytest.mark.parametrize(
159165
"client_class",
160166
[
@@ -2432,7 +2438,6 @@ def test_binauthz_management_service_v1_beta1_transport_auth_adc_old_google_auth
24322438
),
24332439
],
24342440
)
2435-
@requires_api_core_gte_1_26_0
24362441
def test_binauthz_management_service_v1_beta1_transport_create_channel(
24372442
transport_class, grpc_helpers
24382443
):
@@ -2463,85 +2468,6 @@ def test_binauthz_management_service_v1_beta1_transport_create_channel(
24632468
)
24642469

24652470

2466-
@pytest.mark.parametrize(
2467-
"transport_class,grpc_helpers",
2468-
[
2469-
(transports.BinauthzManagementServiceV1Beta1GrpcTransport, grpc_helpers),
2470-
(
2471-
transports.BinauthzManagementServiceV1Beta1GrpcAsyncIOTransport,
2472-
grpc_helpers_async,
2473-
),
2474-
],
2475-
)
2476-
@requires_api_core_lt_1_26_0
2477-
def test_binauthz_management_service_v1_beta1_transport_create_channel_old_api_core(
2478-
transport_class, grpc_helpers
2479-
):
2480-
# If credentials and host are not provided, the transport class should use
2481-
# ADC credentials.
2482-
with mock.patch.object(
2483-
google.auth, "default", autospec=True
2484-
) as adc, mock.patch.object(
2485-
grpc_helpers, "create_channel", autospec=True
2486-
) as create_channel:
2487-
creds = ga_credentials.AnonymousCredentials()
2488-
adc.return_value = (creds, None)
2489-
transport_class(quota_project_id="octopus")
2490-
2491-
create_channel.assert_called_with(
2492-
"binaryauthorization.googleapis.com:443",
2493-
credentials=creds,
2494-
credentials_file=None,
2495-
quota_project_id="octopus",
2496-
scopes=("https://www.googleapis.com/auth/cloud-platform",),
2497-
ssl_credentials=None,
2498-
options=[
2499-
("grpc.max_send_message_length", -1),
2500-
("grpc.max_receive_message_length", -1),
2501-
],
2502-
)
2503-
2504-
2505-
@pytest.mark.parametrize(
2506-
"transport_class,grpc_helpers",
2507-
[
2508-
(transports.BinauthzManagementServiceV1Beta1GrpcTransport, grpc_helpers),
2509-
(
2510-
transports.BinauthzManagementServiceV1Beta1GrpcAsyncIOTransport,
2511-
grpc_helpers_async,
2512-
),
2513-
],
2514-
)
2515-
@requires_api_core_lt_1_26_0
2516-
def test_binauthz_management_service_v1_beta1_transport_create_channel_user_scopes(
2517-
transport_class, grpc_helpers
2518-
):
2519-
# If credentials and host are not provided, the transport class should use
2520-
# ADC credentials.
2521-
with mock.patch.object(
2522-
google.auth, "default", autospec=True
2523-
) as adc, mock.patch.object(
2524-
grpc_helpers, "create_channel", autospec=True
2525-
) as create_channel:
2526-
creds = ga_credentials.AnonymousCredentials()
2527-
adc.return_value = (creds, None)
2528-
2529-
transport_class(quota_project_id="octopus", scopes=["1", "2"])
2530-
2531-
create_channel.assert_called_with(
2532-
"binaryauthorization.googleapis.com:443",
2533-
credentials=creds,
2534-
credentials_file=None,
2535-
quota_project_id="octopus",
2536-
scopes=["1", "2"],
2537-
ssl_credentials=None,
2538-
options=[
2539-
("grpc.max_send_message_length", -1),
2540-
("grpc.max_receive_message_length", -1),
2541-
],
2542-
)
2543-
2544-
25452471
@pytest.mark.parametrize(
25462472
"transport_class",
25472473
[

0 commit comments

Comments
 (0)