Skip to content

Commit 707fad1

Browse files
fix: enable self signed jwt for grpc (#360)
PiperOrigin-RevId: 386504689 Source-Link: googleapis/googleapis@762094a Source-Link: googleapis/googleapis-gen@6bfc480
1 parent aa630bb commit 707fad1

File tree

6 files changed

+66
-39
lines changed

6 files changed

+66
-39
lines changed

google/cloud/logging_v2/services/config_service_v2/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ def __init__(
397397
client_cert_source_for_mtls=client_cert_source_func,
398398
quota_project_id=client_options.quota_project_id,
399399
client_info=client_info,
400+
always_use_jwt_access=(
401+
Transport == type(self).get_transport_class("grpc")
402+
or Transport == type(self).get_transport_class("grpc_asyncio")
403+
),
400404
)
401405

402406
def list_buckets(

google/cloud/logging_v2/services/logging_service_v2/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ def __init__(
351351
client_cert_source_for_mtls=client_cert_source_func,
352352
quota_project_id=client_options.quota_project_id,
353353
client_info=client_info,
354+
always_use_jwt_access=(
355+
Transport == type(self).get_transport_class("grpc")
356+
or Transport == type(self).get_transport_class("grpc_asyncio")
357+
),
354358
)
355359

356360
def delete_log(

google/cloud/logging_v2/services/metrics_service_v2/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def __init__(
344344
client_cert_source_for_mtls=client_cert_source_func,
345345
quota_project_id=client_options.quota_project_id,
346346
client_info=client_info,
347+
always_use_jwt_access=(
348+
Transport == type(self).get_transport_class("grpc")
349+
or Transport == type(self).get_transport_class("grpc_asyncio")
350+
),
347351
)
348352

349353
def list_log_metrics(

tests/unit/gapic/logging_v2/test_config_service_v2.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,14 @@ def test_config_service_v2_client_from_service_account_info(client_class):
122122
assert client.transport._host == "logging.googleapis.com:443"
123123

124124

125-
@pytest.mark.parametrize(
126-
"client_class", [ConfigServiceV2Client, ConfigServiceV2AsyncClient,]
127-
)
128-
def test_config_service_v2_client_service_account_always_use_jwt(client_class):
129-
with mock.patch.object(
130-
service_account.Credentials, "with_always_use_jwt_access", create=True
131-
) as use_jwt:
132-
creds = service_account.Credentials(None, None, None)
133-
client = client_class(credentials=creds)
134-
use_jwt.assert_not_called()
135-
136-
137125
@pytest.mark.parametrize(
138126
"transport_class,transport_name",
139127
[
140128
(transports.ConfigServiceV2GrpcTransport, "grpc"),
141129
(transports.ConfigServiceV2GrpcAsyncIOTransport, "grpc_asyncio"),
142130
],
143131
)
144-
def test_config_service_v2_client_service_account_always_use_jwt_true(
132+
def test_config_service_v2_client_service_account_always_use_jwt(
145133
transport_class, transport_name
146134
):
147135
with mock.patch.object(
@@ -151,6 +139,13 @@ def test_config_service_v2_client_service_account_always_use_jwt_true(
151139
transport = transport_class(credentials=creds, always_use_jwt_access=True)
152140
use_jwt.assert_called_once_with(True)
153141

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

155150
@pytest.mark.parametrize(
156151
"client_class", [ConfigServiceV2Client, ConfigServiceV2AsyncClient,]
@@ -231,6 +226,7 @@ def test_config_service_v2_client_client_options(
231226
client_cert_source_for_mtls=None,
232227
quota_project_id=None,
233228
client_info=transports.base.DEFAULT_CLIENT_INFO,
229+
always_use_jwt_access=True,
234230
)
235231

236232
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -247,6 +243,7 @@ def test_config_service_v2_client_client_options(
247243
client_cert_source_for_mtls=None,
248244
quota_project_id=None,
249245
client_info=transports.base.DEFAULT_CLIENT_INFO,
246+
always_use_jwt_access=True,
250247
)
251248

252249
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -263,6 +260,7 @@ def test_config_service_v2_client_client_options(
263260
client_cert_source_for_mtls=None,
264261
quota_project_id=None,
265262
client_info=transports.base.DEFAULT_CLIENT_INFO,
263+
always_use_jwt_access=True,
266264
)
267265

268266
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -291,6 +289,7 @@ def test_config_service_v2_client_client_options(
291289
client_cert_source_for_mtls=None,
292290
quota_project_id="octopus",
293291
client_info=transports.base.DEFAULT_CLIENT_INFO,
292+
always_use_jwt_access=True,
294293
)
295294

296295

@@ -367,6 +366,7 @@ def test_config_service_v2_client_mtls_env_auto(
367366
client_cert_source_for_mtls=expected_client_cert_source,
368367
quota_project_id=None,
369368
client_info=transports.base.DEFAULT_CLIENT_INFO,
369+
always_use_jwt_access=True,
370370
)
371371

372372
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -400,6 +400,7 @@ def test_config_service_v2_client_mtls_env_auto(
400400
client_cert_source_for_mtls=expected_client_cert_source,
401401
quota_project_id=None,
402402
client_info=transports.base.DEFAULT_CLIENT_INFO,
403+
always_use_jwt_access=True,
403404
)
404405

405406
# Check the case client_cert_source and ADC client cert are not provided.
@@ -421,6 +422,7 @@ def test_config_service_v2_client_mtls_env_auto(
421422
client_cert_source_for_mtls=None,
422423
quota_project_id=None,
423424
client_info=transports.base.DEFAULT_CLIENT_INFO,
425+
always_use_jwt_access=True,
424426
)
425427

426428

@@ -451,6 +453,7 @@ def test_config_service_v2_client_client_options_scopes(
451453
client_cert_source_for_mtls=None,
452454
quota_project_id=None,
453455
client_info=transports.base.DEFAULT_CLIENT_INFO,
456+
always_use_jwt_access=True,
454457
)
455458

456459

@@ -481,6 +484,7 @@ def test_config_service_v2_client_client_options_credentials_file(
481484
client_cert_source_for_mtls=None,
482485
quota_project_id=None,
483486
client_info=transports.base.DEFAULT_CLIENT_INFO,
487+
always_use_jwt_access=True,
484488
)
485489

486490

@@ -500,6 +504,7 @@ def test_config_service_v2_client_client_options_from_dict():
500504
client_cert_source_for_mtls=None,
501505
quota_project_id=None,
502506
client_info=transports.base.DEFAULT_CLIENT_INFO,
507+
always_use_jwt_access=True,
503508
)
504509

505510

tests/unit/gapic/logging_v2/test_logging_service_v2.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,14 @@ def test_logging_service_v2_client_from_service_account_info(client_class):
129129
assert client.transport._host == "logging.googleapis.com:443"
130130

131131

132-
@pytest.mark.parametrize(
133-
"client_class", [LoggingServiceV2Client, LoggingServiceV2AsyncClient,]
134-
)
135-
def test_logging_service_v2_client_service_account_always_use_jwt(client_class):
136-
with mock.patch.object(
137-
service_account.Credentials, "with_always_use_jwt_access", create=True
138-
) as use_jwt:
139-
creds = service_account.Credentials(None, None, None)
140-
client = client_class(credentials=creds)
141-
use_jwt.assert_not_called()
142-
143-
144132
@pytest.mark.parametrize(
145133
"transport_class,transport_name",
146134
[
147135
(transports.LoggingServiceV2GrpcTransport, "grpc"),
148136
(transports.LoggingServiceV2GrpcAsyncIOTransport, "grpc_asyncio"),
149137
],
150138
)
151-
def test_logging_service_v2_client_service_account_always_use_jwt_true(
139+
def test_logging_service_v2_client_service_account_always_use_jwt(
152140
transport_class, transport_name
153141
):
154142
with mock.patch.object(
@@ -158,6 +146,13 @@ def test_logging_service_v2_client_service_account_always_use_jwt_true(
158146
transport = transport_class(credentials=creds, always_use_jwt_access=True)
159147
use_jwt.assert_called_once_with(True)
160148

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

162157
@pytest.mark.parametrize(
163158
"client_class", [LoggingServiceV2Client, LoggingServiceV2AsyncClient,]
@@ -238,6 +233,7 @@ def test_logging_service_v2_client_client_options(
238233
client_cert_source_for_mtls=None,
239234
quota_project_id=None,
240235
client_info=transports.base.DEFAULT_CLIENT_INFO,
236+
always_use_jwt_access=True,
241237
)
242238

243239
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -254,6 +250,7 @@ def test_logging_service_v2_client_client_options(
254250
client_cert_source_for_mtls=None,
255251
quota_project_id=None,
256252
client_info=transports.base.DEFAULT_CLIENT_INFO,
253+
always_use_jwt_access=True,
257254
)
258255

259256
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -270,6 +267,7 @@ def test_logging_service_v2_client_client_options(
270267
client_cert_source_for_mtls=None,
271268
quota_project_id=None,
272269
client_info=transports.base.DEFAULT_CLIENT_INFO,
270+
always_use_jwt_access=True,
273271
)
274272

275273
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -298,6 +296,7 @@ def test_logging_service_v2_client_client_options(
298296
client_cert_source_for_mtls=None,
299297
quota_project_id="octopus",
300298
client_info=transports.base.DEFAULT_CLIENT_INFO,
299+
always_use_jwt_access=True,
301300
)
302301

303302

@@ -374,6 +373,7 @@ def test_logging_service_v2_client_mtls_env_auto(
374373
client_cert_source_for_mtls=expected_client_cert_source,
375374
quota_project_id=None,
376375
client_info=transports.base.DEFAULT_CLIENT_INFO,
376+
always_use_jwt_access=True,
377377
)
378378

379379
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -407,6 +407,7 @@ def test_logging_service_v2_client_mtls_env_auto(
407407
client_cert_source_for_mtls=expected_client_cert_source,
408408
quota_project_id=None,
409409
client_info=transports.base.DEFAULT_CLIENT_INFO,
410+
always_use_jwt_access=True,
410411
)
411412

412413
# Check the case client_cert_source and ADC client cert are not provided.
@@ -428,6 +429,7 @@ def test_logging_service_v2_client_mtls_env_auto(
428429
client_cert_source_for_mtls=None,
429430
quota_project_id=None,
430431
client_info=transports.base.DEFAULT_CLIENT_INFO,
432+
always_use_jwt_access=True,
431433
)
432434

433435

@@ -458,6 +460,7 @@ def test_logging_service_v2_client_client_options_scopes(
458460
client_cert_source_for_mtls=None,
459461
quota_project_id=None,
460462
client_info=transports.base.DEFAULT_CLIENT_INFO,
463+
always_use_jwt_access=True,
461464
)
462465

463466

@@ -488,6 +491,7 @@ def test_logging_service_v2_client_client_options_credentials_file(
488491
client_cert_source_for_mtls=None,
489492
quota_project_id=None,
490493
client_info=transports.base.DEFAULT_CLIENT_INFO,
494+
always_use_jwt_access=True,
491495
)
492496

493497

@@ -507,6 +511,7 @@ def test_logging_service_v2_client_client_options_from_dict():
507511
client_cert_source_for_mtls=None,
508512
quota_project_id=None,
509513
client_info=transports.base.DEFAULT_CLIENT_INFO,
514+
always_use_jwt_access=True,
510515
)
511516

512517

tests/unit/gapic/logging_v2/test_metrics_service_v2.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,14 @@ def test_metrics_service_v2_client_from_service_account_info(client_class):
127127
assert client.transport._host == "logging.googleapis.com:443"
128128

129129

130-
@pytest.mark.parametrize(
131-
"client_class", [MetricsServiceV2Client, MetricsServiceV2AsyncClient,]
132-
)
133-
def test_metrics_service_v2_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.MetricsServiceV2GrpcTransport, "grpc"),
146134
(transports.MetricsServiceV2GrpcAsyncIOTransport, "grpc_asyncio"),
147135
],
148136
)
149-
def test_metrics_service_v2_client_service_account_always_use_jwt_true(
137+
def test_metrics_service_v2_client_service_account_always_use_jwt(
150138
transport_class, transport_name
151139
):
152140
with mock.patch.object(
@@ -156,6 +144,13 @@ def test_metrics_service_v2_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", [MetricsServiceV2Client, MetricsServiceV2AsyncClient,]
@@ -236,6 +231,7 @@ def test_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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_metrics_service_v2_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

0 commit comments

Comments
 (0)