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

Commit a18fe2a

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

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

google/cloud/scheduler_v1/services/cloud_scheduler/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def __init__(
364364
client_cert_source_for_mtls=client_cert_source_func,
365365
quota_project_id=client_options.quota_project_id,
366366
client_info=client_info,
367+
always_use_jwt_access=(
368+
Transport == type(self).get_transport_class("grpc")
369+
or Transport == type(self).get_transport_class("grpc_asyncio")
370+
),
367371
)
368372

369373
def list_jobs(

google/cloud/scheduler_v1beta1/services/cloud_scheduler/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ def __init__(
364364
client_cert_source_for_mtls=client_cert_source_func,
365365
quota_project_id=client_options.quota_project_id,
366366
client_info=client_info,
367+
always_use_jwt_access=(
368+
Transport == type(self).get_transport_class("grpc")
369+
or Transport == type(self).get_transport_class("grpc_asyncio")
370+
),
367371
)
368372

369373
def list_jobs(

tests/unit/gapic/scheduler_v1/test_cloud_scheduler.py

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

128128

129-
@pytest.mark.parametrize(
130-
"client_class", [CloudSchedulerClient, CloudSchedulerAsyncClient,]
131-
)
132-
def test_cloud_scheduler_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-
141129
@pytest.mark.parametrize(
142130
"transport_class,transport_name",
143131
[
144132
(transports.CloudSchedulerGrpcTransport, "grpc"),
145133
(transports.CloudSchedulerGrpcAsyncIOTransport, "grpc_asyncio"),
146134
],
147135
)
148-
def test_cloud_scheduler_client_service_account_always_use_jwt_true(
136+
def test_cloud_scheduler_client_service_account_always_use_jwt(
149137
transport_class, transport_name
150138
):
151139
with mock.patch.object(
@@ -155,6 +143,13 @@ def test_cloud_scheduler_client_service_account_always_use_jwt_true(
155143
transport = transport_class(credentials=creds, always_use_jwt_access=True)
156144
use_jwt.assert_called_once_with(True)
157145

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

159154
@pytest.mark.parametrize(
160155
"client_class", [CloudSchedulerClient, CloudSchedulerAsyncClient,]
@@ -235,6 +230,7 @@ def test_cloud_scheduler_client_client_options(
235230
client_cert_source_for_mtls=None,
236231
quota_project_id=None,
237232
client_info=transports.base.DEFAULT_CLIENT_INFO,
233+
always_use_jwt_access=True,
238234
)
239235

240236
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -251,6 +247,7 @@ def test_cloud_scheduler_client_client_options(
251247
client_cert_source_for_mtls=None,
252248
quota_project_id=None,
253249
client_info=transports.base.DEFAULT_CLIENT_INFO,
250+
always_use_jwt_access=True,
254251
)
255252

256253
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -267,6 +264,7 @@ def test_cloud_scheduler_client_client_options(
267264
client_cert_source_for_mtls=None,
268265
quota_project_id=None,
269266
client_info=transports.base.DEFAULT_CLIENT_INFO,
267+
always_use_jwt_access=True,
270268
)
271269

272270
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -295,6 +293,7 @@ def test_cloud_scheduler_client_client_options(
295293
client_cert_source_for_mtls=None,
296294
quota_project_id="octopus",
297295
client_info=transports.base.DEFAULT_CLIENT_INFO,
296+
always_use_jwt_access=True,
298297
)
299298

300299

@@ -361,6 +360,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
361360
client_cert_source_for_mtls=expected_client_cert_source,
362361
quota_project_id=None,
363362
client_info=transports.base.DEFAULT_CLIENT_INFO,
363+
always_use_jwt_access=True,
364364
)
365365

366366
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -394,6 +394,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
394394
client_cert_source_for_mtls=expected_client_cert_source,
395395
quota_project_id=None,
396396
client_info=transports.base.DEFAULT_CLIENT_INFO,
397+
always_use_jwt_access=True,
397398
)
398399

399400
# Check the case client_cert_source and ADC client cert are not provided.
@@ -415,6 +416,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
415416
client_cert_source_for_mtls=None,
416417
quota_project_id=None,
417418
client_info=transports.base.DEFAULT_CLIENT_INFO,
419+
always_use_jwt_access=True,
418420
)
419421

420422

@@ -445,6 +447,7 @@ def test_cloud_scheduler_client_client_options_scopes(
445447
client_cert_source_for_mtls=None,
446448
quota_project_id=None,
447449
client_info=transports.base.DEFAULT_CLIENT_INFO,
450+
always_use_jwt_access=True,
448451
)
449452

450453

@@ -475,6 +478,7 @@ def test_cloud_scheduler_client_client_options_credentials_file(
475478
client_cert_source_for_mtls=None,
476479
quota_project_id=None,
477480
client_info=transports.base.DEFAULT_CLIENT_INFO,
481+
always_use_jwt_access=True,
478482
)
479483

480484

@@ -494,6 +498,7 @@ def test_cloud_scheduler_client_client_options_from_dict():
494498
client_cert_source_for_mtls=None,
495499
quota_project_id=None,
496500
client_info=transports.base.DEFAULT_CLIENT_INFO,
501+
always_use_jwt_access=True,
497502
)
498503

499504

tests/unit/gapic/scheduler_v1beta1/test_cloud_scheduler.py

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

130130

131-
@pytest.mark.parametrize(
132-
"client_class", [CloudSchedulerClient, CloudSchedulerAsyncClient,]
133-
)
134-
def test_cloud_scheduler_client_service_account_always_use_jwt(client_class):
135-
with mock.patch.object(
136-
service_account.Credentials, "with_always_use_jwt_access", create=True
137-
) as use_jwt:
138-
creds = service_account.Credentials(None, None, None)
139-
client = client_class(credentials=creds)
140-
use_jwt.assert_not_called()
141-
142-
143131
@pytest.mark.parametrize(
144132
"transport_class,transport_name",
145133
[
146134
(transports.CloudSchedulerGrpcTransport, "grpc"),
147135
(transports.CloudSchedulerGrpcAsyncIOTransport, "grpc_asyncio"),
148136
],
149137
)
150-
def test_cloud_scheduler_client_service_account_always_use_jwt_true(
138+
def test_cloud_scheduler_client_service_account_always_use_jwt(
151139
transport_class, transport_name
152140
):
153141
with mock.patch.object(
@@ -157,6 +145,13 @@ def test_cloud_scheduler_client_service_account_always_use_jwt_true(
157145
transport = transport_class(credentials=creds, always_use_jwt_access=True)
158146
use_jwt.assert_called_once_with(True)
159147

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

161156
@pytest.mark.parametrize(
162157
"client_class", [CloudSchedulerClient, CloudSchedulerAsyncClient,]
@@ -237,6 +232,7 @@ def test_cloud_scheduler_client_client_options(
237232
client_cert_source_for_mtls=None,
238233
quota_project_id=None,
239234
client_info=transports.base.DEFAULT_CLIENT_INFO,
235+
always_use_jwt_access=True,
240236
)
241237

242238
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -253,6 +249,7 @@ def test_cloud_scheduler_client_client_options(
253249
client_cert_source_for_mtls=None,
254250
quota_project_id=None,
255251
client_info=transports.base.DEFAULT_CLIENT_INFO,
252+
always_use_jwt_access=True,
256253
)
257254

258255
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -269,6 +266,7 @@ def test_cloud_scheduler_client_client_options(
269266
client_cert_source_for_mtls=None,
270267
quota_project_id=None,
271268
client_info=transports.base.DEFAULT_CLIENT_INFO,
269+
always_use_jwt_access=True,
272270
)
273271

274272
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -297,6 +295,7 @@ def test_cloud_scheduler_client_client_options(
297295
client_cert_source_for_mtls=None,
298296
quota_project_id="octopus",
299297
client_info=transports.base.DEFAULT_CLIENT_INFO,
298+
always_use_jwt_access=True,
300299
)
301300

302301

@@ -363,6 +362,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
363362
client_cert_source_for_mtls=expected_client_cert_source,
364363
quota_project_id=None,
365364
client_info=transports.base.DEFAULT_CLIENT_INFO,
365+
always_use_jwt_access=True,
366366
)
367367

368368
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -396,6 +396,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
396396
client_cert_source_for_mtls=expected_client_cert_source,
397397
quota_project_id=None,
398398
client_info=transports.base.DEFAULT_CLIENT_INFO,
399+
always_use_jwt_access=True,
399400
)
400401

401402
# Check the case client_cert_source and ADC client cert are not provided.
@@ -417,6 +418,7 @@ def test_cloud_scheduler_client_mtls_env_auto(
417418
client_cert_source_for_mtls=None,
418419
quota_project_id=None,
419420
client_info=transports.base.DEFAULT_CLIENT_INFO,
421+
always_use_jwt_access=True,
420422
)
421423

422424

@@ -447,6 +449,7 @@ def test_cloud_scheduler_client_client_options_scopes(
447449
client_cert_source_for_mtls=None,
448450
quota_project_id=None,
449451
client_info=transports.base.DEFAULT_CLIENT_INFO,
452+
always_use_jwt_access=True,
450453
)
451454

452455

@@ -477,6 +480,7 @@ def test_cloud_scheduler_client_client_options_credentials_file(
477480
client_cert_source_for_mtls=None,
478481
quota_project_id=None,
479482
client_info=transports.base.DEFAULT_CLIENT_INFO,
483+
always_use_jwt_access=True,
480484
)
481485

482486

@@ -496,6 +500,7 @@ def test_cloud_scheduler_client_client_options_from_dict():
496500
client_cert_source_for_mtls=None,
497501
quota_project_id=None,
498502
client_info=transports.base.DEFAULT_CLIENT_INFO,
503+
always_use_jwt_access=True,
499504
)
500505

501506

0 commit comments

Comments
 (0)