Skip to content

Commit 9d43a38

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

File tree

6 files changed

+66
-37
lines changed

6 files changed

+66
-37
lines changed

google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/client.py

+4
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_instance(

google/cloud/bigtable_admin_v2/services/bigtable_table_admin/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,10 @@ def __init__(
446446
client_cert_source_for_mtls=client_cert_source_func,
447447
quota_project_id=client_options.quota_project_id,
448448
client_info=client_info,
449+
always_use_jwt_access=(
450+
Transport == type(self).get_transport_class("grpc")
451+
or Transport == type(self).get_transport_class("grpc_asyncio")
452+
),
449453
)
450454

451455
def create_table(

google/cloud/bigtable_v2/services/bigtable/client.py

+4
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def __init__(
344344
client_cert_source_for_mtls=client_cert_source_func,
345345
quota_project_id=client_options.quota_project_id,
346346
client_info=client_info,
347+
always_use_jwt_access=(
348+
Transport == type(self).get_transport_class("grpc")
349+
or Transport == type(self).get_transport_class("grpc_asyncio")
350+
),
347351
)
348352

349353
def read_rows(

tests/unit/gapic/bigtable_admin_v2/test_bigtable_instance_admin.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,14 @@ def test_bigtable_instance_admin_client_from_service_account_info(client_class):
135135
assert client.transport._host == "bigtableadmin.googleapis.com:443"
136136

137137

138-
@pytest.mark.parametrize(
139-
"client_class", [BigtableInstanceAdminClient, BigtableInstanceAdminAsyncClient,]
140-
)
141-
def test_bigtable_instance_admin_client_service_account_always_use_jwt(client_class):
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-
client = client_class(credentials=creds)
147-
use_jwt.assert_not_called()
148-
149-
150138
@pytest.mark.parametrize(
151139
"transport_class,transport_name",
152140
[
153141
(transports.BigtableInstanceAdminGrpcTransport, "grpc"),
154142
(transports.BigtableInstanceAdminGrpcAsyncIOTransport, "grpc_asyncio"),
155143
],
156144
)
157-
def test_bigtable_instance_admin_client_service_account_always_use_jwt_true(
145+
def test_bigtable_instance_admin_client_service_account_always_use_jwt(
158146
transport_class, transport_name
159147
):
160148
with mock.patch.object(
@@ -164,6 +152,13 @@ def test_bigtable_instance_admin_client_service_account_always_use_jwt_true(
164152
transport = transport_class(credentials=creds, always_use_jwt_access=True)
165153
use_jwt.assert_called_once_with(True)
166154

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

168163
@pytest.mark.parametrize(
169164
"client_class", [BigtableInstanceAdminClient, BigtableInstanceAdminAsyncClient,]
@@ -248,6 +243,7 @@ def test_bigtable_instance_admin_client_client_options(
248243
client_cert_source_for_mtls=None,
249244
quota_project_id=None,
250245
client_info=transports.base.DEFAULT_CLIENT_INFO,
246+
always_use_jwt_access=True,
251247
)
252248

253249
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -264,6 +260,7 @@ def test_bigtable_instance_admin_client_client_options(
264260
client_cert_source_for_mtls=None,
265261
quota_project_id=None,
266262
client_info=transports.base.DEFAULT_CLIENT_INFO,
263+
always_use_jwt_access=True,
267264
)
268265

269266
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -280,6 +277,7 @@ def test_bigtable_instance_admin_client_client_options(
280277
client_cert_source_for_mtls=None,
281278
quota_project_id=None,
282279
client_info=transports.base.DEFAULT_CLIENT_INFO,
280+
always_use_jwt_access=True,
283281
)
284282

285283
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -308,6 +306,7 @@ def test_bigtable_instance_admin_client_client_options(
308306
client_cert_source_for_mtls=None,
309307
quota_project_id="octopus",
310308
client_info=transports.base.DEFAULT_CLIENT_INFO,
309+
always_use_jwt_access=True,
311310
)
312311

313312

@@ -384,6 +383,7 @@ def test_bigtable_instance_admin_client_mtls_env_auto(
384383
client_cert_source_for_mtls=expected_client_cert_source,
385384
quota_project_id=None,
386385
client_info=transports.base.DEFAULT_CLIENT_INFO,
386+
always_use_jwt_access=True,
387387
)
388388

389389
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -417,6 +417,7 @@ def test_bigtable_instance_admin_client_mtls_env_auto(
417417
client_cert_source_for_mtls=expected_client_cert_source,
418418
quota_project_id=None,
419419
client_info=transports.base.DEFAULT_CLIENT_INFO,
420+
always_use_jwt_access=True,
420421
)
421422

422423
# Check the case client_cert_source and ADC client cert are not provided.
@@ -438,6 +439,7 @@ def test_bigtable_instance_admin_client_mtls_env_auto(
438439
client_cert_source_for_mtls=None,
439440
quota_project_id=None,
440441
client_info=transports.base.DEFAULT_CLIENT_INFO,
442+
always_use_jwt_access=True,
441443
)
442444

443445

@@ -472,6 +474,7 @@ def test_bigtable_instance_admin_client_client_options_scopes(
472474
client_cert_source_for_mtls=None,
473475
quota_project_id=None,
474476
client_info=transports.base.DEFAULT_CLIENT_INFO,
477+
always_use_jwt_access=True,
475478
)
476479

477480

@@ -506,6 +509,7 @@ def test_bigtable_instance_admin_client_client_options_credentials_file(
506509
client_cert_source_for_mtls=None,
507510
quota_project_id=None,
508511
client_info=transports.base.DEFAULT_CLIENT_INFO,
512+
always_use_jwt_access=True,
509513
)
510514

511515

@@ -525,6 +529,7 @@ def test_bigtable_instance_admin_client_client_options_from_dict():
525529
client_cert_source_for_mtls=None,
526530
quota_project_id=None,
527531
client_info=transports.base.DEFAULT_CLIENT_INFO,
532+
always_use_jwt_access=True,
528533
)
529534

530535

tests/unit/gapic/bigtable_admin_v2/test_bigtable_table_admin.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,14 @@ def test_bigtable_table_admin_client_from_service_account_info(client_class):
138138
assert client.transport._host == "bigtableadmin.googleapis.com:443"
139139

140140

141-
@pytest.mark.parametrize(
142-
"client_class", [BigtableTableAdminClient, BigtableTableAdminAsyncClient,]
143-
)
144-
def test_bigtable_table_admin_client_service_account_always_use_jwt(client_class):
145-
with mock.patch.object(
146-
service_account.Credentials, "with_always_use_jwt_access", create=True
147-
) as use_jwt:
148-
creds = service_account.Credentials(None, None, None)
149-
client = client_class(credentials=creds)
150-
use_jwt.assert_not_called()
151-
152-
153141
@pytest.mark.parametrize(
154142
"transport_class,transport_name",
155143
[
156144
(transports.BigtableTableAdminGrpcTransport, "grpc"),
157145
(transports.BigtableTableAdminGrpcAsyncIOTransport, "grpc_asyncio"),
158146
],
159147
)
160-
def test_bigtable_table_admin_client_service_account_always_use_jwt_true(
148+
def test_bigtable_table_admin_client_service_account_always_use_jwt(
161149
transport_class, transport_name
162150
):
163151
with mock.patch.object(
@@ -167,6 +155,13 @@ def test_bigtable_table_admin_client_service_account_always_use_jwt_true(
167155
transport = transport_class(credentials=creds, always_use_jwt_access=True)
168156
use_jwt.assert_called_once_with(True)
169157

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

171166
@pytest.mark.parametrize(
172167
"client_class", [BigtableTableAdminClient, BigtableTableAdminAsyncClient,]
@@ -247,6 +242,7 @@ def test_bigtable_table_admin_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_bigtable_table_admin_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_bigtable_table_admin_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_bigtable_table_admin_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_bigtable_table_admin_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_bigtable_table_admin_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_bigtable_table_admin_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

@@ -467,6 +469,7 @@ def test_bigtable_table_admin_client_client_options_scopes(
467469
client_cert_source_for_mtls=None,
468470
quota_project_id=None,
469471
client_info=transports.base.DEFAULT_CLIENT_INFO,
472+
always_use_jwt_access=True,
470473
)
471474

472475

@@ -497,6 +500,7 @@ def test_bigtable_table_admin_client_client_options_credentials_file(
497500
client_cert_source_for_mtls=None,
498501
quota_project_id=None,
499502
client_info=transports.base.DEFAULT_CLIENT_INFO,
503+
always_use_jwt_access=True,
500504
)
501505

502506

@@ -516,6 +520,7 @@ def test_bigtable_table_admin_client_client_options_from_dict():
516520
client_cert_source_for_mtls=None,
517521
quota_project_id=None,
518522
client_info=transports.base.DEFAULT_CLIENT_INFO,
523+
always_use_jwt_access=True,
519524
)
520525

521526

tests/unit/gapic/bigtable_v2/test_bigtable.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,14 @@ def test_bigtable_client_from_service_account_info(client_class):
111111
assert client.transport._host == "bigtable.googleapis.com:443"
112112

113113

114-
@pytest.mark.parametrize("client_class", [BigtableClient, BigtableAsyncClient,])
115-
def test_bigtable_client_service_account_always_use_jwt(client_class):
116-
with mock.patch.object(
117-
service_account.Credentials, "with_always_use_jwt_access", create=True
118-
) as use_jwt:
119-
creds = service_account.Credentials(None, None, None)
120-
client = client_class(credentials=creds)
121-
use_jwt.assert_not_called()
122-
123-
124114
@pytest.mark.parametrize(
125115
"transport_class,transport_name",
126116
[
127117
(transports.BigtableGrpcTransport, "grpc"),
128118
(transports.BigtableGrpcAsyncIOTransport, "grpc_asyncio"),
129119
],
130120
)
131-
def test_bigtable_client_service_account_always_use_jwt_true(
121+
def test_bigtable_client_service_account_always_use_jwt(
132122
transport_class, transport_name
133123
):
134124
with mock.patch.object(
@@ -138,6 +128,13 @@ def test_bigtable_client_service_account_always_use_jwt_true(
138128
transport = transport_class(credentials=creds, always_use_jwt_access=True)
139129
use_jwt.assert_called_once_with(True)
140130

131+
with mock.patch.object(
132+
service_account.Credentials, "with_always_use_jwt_access", create=True
133+
) as use_jwt:
134+
creds = service_account.Credentials(None, None, None)
135+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
136+
use_jwt.assert_not_called()
137+
141138

142139
@pytest.mark.parametrize("client_class", [BigtableClient, BigtableAsyncClient,])
143140
def test_bigtable_client_from_service_account_file(client_class):
@@ -208,6 +205,7 @@ def test_bigtable_client_client_options(client_class, transport_class, transport
208205
client_cert_source_for_mtls=None,
209206
quota_project_id=None,
210207
client_info=transports.base.DEFAULT_CLIENT_INFO,
208+
always_use_jwt_access=True,
211209
)
212210

213211
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -224,6 +222,7 @@ def test_bigtable_client_client_options(client_class, transport_class, transport
224222
client_cert_source_for_mtls=None,
225223
quota_project_id=None,
226224
client_info=transports.base.DEFAULT_CLIENT_INFO,
225+
always_use_jwt_access=True,
227226
)
228227

229228
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -240,6 +239,7 @@ def test_bigtable_client_client_options(client_class, transport_class, transport
240239
client_cert_source_for_mtls=None,
241240
quota_project_id=None,
242241
client_info=transports.base.DEFAULT_CLIENT_INFO,
242+
always_use_jwt_access=True,
243243
)
244244

245245
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -268,6 +268,7 @@ def test_bigtable_client_client_options(client_class, transport_class, transport
268268
client_cert_source_for_mtls=None,
269269
quota_project_id="octopus",
270270
client_info=transports.base.DEFAULT_CLIENT_INFO,
271+
always_use_jwt_access=True,
271272
)
272273

273274

@@ -332,6 +333,7 @@ def test_bigtable_client_mtls_env_auto(
332333
client_cert_source_for_mtls=expected_client_cert_source,
333334
quota_project_id=None,
334335
client_info=transports.base.DEFAULT_CLIENT_INFO,
336+
always_use_jwt_access=True,
335337
)
336338

337339
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -365,6 +367,7 @@ def test_bigtable_client_mtls_env_auto(
365367
client_cert_source_for_mtls=expected_client_cert_source,
366368
quota_project_id=None,
367369
client_info=transports.base.DEFAULT_CLIENT_INFO,
370+
always_use_jwt_access=True,
368371
)
369372

370373
# Check the case client_cert_source and ADC client cert are not provided.
@@ -386,6 +389,7 @@ def test_bigtable_client_mtls_env_auto(
386389
client_cert_source_for_mtls=None,
387390
quota_project_id=None,
388391
client_info=transports.base.DEFAULT_CLIENT_INFO,
392+
always_use_jwt_access=True,
389393
)
390394

391395

@@ -412,6 +416,7 @@ def test_bigtable_client_client_options_scopes(
412416
client_cert_source_for_mtls=None,
413417
quota_project_id=None,
414418
client_info=transports.base.DEFAULT_CLIENT_INFO,
419+
always_use_jwt_access=True,
415420
)
416421

417422

@@ -438,6 +443,7 @@ def test_bigtable_client_client_options_credentials_file(
438443
client_cert_source_for_mtls=None,
439444
quota_project_id=None,
440445
client_info=transports.base.DEFAULT_CLIENT_INFO,
446+
always_use_jwt_access=True,
441447
)
442448

443449

@@ -455,6 +461,7 @@ def test_bigtable_client_client_options_from_dict():
455461
client_cert_source_for_mtls=None,
456462
quota_project_id=None,
457463
client_info=transports.base.DEFAULT_CLIENT_INFO,
464+
always_use_jwt_access=True,
458465
)
459466

460467

0 commit comments

Comments
 (0)