GeezerVison is showing github.com — fix: enable self signed jwt for grpc (#184) · googleapis/python-documentai@1e35b42
This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings documentai_v1beta2/services/document_understanding_service documentai_v1beta3/services/document_processor_service documentai_v1/services/document_processor_service
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: google/cloud/documentai_v1/services/document_processor_service/client.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -369,6 +369,10 @@ def __init__( | 369 | 369 | client_cert_source_for_mtls=client_cert_source_func, | 370 | 370 | quota_project_id=client_options.quota_project_id, | 371 | 371 | 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 | + ), | 372 | 376 | ) | 373 | 377 |
| 374 | 378 | def process_document( | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/documentai_v1beta2/services/document_understanding_service/client.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -340,6 +340,10 @@ def __init__( | 340 | 340 | client_cert_source_for_mtls=client_cert_source_func, | 341 | 341 | quota_project_id=client_options.quota_project_id, | 342 | 342 | 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 | + ), | 343 | 347 | ) | 344 | 348 |
| 345 | 349 | def batch_process_documents( | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/documentai_v1beta3/services/document_processor_service/client.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -391,6 +391,10 @@ def __init__( | 391 | 391 | client_cert_source_for_mtls=client_cert_source_func, | 392 | 392 | quota_project_id=client_options.quota_project_id, | 393 | 393 | 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 | + ), | 394 | 398 | ) | 395 | 399 |
| 396 | 400 | def process_document( | |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/gapic/documentai_v1/test_document_processor_service.py +18-14Lines changed: 18 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -140,27 +140,14 @@ def test_document_processor_service_client_from_service_account_info(client_clas | 140 | 140 | assert client.transport._host == "documentai.googleapis.com:443" | 141 | 141 |
| 142 | 142 |
| 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 | | - | 156 | 143 | @pytest.mark.parametrize( | 157 | 144 | "transport_class,transport_name", | 158 | 145 | [ | 159 | 146 | (transports.DocumentProcessorServiceGrpcTransport, "grpc"), | 160 | 147 | (transports.DocumentProcessorServiceGrpcAsyncIOTransport, "grpc_asyncio"), | 161 | 148 | ], | 162 | 149 | ) | 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( | 164 | 151 | transport_class, transport_name | 165 | 152 | ): | 166 | 153 | with mock.patch.object( | @@ -170,6 +157,13 @@ def test_document_processor_service_client_service_account_always_use_jwt_true( | 170 | 157 | transport = transport_class(credentials=creds, always_use_jwt_access=True) | 171 | 158 | use_jwt.assert_called_once_with(True) | 172 | 159 |
| | 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 | + | 173 | 167 |
| 174 | 168 | @pytest.mark.parametrize( | 175 | 169 | "client_class", | @@ -259,6 +253,7 @@ def test_document_processor_service_client_client_options( | 259 | 253 | client_cert_source_for_mtls=None, | 260 | 254 | quota_project_id=None, | 261 | 255 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 256 | + always_use_jwt_access=True, | 262 | 257 | ) | 263 | 258 |
| 264 | 259 | # 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( | 275 | 270 | client_cert_source_for_mtls=None, | 276 | 271 | quota_project_id=None, | 277 | 272 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 273 | + always_use_jwt_access=True, | 278 | 274 | ) | 279 | 275 |
| 280 | 276 | # 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( | 291 | 287 | client_cert_source_for_mtls=None, | 292 | 288 | quota_project_id=None, | 293 | 289 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 290 | + always_use_jwt_access=True, | 294 | 291 | ) | 295 | 292 |
| 296 | 293 | # 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( | 319 | 316 | client_cert_source_for_mtls=None, | 320 | 317 | quota_project_id="octopus", | 321 | 318 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 319 | + always_use_jwt_access=True, | 322 | 320 | ) | 323 | 321 |
| 324 | 322 |
| @@ -395,6 +393,7 @@ def test_document_processor_service_client_mtls_env_auto( | 395 | 393 | client_cert_source_for_mtls=expected_client_cert_source, | 396 | 394 | quota_project_id=None, | 397 | 395 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 396 | + always_use_jwt_access=True, | 398 | 397 | ) | 399 | 398 |
| 400 | 399 | # 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( | 428 | 427 | client_cert_source_for_mtls=expected_client_cert_source, | 429 | 428 | quota_project_id=None, | 430 | 429 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 430 | + always_use_jwt_access=True, | 431 | 431 | ) | 432 | 432 |
| 433 | 433 | # 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( | 449 | 449 | client_cert_source_for_mtls=None, | 450 | 450 | quota_project_id=None, | 451 | 451 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 452 | + always_use_jwt_access=True, | 452 | 453 | ) | 453 | 454 |
| 454 | 455 |
| @@ -483,6 +484,7 @@ def test_document_processor_service_client_client_options_scopes( | 483 | 484 | client_cert_source_for_mtls=None, | 484 | 485 | quota_project_id=None, | 485 | 486 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 487 | + always_use_jwt_access=True, | 486 | 488 | ) | 487 | 489 |
| 488 | 490 |
| @@ -517,6 +519,7 @@ def test_document_processor_service_client_client_options_credentials_file( | 517 | 519 | client_cert_source_for_mtls=None, | 518 | 520 | quota_project_id=None, | 519 | 521 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 522 | + always_use_jwt_access=True, | 520 | 523 | ) | 521 | 524 |
| 522 | 525 |
| @@ -536,6 +539,7 @@ def test_document_processor_service_client_client_options_from_dict(): | 536 | 539 | client_cert_source_for_mtls=None, | 537 | 540 | quota_project_id=None, | 538 | 541 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 542 | + always_use_jwt_access=True, | 539 | 543 | ) | 540 | 544 |
| 541 | 545 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py +18-16Lines changed: 18 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -134,29 +134,14 @@ def test_document_understanding_service_client_from_service_account_info(client_ | 134 | 134 | assert client.transport._host == "us-documentai.googleapis.com:443" | 135 | 135 |
| 136 | 136 |
| 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 | | - | 152 | 137 | @pytest.mark.parametrize( | 153 | 138 | "transport_class,transport_name", | 154 | 139 | [ | 155 | 140 | (transports.DocumentUnderstandingServiceGrpcTransport, "grpc"), | 156 | 141 | (transports.DocumentUnderstandingServiceGrpcAsyncIOTransport, "grpc_asyncio"), | 157 | 142 | ], | 158 | 143 | ) | 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( | 160 | 145 | transport_class, transport_name | 161 | 146 | ): | 162 | 147 | with mock.patch.object( | @@ -166,6 +151,13 @@ def test_document_understanding_service_client_service_account_always_use_jwt_tr | 166 | 151 | transport = transport_class(credentials=creds, always_use_jwt_access=True) | 167 | 152 | use_jwt.assert_called_once_with(True) | 168 | 153 |
| | 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 | + | 169 | 161 |
| 170 | 162 | @pytest.mark.parametrize( | 171 | 163 | "client_class", | @@ -255,6 +247,7 @@ def test_document_understanding_service_client_client_options( | 255 | 247 | client_cert_source_for_mtls=None, | 256 | 248 | quota_project_id=None, | 257 | 249 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 250 | + always_use_jwt_access=True, | 258 | 251 | ) | 259 | 252 |
| 260 | 253 | # 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( | 271 | 264 | client_cert_source_for_mtls=None, | 272 | 265 | quota_project_id=None, | 273 | 266 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 267 | + always_use_jwt_access=True, | 274 | 268 | ) | 275 | 269 |
| 276 | 270 | # 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( | 287 | 281 | client_cert_source_for_mtls=None, | 288 | 282 | quota_project_id=None, | 289 | 283 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 284 | + always_use_jwt_access=True, | 290 | 285 | ) | 291 | 286 |
| 292 | 287 | # 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( | 315 | 310 | client_cert_source_for_mtls=None, | 316 | 311 | quota_project_id="octopus", | 317 | 312 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 313 | + always_use_jwt_access=True, | 318 | 314 | ) | 319 | 315 |
| 320 | 316 |
| @@ -391,6 +387,7 @@ def test_document_understanding_service_client_mtls_env_auto( | 391 | 387 | client_cert_source_for_mtls=expected_client_cert_source, | 392 | 388 | quota_project_id=None, | 393 | 389 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 390 | + always_use_jwt_access=True, | 394 | 391 | ) | 395 | 392 |
| 396 | 393 | # 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( | 424 | 421 | client_cert_source_for_mtls=expected_client_cert_source, | 425 | 422 | quota_project_id=None, | 426 | 423 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 424 | + always_use_jwt_access=True, | 427 | 425 | ) | 428 | 426 |
| 429 | 427 | # 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( | 445 | 443 | client_cert_source_for_mtls=None, | 446 | 444 | quota_project_id=None, | 447 | 445 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 446 | + always_use_jwt_access=True, | 448 | 447 | ) | 449 | 448 |
| 450 | 449 |
| @@ -479,6 +478,7 @@ def test_document_understanding_service_client_client_options_scopes( | 479 | 478 | client_cert_source_for_mtls=None, | 480 | 479 | quota_project_id=None, | 481 | 480 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 481 | + always_use_jwt_access=True, | 482 | 482 | ) | 483 | 483 |
| 484 | 484 |
| @@ -513,6 +513,7 @@ def test_document_understanding_service_client_client_options_credentials_file( | 513 | 513 | client_cert_source_for_mtls=None, | 514 | 514 | quota_project_id=None, | 515 | 515 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 516 | + always_use_jwt_access=True, | 516 | 517 | ) | 517 | 518 |
| 518 | 519 |
| @@ -532,6 +533,7 @@ def test_document_understanding_service_client_client_options_from_dict(): | 532 | 533 | client_cert_source_for_mtls=None, | 533 | 534 | quota_project_id=None, | 534 | 535 | client_info=transports.base.DEFAULT_CLIENT_INFO, | | 536 | + always_use_jwt_access=True, | 535 | 537 | ) | 536 | 538 |
| 537 | 539 |
| |
|