Skip to content

Commit a7e8d91

Browse files
fix: enable self signed jwt for grpc (#249)
PiperOrigin-RevId: 386504689 Source-Link: googleapis/googleapis@762094a Source-Link: googleapis/googleapis-gen@6bfc480
1 parent d64ea13 commit a7e8d91

File tree

6 files changed

+66
-35
lines changed

6 files changed

+66
-35
lines changed

google/cloud/bigquery_storage_v1/services/big_query_read/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,10 @@ def __init__(
381381
client_cert_source_for_mtls=client_cert_source_func,
382382
quota_project_id=client_options.quota_project_id,
383383
client_info=client_info,
384+
always_use_jwt_access=(
385+
Transport == type(self).get_transport_class("grpc")
386+
or Transport == type(self).get_transport_class("grpc_asyncio")
387+
),
384388
)
385389

386390
def create_read_session(

google/cloud/bigquery_storage_v1beta2/services/big_query_read/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 create_read_session(

google/cloud/bigquery_storage_v1beta2/services/big_query_write/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ def __init__(
373373
client_cert_source_for_mtls=client_cert_source_func,
374374
quota_project_id=client_options.quota_project_id,
375375
client_info=client_info,
376+
always_use_jwt_access=(
377+
Transport == type(self).get_transport_class("grpc")
378+
or Transport == type(self).get_transport_class("grpc_asyncio")
379+
),
376380
)
377381

378382
def create_write_stream(

tests/unit/gapic/bigquery_storage_v1/test_big_query_read.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,14 @@ def test_big_query_read_client_from_service_account_info(client_class):
118118
assert client.transport._host == "bigquerystorage.googleapis.com:443"
119119

120120

121-
@pytest.mark.parametrize("client_class", [BigQueryReadClient, BigQueryReadAsyncClient,])
122-
def test_big_query_read_client_service_account_always_use_jwt(client_class):
123-
with mock.patch.object(
124-
service_account.Credentials, "with_always_use_jwt_access", create=True
125-
) as use_jwt:
126-
creds = service_account.Credentials(None, None, None)
127-
client = client_class(credentials=creds)
128-
use_jwt.assert_not_called()
129-
130-
131121
@pytest.mark.parametrize(
132122
"transport_class,transport_name",
133123
[
134124
(transports.BigQueryReadGrpcTransport, "grpc"),
135125
(transports.BigQueryReadGrpcAsyncIOTransport, "grpc_asyncio"),
136126
],
137127
)
138-
def test_big_query_read_client_service_account_always_use_jwt_true(
128+
def test_big_query_read_client_service_account_always_use_jwt(
139129
transport_class, transport_name
140130
):
141131
with mock.patch.object(
@@ -145,6 +135,13 @@ def test_big_query_read_client_service_account_always_use_jwt_true(
145135
transport = transport_class(credentials=creds, always_use_jwt_access=True)
146136
use_jwt.assert_called_once_with(True)
147137

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

149146
@pytest.mark.parametrize("client_class", [BigQueryReadClient, BigQueryReadAsyncClient,])
150147
def test_big_query_read_client_from_service_account_file(client_class):
@@ -221,6 +218,7 @@ def test_big_query_read_client_client_options(
221218
client_cert_source_for_mtls=None,
222219
quota_project_id=None,
223220
client_info=transports.base.DEFAULT_CLIENT_INFO,
221+
always_use_jwt_access=True,
224222
)
225223

226224
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -237,6 +235,7 @@ def test_big_query_read_client_client_options(
237235
client_cert_source_for_mtls=None,
238236
quota_project_id=None,
239237
client_info=transports.base.DEFAULT_CLIENT_INFO,
238+
always_use_jwt_access=True,
240239
)
241240

242241
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -253,6 +252,7 @@ def test_big_query_read_client_client_options(
253252
client_cert_source_for_mtls=None,
254253
quota_project_id=None,
255254
client_info=transports.base.DEFAULT_CLIENT_INFO,
255+
always_use_jwt_access=True,
256256
)
257257

258258
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -281,6 +281,7 @@ def test_big_query_read_client_client_options(
281281
client_cert_source_for_mtls=None,
282282
quota_project_id="octopus",
283283
client_info=transports.base.DEFAULT_CLIENT_INFO,
284+
always_use_jwt_access=True,
284285
)
285286

286287

@@ -345,6 +346,7 @@ def test_big_query_read_client_mtls_env_auto(
345346
client_cert_source_for_mtls=expected_client_cert_source,
346347
quota_project_id=None,
347348
client_info=transports.base.DEFAULT_CLIENT_INFO,
349+
always_use_jwt_access=True,
348350
)
349351

350352
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -378,6 +380,7 @@ def test_big_query_read_client_mtls_env_auto(
378380
client_cert_source_for_mtls=expected_client_cert_source,
379381
quota_project_id=None,
380382
client_info=transports.base.DEFAULT_CLIENT_INFO,
383+
always_use_jwt_access=True,
381384
)
382385

383386
# Check the case client_cert_source and ADC client cert are not provided.
@@ -399,6 +402,7 @@ def test_big_query_read_client_mtls_env_auto(
399402
client_cert_source_for_mtls=None,
400403
quota_project_id=None,
401404
client_info=transports.base.DEFAULT_CLIENT_INFO,
405+
always_use_jwt_access=True,
402406
)
403407

404408

@@ -429,6 +433,7 @@ def test_big_query_read_client_client_options_scopes(
429433
client_cert_source_for_mtls=None,
430434
quota_project_id=None,
431435
client_info=transports.base.DEFAULT_CLIENT_INFO,
436+
always_use_jwt_access=True,
432437
)
433438

434439

@@ -459,6 +464,7 @@ def test_big_query_read_client_client_options_credentials_file(
459464
client_cert_source_for_mtls=None,
460465
quota_project_id=None,
461466
client_info=transports.base.DEFAULT_CLIENT_INFO,
467+
always_use_jwt_access=True,
462468
)
463469

464470

@@ -476,6 +482,7 @@ def test_big_query_read_client_client_options_from_dict():
476482
client_cert_source_for_mtls=None,
477483
quota_project_id=None,
478484
client_info=transports.base.DEFAULT_CLIENT_INFO,
485+
always_use_jwt_access=True,
479486
)
480487

481488

tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_read.py

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

122122

123-
@pytest.mark.parametrize("client_class", [BigQueryReadClient, BigQueryReadAsyncClient,])
124-
def test_big_query_read_client_service_account_always_use_jwt(client_class):
125-
with mock.patch.object(
126-
service_account.Credentials, "with_always_use_jwt_access", create=True
127-
) as use_jwt:
128-
creds = service_account.Credentials(None, None, None)
129-
client = client_class(credentials=creds)
130-
use_jwt.assert_not_called()
131-
132-
133123
@pytest.mark.parametrize(
134124
"transport_class,transport_name",
135125
[
136126
(transports.BigQueryReadGrpcTransport, "grpc"),
137127
(transports.BigQueryReadGrpcAsyncIOTransport, "grpc_asyncio"),
138128
],
139129
)
140-
def test_big_query_read_client_service_account_always_use_jwt_true(
130+
def test_big_query_read_client_service_account_always_use_jwt(
141131
transport_class, transport_name
142132
):
143133
with mock.patch.object(
@@ -147,6 +137,13 @@ def test_big_query_read_client_service_account_always_use_jwt_true(
147137
transport = transport_class(credentials=creds, always_use_jwt_access=True)
148138
use_jwt.assert_called_once_with(True)
149139

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

151148
@pytest.mark.parametrize("client_class", [BigQueryReadClient, BigQueryReadAsyncClient,])
152149
def test_big_query_read_client_from_service_account_file(client_class):
@@ -223,6 +220,7 @@ def test_big_query_read_client_client_options(
223220
client_cert_source_for_mtls=None,
224221
quota_project_id=None,
225222
client_info=transports.base.DEFAULT_CLIENT_INFO,
223+
always_use_jwt_access=True,
226224
)
227225

228226
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -239,6 +237,7 @@ def test_big_query_read_client_client_options(
239237
client_cert_source_for_mtls=None,
240238
quota_project_id=None,
241239
client_info=transports.base.DEFAULT_CLIENT_INFO,
240+
always_use_jwt_access=True,
242241
)
243242

244243
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -255,6 +254,7 @@ def test_big_query_read_client_client_options(
255254
client_cert_source_for_mtls=None,
256255
quota_project_id=None,
257256
client_info=transports.base.DEFAULT_CLIENT_INFO,
257+
always_use_jwt_access=True,
258258
)
259259

260260
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -283,6 +283,7 @@ def test_big_query_read_client_client_options(
283283
client_cert_source_for_mtls=None,
284284
quota_project_id="octopus",
285285
client_info=transports.base.DEFAULT_CLIENT_INFO,
286+
always_use_jwt_access=True,
286287
)
287288

288289

@@ -347,6 +348,7 @@ def test_big_query_read_client_mtls_env_auto(
347348
client_cert_source_for_mtls=expected_client_cert_source,
348349
quota_project_id=None,
349350
client_info=transports.base.DEFAULT_CLIENT_INFO,
351+
always_use_jwt_access=True,
350352
)
351353

352354
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -380,6 +382,7 @@ def test_big_query_read_client_mtls_env_auto(
380382
client_cert_source_for_mtls=expected_client_cert_source,
381383
quota_project_id=None,
382384
client_info=transports.base.DEFAULT_CLIENT_INFO,
385+
always_use_jwt_access=True,
383386
)
384387

385388
# Check the case client_cert_source and ADC client cert are not provided.
@@ -401,6 +404,7 @@ def test_big_query_read_client_mtls_env_auto(
401404
client_cert_source_for_mtls=None,
402405
quota_project_id=None,
403406
client_info=transports.base.DEFAULT_CLIENT_INFO,
407+
always_use_jwt_access=True,
404408
)
405409

406410

@@ -431,6 +435,7 @@ def test_big_query_read_client_client_options_scopes(
431435
client_cert_source_for_mtls=None,
432436
quota_project_id=None,
433437
client_info=transports.base.DEFAULT_CLIENT_INFO,
438+
always_use_jwt_access=True,
434439
)
435440

436441

@@ -461,6 +466,7 @@ def test_big_query_read_client_client_options_credentials_file(
461466
client_cert_source_for_mtls=None,
462467
quota_project_id=None,
463468
client_info=transports.base.DEFAULT_CLIENT_INFO,
469+
always_use_jwt_access=True,
464470
)
465471

466472

@@ -478,6 +484,7 @@ def test_big_query_read_client_client_options_from_dict():
478484
client_cert_source_for_mtls=None,
479485
quota_project_id=None,
480486
client_info=transports.base.DEFAULT_CLIENT_INFO,
487+
always_use_jwt_access=True,
481488
)
482489

483490

tests/unit/gapic/bigquery_storage_v1beta2/test_big_query_write.py

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

130130

131-
@pytest.mark.parametrize(
132-
"client_class", [BigQueryWriteClient, BigQueryWriteAsyncClient,]
133-
)
134-
def test_big_query_write_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.BigQueryWriteGrpcTransport, "grpc"),
147135
(transports.BigQueryWriteGrpcAsyncIOTransport, "grpc_asyncio"),
148136
],
149137
)
150-
def test_big_query_write_client_service_account_always_use_jwt_true(
138+
def test_big_query_write_client_service_account_always_use_jwt(
151139
transport_class, transport_name
152140
):
153141
with mock.patch.object(
@@ -157,6 +145,13 @@ def test_big_query_write_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", [BigQueryWriteClient, BigQueryWriteAsyncClient,]
@@ -237,6 +232,7 @@ def test_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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_big_query_write_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)