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

Commit 7df4fef

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

File tree

16 files changed

+176
-106
lines changed

16 files changed

+176
-106
lines changed

google/cloud/dataproc_v1/services/autoscaling_policy_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def __init__(
350350
client_cert_source_for_mtls=client_cert_source_func,
351351
quota_project_id=client_options.quota_project_id,
352352
client_info=client_info,
353+
always_use_jwt_access=(
354+
Transport == type(self).get_transport_class("grpc")
355+
or Transport == type(self).get_transport_class("grpc_asyncio")
356+
),
353357
)
354358

355359
def create_autoscaling_policy(

google/cloud/dataproc_v1/services/cluster_controller/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372+
always_use_jwt_access=(
373+
Transport == type(self).get_transport_class("grpc")
374+
or Transport == type(self).get_transport_class("grpc_asyncio")
375+
),
372376
)
373377

374378
def create_cluster(

google/cloud/dataproc_v1/services/job_controller/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def __init__(
328328
client_cert_source_for_mtls=client_cert_source_func,
329329
quota_project_id=client_options.quota_project_id,
330330
client_info=client_info,
331+
always_use_jwt_access=(
332+
Transport == type(self).get_transport_class("grpc")
333+
or Transport == type(self).get_transport_class("grpc_asyncio")
334+
),
331335
)
332336

333337
def submit_job(

google/cloud/dataproc_v1/services/workflow_template_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ def __init__(
387387
client_cert_source_for_mtls=client_cert_source_func,
388388
quota_project_id=client_options.quota_project_id,
389389
client_info=client_info,
390+
always_use_jwt_access=(
391+
Transport == type(self).get_transport_class("grpc")
392+
or Transport == type(self).get_transport_class("grpc_asyncio")
393+
),
390394
)
391395

392396
def create_workflow_template(

google/cloud/dataproc_v1beta2/services/autoscaling_policy_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ def __init__(
350350
client_cert_source_for_mtls=client_cert_source_func,
351351
quota_project_id=client_options.quota_project_id,
352352
client_info=client_info,
353+
always_use_jwt_access=(
354+
Transport == type(self).get_transport_class("grpc")
355+
or Transport == type(self).get_transport_class("grpc_asyncio")
356+
),
353357
)
354358

355359
def create_autoscaling_policy(

google/cloud/dataproc_v1beta2/services/cluster_controller/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ def __init__(
353353
client_cert_source_for_mtls=client_cert_source_func,
354354
quota_project_id=client_options.quota_project_id,
355355
client_info=client_info,
356+
always_use_jwt_access=(
357+
Transport == type(self).get_transport_class("grpc")
358+
or Transport == type(self).get_transport_class("grpc_asyncio")
359+
),
356360
)
357361

358362
def create_cluster(

google/cloud/dataproc_v1beta2/services/job_controller/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def __init__(
328328
client_cert_source_for_mtls=client_cert_source_func,
329329
quota_project_id=client_options.quota_project_id,
330330
client_info=client_info,
331+
always_use_jwt_access=(
332+
Transport == type(self).get_transport_class("grpc")
333+
or Transport == type(self).get_transport_class("grpc_asyncio")
334+
),
331335
)
332336

333337
def submit_job(

google/cloud/dataproc_v1beta2/services/workflow_template_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def __init__(
371371
client_cert_source_for_mtls=client_cert_source_func,
372372
quota_project_id=client_options.quota_project_id,
373373
client_info=client_info,
374+
always_use_jwt_access=(
375+
Transport == type(self).get_transport_class("grpc")
376+
or Transport == type(self).get_transport_class("grpc_asyncio")
377+
),
374378
)
375379

376380
def create_workflow_template(

tests/unit/gapic/dataproc_v1/test_autoscaling_policy_service.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,14 @@ def test_autoscaling_policy_service_client_from_service_account_info(client_clas
125125
assert client.transport._host == "dataproc.googleapis.com:443"
126126

127127

128-
@pytest.mark.parametrize(
129-
"client_class",
130-
[AutoscalingPolicyServiceClient, AutoscalingPolicyServiceAsyncClient,],
131-
)
132-
def test_autoscaling_policy_service_client_service_account_always_use_jwt(client_class):
133-
with mock.patch.object(
134-
service_account.Credentials, "with_always_use_jwt_access", create=True
135-
) as use_jwt:
136-
creds = service_account.Credentials(None, None, None)
137-
client = client_class(credentials=creds)
138-
use_jwt.assert_not_called()
139-
140-
141128
@pytest.mark.parametrize(
142129
"transport_class,transport_name",
143130
[
144131
(transports.AutoscalingPolicyServiceGrpcTransport, "grpc"),
145132
(transports.AutoscalingPolicyServiceGrpcAsyncIOTransport, "grpc_asyncio"),
146133
],
147134
)
148-
def test_autoscaling_policy_service_client_service_account_always_use_jwt_true(
135+
def test_autoscaling_policy_service_client_service_account_always_use_jwt(
149136
transport_class, transport_name
150137
):
151138
with mock.patch.object(
@@ -155,6 +142,13 @@ def test_autoscaling_policy_service_client_service_account_always_use_jwt_true(
155142
transport = transport_class(credentials=creds, always_use_jwt_access=True)
156143
use_jwt.assert_called_once_with(True)
157144

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

159153
@pytest.mark.parametrize(
160154
"client_class",
@@ -244,6 +238,7 @@ def test_autoscaling_policy_service_client_client_options(
244238
client_cert_source_for_mtls=None,
245239
quota_project_id=None,
246240
client_info=transports.base.DEFAULT_CLIENT_INFO,
241+
always_use_jwt_access=True,
247242
)
248243

249244
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -260,6 +255,7 @@ def test_autoscaling_policy_service_client_client_options(
260255
client_cert_source_for_mtls=None,
261256
quota_project_id=None,
262257
client_info=transports.base.DEFAULT_CLIENT_INFO,
258+
always_use_jwt_access=True,
263259
)
264260

265261
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -276,6 +272,7 @@ def test_autoscaling_policy_service_client_client_options(
276272
client_cert_source_for_mtls=None,
277273
quota_project_id=None,
278274
client_info=transports.base.DEFAULT_CLIENT_INFO,
275+
always_use_jwt_access=True,
279276
)
280277

281278
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -304,6 +301,7 @@ def test_autoscaling_policy_service_client_client_options(
304301
client_cert_source_for_mtls=None,
305302
quota_project_id="octopus",
306303
client_info=transports.base.DEFAULT_CLIENT_INFO,
304+
always_use_jwt_access=True,
307305
)
308306

309307

@@ -380,6 +378,7 @@ def test_autoscaling_policy_service_client_mtls_env_auto(
380378
client_cert_source_for_mtls=expected_client_cert_source,
381379
quota_project_id=None,
382380
client_info=transports.base.DEFAULT_CLIENT_INFO,
381+
always_use_jwt_access=True,
383382
)
384383

385384
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -413,6 +412,7 @@ def test_autoscaling_policy_service_client_mtls_env_auto(
413412
client_cert_source_for_mtls=expected_client_cert_source,
414413
quota_project_id=None,
415414
client_info=transports.base.DEFAULT_CLIENT_INFO,
415+
always_use_jwt_access=True,
416416
)
417417

418418
# Check the case client_cert_source and ADC client cert are not provided.
@@ -434,6 +434,7 @@ def test_autoscaling_policy_service_client_mtls_env_auto(
434434
client_cert_source_for_mtls=None,
435435
quota_project_id=None,
436436
client_info=transports.base.DEFAULT_CLIENT_INFO,
437+
always_use_jwt_access=True,
437438
)
438439

439440

@@ -468,6 +469,7 @@ def test_autoscaling_policy_service_client_client_options_scopes(
468469
client_cert_source_for_mtls=None,
469470
quota_project_id=None,
470471
client_info=transports.base.DEFAULT_CLIENT_INFO,
472+
always_use_jwt_access=True,
471473
)
472474

473475

@@ -502,6 +504,7 @@ def test_autoscaling_policy_service_client_client_options_credentials_file(
502504
client_cert_source_for_mtls=None,
503505
quota_project_id=None,
504506
client_info=transports.base.DEFAULT_CLIENT_INFO,
507+
always_use_jwt_access=True,
505508
)
506509

507510

@@ -521,6 +524,7 @@ def test_autoscaling_policy_service_client_client_options_from_dict():
521524
client_cert_source_for_mtls=None,
522525
quota_project_id=None,
523526
client_info=transports.base.DEFAULT_CLIENT_INFO,
527+
always_use_jwt_access=True,
524528
)
525529

526530

tests/unit/gapic/dataproc_v1/test_cluster_controller.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,14 @@ def test_cluster_controller_client_from_service_account_info(client_class):
130130
assert client.transport._host == "dataproc.googleapis.com:443"
131131

132132

133-
@pytest.mark.parametrize(
134-
"client_class", [ClusterControllerClient, ClusterControllerAsyncClient,]
135-
)
136-
def test_cluster_controller_client_service_account_always_use_jwt(client_class):
137-
with mock.patch.object(
138-
service_account.Credentials, "with_always_use_jwt_access", create=True
139-
) as use_jwt:
140-
creds = service_account.Credentials(None, None, None)
141-
client = client_class(credentials=creds)
142-
use_jwt.assert_not_called()
143-
144-
145133
@pytest.mark.parametrize(
146134
"transport_class,transport_name",
147135
[
148136
(transports.ClusterControllerGrpcTransport, "grpc"),
149137
(transports.ClusterControllerGrpcAsyncIOTransport, "grpc_asyncio"),
150138
],
151139
)
152-
def test_cluster_controller_client_service_account_always_use_jwt_true(
140+
def test_cluster_controller_client_service_account_always_use_jwt(
153141
transport_class, transport_name
154142
):
155143
with mock.patch.object(
@@ -159,6 +147,13 @@ def test_cluster_controller_client_service_account_always_use_jwt_true(
159147
transport = transport_class(credentials=creds, always_use_jwt_access=True)
160148
use_jwt.assert_called_once_with(True)
161149

150+
with mock.patch.object(
151+
service_account.Credentials, "with_always_use_jwt_access", create=True
152+
) as use_jwt:
153+
creds = service_account.Credentials(None, None, None)
154+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
155+
use_jwt.assert_not_called()
156+
162157

163158
@pytest.mark.parametrize(
164159
"client_class", [ClusterControllerClient, ClusterControllerAsyncClient,]
@@ -239,6 +234,7 @@ def test_cluster_controller_client_client_options(
239234
client_cert_source_for_mtls=None,
240235
quota_project_id=None,
241236
client_info=transports.base.DEFAULT_CLIENT_INFO,
237+
always_use_jwt_access=True,
242238
)
243239

244240
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -255,6 +251,7 @@ def test_cluster_controller_client_client_options(
255251
client_cert_source_for_mtls=None,
256252
quota_project_id=None,
257253
client_info=transports.base.DEFAULT_CLIENT_INFO,
254+
always_use_jwt_access=True,
258255
)
259256

260257
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -271,6 +268,7 @@ def test_cluster_controller_client_client_options(
271268
client_cert_source_for_mtls=None,
272269
quota_project_id=None,
273270
client_info=transports.base.DEFAULT_CLIENT_INFO,
271+
always_use_jwt_access=True,
274272
)
275273

276274
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -299,6 +297,7 @@ def test_cluster_controller_client_client_options(
299297
client_cert_source_for_mtls=None,
300298
quota_project_id="octopus",
301299
client_info=transports.base.DEFAULT_CLIENT_INFO,
300+
always_use_jwt_access=True,
302301
)
303302

304303

@@ -375,6 +374,7 @@ def test_cluster_controller_client_mtls_env_auto(
375374
client_cert_source_for_mtls=expected_client_cert_source,
376375
quota_project_id=None,
377376
client_info=transports.base.DEFAULT_CLIENT_INFO,
377+
always_use_jwt_access=True,
378378
)
379379

380380
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -408,6 +408,7 @@ def test_cluster_controller_client_mtls_env_auto(
408408
client_cert_source_for_mtls=expected_client_cert_source,
409409
quota_project_id=None,
410410
client_info=transports.base.DEFAULT_CLIENT_INFO,
411+
always_use_jwt_access=True,
411412
)
412413

413414
# Check the case client_cert_source and ADC client cert are not provided.
@@ -429,6 +430,7 @@ def test_cluster_controller_client_mtls_env_auto(
429430
client_cert_source_for_mtls=None,
430431
quota_project_id=None,
431432
client_info=transports.base.DEFAULT_CLIENT_INFO,
433+
always_use_jwt_access=True,
432434
)
433435

434436

@@ -459,6 +461,7 @@ def test_cluster_controller_client_client_options_scopes(
459461
client_cert_source_for_mtls=None,
460462
quota_project_id=None,
461463
client_info=transports.base.DEFAULT_CLIENT_INFO,
464+
always_use_jwt_access=True,
462465
)
463466

464467

@@ -489,6 +492,7 @@ def test_cluster_controller_client_client_options_credentials_file(
489492
client_cert_source_for_mtls=None,
490493
quota_project_id=None,
491494
client_info=transports.base.DEFAULT_CLIENT_INFO,
495+
always_use_jwt_access=True,
492496
)
493497

494498

@@ -508,6 +512,7 @@ def test_cluster_controller_client_client_options_from_dict():
508512
client_cert_source_for_mtls=None,
509513
quota_project_id=None,
510514
client_info=transports.base.DEFAULT_CLIENT_INFO,
515+
always_use_jwt_access=True,
511516
)
512517

513518

0 commit comments

Comments
 (0)