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

Commit dd2235c

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

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

google/analytics/admin_v1alpha/services/analytics_admin_service/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ def __init__(
570570
client_cert_source_for_mtls=client_cert_source_func,
571571
quota_project_id=client_options.quota_project_id,
572572
client_info=client_info,
573+
always_use_jwt_access=(
574+
Transport == type(self).get_transport_class("grpc")
575+
or Transport == type(self).get_transport_class("grpc_asyncio")
576+
),
573577
)
574578

575579
def get_account(

tests/unit/gapic/admin_v1alpha/test_analytics_admin_service.py

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

129129

130-
@pytest.mark.parametrize(
131-
"client_class", [AnalyticsAdminServiceClient, AnalyticsAdminServiceAsyncClient,]
132-
)
133-
def test_analytics_admin_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.AnalyticsAdminServiceGrpcTransport, "grpc"),
146134
(transports.AnalyticsAdminServiceGrpcAsyncIOTransport, "grpc_asyncio"),
147135
],
148136
)
149-
def test_analytics_admin_service_client_service_account_always_use_jwt_true(
137+
def test_analytics_admin_service_client_service_account_always_use_jwt(
150138
transport_class, transport_name
151139
):
152140
with mock.patch.object(
@@ -156,6 +144,13 @@ def test_analytics_admin_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", [AnalyticsAdminServiceClient, AnalyticsAdminServiceAsyncClient,]
@@ -240,6 +235,7 @@ def test_analytics_admin_service_client_client_options(
240235
client_cert_source_for_mtls=None,
241236
quota_project_id=None,
242237
client_info=transports.base.DEFAULT_CLIENT_INFO,
238+
always_use_jwt_access=True,
243239
)
244240

245241
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -256,6 +252,7 @@ def test_analytics_admin_service_client_client_options(
256252
client_cert_source_for_mtls=None,
257253
quota_project_id=None,
258254
client_info=transports.base.DEFAULT_CLIENT_INFO,
255+
always_use_jwt_access=True,
259256
)
260257

261258
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -272,6 +269,7 @@ def test_analytics_admin_service_client_client_options(
272269
client_cert_source_for_mtls=None,
273270
quota_project_id=None,
274271
client_info=transports.base.DEFAULT_CLIENT_INFO,
272+
always_use_jwt_access=True,
275273
)
276274

277275
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -300,6 +298,7 @@ def test_analytics_admin_service_client_client_options(
300298
client_cert_source_for_mtls=None,
301299
quota_project_id="octopus",
302300
client_info=transports.base.DEFAULT_CLIENT_INFO,
301+
always_use_jwt_access=True,
303302
)
304303

305304

@@ -376,6 +375,7 @@ def test_analytics_admin_service_client_mtls_env_auto(
376375
client_cert_source_for_mtls=expected_client_cert_source,
377376
quota_project_id=None,
378377
client_info=transports.base.DEFAULT_CLIENT_INFO,
378+
always_use_jwt_access=True,
379379
)
380380

381381
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -409,6 +409,7 @@ def test_analytics_admin_service_client_mtls_env_auto(
409409
client_cert_source_for_mtls=expected_client_cert_source,
410410
quota_project_id=None,
411411
client_info=transports.base.DEFAULT_CLIENT_INFO,
412+
always_use_jwt_access=True,
412413
)
413414

414415
# Check the case client_cert_source and ADC client cert are not provided.
@@ -430,6 +431,7 @@ def test_analytics_admin_service_client_mtls_env_auto(
430431
client_cert_source_for_mtls=None,
431432
quota_project_id=None,
432433
client_info=transports.base.DEFAULT_CLIENT_INFO,
434+
always_use_jwt_access=True,
433435
)
434436

435437

@@ -464,6 +466,7 @@ def test_analytics_admin_service_client_client_options_scopes(
464466
client_cert_source_for_mtls=None,
465467
quota_project_id=None,
466468
client_info=transports.base.DEFAULT_CLIENT_INFO,
469+
always_use_jwt_access=True,
467470
)
468471

469472

@@ -498,6 +501,7 @@ def test_analytics_admin_service_client_client_options_credentials_file(
498501
client_cert_source_for_mtls=None,
499502
quota_project_id=None,
500503
client_info=transports.base.DEFAULT_CLIENT_INFO,
504+
always_use_jwt_access=True,
501505
)
502506

503507

@@ -517,6 +521,7 @@ def test_analytics_admin_service_client_client_options_from_dict():
517521
client_cert_source_for_mtls=None,
518522
quota_project_id=None,
519523
client_info=transports.base.DEFAULT_CLIENT_INFO,
524+
always_use_jwt_access=True,
520525
)
521526

522527

0 commit comments

Comments
 (0)