Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit a15e185

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

File tree

10 files changed

+110
-55
lines changed

10 files changed

+110
-55
lines changed

google/cloud/asset_v1/services/asset_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 export_assets(

google/cloud/asset_v1p1beta1/services/asset_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ def __init__(
327327
client_cert_source_for_mtls=client_cert_source_func,
328328
quota_project_id=client_options.quota_project_id,
329329
client_info=client_info,
330+
always_use_jwt_access=(
331+
Transport == type(self).get_transport_class("grpc")
332+
or Transport == type(self).get_transport_class("grpc_asyncio")
333+
),
330334
)
331335

332336
def search_all_resources(

google/cloud/asset_v1p2beta1/services/asset_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ def __init__(
336336
client_cert_source_for_mtls=client_cert_source_func,
337337
quota_project_id=client_options.quota_project_id,
338338
client_info=client_info,
339+
always_use_jwt_access=(
340+
Transport == type(self).get_transport_class("grpc")
341+
or Transport == type(self).get_transport_class("grpc_asyncio")
342+
),
339343
)
340344

341345
def create_feed(

google/cloud/asset_v1p4beta1/services/asset_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def __init__(
328328
client_cert_source_for_mtls=client_cert_source_func,
329329
quota_project_id=client_options.quota_project_id,
330330
client_info=client_info,
331+
always_use_jwt_access=(
332+
Transport == type(self).get_transport_class("grpc")
333+
or Transport == type(self).get_transport_class("grpc_asyncio")
334+
),
331335
)
332336

333337
def analyze_iam_policy(

google/cloud/asset_v1p5beta1/services/asset_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ def __init__(
332332
client_cert_source_for_mtls=client_cert_source_func,
333333
quota_project_id=client_options.quota_project_id,
334334
client_info=client_info,
335+
always_use_jwt_access=(
336+
Transport == type(self).get_transport_class("grpc")
337+
or Transport == type(self).get_transport_class("grpc_asyncio")
338+
),
335339
)
336340

337341
def list_assets(

tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,14 @@ def test_asset_service_client_from_service_account_info(client_class):
122122
assert client.transport._host == "cloudasset.googleapis.com:443"
123123

124124

125-
@pytest.mark.parametrize("client_class", [AssetServiceClient, AssetServiceAsyncClient,])
126-
def test_asset_service_client_service_account_always_use_jwt(client_class):
127-
with mock.patch.object(
128-
service_account.Credentials, "with_always_use_jwt_access", create=True
129-
) as use_jwt:
130-
creds = service_account.Credentials(None, None, None)
131-
client = client_class(credentials=creds)
132-
use_jwt.assert_not_called()
133-
134-
135125
@pytest.mark.parametrize(
136126
"transport_class,transport_name",
137127
[
138128
(transports.AssetServiceGrpcTransport, "grpc"),
139129
(transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
140130
],
141131
)
142-
def test_asset_service_client_service_account_always_use_jwt_true(
132+
def test_asset_service_client_service_account_always_use_jwt(
143133
transport_class, transport_name
144134
):
145135
with mock.patch.object(
@@ -149,6 +139,13 @@ def test_asset_service_client_service_account_always_use_jwt_true(
149139
transport = transport_class(credentials=creds, always_use_jwt_access=True)
150140
use_jwt.assert_called_once_with(True)
151141

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+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
147+
use_jwt.assert_not_called()
148+
152149

153150
@pytest.mark.parametrize("client_class", [AssetServiceClient, AssetServiceAsyncClient,])
154151
def test_asset_service_client_from_service_account_file(client_class):
@@ -225,6 +222,7 @@ def test_asset_service_client_client_options(
225222
client_cert_source_for_mtls=None,
226223
quota_project_id=None,
227224
client_info=transports.base.DEFAULT_CLIENT_INFO,
225+
always_use_jwt_access=True,
228226
)
229227

230228
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -241,6 +239,7 @@ def test_asset_service_client_client_options(
241239
client_cert_source_for_mtls=None,
242240
quota_project_id=None,
243241
client_info=transports.base.DEFAULT_CLIENT_INFO,
242+
always_use_jwt_access=True,
244243
)
245244

246245
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -257,6 +256,7 @@ def test_asset_service_client_client_options(
257256
client_cert_source_for_mtls=None,
258257
quota_project_id=None,
259258
client_info=transports.base.DEFAULT_CLIENT_INFO,
259+
always_use_jwt_access=True,
260260
)
261261

262262
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -285,6 +285,7 @@ def test_asset_service_client_client_options(
285285
client_cert_source_for_mtls=None,
286286
quota_project_id="octopus",
287287
client_info=transports.base.DEFAULT_CLIENT_INFO,
288+
always_use_jwt_access=True,
288289
)
289290

290291

@@ -349,6 +350,7 @@ def test_asset_service_client_mtls_env_auto(
349350
client_cert_source_for_mtls=expected_client_cert_source,
350351
quota_project_id=None,
351352
client_info=transports.base.DEFAULT_CLIENT_INFO,
353+
always_use_jwt_access=True,
352354
)
353355

354356
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -382,6 +384,7 @@ def test_asset_service_client_mtls_env_auto(
382384
client_cert_source_for_mtls=expected_client_cert_source,
383385
quota_project_id=None,
384386
client_info=transports.base.DEFAULT_CLIENT_INFO,
387+
always_use_jwt_access=True,
385388
)
386389

387390
# Check the case client_cert_source and ADC client cert are not provided.
@@ -403,6 +406,7 @@ def test_asset_service_client_mtls_env_auto(
403406
client_cert_source_for_mtls=None,
404407
quota_project_id=None,
405408
client_info=transports.base.DEFAULT_CLIENT_INFO,
409+
always_use_jwt_access=True,
406410
)
407411

408412

@@ -433,6 +437,7 @@ def test_asset_service_client_client_options_scopes(
433437
client_cert_source_for_mtls=None,
434438
quota_project_id=None,
435439
client_info=transports.base.DEFAULT_CLIENT_INFO,
440+
always_use_jwt_access=True,
436441
)
437442

438443

@@ -463,6 +468,7 @@ def test_asset_service_client_client_options_credentials_file(
463468
client_cert_source_for_mtls=None,
464469
quota_project_id=None,
465470
client_info=transports.base.DEFAULT_CLIENT_INFO,
471+
always_use_jwt_access=True,
466472
)
467473

468474

@@ -480,6 +486,7 @@ def test_asset_service_client_client_options_from_dict():
480486
client_cert_source_for_mtls=None,
481487
quota_project_id=None,
482488
client_info=transports.base.DEFAULT_CLIENT_INFO,
489+
always_use_jwt_access=True,
483490
)
484491

485492

tests/unit/gapic/asset_v1p1beta1/test_asset_service.py

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

116116

117-
@pytest.mark.parametrize("client_class", [AssetServiceClient, AssetServiceAsyncClient,])
118-
def test_asset_service_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.AssetServiceGrpcTransport, "grpc"),
131121
(transports.AssetServiceGrpcAsyncIOTransport, "grpc_asyncio"),
132122
],
133123
)
134-
def test_asset_service_client_service_account_always_use_jwt_true(
124+
def test_asset_service_client_service_account_always_use_jwt(
135125
transport_class, transport_name
136126
):
137127
with mock.patch.object(
@@ -141,6 +131,13 @@ def test_asset_service_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", [AssetServiceClient, AssetServiceAsyncClient,])
146143
def test_asset_service_client_from_service_account_file(client_class):
@@ -217,6 +214,7 @@ def test_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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_asset_service_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)