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

Commit 5f6c367

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

4 files changed

Lines changed: 44 additions & 26 deletions

File tree

‎google/cloud/osconfig_v1/services/os_config_service/client.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def __init__(
380380
client_cert_source_for_mtls=client_cert_source_func,
381381
quota_project_id=client_options.quota_project_id,
382382
client_info=client_info,
383+
always_use_jwt_access=(
384+
Transport == type(self).get_transport_class("grpc")
385+
or Transport == type(self).get_transport_class("grpc_asyncio")
386+
),
383387
)
384388

385389
def execute_patch_job(

‎google/cloud/osconfig_v1alpha/services/os_config_zonal_service/client.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ def __init__(
430430
client_cert_source_for_mtls=client_cert_source_func,
431431
quota_project_id=client_options.quota_project_id,
432432
client_info=client_info,
433+
always_use_jwt_access=(
434+
Transport == type(self).get_transport_class("grpc")
435+
or Transport == type(self).get_transport_class("grpc_asyncio")
436+
),
433437
)
434438

435439
def create_os_policy_assignment(

‎tests/unit/gapic/osconfig_v1/test_os_config_service.py‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,14 @@ def test_os_config_service_client_from_service_account_info(client_class):
127127
assert client.transport._host == "osconfig.googleapis.com:443"
128128

129129

130-
@pytest.mark.parametrize(
131-
"client_class", [OsConfigServiceClient, OsConfigServiceAsyncClient,]
132-
)
133-
def test_os_config_service_client_service_account_always_use_jwt(client_class):
134-
with mock.patch.object(
135-
service_account.Credentials, "with_always_use_jwt_access", create=True
136-
) as use_jwt:
137-
creds = service_account.Credentials(None, None, None)
138-
client = client_class(credentials=creds)
139-
use_jwt.assert_not_called()
140-
141-
142130
@pytest.mark.parametrize(
143131
"transport_class,transport_name",
144132
[
145133
(transports.OsConfigServiceGrpcTransport, "grpc"),
146134
(transports.OsConfigServiceGrpcAsyncIOTransport, "grpc_asyncio"),
147135
],
148136
)
149-
def test_os_config_service_client_service_account_always_use_jwt_true(
137+
def test_os_config_service_client_service_account_always_use_jwt(
150138
transport_class, transport_name
151139
):
152140
with mock.patch.object(
@@ -156,6 +144,13 @@ def test_os_config_service_client_service_account_always_use_jwt_true(
156144
transport = transport_class(credentials=creds, always_use_jwt_access=True)
157145
use_jwt.assert_called_once_with(True)
158146

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

160155
@pytest.mark.parametrize(
161156
"client_class", [OsConfigServiceClient, OsConfigServiceAsyncClient,]
@@ -236,6 +231,7 @@ def test_os_config_service_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_os_config_service_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_os_config_service_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_os_config_service_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_os_config_service_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_os_config_service_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_os_config_service_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

@@ -456,6 +458,7 @@ def test_os_config_service_client_client_options_scopes(
456458
client_cert_source_for_mtls=None,
457459
quota_project_id=None,
458460
client_info=transports.base.DEFAULT_CLIENT_INFO,
461+
always_use_jwt_access=True,
459462
)
460463

461464

@@ -486,6 +489,7 @@ def test_os_config_service_client_client_options_credentials_file(
486489
client_cert_source_for_mtls=None,
487490
quota_project_id=None,
488491
client_info=transports.base.DEFAULT_CLIENT_INFO,
492+
always_use_jwt_access=True,
489493
)
490494

491495

@@ -505,6 +509,7 @@ def test_os_config_service_client_client_options_from_dict():
505509
client_cert_source_for_mtls=None,
506510
quota_project_id=None,
507511
client_info=transports.base.DEFAULT_CLIENT_INFO,
512+
always_use_jwt_access=True,
508513
)
509514

510515

‎tests/unit/gapic/osconfig_v1alpha/test_os_config_zonal_service.py‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,14 @@ def test_os_config_zonal_service_client_from_service_account_info(client_class):
136136
assert client.transport._host == "osconfig.googleapis.com:443"
137137

138138

139-
@pytest.mark.parametrize(
140-
"client_class", [OsConfigZonalServiceClient, OsConfigZonalServiceAsyncClient,]
141-
)
142-
def test_os_config_zonal_service_client_service_account_always_use_jwt(client_class):
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-
client = client_class(credentials=creds)
148-
use_jwt.assert_not_called()
149-
150-
151139
@pytest.mark.parametrize(
152140
"transport_class,transport_name",
153141
[
154142
(transports.OsConfigZonalServiceGrpcTransport, "grpc"),
155143
(transports.OsConfigZonalServiceGrpcAsyncIOTransport, "grpc_asyncio"),
156144
],
157145
)
158-
def test_os_config_zonal_service_client_service_account_always_use_jwt_true(
146+
def test_os_config_zonal_service_client_service_account_always_use_jwt(
159147
transport_class, transport_name
160148
):
161149
with mock.patch.object(
@@ -165,6 +153,13 @@ def test_os_config_zonal_service_client_service_account_always_use_jwt_true(
165153
transport = transport_class(credentials=creds, always_use_jwt_access=True)
166154
use_jwt.assert_called_once_with(True)
167155

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+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
161+
use_jwt.assert_not_called()
162+
168163

169164
@pytest.mark.parametrize(
170165
"client_class", [OsConfigZonalServiceClient, OsConfigZonalServiceAsyncClient,]
@@ -249,6 +244,7 @@ def test_os_config_zonal_service_client_client_options(
249244
client_cert_source_for_mtls=None,
250245
quota_project_id=None,
251246
client_info=transports.base.DEFAULT_CLIENT_INFO,
247+
always_use_jwt_access=True,
252248
)
253249

254250
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -265,6 +261,7 @@ def test_os_config_zonal_service_client_client_options(
265261
client_cert_source_for_mtls=None,
266262
quota_project_id=None,
267263
client_info=transports.base.DEFAULT_CLIENT_INFO,
264+
always_use_jwt_access=True,
268265
)
269266

270267
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -281,6 +278,7 @@ def test_os_config_zonal_service_client_client_options(
281278
client_cert_source_for_mtls=None,
282279
quota_project_id=None,
283280
client_info=transports.base.DEFAULT_CLIENT_INFO,
281+
always_use_jwt_access=True,
284282
)
285283

286284
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -309,6 +307,7 @@ def test_os_config_zonal_service_client_client_options(
309307
client_cert_source_for_mtls=None,
310308
quota_project_id="octopus",
311309
client_info=transports.base.DEFAULT_CLIENT_INFO,
310+
always_use_jwt_access=True,
312311
)
313312

314313

@@ -385,6 +384,7 @@ def test_os_config_zonal_service_client_mtls_env_auto(
385384
client_cert_source_for_mtls=expected_client_cert_source,
386385
quota_project_id=None,
387386
client_info=transports.base.DEFAULT_CLIENT_INFO,
387+
always_use_jwt_access=True,
388388
)
389389

390390
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -418,6 +418,7 @@ def test_os_config_zonal_service_client_mtls_env_auto(
418418
client_cert_source_for_mtls=expected_client_cert_source,
419419
quota_project_id=None,
420420
client_info=transports.base.DEFAULT_CLIENT_INFO,
421+
always_use_jwt_access=True,
421422
)
422423

423424
# Check the case client_cert_source and ADC client cert are not provided.
@@ -439,6 +440,7 @@ def test_os_config_zonal_service_client_mtls_env_auto(
439440
client_cert_source_for_mtls=None,
440441
quota_project_id=None,
441442
client_info=transports.base.DEFAULT_CLIENT_INFO,
443+
always_use_jwt_access=True,
442444
)
443445

444446

@@ -473,6 +475,7 @@ def test_os_config_zonal_service_client_client_options_scopes(
473475
client_cert_source_for_mtls=None,
474476
quota_project_id=None,
475477
client_info=transports.base.DEFAULT_CLIENT_INFO,
478+
always_use_jwt_access=True,
476479
)
477480

478481

@@ -507,6 +510,7 @@ def test_os_config_zonal_service_client_client_options_credentials_file(
507510
client_cert_source_for_mtls=None,
508511
quota_project_id=None,
509512
client_info=transports.base.DEFAULT_CLIENT_INFO,
513+
always_use_jwt_access=True,
510514
)
511515

512516

@@ -526,6 +530,7 @@ def test_os_config_zonal_service_client_client_options_from_dict():
526530
client_cert_source_for_mtls=None,
527531
quota_project_id=None,
528532
client_info=transports.base.DEFAULT_CLIENT_INFO,
533+
always_use_jwt_access=True,
529534
)
530535

531536

0 commit comments

Comments
 (0)