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

Commit 1e35b42

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

6 files changed

Lines changed: 66 additions & 44 deletions

File tree

google/cloud/documentai_v1/services/document_processor_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ def __init__(
369369
client_cert_source_for_mtls=client_cert_source_func,
370370
quota_project_id=client_options.quota_project_id,
371371
client_info=client_info,
372+
always_use_jwt_access=(
373+
Transport == type(self).get_transport_class("grpc")
374+
or Transport == type(self).get_transport_class("grpc_asyncio")
375+
),
372376
)
373377

374378
def process_document(

google/cloud/documentai_v1beta2/services/document_understanding_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ def __init__(
340340
client_cert_source_for_mtls=client_cert_source_func,
341341
quota_project_id=client_options.quota_project_id,
342342
client_info=client_info,
343+
always_use_jwt_access=(
344+
Transport == type(self).get_transport_class("grpc")
345+
or Transport == type(self).get_transport_class("grpc_asyncio")
346+
),
343347
)
344348

345349
def batch_process_documents(

google/cloud/documentai_v1beta3/services/document_processor_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,10 @@ def __init__(
391391
client_cert_source_for_mtls=client_cert_source_func,
392392
quota_project_id=client_options.quota_project_id,
393393
client_info=client_info,
394+
always_use_jwt_access=(
395+
Transport == type(self).get_transport_class("grpc")
396+
or Transport == type(self).get_transport_class("grpc_asyncio")
397+
),
394398
)
395399

396400
def process_document(

tests/unit/gapic/documentai_v1/test_document_processor_service.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,14 @@ def test_document_processor_service_client_from_service_account_info(client_clas
140140
assert client.transport._host == "documentai.googleapis.com:443"
141141

142142

143-
@pytest.mark.parametrize(
144-
"client_class",
145-
[DocumentProcessorServiceClient, DocumentProcessorServiceAsyncClient,],
146-
)
147-
def test_document_processor_service_client_service_account_always_use_jwt(client_class):
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-
client = client_class(credentials=creds)
153-
use_jwt.assert_not_called()
154-
155-
156143
@pytest.mark.parametrize(
157144
"transport_class,transport_name",
158145
[
159146
(transports.DocumentProcessorServiceGrpcTransport, "grpc"),
160147
(transports.DocumentProcessorServiceGrpcAsyncIOTransport, "grpc_asyncio"),
161148
],
162149
)
163-
def test_document_processor_service_client_service_account_always_use_jwt_true(
150+
def test_document_processor_service_client_service_account_always_use_jwt(
164151
transport_class, transport_name
165152
):
166153
with mock.patch.object(
@@ -170,6 +157,13 @@ def test_document_processor_service_client_service_account_always_use_jwt_true(
170157
transport = transport_class(credentials=creds, always_use_jwt_access=True)
171158
use_jwt.assert_called_once_with(True)
172159

160+
with mock.patch.object(
161+
service_account.Credentials, "with_always_use_jwt_access", create=True
162+
) as use_jwt:
163+
creds = service_account.Credentials(None, None, None)
164+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
165+
use_jwt.assert_not_called()
166+
173167

174168
@pytest.mark.parametrize(
175169
"client_class",
@@ -259,6 +253,7 @@ def test_document_processor_service_client_client_options(
259253
client_cert_source_for_mtls=None,
260254
quota_project_id=None,
261255
client_info=transports.base.DEFAULT_CLIENT_INFO,
256+
always_use_jwt_access=True,
262257
)
263258

264259
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -275,6 +270,7 @@ def test_document_processor_service_client_client_options(
275270
client_cert_source_for_mtls=None,
276271
quota_project_id=None,
277272
client_info=transports.base.DEFAULT_CLIENT_INFO,
273+
always_use_jwt_access=True,
278274
)
279275

280276
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -291,6 +287,7 @@ def test_document_processor_service_client_client_options(
291287
client_cert_source_for_mtls=None,
292288
quota_project_id=None,
293289
client_info=transports.base.DEFAULT_CLIENT_INFO,
290+
always_use_jwt_access=True,
294291
)
295292

296293
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -319,6 +316,7 @@ def test_document_processor_service_client_client_options(
319316
client_cert_source_for_mtls=None,
320317
quota_project_id="octopus",
321318
client_info=transports.base.DEFAULT_CLIENT_INFO,
319+
always_use_jwt_access=True,
322320
)
323321

324322

@@ -395,6 +393,7 @@ def test_document_processor_service_client_mtls_env_auto(
395393
client_cert_source_for_mtls=expected_client_cert_source,
396394
quota_project_id=None,
397395
client_info=transports.base.DEFAULT_CLIENT_INFO,
396+
always_use_jwt_access=True,
398397
)
399398

400399
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -428,6 +427,7 @@ def test_document_processor_service_client_mtls_env_auto(
428427
client_cert_source_for_mtls=expected_client_cert_source,
429428
quota_project_id=None,
430429
client_info=transports.base.DEFAULT_CLIENT_INFO,
430+
always_use_jwt_access=True,
431431
)
432432

433433
# Check the case client_cert_source and ADC client cert are not provided.
@@ -449,6 +449,7 @@ def test_document_processor_service_client_mtls_env_auto(
449449
client_cert_source_for_mtls=None,
450450
quota_project_id=None,
451451
client_info=transports.base.DEFAULT_CLIENT_INFO,
452+
always_use_jwt_access=True,
452453
)
453454

454455

@@ -483,6 +484,7 @@ def test_document_processor_service_client_client_options_scopes(
483484
client_cert_source_for_mtls=None,
484485
quota_project_id=None,
485486
client_info=transports.base.DEFAULT_CLIENT_INFO,
487+
always_use_jwt_access=True,
486488
)
487489

488490

@@ -517,6 +519,7 @@ def test_document_processor_service_client_client_options_credentials_file(
517519
client_cert_source_for_mtls=None,
518520
quota_project_id=None,
519521
client_info=transports.base.DEFAULT_CLIENT_INFO,
522+
always_use_jwt_access=True,
520523
)
521524

522525

@@ -536,6 +539,7 @@ def test_document_processor_service_client_client_options_from_dict():
536539
client_cert_source_for_mtls=None,
537540
quota_project_id=None,
538541
client_info=transports.base.DEFAULT_CLIENT_INFO,
542+
always_use_jwt_access=True,
539543
)
540544

541545

tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,14 @@ def test_document_understanding_service_client_from_service_account_info(client_
134134
assert client.transport._host == "us-documentai.googleapis.com:443"
135135

136136

137-
@pytest.mark.parametrize(
138-
"client_class",
139-
[DocumentUnderstandingServiceClient, DocumentUnderstandingServiceAsyncClient,],
140-
)
141-
def test_document_understanding_service_client_service_account_always_use_jwt(
142-
client_class,
143-
):
144-
with mock.patch.object(
145-
service_account.Credentials, "with_always_use_jwt_access", create=True
146-
) as use_jwt:
147-
creds = service_account.Credentials(None, None, None)
148-
client = client_class(credentials=creds)
149-
use_jwt.assert_not_called()
150-
151-
152137
@pytest.mark.parametrize(
153138
"transport_class,transport_name",
154139
[
155140
(transports.DocumentUnderstandingServiceGrpcTransport, "grpc"),
156141
(transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, "grpc_asyncio"),
157142
],
158143
)
159-
def test_document_understanding_service_client_service_account_always_use_jwt_true(
144+
def test_document_understanding_service_client_service_account_always_use_jwt(
160145
transport_class, transport_name
161146
):
162147
with mock.patch.object(
@@ -166,6 +151,13 @@ def test_document_understanding_service_client_service_account_always_use_jwt_tr
166151
transport = transport_class(credentials=creds, always_use_jwt_access=True)
167152
use_jwt.assert_called_once_with(True)
168153

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+
169161

170162
@pytest.mark.parametrize(
171163
"client_class",
@@ -255,6 +247,7 @@ def test_document_understanding_service_client_client_options(
255247
client_cert_source_for_mtls=None,
256248
quota_project_id=None,
257249
client_info=transports.base.DEFAULT_CLIENT_INFO,
250+
always_use_jwt_access=True,
258251
)
259252

260253
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -271,6 +264,7 @@ def test_document_understanding_service_client_client_options(
271264
client_cert_source_for_mtls=None,
272265
quota_project_id=None,
273266
client_info=transports.base.DEFAULT_CLIENT_INFO,
267+
always_use_jwt_access=True,
274268
)
275269

276270
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -287,6 +281,7 @@ def test_document_understanding_service_client_client_options(
287281
client_cert_source_for_mtls=None,
288282
quota_project_id=None,
289283
client_info=transports.base.DEFAULT_CLIENT_INFO,
284+
always_use_jwt_access=True,
290285
)
291286

292287
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -315,6 +310,7 @@ def test_document_understanding_service_client_client_options(
315310
client_cert_source_for_mtls=None,
316311
quota_project_id="octopus",
317312
client_info=transports.base.DEFAULT_CLIENT_INFO,
313+
always_use_jwt_access=True,
318314
)
319315

320316

@@ -391,6 +387,7 @@ def test_document_understanding_service_client_mtls_env_auto(
391387
client_cert_source_for_mtls=expected_client_cert_source,
392388
quota_project_id=None,
393389
client_info=transports.base.DEFAULT_CLIENT_INFO,
390+
always_use_jwt_access=True,
394391
)
395392

396393
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -424,6 +421,7 @@ def test_document_understanding_service_client_mtls_env_auto(
424421
client_cert_source_for_mtls=expected_client_cert_source,
425422
quota_project_id=None,
426423
client_info=transports.base.DEFAULT_CLIENT_INFO,
424+
always_use_jwt_access=True,
427425
)
428426

429427
# Check the case client_cert_source and ADC client cert are not provided.
@@ -445,6 +443,7 @@ def test_document_understanding_service_client_mtls_env_auto(
445443
client_cert_source_for_mtls=None,
446444
quota_project_id=None,
447445
client_info=transports.base.DEFAULT_CLIENT_INFO,
446+
always_use_jwt_access=True,
448447
)
449448

450449

@@ -479,6 +478,7 @@ def test_document_understanding_service_client_client_options_scopes(
479478
client_cert_source_for_mtls=None,
480479
quota_project_id=None,
481480
client_info=transports.base.DEFAULT_CLIENT_INFO,
481+
always_use_jwt_access=True,
482482
)
483483

484484

@@ -513,6 +513,7 @@ def test_document_understanding_service_client_client_options_credentials_file(
513513
client_cert_source_for_mtls=None,
514514
quota_project_id=None,
515515
client_info=transports.base.DEFAULT_CLIENT_INFO,
516+
always_use_jwt_access=True,
516517
)
517518

518519

@@ -532,6 +533,7 @@ def test_document_understanding_service_client_client_options_from_dict():
532533
client_cert_source_for_mtls=None,
533534
quota_project_id=None,
534535
client_info=transports.base.DEFAULT_CLIENT_INFO,
536+
always_use_jwt_access=True,
535537
)
536538

537539

0 commit comments

Comments
 (0)