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

Commit 84835a8

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

4 files changed

Lines changed: 44 additions & 26 deletions

File tree

google/cloud/billing/budgets_v1/services/budget_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ def __init__(
346346
client_cert_source_for_mtls=client_cert_source_func,
347347
quota_project_id=client_options.quota_project_id,
348348
client_info=client_info,
349+
always_use_jwt_access=(
350+
Transport == type(self).get_transport_class("grpc")
351+
or Transport == type(self).get_transport_class("grpc_asyncio")
352+
),
349353
)
350354

351355
def create_budget(

google/cloud/billing/budgets_v1beta1/services/budget_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ def __init__(
345345
client_cert_source_for_mtls=client_cert_source_func,
346346
quota_project_id=client_options.quota_project_id,
347347
client_info=client_info,
348+
always_use_jwt_access=(
349+
Transport == type(self).get_transport_class("grpc")
350+
or Transport == type(self).get_transport_class("grpc_asyncio")
351+
),
348352
)
349353

350354
def create_budget(

tests/unit/gapic/budgets_v1/test_budget_service.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,14 @@ def test_budget_service_client_from_service_account_info(client_class):
125125
assert client.transport._host == "billingbudgets.googleapis.com:443"
126126

127127

128-
@pytest.mark.parametrize(
129-
"client_class", [BudgetServiceClient, BudgetServiceAsyncClient,]
130-
)
131-
def test_budget_service_client_service_account_always_use_jwt(client_class):
132-
with mock.patch.object(
133-
service_account.Credentials, "with_always_use_jwt_access", create=True
134-
) as use_jwt:
135-
creds = service_account.Credentials(None, None, None)
136-
client = client_class(credentials=creds)
137-
use_jwt.assert_not_called()
138-
139-
140128
@pytest.mark.parametrize(
141129
"transport_class,transport_name",
142130
[
143131
(transports.BudgetServiceGrpcTransport, "grpc"),
144132
(transports.BudgetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
145133
],
146134
)
147-
def test_budget_service_client_service_account_always_use_jwt_true(
135+
def test_budget_service_client_service_account_always_use_jwt(
148136
transport_class, transport_name
149137
):
150138
with mock.patch.object(
@@ -154,6 +142,13 @@ def test_budget_service_client_service_account_always_use_jwt_true(
154142
transport = transport_class(credentials=creds, always_use_jwt_access=True)
155143
use_jwt.assert_called_once_with(True)
156144

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+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
150+
use_jwt.assert_not_called()
151+
157152

158153
@pytest.mark.parametrize(
159154
"client_class", [BudgetServiceClient, BudgetServiceAsyncClient,]
@@ -234,6 +229,7 @@ def test_budget_service_client_client_options(
234229
client_cert_source_for_mtls=None,
235230
quota_project_id=None,
236231
client_info=transports.base.DEFAULT_CLIENT_INFO,
232+
always_use_jwt_access=True,
237233
)
238234

239235
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -250,6 +246,7 @@ def test_budget_service_client_client_options(
250246
client_cert_source_for_mtls=None,
251247
quota_project_id=None,
252248
client_info=transports.base.DEFAULT_CLIENT_INFO,
249+
always_use_jwt_access=True,
253250
)
254251

255252
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -266,6 +263,7 @@ def test_budget_service_client_client_options(
266263
client_cert_source_for_mtls=None,
267264
quota_project_id=None,
268265
client_info=transports.base.DEFAULT_CLIENT_INFO,
266+
always_use_jwt_access=True,
269267
)
270268

271269
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -294,6 +292,7 @@ def test_budget_service_client_client_options(
294292
client_cert_source_for_mtls=None,
295293
quota_project_id="octopus",
296294
client_info=transports.base.DEFAULT_CLIENT_INFO,
295+
always_use_jwt_access=True,
297296
)
298297

299298

@@ -360,6 +359,7 @@ def test_budget_service_client_mtls_env_auto(
360359
client_cert_source_for_mtls=expected_client_cert_source,
361360
quota_project_id=None,
362361
client_info=transports.base.DEFAULT_CLIENT_INFO,
362+
always_use_jwt_access=True,
363363
)
364364

365365
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -393,6 +393,7 @@ def test_budget_service_client_mtls_env_auto(
393393
client_cert_source_for_mtls=expected_client_cert_source,
394394
quota_project_id=None,
395395
client_info=transports.base.DEFAULT_CLIENT_INFO,
396+
always_use_jwt_access=True,
396397
)
397398

398399
# Check the case client_cert_source and ADC client cert are not provided.
@@ -414,6 +415,7 @@ def test_budget_service_client_mtls_env_auto(
414415
client_cert_source_for_mtls=None,
415416
quota_project_id=None,
416417
client_info=transports.base.DEFAULT_CLIENT_INFO,
418+
always_use_jwt_access=True,
417419
)
418420

419421

@@ -444,6 +446,7 @@ def test_budget_service_client_client_options_scopes(
444446
client_cert_source_for_mtls=None,
445447
quota_project_id=None,
446448
client_info=transports.base.DEFAULT_CLIENT_INFO,
449+
always_use_jwt_access=True,
447450
)
448451

449452

@@ -474,6 +477,7 @@ def test_budget_service_client_client_options_credentials_file(
474477
client_cert_source_for_mtls=None,
475478
quota_project_id=None,
476479
client_info=transports.base.DEFAULT_CLIENT_INFO,
480+
always_use_jwt_access=True,
477481
)
478482

479483

@@ -493,6 +497,7 @@ def test_budget_service_client_client_options_from_dict():
493497
client_cert_source_for_mtls=None,
494498
quota_project_id=None,
495499
client_info=transports.base.DEFAULT_CLIENT_INFO,
500+
always_use_jwt_access=True,
496501
)
497502

498503

tests/unit/gapic/budgets_v1beta1/test_budget_service.py

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

129129

130-
@pytest.mark.parametrize(
131-
"client_class", [BudgetServiceClient, BudgetServiceAsyncClient,]
132-
)
133-
def test_budget_service_client_service_account_always_use_jwt(client_class):
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-
client = client_class(credentials=creds)
139-
use_jwt.assert_not_called()
140-
141-
142130
@pytest.mark.parametrize(
143131
"transport_class,transport_name",
144132
[
145133
(transports.BudgetServiceGrpcTransport, "grpc"),
146134
(transports.BudgetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
147135
],
148136
)
149-
def test_budget_service_client_service_account_always_use_jwt_true(
137+
def test_budget_service_client_service_account_always_use_jwt(
150138
transport_class, transport_name
151139
):
152140
with mock.patch.object(
@@ -156,6 +144,13 @@ def test_budget_service_client_service_account_always_use_jwt_true(
156144
transport = transport_class(credentials=creds, always_use_jwt_access=True)
157145
use_jwt.assert_called_once_with(True)
158146

147+
with mock.patch.object(
148+
service_account.Credentials, "with_always_use_jwt_access", create=True
149+
) as use_jwt:
150+
creds = service_account.Credentials(None, None, None)
151+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
152+
use_jwt.assert_not_called()
153+
159154

160155
@pytest.mark.parametrize(
161156
"client_class", [BudgetServiceClient, BudgetServiceAsyncClient,]
@@ -236,6 +231,7 @@ def test_budget_service_client_client_options(
236231
client_cert_source_for_mtls=None,
237232
quota_project_id=None,
238233
client_info=transports.base.DEFAULT_CLIENT_INFO,
234+
always_use_jwt_access=True,
239235
)
240236

241237
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -252,6 +248,7 @@ def test_budget_service_client_client_options(
252248
client_cert_source_for_mtls=None,
253249
quota_project_id=None,
254250
client_info=transports.base.DEFAULT_CLIENT_INFO,
251+
always_use_jwt_access=True,
255252
)
256253

257254
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -268,6 +265,7 @@ def test_budget_service_client_client_options(
268265
client_cert_source_for_mtls=None,
269266
quota_project_id=None,
270267
client_info=transports.base.DEFAULT_CLIENT_INFO,
268+
always_use_jwt_access=True,
271269
)
272270

273271
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -296,6 +294,7 @@ def test_budget_service_client_client_options(
296294
client_cert_source_for_mtls=None,
297295
quota_project_id="octopus",
298296
client_info=transports.base.DEFAULT_CLIENT_INFO,
297+
always_use_jwt_access=True,
299298
)
300299

301300

@@ -362,6 +361,7 @@ def test_budget_service_client_mtls_env_auto(
362361
client_cert_source_for_mtls=expected_client_cert_source,
363362
quota_project_id=None,
364363
client_info=transports.base.DEFAULT_CLIENT_INFO,
364+
always_use_jwt_access=True,
365365
)
366366

367367
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -395,6 +395,7 @@ def test_budget_service_client_mtls_env_auto(
395395
client_cert_source_for_mtls=expected_client_cert_source,
396396
quota_project_id=None,
397397
client_info=transports.base.DEFAULT_CLIENT_INFO,
398+
always_use_jwt_access=True,
398399
)
399400

400401
# Check the case client_cert_source and ADC client cert are not provided.
@@ -416,6 +417,7 @@ def test_budget_service_client_mtls_env_auto(
416417
client_cert_source_for_mtls=None,
417418
quota_project_id=None,
418419
client_info=transports.base.DEFAULT_CLIENT_INFO,
420+
always_use_jwt_access=True,
419421
)
420422

421423

@@ -446,6 +448,7 @@ def test_budget_service_client_client_options_scopes(
446448
client_cert_source_for_mtls=None,
447449
quota_project_id=None,
448450
client_info=transports.base.DEFAULT_CLIENT_INFO,
451+
always_use_jwt_access=True,
449452
)
450453

451454

@@ -476,6 +479,7 @@ def test_budget_service_client_client_options_credentials_file(
476479
client_cert_source_for_mtls=None,
477480
quota_project_id=None,
478481
client_info=transports.base.DEFAULT_CLIENT_INFO,
482+
always_use_jwt_access=True,
479483
)
480484

481485

@@ -495,6 +499,7 @@ def test_budget_service_client_client_options_from_dict():
495499
client_cert_source_for_mtls=None,
496500
quota_project_id=None,
497501
client_info=transports.base.DEFAULT_CLIENT_INFO,
502+
always_use_jwt_access=True,
498503
)
499504

500505

0 commit comments

Comments
 (0)