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

Commit 1cea5e5

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

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

‎google/cloud/network_security_v1beta1/services/network_security/client.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ def __init__(
402402
client_cert_source_for_mtls=client_cert_source_func,
403403
quota_project_id=client_options.quota_project_id,
404404
client_info=client_info,
405+
always_use_jwt_access=(
406+
Transport == type(self).get_transport_class("grpc")
407+
or Transport == type(self).get_transport_class("grpc_asyncio")
408+
),
405409
)
406410

407411
def list_authorization_policies(

‎tests/unit/gapic/network_security_v1beta1/test_network_security.py‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,26 +141,14 @@ def test_network_security_client_from_service_account_info(client_class):
141141
assert client.transport._host == "networksecurity.googleapis.com:443"
142142

143143

144-
@pytest.mark.parametrize(
145-
"client_class", [NetworkSecurityClient, NetworkSecurityAsyncClient,]
146-
)
147-
def test_network_security_client_service_account_always_use_jwt(client_class):
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-
client = client_class(credentials=creds)
153-
use_jwt.assert_not_called()
154-
155-
156144
@pytest.mark.parametrize(
157145
"transport_class,transport_name",
158146
[
159147
(transports.NetworkSecurityGrpcTransport, "grpc"),
160148
(transports.NetworkSecurityGrpcAsyncIOTransport, "grpc_asyncio"),
161149
],
162150
)
163-
def test_network_security_client_service_account_always_use_jwt_true(
151+
def test_network_security_client_service_account_always_use_jwt(
164152
transport_class, transport_name
165153
):
166154
with mock.patch.object(
@@ -170,6 +158,13 @@ def test_network_security_client_service_account_always_use_jwt_true(
170158
transport = transport_class(credentials=creds, always_use_jwt_access=True)
171159
use_jwt.assert_called_once_with(True)
172160

161+
with mock.patch.object(
162+
service_account.Credentials, "with_always_use_jwt_access", create=True
163+
) as use_jwt:
164+
creds = service_account.Credentials(None, None, None)
165+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
166+
use_jwt.assert_not_called()
167+
173168

174169
@pytest.mark.parametrize(
175170
"client_class", [NetworkSecurityClient, NetworkSecurityAsyncClient,]
@@ -250,6 +245,7 @@ def test_network_security_client_client_options(
250245
client_cert_source_for_mtls=None,
251246
quota_project_id=None,
252247
client_info=transports.base.DEFAULT_CLIENT_INFO,
248+
always_use_jwt_access=True,
253249
)
254250

255251
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -266,6 +262,7 @@ def test_network_security_client_client_options(
266262
client_cert_source_for_mtls=None,
267263
quota_project_id=None,
268264
client_info=transports.base.DEFAULT_CLIENT_INFO,
265+
always_use_jwt_access=True,
269266
)
270267

271268
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -282,6 +279,7 @@ def test_network_security_client_client_options(
282279
client_cert_source_for_mtls=None,
283280
quota_project_id=None,
284281
client_info=transports.base.DEFAULT_CLIENT_INFO,
282+
always_use_jwt_access=True,
285283
)
286284

287285
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -310,6 +308,7 @@ def test_network_security_client_client_options(
310308
client_cert_source_for_mtls=None,
311309
quota_project_id="octopus",
312310
client_info=transports.base.DEFAULT_CLIENT_INFO,
311+
always_use_jwt_access=True,
313312
)
314313

315314

@@ -386,6 +385,7 @@ def test_network_security_client_mtls_env_auto(
386385
client_cert_source_for_mtls=expected_client_cert_source,
387386
quota_project_id=None,
388387
client_info=transports.base.DEFAULT_CLIENT_INFO,
388+
always_use_jwt_access=True,
389389
)
390390

391391
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -419,6 +419,7 @@ def test_network_security_client_mtls_env_auto(
419419
client_cert_source_for_mtls=expected_client_cert_source,
420420
quota_project_id=None,
421421
client_info=transports.base.DEFAULT_CLIENT_INFO,
422+
always_use_jwt_access=True,
422423
)
423424

424425
# Check the case client_cert_source and ADC client cert are not provided.
@@ -440,6 +441,7 @@ def test_network_security_client_mtls_env_auto(
440441
client_cert_source_for_mtls=None,
441442
quota_project_id=None,
442443
client_info=transports.base.DEFAULT_CLIENT_INFO,
444+
always_use_jwt_access=True,
443445
)
444446

445447

@@ -470,6 +472,7 @@ def test_network_security_client_client_options_scopes(
470472
client_cert_source_for_mtls=None,
471473
quota_project_id=None,
472474
client_info=transports.base.DEFAULT_CLIENT_INFO,
475+
always_use_jwt_access=True,
473476
)
474477

475478

@@ -500,6 +503,7 @@ def test_network_security_client_client_options_credentials_file(
500503
client_cert_source_for_mtls=None,
501504
quota_project_id=None,
502505
client_info=transports.base.DEFAULT_CLIENT_INFO,
506+
always_use_jwt_access=True,
503507
)
504508

505509

@@ -519,6 +523,7 @@ def test_network_security_client_client_options_from_dict():
519523
client_cert_source_for_mtls=None,
520524
quota_project_id=None,
521525
client_info=transports.base.DEFAULT_CLIENT_INFO,
526+
always_use_jwt_access=True,
522527
)
523528

524529

0 commit comments

Comments
 (0)