Skip to content

Commit cd0c1a4

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

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_billing/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ def __init__(
330330
client_cert_source_for_mtls=client_cert_source_func,
331331
quota_project_id=client_options.quota_project_id,
332332
client_info=client_info,
333+
always_use_jwt_access=(
334+
Transport == type(self).get_transport_class("grpc")
335+
or Transport == type(self).get_transport_class("grpc_asyncio")
336+
),
333337
)
334338

335339
def get_billing_account(

packages/google-cloud-billing/google/cloud/billing_v1/services/cloud_catalog/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ def __init__(
351351
client_cert_source_for_mtls=client_cert_source_func,
352352
quota_project_id=client_options.quota_project_id,
353353
client_info=client_info,
354+
always_use_jwt_access=(
355+
Transport == type(self).get_transport_class("grpc")
356+
or Transport == type(self).get_transport_class("grpc_asyncio")
357+
),
354358
)
355359

356360
def list_services(

packages/google-cloud-billing/tests/unit/gapic/billing_v1/test_cloud_billing.py

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

120120

121-
@pytest.mark.parametrize("client_class", [CloudBillingClient, CloudBillingAsyncClient,])
122-
def test_cloud_billing_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.CloudBillingGrpcTransport, "grpc"),
135125
(transports.CloudBillingGrpcAsyncIOTransport, "grpc_asyncio"),
136126
],
137127
)
138-
def test_cloud_billing_client_service_account_always_use_jwt_true(
128+
def test_cloud_billing_client_service_account_always_use_jwt(
139129
transport_class, transport_name
140130
):
141131
with mock.patch.object(
@@ -145,6 +135,13 @@ def test_cloud_billing_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", [CloudBillingClient, CloudBillingAsyncClient,])
150147
def test_cloud_billing_client_from_service_account_file(client_class):
@@ -221,6 +218,7 @@ def test_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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_cloud_billing_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

packages/google-cloud-billing/tests/unit/gapic/billing_v1/test_cloud_catalog.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,14 @@ def test_cloud_catalog_client_from_service_account_info(client_class):
114114
assert client.transport._host == "cloudbilling.googleapis.com:443"
115115

116116

117-
@pytest.mark.parametrize("client_class", [CloudCatalogClient, CloudCatalogAsyncClient,])
118-
def test_cloud_catalog_client_service_account_always_use_jwt(client_class):
119-
with mock.patch.object(
120-
service_account.Credentials, "with_always_use_jwt_access", create=True
121-
) as use_jwt:
122-
creds = service_account.Credentials(None, None, None)
123-
client = client_class(credentials=creds)
124-
use_jwt.assert_not_called()
125-
126-
127117
@pytest.mark.parametrize(
128118
"transport_class,transport_name",
129119
[
130120
(transports.CloudCatalogGrpcTransport, "grpc"),
131121
(transports.CloudCatalogGrpcAsyncIOTransport, "grpc_asyncio"),
132122
],
133123
)
134-
def test_cloud_catalog_client_service_account_always_use_jwt_true(
124+
def test_cloud_catalog_client_service_account_always_use_jwt(
135125
transport_class, transport_name
136126
):
137127
with mock.patch.object(
@@ -141,6 +131,13 @@ def test_cloud_catalog_client_service_account_always_use_jwt_true(
141131
transport = transport_class(credentials=creds, always_use_jwt_access=True)
142132
use_jwt.assert_called_once_with(True)
143133

134+
with mock.patch.object(
135+
service_account.Credentials, "with_always_use_jwt_access", create=True
136+
) as use_jwt:
137+
creds = service_account.Credentials(None, None, None)
138+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
139+
use_jwt.assert_not_called()
140+
144141

145142
@pytest.mark.parametrize("client_class", [CloudCatalogClient, CloudCatalogAsyncClient,])
146143
def test_cloud_catalog_client_from_service_account_file(client_class):
@@ -217,6 +214,7 @@ def test_cloud_catalog_client_client_options(
217214
client_cert_source_for_mtls=None,
218215
quota_project_id=None,
219216
client_info=transports.base.DEFAULT_CLIENT_INFO,
217+
always_use_jwt_access=True,
220218
)
221219

222220
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -233,6 +231,7 @@ def test_cloud_catalog_client_client_options(
233231
client_cert_source_for_mtls=None,
234232
quota_project_id=None,
235233
client_info=transports.base.DEFAULT_CLIENT_INFO,
234+
always_use_jwt_access=True,
236235
)
237236

238237
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -249,6 +248,7 @@ def test_cloud_catalog_client_client_options(
249248
client_cert_source_for_mtls=None,
250249
quota_project_id=None,
251250
client_info=transports.base.DEFAULT_CLIENT_INFO,
251+
always_use_jwt_access=True,
252252
)
253253

254254
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -277,6 +277,7 @@ def test_cloud_catalog_client_client_options(
277277
client_cert_source_for_mtls=None,
278278
quota_project_id="octopus",
279279
client_info=transports.base.DEFAULT_CLIENT_INFO,
280+
always_use_jwt_access=True,
280281
)
281282

282283

@@ -341,6 +342,7 @@ def test_cloud_catalog_client_mtls_env_auto(
341342
client_cert_source_for_mtls=expected_client_cert_source,
342343
quota_project_id=None,
343344
client_info=transports.base.DEFAULT_CLIENT_INFO,
345+
always_use_jwt_access=True,
344346
)
345347

346348
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -374,6 +376,7 @@ def test_cloud_catalog_client_mtls_env_auto(
374376
client_cert_source_for_mtls=expected_client_cert_source,
375377
quota_project_id=None,
376378
client_info=transports.base.DEFAULT_CLIENT_INFO,
379+
always_use_jwt_access=True,
377380
)
378381

379382
# Check the case client_cert_source and ADC client cert are not provided.
@@ -395,6 +398,7 @@ def test_cloud_catalog_client_mtls_env_auto(
395398
client_cert_source_for_mtls=None,
396399
quota_project_id=None,
397400
client_info=transports.base.DEFAULT_CLIENT_INFO,
401+
always_use_jwt_access=True,
398402
)
399403

400404

@@ -425,6 +429,7 @@ def test_cloud_catalog_client_client_options_scopes(
425429
client_cert_source_for_mtls=None,
426430
quota_project_id=None,
427431
client_info=transports.base.DEFAULT_CLIENT_INFO,
432+
always_use_jwt_access=True,
428433
)
429434

430435

@@ -455,6 +460,7 @@ def test_cloud_catalog_client_client_options_credentials_file(
455460
client_cert_source_for_mtls=None,
456461
quota_project_id=None,
457462
client_info=transports.base.DEFAULT_CLIENT_INFO,
463+
always_use_jwt_access=True,
458464
)
459465

460466

@@ -472,6 +478,7 @@ def test_cloud_catalog_client_client_options_from_dict():
472478
client_cert_source_for_mtls=None,
473479
quota_project_id=None,
474480
client_info=transports.base.DEFAULT_CLIENT_INFO,
481+
always_use_jwt_access=True,
475482
)
476483

477484

0 commit comments

Comments
 (0)