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

Commit cb9bed0

Browse files
fix: enable self signed jwt for grpc (#24)
PiperOrigin-RevId: 386504689 Source-Link: googleapis/googleapis@762094a Source-Link: https://github.com/googleapis/googleapis-gen/commit/6bfc480e1a161d5de121c2bcc3745885d33b265a
1 parent 4584099 commit cb9bed0

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

google/cloud/service_usage_v1/services/service_usage/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ def __init__(
335335
client_cert_source_for_mtls=client_cert_source_func,
336336
quota_project_id=client_options.quota_project_id,
337337
client_info=client_info,
338+
always_use_jwt_access=(
339+
Transport == type(self).get_transport_class("grpc")
340+
or Transport == type(self).get_transport_class("grpc_asyncio")
341+
),
338342
)
339343

340344
def enable_service(

tests/unit/gapic/service_usage_v1/test_service_usage.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,14 @@ def test_service_usage_client_from_service_account_info(client_class):
118118
assert client.transport._host == "serviceusage.googleapis.com:443"
119119

120120

121-
@pytest.mark.parametrize("client_class", [ServiceUsageClient, ServiceUsageAsyncClient,])
122-
def test_service_usage_client_service_account_always_use_jwt(client_class):
123-
with mock.patch.object(
124-
service_account.Credentials, "with_always_use_jwt_access", create=True
125-
) as use_jwt:
126-
creds = service_account.Credentials(None, None, None)
127-
client = client_class(credentials=creds)
128-
use_jwt.assert_not_called()
129-
130-
131121
@pytest.mark.parametrize(
132122
"transport_class,transport_name",
133123
[
134124
(transports.ServiceUsageGrpcTransport, "grpc"),
135125
(transports.ServiceUsageGrpcAsyncIOTransport, "grpc_asyncio"),
136126
],
137127
)
138-
def test_service_usage_client_service_account_always_use_jwt_true(
128+
def test_service_usage_client_service_account_always_use_jwt(
139129
transport_class, transport_name
140130
):
141131
with mock.patch.object(
@@ -145,6 +135,13 @@ def test_service_usage_client_service_account_always_use_jwt_true(
145135
transport = transport_class(credentials=creds, always_use_jwt_access=True)
146136
use_jwt.assert_called_once_with(True)
147137

138+
with mock.patch.object(
139+
service_account.Credentials, "with_always_use_jwt_access", create=True
140+
) as use_jwt:
141+
creds = service_account.Credentials(None, None, None)
142+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
143+
use_jwt.assert_not_called()
144+
148145

149146
@pytest.mark.parametrize("client_class", [ServiceUsageClient, ServiceUsageAsyncClient,])
150147
def test_service_usage_client_from_service_account_file(client_class):
@@ -221,6 +218,7 @@ def test_service_usage_client_client_options(
221218
client_cert_source_for_mtls=None,
222219
quota_project_id=None,
223220
client_info=transports.base.DEFAULT_CLIENT_INFO,
221+
always_use_jwt_access=True,
224222
)
225223

226224
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -237,6 +235,7 @@ def test_service_usage_client_client_options(
237235
client_cert_source_for_mtls=None,
238236
quota_project_id=None,
239237
client_info=transports.base.DEFAULT_CLIENT_INFO,
238+
always_use_jwt_access=True,
240239
)
241240

242241
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -253,6 +252,7 @@ def test_service_usage_client_client_options(
253252
client_cert_source_for_mtls=None,
254253
quota_project_id=None,
255254
client_info=transports.base.DEFAULT_CLIENT_INFO,
255+
always_use_jwt_access=True,
256256
)
257257

258258
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -281,6 +281,7 @@ def test_service_usage_client_client_options(
281281
client_cert_source_for_mtls=None,
282282
quota_project_id="octopus",
283283
client_info=transports.base.DEFAULT_CLIENT_INFO,
284+
always_use_jwt_access=True,
284285
)
285286

286287

@@ -345,6 +346,7 @@ def test_service_usage_client_mtls_env_auto(
345346
client_cert_source_for_mtls=expected_client_cert_source,
346347
quota_project_id=None,
347348
client_info=transports.base.DEFAULT_CLIENT_INFO,
349+
always_use_jwt_access=True,
348350
)
349351

350352
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -378,6 +380,7 @@ def test_service_usage_client_mtls_env_auto(
378380
client_cert_source_for_mtls=expected_client_cert_source,
379381
quota_project_id=None,
380382
client_info=transports.base.DEFAULT_CLIENT_INFO,
383+
always_use_jwt_access=True,
381384
)
382385

383386
# Check the case client_cert_source and ADC client cert are not provided.
@@ -399,6 +402,7 @@ def test_service_usage_client_mtls_env_auto(
399402
client_cert_source_for_mtls=None,
400403
quota_project_id=None,
401404
client_info=transports.base.DEFAULT_CLIENT_INFO,
405+
always_use_jwt_access=True,
402406
)
403407

404408

@@ -429,6 +433,7 @@ def test_service_usage_client_client_options_scopes(
429433
client_cert_source_for_mtls=None,
430434
quota_project_id=None,
431435
client_info=transports.base.DEFAULT_CLIENT_INFO,
436+
always_use_jwt_access=True,
432437
)
433438

434439

@@ -459,6 +464,7 @@ def test_service_usage_client_client_options_credentials_file(
459464
client_cert_source_for_mtls=None,
460465
quota_project_id=None,
461466
client_info=transports.base.DEFAULT_CLIENT_INFO,
467+
always_use_jwt_access=True,
462468
)
463469

464470

@@ -476,6 +482,7 @@ def test_service_usage_client_client_options_from_dict():
476482
client_cert_source_for_mtls=None,
477483
quota_project_id=None,
478484
client_info=transports.base.DEFAULT_CLIENT_INFO,
485+
always_use_jwt_access=True,
479486
)
480487

481488

0 commit comments

Comments
 (0)