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

Commit 9d23b1a

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

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

google/cloud/dataqna_v1alpha/services/auto_suggestion_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ def __init__(
400400
client_cert_source_for_mtls=client_cert_source_func,
401401
quota_project_id=client_options.quota_project_id,
402402
client_info=client_info,
403+
always_use_jwt_access=(
404+
Transport == type(self).get_transport_class("grpc")
405+
or Transport == type(self).get_transport_class("grpc_asyncio")
406+
),
403407
)
404408

405409
def suggest_queries(

google/cloud/dataqna_v1alpha/services/question_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ def __init__(
383383
client_cert_source_for_mtls=client_cert_source_func,
384384
quota_project_id=client_options.quota_project_id,
385385
client_info=client_info,
386+
always_use_jwt_access=(
387+
Transport == type(self).get_transport_class("grpc")
388+
or Transport == type(self).get_transport_class("grpc_asyncio")
389+
),
386390
)
387391

388392
def get_question(

tests/unit/gapic/dataqna_v1alpha/test_auto_suggestion_service.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,14 @@ def test_auto_suggestion_service_client_from_service_account_info(client_class):
122122
assert client.transport._host == "dataqna.googleapis.com:443"
123123

124124

125-
@pytest.mark.parametrize(
126-
"client_class", [AutoSuggestionServiceClient, AutoSuggestionServiceAsyncClient,]
127-
)
128-
def test_auto_suggestion_service_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.AutoSuggestionServiceGrpcTransport, "grpc"),
141129
(transports.AutoSuggestionServiceGrpcAsyncIOTransport, "grpc_asyncio"),
142130
],
143131
)
144-
def test_auto_suggestion_service_client_service_account_always_use_jwt_true(
132+
def test_auto_suggestion_service_client_service_account_always_use_jwt(
145133
transport_class, transport_name
146134
):
147135
with mock.patch.object(
@@ -151,6 +139,13 @@ def test_auto_suggestion_service_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", [AutoSuggestionServiceClient, AutoSuggestionServiceAsyncClient,]
@@ -235,6 +230,7 @@ def test_auto_suggestion_service_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_auto_suggestion_service_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_auto_suggestion_service_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_auto_suggestion_service_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

@@ -371,6 +370,7 @@ def test_auto_suggestion_service_client_mtls_env_auto(
371370
client_cert_source_for_mtls=expected_client_cert_source,
372371
quota_project_id=None,
373372
client_info=transports.base.DEFAULT_CLIENT_INFO,
373+
always_use_jwt_access=True,
374374
)
375375

376376
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -404,6 +404,7 @@ def test_auto_suggestion_service_client_mtls_env_auto(
404404
client_cert_source_for_mtls=expected_client_cert_source,
405405
quota_project_id=None,
406406
client_info=transports.base.DEFAULT_CLIENT_INFO,
407+
always_use_jwt_access=True,
407408
)
408409

409410
# Check the case client_cert_source and ADC client cert are not provided.
@@ -425,6 +426,7 @@ def test_auto_suggestion_service_client_mtls_env_auto(
425426
client_cert_source_for_mtls=None,
426427
quota_project_id=None,
427428
client_info=transports.base.DEFAULT_CLIENT_INFO,
429+
always_use_jwt_access=True,
428430
)
429431

430432

@@ -459,6 +461,7 @@ def test_auto_suggestion_service_client_client_options_scopes(
459461
client_cert_source_for_mtls=None,
460462
quota_project_id=None,
461463
client_info=transports.base.DEFAULT_CLIENT_INFO,
464+
always_use_jwt_access=True,
462465
)
463466

464467

@@ -493,6 +496,7 @@ def test_auto_suggestion_service_client_client_options_credentials_file(
493496
client_cert_source_for_mtls=None,
494497
quota_project_id=None,
495498
client_info=transports.base.DEFAULT_CLIENT_INFO,
499+
always_use_jwt_access=True,
496500
)
497501

498502

@@ -512,6 +516,7 @@ def test_auto_suggestion_service_client_client_options_from_dict():
512516
client_cert_source_for_mtls=None,
513517
quota_project_id=None,
514518
client_info=transports.base.DEFAULT_CLIENT_INFO,
519+
always_use_jwt_access=True,
515520
)
516521

517522

tests/unit/gapic/dataqna_v1alpha/test_question_service.py

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

130130

131-
@pytest.mark.parametrize(
132-
"client_class", [QuestionServiceClient, QuestionServiceAsyncClient,]
133-
)
134-
def test_question_service_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.QuestionServiceGrpcTransport, "grpc"),
147135
(transports.QuestionServiceGrpcAsyncIOTransport, "grpc_asyncio"),
148136
],
149137
)
150-
def test_question_service_client_service_account_always_use_jwt_true(
138+
def test_question_service_client_service_account_always_use_jwt(
151139
transport_class, transport_name
152140
):
153141
with mock.patch.object(
@@ -157,6 +145,13 @@ def test_question_service_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", [QuestionServiceClient, QuestionServiceAsyncClient,]
@@ -237,6 +232,7 @@ def test_question_service_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_question_service_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_question_service_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_question_service_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

@@ -373,6 +372,7 @@ def test_question_service_client_mtls_env_auto(
373372
client_cert_source_for_mtls=expected_client_cert_source,
374373
quota_project_id=None,
375374
client_info=transports.base.DEFAULT_CLIENT_INFO,
375+
always_use_jwt_access=True,
376376
)
377377

378378
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -406,6 +406,7 @@ def test_question_service_client_mtls_env_auto(
406406
client_cert_source_for_mtls=expected_client_cert_source,
407407
quota_project_id=None,
408408
client_info=transports.base.DEFAULT_CLIENT_INFO,
409+
always_use_jwt_access=True,
409410
)
410411

411412
# Check the case client_cert_source and ADC client cert are not provided.
@@ -427,6 +428,7 @@ def test_question_service_client_mtls_env_auto(
427428
client_cert_source_for_mtls=None,
428429
quota_project_id=None,
429430
client_info=transports.base.DEFAULT_CLIENT_INFO,
431+
always_use_jwt_access=True,
430432
)
431433

432434

@@ -457,6 +459,7 @@ def test_question_service_client_client_options_scopes(
457459
client_cert_source_for_mtls=None,
458460
quota_project_id=None,
459461
client_info=transports.base.DEFAULT_CLIENT_INFO,
462+
always_use_jwt_access=True,
460463
)
461464

462465

@@ -487,6 +490,7 @@ def test_question_service_client_client_options_credentials_file(
487490
client_cert_source_for_mtls=None,
488491
quota_project_id=None,
489492
client_info=transports.base.DEFAULT_CLIENT_INFO,
493+
always_use_jwt_access=True,
490494
)
491495

492496

@@ -506,6 +510,7 @@ def test_question_service_client_client_options_from_dict():
506510
client_cert_source_for_mtls=None,
507511
quota_project_id=None,
508512
client_info=transports.base.DEFAULT_CLIENT_INFO,
513+
always_use_jwt_access=True,
509514
)
510515

511516

0 commit comments

Comments
 (0)