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

Commit 797d46c

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

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

google/cloud/webrisk_v1/services/web_risk_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ def __init__(
330330
client_cert_source_for_mtls=client_cert_source_func,
331331
quota_project_id=client_options.quota_project_id,
332332
client_info=client_info,
333+
always_use_jwt_access=(
334+
Transport == type(self).get_transport_class("grpc")
335+
or Transport == type(self).get_transport_class("grpc_asyncio")
336+
),
333337
)
334338

335339
def compute_threat_list_diff(

google/cloud/webrisk_v1beta1/services/web_risk_service_v1_beta1/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ def __init__(
332332
client_cert_source_for_mtls=client_cert_source_func,
333333
quota_project_id=client_options.quota_project_id,
334334
client_info=client_info,
335+
always_use_jwt_access=(
336+
Transport == type(self).get_transport_class("grpc")
337+
or Transport == type(self).get_transport_class("grpc_asyncio")
338+
),
335339
)
336340

337341
def compute_threat_list_diff(

tests/unit/gapic/webrisk_v1/test_web_risk_service.py

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

120120

121-
@pytest.mark.parametrize(
122-
"client_class", [WebRiskServiceClient, WebRiskServiceAsyncClient,]
123-
)
124-
def test_web_risk_service_client_service_account_always_use_jwt(client_class):
125-
with mock.patch.object(
126-
service_account.Credentials, "with_always_use_jwt_access", create=True
127-
) as use_jwt:
128-
creds = service_account.Credentials(None, None, None)
129-
client = client_class(credentials=creds)
130-
use_jwt.assert_not_called()
131-
132-
133121
@pytest.mark.parametrize(
134122
"transport_class,transport_name",
135123
[
136124
(transports.WebRiskServiceGrpcTransport, "grpc"),
137125
(transports.WebRiskServiceGrpcAsyncIOTransport, "grpc_asyncio"),
138126
],
139127
)
140-
def test_web_risk_service_client_service_account_always_use_jwt_true(
128+
def test_web_risk_service_client_service_account_always_use_jwt(
141129
transport_class, transport_name
142130
):
143131
with mock.patch.object(
@@ -147,6 +135,13 @@ def test_web_risk_service_client_service_account_always_use_jwt_true(
147135
transport = transport_class(credentials=creds, always_use_jwt_access=True)
148136
use_jwt.assert_called_once_with(True)
149137

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+
150145

151146
@pytest.mark.parametrize(
152147
"client_class", [WebRiskServiceClient, WebRiskServiceAsyncClient,]
@@ -227,6 +222,7 @@ def test_web_risk_service_client_client_options(
227222
client_cert_source_for_mtls=None,
228223
quota_project_id=None,
229224
client_info=transports.base.DEFAULT_CLIENT_INFO,
225+
always_use_jwt_access=True,
230226
)
231227

232228
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -243,6 +239,7 @@ def test_web_risk_service_client_client_options(
243239
client_cert_source_for_mtls=None,
244240
quota_project_id=None,
245241
client_info=transports.base.DEFAULT_CLIENT_INFO,
242+
always_use_jwt_access=True,
246243
)
247244

248245
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -259,6 +256,7 @@ def test_web_risk_service_client_client_options(
259256
client_cert_source_for_mtls=None,
260257
quota_project_id=None,
261258
client_info=transports.base.DEFAULT_CLIENT_INFO,
259+
always_use_jwt_access=True,
262260
)
263261

264262
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -287,6 +285,7 @@ def test_web_risk_service_client_client_options(
287285
client_cert_source_for_mtls=None,
288286
quota_project_id="octopus",
289287
client_info=transports.base.DEFAULT_CLIENT_INFO,
288+
always_use_jwt_access=True,
290289
)
291290

292291

@@ -353,6 +352,7 @@ def test_web_risk_service_client_mtls_env_auto(
353352
client_cert_source_for_mtls=expected_client_cert_source,
354353
quota_project_id=None,
355354
client_info=transports.base.DEFAULT_CLIENT_INFO,
355+
always_use_jwt_access=True,
356356
)
357357

358358
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -386,6 +386,7 @@ def test_web_risk_service_client_mtls_env_auto(
386386
client_cert_source_for_mtls=expected_client_cert_source,
387387
quota_project_id=None,
388388
client_info=transports.base.DEFAULT_CLIENT_INFO,
389+
always_use_jwt_access=True,
389390
)
390391

391392
# Check the case client_cert_source and ADC client cert are not provided.
@@ -407,6 +408,7 @@ def test_web_risk_service_client_mtls_env_auto(
407408
client_cert_source_for_mtls=None,
408409
quota_project_id=None,
409410
client_info=transports.base.DEFAULT_CLIENT_INFO,
411+
always_use_jwt_access=True,
410412
)
411413

412414

@@ -437,6 +439,7 @@ def test_web_risk_service_client_client_options_scopes(
437439
client_cert_source_for_mtls=None,
438440
quota_project_id=None,
439441
client_info=transports.base.DEFAULT_CLIENT_INFO,
442+
always_use_jwt_access=True,
440443
)
441444

442445

@@ -467,6 +470,7 @@ def test_web_risk_service_client_client_options_credentials_file(
467470
client_cert_source_for_mtls=None,
468471
quota_project_id=None,
469472
client_info=transports.base.DEFAULT_CLIENT_INFO,
473+
always_use_jwt_access=True,
470474
)
471475

472476

@@ -486,6 +490,7 @@ def test_web_risk_service_client_client_options_from_dict():
486490
client_cert_source_for_mtls=None,
487491
quota_project_id=None,
488492
client_info=transports.base.DEFAULT_CLIENT_INFO,
493+
always_use_jwt_access=True,
489494
)
490495

491496

tests/unit/gapic/webrisk_v1beta1/test_web_risk_service_v1_beta1.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,14 @@ def test_web_risk_service_v1_beta1_client_from_service_account_info(client_class
123123
assert client.transport._host == "webrisk.googleapis.com:443"
124124

125125

126-
@pytest.mark.parametrize(
127-
"client_class", [WebRiskServiceV1Beta1Client, WebRiskServiceV1Beta1AsyncClient,]
128-
)
129-
def test_web_risk_service_v1_beta1_client_service_account_always_use_jwt(client_class):
130-
with mock.patch.object(
131-
service_account.Credentials, "with_always_use_jwt_access", create=True
132-
) as use_jwt:
133-
creds = service_account.Credentials(None, None, None)
134-
client = client_class(credentials=creds)
135-
use_jwt.assert_not_called()
136-
137-
138126
@pytest.mark.parametrize(
139127
"transport_class,transport_name",
140128
[
141129
(transports.WebRiskServiceV1Beta1GrpcTransport, "grpc"),
142130
(transports.WebRiskServiceV1Beta1GrpcAsyncIOTransport, "grpc_asyncio"),
143131
],
144132
)
145-
def test_web_risk_service_v1_beta1_client_service_account_always_use_jwt_true(
133+
def test_web_risk_service_v1_beta1_client_service_account_always_use_jwt(
146134
transport_class, transport_name
147135
):
148136
with mock.patch.object(
@@ -152,6 +140,13 @@ def test_web_risk_service_v1_beta1_client_service_account_always_use_jwt_true(
152140
transport = transport_class(credentials=creds, always_use_jwt_access=True)
153141
use_jwt.assert_called_once_with(True)
154142

143+
with mock.patch.object(
144+
service_account.Credentials, "with_always_use_jwt_access", create=True
145+
) as use_jwt:
146+
creds = service_account.Credentials(None, None, None)
147+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
148+
use_jwt.assert_not_called()
149+
155150

156151
@pytest.mark.parametrize(
157152
"client_class", [WebRiskServiceV1Beta1Client, WebRiskServiceV1Beta1AsyncClient,]
@@ -236,6 +231,7 @@ def test_web_risk_service_v1_beta1_client_client_options(
236231
client_cert_source_for_mtls=None,
237232
quota_project_id=None,
238233
client_info=transports.base.DEFAULT_CLIENT_INFO,
234+
always_use_jwt_access=True,
239235
)
240236

241237
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -252,6 +248,7 @@ def test_web_risk_service_v1_beta1_client_client_options(
252248
client_cert_source_for_mtls=None,
253249
quota_project_id=None,
254250
client_info=transports.base.DEFAULT_CLIENT_INFO,
251+
always_use_jwt_access=True,
255252
)
256253

257254
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -268,6 +265,7 @@ def test_web_risk_service_v1_beta1_client_client_options(
268265
client_cert_source_for_mtls=None,
269266
quota_project_id=None,
270267
client_info=transports.base.DEFAULT_CLIENT_INFO,
268+
always_use_jwt_access=True,
271269
)
272270

273271
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -296,6 +294,7 @@ def test_web_risk_service_v1_beta1_client_client_options(
296294
client_cert_source_for_mtls=None,
297295
quota_project_id="octopus",
298296
client_info=transports.base.DEFAULT_CLIENT_INFO,
297+
always_use_jwt_access=True,
299298
)
300299

301300

@@ -372,6 +371,7 @@ def test_web_risk_service_v1_beta1_client_mtls_env_auto(
372371
client_cert_source_for_mtls=expected_client_cert_source,
373372
quota_project_id=None,
374373
client_info=transports.base.DEFAULT_CLIENT_INFO,
374+
always_use_jwt_access=True,
375375
)
376376

377377
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -405,6 +405,7 @@ def test_web_risk_service_v1_beta1_client_mtls_env_auto(
405405
client_cert_source_for_mtls=expected_client_cert_source,
406406
quota_project_id=None,
407407
client_info=transports.base.DEFAULT_CLIENT_INFO,
408+
always_use_jwt_access=True,
408409
)
409410

410411
# Check the case client_cert_source and ADC client cert are not provided.
@@ -426,6 +427,7 @@ def test_web_risk_service_v1_beta1_client_mtls_env_auto(
426427
client_cert_source_for_mtls=None,
427428
quota_project_id=None,
428429
client_info=transports.base.DEFAULT_CLIENT_INFO,
430+
always_use_jwt_access=True,
429431
)
430432

431433

@@ -460,6 +462,7 @@ def test_web_risk_service_v1_beta1_client_client_options_scopes(
460462
client_cert_source_for_mtls=None,
461463
quota_project_id=None,
462464
client_info=transports.base.DEFAULT_CLIENT_INFO,
465+
always_use_jwt_access=True,
463466
)
464467

465468

@@ -494,6 +497,7 @@ def test_web_risk_service_v1_beta1_client_client_options_credentials_file(
494497
client_cert_source_for_mtls=None,
495498
quota_project_id=None,
496499
client_info=transports.base.DEFAULT_CLIENT_INFO,
500+
always_use_jwt_access=True,
497501
)
498502

499503

@@ -513,6 +517,7 @@ def test_web_risk_service_v1_beta1_client_client_options_from_dict():
513517
client_cert_source_for_mtls=None,
514518
quota_project_id=None,
515519
client_info=transports.base.DEFAULT_CLIENT_INFO,
520+
always_use_jwt_access=True,
516521
)
517522

518523

0 commit comments

Comments
 (0)