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

Commit b81c721

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 4f25ae8 commit b81c721

File tree

8 files changed

+88
-52
lines changed

8 files changed

+88
-52
lines changed

google/cloud/servicedirectory_v1/services/lookup_service/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 resolve_service(

google/cloud/servicedirectory_v1/services/registration_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ def __init__(
408408
client_cert_source_for_mtls=client_cert_source_func,
409409
quota_project_id=client_options.quota_project_id,
410410
client_info=client_info,
411+
always_use_jwt_access=(
412+
Transport == type(self).get_transport_class("grpc")
413+
or Transport == type(self).get_transport_class("grpc_asyncio")
414+
),
411415
)
412416

413417
def create_namespace(

google/cloud/servicedirectory_v1beta1/services/lookup_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 resolve_service(

google/cloud/servicedirectory_v1beta1/services/registration_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ def __init__(
426426
client_cert_source_for_mtls=client_cert_source_func,
427427
quota_project_id=client_options.quota_project_id,
428428
client_info=client_info,
429+
always_use_jwt_access=(
430+
Transport == type(self).get_transport_class("grpc")
431+
or Transport == type(self).get_transport_class("grpc_asyncio")
432+
),
429433
)
430434

431435
def create_namespace(

tests/unit/gapic/servicedirectory_v1/test_lookup_service.py

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

122122

123-
@pytest.mark.parametrize(
124-
"client_class", [LookupServiceClient, LookupServiceAsyncClient,]
125-
)
126-
def test_lookup_service_client_service_account_always_use_jwt(client_class):
127-
with mock.patch.object(
128-
service_account.Credentials, "with_always_use_jwt_access", create=True
129-
) as use_jwt:
130-
creds = service_account.Credentials(None, None, None)
131-
client = client_class(credentials=creds)
132-
use_jwt.assert_not_called()
133-
134-
135123
@pytest.mark.parametrize(
136124
"transport_class,transport_name",
137125
[
138126
(transports.LookupServiceGrpcTransport, "grpc"),
139127
(transports.LookupServiceGrpcAsyncIOTransport, "grpc_asyncio"),
140128
],
141129
)
142-
def test_lookup_service_client_service_account_always_use_jwt_true(
130+
def test_lookup_service_client_service_account_always_use_jwt(
143131
transport_class, transport_name
144132
):
145133
with mock.patch.object(
@@ -149,6 +137,13 @@ def test_lookup_service_client_service_account_always_use_jwt_true(
149137
transport = transport_class(credentials=creds, always_use_jwt_access=True)
150138
use_jwt.assert_called_once_with(True)
151139

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

153148
@pytest.mark.parametrize(
154149
"client_class", [LookupServiceClient, LookupServiceAsyncClient,]
@@ -229,6 +224,7 @@ def test_lookup_service_client_client_options(
229224
client_cert_source_for_mtls=None,
230225
quota_project_id=None,
231226
client_info=transports.base.DEFAULT_CLIENT_INFO,
227+
always_use_jwt_access=True,
232228
)
233229

234230
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -245,6 +241,7 @@ def test_lookup_service_client_client_options(
245241
client_cert_source_for_mtls=None,
246242
quota_project_id=None,
247243
client_info=transports.base.DEFAULT_CLIENT_INFO,
244+
always_use_jwt_access=True,
248245
)
249246

250247
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -261,6 +258,7 @@ def test_lookup_service_client_client_options(
261258
client_cert_source_for_mtls=None,
262259
quota_project_id=None,
263260
client_info=transports.base.DEFAULT_CLIENT_INFO,
261+
always_use_jwt_access=True,
264262
)
265263

266264
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -289,6 +287,7 @@ def test_lookup_service_client_client_options(
289287
client_cert_source_for_mtls=None,
290288
quota_project_id="octopus",
291289
client_info=transports.base.DEFAULT_CLIENT_INFO,
290+
always_use_jwt_access=True,
292291
)
293292

294293

@@ -355,6 +354,7 @@ def test_lookup_service_client_mtls_env_auto(
355354
client_cert_source_for_mtls=expected_client_cert_source,
356355
quota_project_id=None,
357356
client_info=transports.base.DEFAULT_CLIENT_INFO,
357+
always_use_jwt_access=True,
358358
)
359359

360360
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -388,6 +388,7 @@ def test_lookup_service_client_mtls_env_auto(
388388
client_cert_source_for_mtls=expected_client_cert_source,
389389
quota_project_id=None,
390390
client_info=transports.base.DEFAULT_CLIENT_INFO,
391+
always_use_jwt_access=True,
391392
)
392393

393394
# Check the case client_cert_source and ADC client cert are not provided.
@@ -409,6 +410,7 @@ def test_lookup_service_client_mtls_env_auto(
409410
client_cert_source_for_mtls=None,
410411
quota_project_id=None,
411412
client_info=transports.base.DEFAULT_CLIENT_INFO,
413+
always_use_jwt_access=True,
412414
)
413415

414416

@@ -439,6 +441,7 @@ def test_lookup_service_client_client_options_scopes(
439441
client_cert_source_for_mtls=None,
440442
quota_project_id=None,
441443
client_info=transports.base.DEFAULT_CLIENT_INFO,
444+
always_use_jwt_access=True,
442445
)
443446

444447

@@ -469,6 +472,7 @@ def test_lookup_service_client_client_options_credentials_file(
469472
client_cert_source_for_mtls=None,
470473
quota_project_id=None,
471474
client_info=transports.base.DEFAULT_CLIENT_INFO,
475+
always_use_jwt_access=True,
472476
)
473477

474478

@@ -488,6 +492,7 @@ def test_lookup_service_client_client_options_from_dict():
488492
client_cert_source_for_mtls=None,
489493
quota_project_id=None,
490494
client_info=transports.base.DEFAULT_CLIENT_INFO,
495+
always_use_jwt_access=True,
491496
)
492497

493498

tests/unit/gapic/servicedirectory_v1/test_registration_service.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,14 @@ def test_registration_service_client_from_service_account_info(client_class):
134134
assert client.transport._host == "servicedirectory.googleapis.com:443"
135135

136136

137-
@pytest.mark.parametrize(
138-
"client_class", [RegistrationServiceClient, RegistrationServiceAsyncClient,]
139-
)
140-
def test_registration_service_client_service_account_always_use_jwt(client_class):
141-
with mock.patch.object(
142-
service_account.Credentials, "with_always_use_jwt_access", create=True
143-
) as use_jwt:
144-
creds = service_account.Credentials(None, None, None)
145-
client = client_class(credentials=creds)
146-
use_jwt.assert_not_called()
147-
148-
149137
@pytest.mark.parametrize(
150138
"transport_class,transport_name",
151139
[
152140
(transports.RegistrationServiceGrpcTransport, "grpc"),
153141
(transports.RegistrationServiceGrpcAsyncIOTransport, "grpc_asyncio"),
154142
],
155143
)
156-
def test_registration_service_client_service_account_always_use_jwt_true(
144+
def test_registration_service_client_service_account_always_use_jwt(
157145
transport_class, transport_name
158146
):
159147
with mock.patch.object(
@@ -163,6 +151,13 @@ def test_registration_service_client_service_account_always_use_jwt_true(
163151
transport = transport_class(credentials=creds, always_use_jwt_access=True)
164152
use_jwt.assert_called_once_with(True)
165153

154+
with mock.patch.object(
155+
service_account.Credentials, "with_always_use_jwt_access", create=True
156+
) as use_jwt:
157+
creds = service_account.Credentials(None, None, None)
158+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
159+
use_jwt.assert_not_called()
160+
166161

167162
@pytest.mark.parametrize(
168163
"client_class", [RegistrationServiceClient, RegistrationServiceAsyncClient,]
@@ -247,6 +242,7 @@ def test_registration_service_client_client_options(
247242
client_cert_source_for_mtls=None,
248243
quota_project_id=None,
249244
client_info=transports.base.DEFAULT_CLIENT_INFO,
245+
always_use_jwt_access=True,
250246
)
251247

252248
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -263,6 +259,7 @@ def test_registration_service_client_client_options(
263259
client_cert_source_for_mtls=None,
264260
quota_project_id=None,
265261
client_info=transports.base.DEFAULT_CLIENT_INFO,
262+
always_use_jwt_access=True,
266263
)
267264

268265
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -279,6 +276,7 @@ def test_registration_service_client_client_options(
279276
client_cert_source_for_mtls=None,
280277
quota_project_id=None,
281278
client_info=transports.base.DEFAULT_CLIENT_INFO,
279+
always_use_jwt_access=True,
282280
)
283281

284282
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -307,6 +305,7 @@ def test_registration_service_client_client_options(
307305
client_cert_source_for_mtls=None,
308306
quota_project_id="octopus",
309307
client_info=transports.base.DEFAULT_CLIENT_INFO,
308+
always_use_jwt_access=True,
310309
)
311310

312311

@@ -383,6 +382,7 @@ def test_registration_service_client_mtls_env_auto(
383382
client_cert_source_for_mtls=expected_client_cert_source,
384383
quota_project_id=None,
385384
client_info=transports.base.DEFAULT_CLIENT_INFO,
385+
always_use_jwt_access=True,
386386
)
387387

388388
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -416,6 +416,7 @@ def test_registration_service_client_mtls_env_auto(
416416
client_cert_source_for_mtls=expected_client_cert_source,
417417
quota_project_id=None,
418418
client_info=transports.base.DEFAULT_CLIENT_INFO,
419+
always_use_jwt_access=True,
419420
)
420421

421422
# Check the case client_cert_source and ADC client cert are not provided.
@@ -437,6 +438,7 @@ def test_registration_service_client_mtls_env_auto(
437438
client_cert_source_for_mtls=None,
438439
quota_project_id=None,
439440
client_info=transports.base.DEFAULT_CLIENT_INFO,
441+
always_use_jwt_access=True,
440442
)
441443

442444

@@ -471,6 +473,7 @@ def test_registration_service_client_client_options_scopes(
471473
client_cert_source_for_mtls=None,
472474
quota_project_id=None,
473475
client_info=transports.base.DEFAULT_CLIENT_INFO,
476+
always_use_jwt_access=True,
474477
)
475478

476479

@@ -505,6 +508,7 @@ def test_registration_service_client_client_options_credentials_file(
505508
client_cert_source_for_mtls=None,
506509
quota_project_id=None,
507510
client_info=transports.base.DEFAULT_CLIENT_INFO,
511+
always_use_jwt_access=True,
508512
)
509513

510514

@@ -524,6 +528,7 @@ def test_registration_service_client_client_options_from_dict():
524528
client_cert_source_for_mtls=None,
525529
quota_project_id=None,
526530
client_info=transports.base.DEFAULT_CLIENT_INFO,
531+
always_use_jwt_access=True,
527532
)
528533

529534

0 commit comments

Comments
 (0)