Skip to content

Commit b26cea5

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 2f3748f commit b26cea5

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

packages/google-cloud-recaptcha-enterprise/google/cloud/recaptchaenterprise_v1/services/recaptcha_enterprise_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ def __init__(
359359
client_cert_source_for_mtls=client_cert_source_func,
360360
quota_project_id=client_options.quota_project_id,
361361
client_info=client_info,
362+
always_use_jwt_access=(
363+
Transport == type(self).get_transport_class("grpc")
364+
or Transport == type(self).get_transport_class("grpc_asyncio")
365+
),
362366
)
363367

364368
def create_assessment(

packages/google-cloud-recaptcha-enterprise/tests/unit/gapic/recaptchaenterprise_v1/test_recaptcha_enterprise_service.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,14 @@ def test_recaptcha_enterprise_service_client_from_service_account_info(client_cl
132132
assert client.transport._host == "recaptchaenterprise.googleapis.com:443"
133133

134134

135-
@pytest.mark.parametrize(
136-
"client_class",
137-
[RecaptchaEnterpriseServiceClient, RecaptchaEnterpriseServiceAsyncClient,],
138-
)
139-
def test_recaptcha_enterprise_service_client_service_account_always_use_jwt(
140-
client_class,
141-
):
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-
150135
@pytest.mark.parametrize(
151136
"transport_class,transport_name",
152137
[
153138
(transports.RecaptchaEnterpriseServiceGrpcTransport, "grpc"),
154139
(transports.RecaptchaEnterpriseServiceGrpcAsyncIOTransport, "grpc_asyncio"),
155140
],
156141
)
157-
def test_recaptcha_enterprise_service_client_service_account_always_use_jwt_true(
142+
def test_recaptcha_enterprise_service_client_service_account_always_use_jwt(
158143
transport_class, transport_name
159144
):
160145
with mock.patch.object(
@@ -164,6 +149,13 @@ def test_recaptcha_enterprise_service_client_service_account_always_use_jwt_true
164149
transport = transport_class(credentials=creds, always_use_jwt_access=True)
165150
use_jwt.assert_called_once_with(True)
166151

152+
with mock.patch.object(
153+
service_account.Credentials, "with_always_use_jwt_access", create=True
154+
) as use_jwt:
155+
creds = service_account.Credentials(None, None, None)
156+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
157+
use_jwt.assert_not_called()
158+
167159

168160
@pytest.mark.parametrize(
169161
"client_class",
@@ -253,6 +245,7 @@ def test_recaptcha_enterprise_service_client_client_options(
253245
client_cert_source_for_mtls=None,
254246
quota_project_id=None,
255247
client_info=transports.base.DEFAULT_CLIENT_INFO,
248+
always_use_jwt_access=True,
256249
)
257250

258251
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -269,6 +262,7 @@ def test_recaptcha_enterprise_service_client_client_options(
269262
client_cert_source_for_mtls=None,
270263
quota_project_id=None,
271264
client_info=transports.base.DEFAULT_CLIENT_INFO,
265+
always_use_jwt_access=True,
272266
)
273267

274268
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -285,6 +279,7 @@ def test_recaptcha_enterprise_service_client_client_options(
285279
client_cert_source_for_mtls=None,
286280
quota_project_id=None,
287281
client_info=transports.base.DEFAULT_CLIENT_INFO,
282+
always_use_jwt_access=True,
288283
)
289284

290285
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -313,6 +308,7 @@ def test_recaptcha_enterprise_service_client_client_options(
313308
client_cert_source_for_mtls=None,
314309
quota_project_id="octopus",
315310
client_info=transports.base.DEFAULT_CLIENT_INFO,
311+
always_use_jwt_access=True,
316312
)
317313

318314

@@ -389,6 +385,7 @@ def test_recaptcha_enterprise_service_client_mtls_env_auto(
389385
client_cert_source_for_mtls=expected_client_cert_source,
390386
quota_project_id=None,
391387
client_info=transports.base.DEFAULT_CLIENT_INFO,
388+
always_use_jwt_access=True,
392389
)
393390

394391
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -422,6 +419,7 @@ def test_recaptcha_enterprise_service_client_mtls_env_auto(
422419
client_cert_source_for_mtls=expected_client_cert_source,
423420
quota_project_id=None,
424421
client_info=transports.base.DEFAULT_CLIENT_INFO,
422+
always_use_jwt_access=True,
425423
)
426424

427425
# Check the case client_cert_source and ADC client cert are not provided.
@@ -443,6 +441,7 @@ def test_recaptcha_enterprise_service_client_mtls_env_auto(
443441
client_cert_source_for_mtls=None,
444442
quota_project_id=None,
445443
client_info=transports.base.DEFAULT_CLIENT_INFO,
444+
always_use_jwt_access=True,
446445
)
447446

448447

@@ -477,6 +476,7 @@ def test_recaptcha_enterprise_service_client_client_options_scopes(
477476
client_cert_source_for_mtls=None,
478477
quota_project_id=None,
479478
client_info=transports.base.DEFAULT_CLIENT_INFO,
479+
always_use_jwt_access=True,
480480
)
481481

482482

@@ -511,6 +511,7 @@ def test_recaptcha_enterprise_service_client_client_options_credentials_file(
511511
client_cert_source_for_mtls=None,
512512
quota_project_id=None,
513513
client_info=transports.base.DEFAULT_CLIENT_INFO,
514+
always_use_jwt_access=True,
514515
)
515516

516517

@@ -530,6 +531,7 @@ def test_recaptcha_enterprise_service_client_client_options_from_dict():
530531
client_cert_source_for_mtls=None,
531532
quota_project_id=None,
532533
client_info=transports.base.DEFAULT_CLIENT_INFO,
534+
always_use_jwt_access=True,
533535
)
534536

535537

0 commit comments

Comments
 (0)