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

Commit 6e34b81

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

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

google/cloud/container_v1/services/cluster_manager/client.py

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

334338
def list_clusters(

google/cloud/container_v1beta1/services/cluster_manager/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ def __init__(
341341
client_cert_source_for_mtls=client_cert_source_func,
342342
quota_project_id=client_options.quota_project_id,
343343
client_info=client_info,
344+
always_use_jwt_access=(
345+
Transport == type(self).get_transport_class("grpc")
346+
or Transport == type(self).get_transport_class("grpc_asyncio")
347+
),
344348
)
345349

346350
def list_clusters(

tests/unit/gapic/container_v1/test_cluster_manager.py

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

121121

122-
@pytest.mark.parametrize(
123-
"client_class", [ClusterManagerClient, ClusterManagerAsyncClient,]
124-
)
125-
def test_cluster_manager_client_service_account_always_use_jwt(client_class):
126-
with mock.patch.object(
127-
service_account.Credentials, "with_always_use_jwt_access", create=True
128-
) as use_jwt:
129-
creds = service_account.Credentials(None, None, None)
130-
client = client_class(credentials=creds)
131-
use_jwt.assert_not_called()
132-
133-
134122
@pytest.mark.parametrize(
135123
"transport_class,transport_name",
136124
[
137125
(transports.ClusterManagerGrpcTransport, "grpc"),
138126
(transports.ClusterManagerGrpcAsyncIOTransport, "grpc_asyncio"),
139127
],
140128
)
141-
def test_cluster_manager_client_service_account_always_use_jwt_true(
129+
def test_cluster_manager_client_service_account_always_use_jwt(
142130
transport_class, transport_name
143131
):
144132
with mock.patch.object(
@@ -148,6 +136,13 @@ def test_cluster_manager_client_service_account_always_use_jwt_true(
148136
transport = transport_class(credentials=creds, always_use_jwt_access=True)
149137
use_jwt.assert_called_once_with(True)
150138

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

152147
@pytest.mark.parametrize(
153148
"client_class", [ClusterManagerClient, ClusterManagerAsyncClient,]
@@ -228,6 +223,7 @@ def test_cluster_manager_client_client_options(
228223
client_cert_source_for_mtls=None,
229224
quota_project_id=None,
230225
client_info=transports.base.DEFAULT_CLIENT_INFO,
226+
always_use_jwt_access=True,
231227
)
232228

233229
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -244,6 +240,7 @@ def test_cluster_manager_client_client_options(
244240
client_cert_source_for_mtls=None,
245241
quota_project_id=None,
246242
client_info=transports.base.DEFAULT_CLIENT_INFO,
243+
always_use_jwt_access=True,
247244
)
248245

249246
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -260,6 +257,7 @@ def test_cluster_manager_client_client_options(
260257
client_cert_source_for_mtls=None,
261258
quota_project_id=None,
262259
client_info=transports.base.DEFAULT_CLIENT_INFO,
260+
always_use_jwt_access=True,
263261
)
264262

265263
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -288,6 +286,7 @@ def test_cluster_manager_client_client_options(
288286
client_cert_source_for_mtls=None,
289287
quota_project_id="octopus",
290288
client_info=transports.base.DEFAULT_CLIENT_INFO,
289+
always_use_jwt_access=True,
291290
)
292291

293292

@@ -354,6 +353,7 @@ def test_cluster_manager_client_mtls_env_auto(
354353
client_cert_source_for_mtls=expected_client_cert_source,
355354
quota_project_id=None,
356355
client_info=transports.base.DEFAULT_CLIENT_INFO,
356+
always_use_jwt_access=True,
357357
)
358358

359359
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -387,6 +387,7 @@ def test_cluster_manager_client_mtls_env_auto(
387387
client_cert_source_for_mtls=expected_client_cert_source,
388388
quota_project_id=None,
389389
client_info=transports.base.DEFAULT_CLIENT_INFO,
390+
always_use_jwt_access=True,
390391
)
391392

392393
# Check the case client_cert_source and ADC client cert are not provided.
@@ -408,6 +409,7 @@ def test_cluster_manager_client_mtls_env_auto(
408409
client_cert_source_for_mtls=None,
409410
quota_project_id=None,
410411
client_info=transports.base.DEFAULT_CLIENT_INFO,
412+
always_use_jwt_access=True,
411413
)
412414

413415

@@ -438,6 +440,7 @@ def test_cluster_manager_client_client_options_scopes(
438440
client_cert_source_for_mtls=None,
439441
quota_project_id=None,
440442
client_info=transports.base.DEFAULT_CLIENT_INFO,
443+
always_use_jwt_access=True,
441444
)
442445

443446

@@ -468,6 +471,7 @@ def test_cluster_manager_client_client_options_credentials_file(
468471
client_cert_source_for_mtls=None,
469472
quota_project_id=None,
470473
client_info=transports.base.DEFAULT_CLIENT_INFO,
474+
always_use_jwt_access=True,
471475
)
472476

473477

@@ -487,6 +491,7 @@ def test_cluster_manager_client_client_options_from_dict():
487491
client_cert_source_for_mtls=None,
488492
quota_project_id=None,
489493
client_info=transports.base.DEFAULT_CLIENT_INFO,
494+
always_use_jwt_access=True,
490495
)
491496

492497

tests/unit/gapic/container_v1beta1/test_cluster_manager.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,26 +124,14 @@ def test_cluster_manager_client_from_service_account_info(client_class):
124124
assert client.transport._host == "container.googleapis.com:443"
125125

126126

127-
@pytest.mark.parametrize(
128-
"client_class", [ClusterManagerClient, ClusterManagerAsyncClient,]
129-
)
130-
def test_cluster_manager_client_service_account_always_use_jwt(client_class):
131-
with mock.patch.object(
132-
service_account.Credentials, "with_always_use_jwt_access", create=True
133-
) as use_jwt:
134-
creds = service_account.Credentials(None, None, None)
135-
client = client_class(credentials=creds)
136-
use_jwt.assert_not_called()
137-
138-
139127
@pytest.mark.parametrize(
140128
"transport_class,transport_name",
141129
[
142130
(transports.ClusterManagerGrpcTransport, "grpc"),
143131
(transports.ClusterManagerGrpcAsyncIOTransport, "grpc_asyncio"),
144132
],
145133
)
146-
def test_cluster_manager_client_service_account_always_use_jwt_true(
134+
def test_cluster_manager_client_service_account_always_use_jwt(
147135
transport_class, transport_name
148136
):
149137
with mock.patch.object(
@@ -153,6 +141,13 @@ def test_cluster_manager_client_service_account_always_use_jwt_true(
153141
transport = transport_class(credentials=creds, always_use_jwt_access=True)
154142
use_jwt.assert_called_once_with(True)
155143

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

157152
@pytest.mark.parametrize(
158153
"client_class", [ClusterManagerClient, ClusterManagerAsyncClient,]
@@ -233,6 +228,7 @@ def test_cluster_manager_client_client_options(
233228
client_cert_source_for_mtls=None,
234229
quota_project_id=None,
235230
client_info=transports.base.DEFAULT_CLIENT_INFO,
231+
always_use_jwt_access=True,
236232
)
237233

238234
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -249,6 +245,7 @@ def test_cluster_manager_client_client_options(
249245
client_cert_source_for_mtls=None,
250246
quota_project_id=None,
251247
client_info=transports.base.DEFAULT_CLIENT_INFO,
248+
always_use_jwt_access=True,
252249
)
253250

254251
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -265,6 +262,7 @@ def test_cluster_manager_client_client_options(
265262
client_cert_source_for_mtls=None,
266263
quota_project_id=None,
267264
client_info=transports.base.DEFAULT_CLIENT_INFO,
265+
always_use_jwt_access=True,
268266
)
269267

270268
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -293,6 +291,7 @@ def test_cluster_manager_client_client_options(
293291
client_cert_source_for_mtls=None,
294292
quota_project_id="octopus",
295293
client_info=transports.base.DEFAULT_CLIENT_INFO,
294+
always_use_jwt_access=True,
296295
)
297296

298297

@@ -359,6 +358,7 @@ def test_cluster_manager_client_mtls_env_auto(
359358
client_cert_source_for_mtls=expected_client_cert_source,
360359
quota_project_id=None,
361360
client_info=transports.base.DEFAULT_CLIENT_INFO,
361+
always_use_jwt_access=True,
362362
)
363363

364364
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -392,6 +392,7 @@ def test_cluster_manager_client_mtls_env_auto(
392392
client_cert_source_for_mtls=expected_client_cert_source,
393393
quota_project_id=None,
394394
client_info=transports.base.DEFAULT_CLIENT_INFO,
395+
always_use_jwt_access=True,
395396
)
396397

397398
# Check the case client_cert_source and ADC client cert are not provided.
@@ -413,6 +414,7 @@ def test_cluster_manager_client_mtls_env_auto(
413414
client_cert_source_for_mtls=None,
414415
quota_project_id=None,
415416
client_info=transports.base.DEFAULT_CLIENT_INFO,
417+
always_use_jwt_access=True,
416418
)
417419

418420

@@ -443,6 +445,7 @@ def test_cluster_manager_client_client_options_scopes(
443445
client_cert_source_for_mtls=None,
444446
quota_project_id=None,
445447
client_info=transports.base.DEFAULT_CLIENT_INFO,
448+
always_use_jwt_access=True,
446449
)
447450

448451

@@ -473,6 +476,7 @@ def test_cluster_manager_client_client_options_credentials_file(
473476
client_cert_source_for_mtls=None,
474477
quota_project_id=None,
475478
client_info=transports.base.DEFAULT_CLIENT_INFO,
479+
always_use_jwt_access=True,
476480
)
477481

478482

@@ -492,6 +496,7 @@ def test_cluster_manager_client_client_options_from_dict():
492496
client_cert_source_for_mtls=None,
493497
quota_project_id=None,
494498
client_info=transports.base.DEFAULT_CLIENT_INFO,
499+
always_use_jwt_access=True,
495500
)
496501

497502

0 commit comments

Comments
 (0)