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

Commit 0ddd8eb

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

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

google/cloud/memcache_v1/services/cloud_memcache/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ def __init__(
366366
client_cert_source_for_mtls=client_cert_source_func,
367367
quota_project_id=client_options.quota_project_id,
368368
client_info=client_info,
369+
always_use_jwt_access=(
370+
Transport == type(self).get_transport_class("grpc")
371+
or Transport == type(self).get_transport_class("grpc_asyncio")
372+
),
369373
)
370374

371375
def list_instances(

google/cloud/memcache_v1beta2/services/cloud_memcache/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ def __init__(
366366
client_cert_source_for_mtls=client_cert_source_func,
367367
quota_project_id=client_options.quota_project_id,
368368
client_info=client_info,
369+
always_use_jwt_access=(
370+
Transport == type(self).get_transport_class("grpc")
371+
or Transport == type(self).get_transport_class("grpc_asyncio")
372+
),
369373
)
370374

371375
def list_instances(

tests/unit/gapic/memcache_v1/test_cloud_memcache.py

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

126126

127-
@pytest.mark.parametrize(
128-
"client_class", [CloudMemcacheClient, CloudMemcacheAsyncClient,]
129-
)
130-
def test_cloud_memcache_client_service_account_always_use_jwt(client_class):
131-
with mock.patch.object(
132-
service_account.Credentials, "with_always_use_jwt_access", create=True
133-
) as use_jwt:
134-
creds = service_account.Credentials(None, None, None)
135-
client = client_class(credentials=creds)
136-
use_jwt.assert_not_called()
137-
138-
139127
@pytest.mark.parametrize(
140128
"transport_class,transport_name",
141129
[
142130
(transports.CloudMemcacheGrpcTransport, "grpc"),
143131
(transports.CloudMemcacheGrpcAsyncIOTransport, "grpc_asyncio"),
144132
],
145133
)
146-
def test_cloud_memcache_client_service_account_always_use_jwt_true(
134+
def test_cloud_memcache_client_service_account_always_use_jwt(
147135
transport_class, transport_name
148136
):
149137
with mock.patch.object(
@@ -153,6 +141,13 @@ def test_cloud_memcache_client_service_account_always_use_jwt_true(
153141
transport = transport_class(credentials=creds, always_use_jwt_access=True)
154142
use_jwt.assert_called_once_with(True)
155143

144+
with mock.patch.object(
145+
service_account.Credentials, "with_always_use_jwt_access", create=True
146+
) as use_jwt:
147+
creds = service_account.Credentials(None, None, None)
148+
transport = transport_class(credentials=creds, always_use_jwt_access=False)
149+
use_jwt.assert_not_called()
150+
156151

157152
@pytest.mark.parametrize(
158153
"client_class", [CloudMemcacheClient, CloudMemcacheAsyncClient,]
@@ -233,6 +228,7 @@ def test_cloud_memcache_client_client_options(
233228
client_cert_source_for_mtls=None,
234229
quota_project_id=None,
235230
client_info=transports.base.DEFAULT_CLIENT_INFO,
231+
always_use_jwt_access=True,
236232
)
237233

238234
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -249,6 +245,7 @@ def test_cloud_memcache_client_client_options(
249245
client_cert_source_for_mtls=None,
250246
quota_project_id=None,
251247
client_info=transports.base.DEFAULT_CLIENT_INFO,
248+
always_use_jwt_access=True,
252249
)
253250

254251
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -265,6 +262,7 @@ def test_cloud_memcache_client_client_options(
265262
client_cert_source_for_mtls=None,
266263
quota_project_id=None,
267264
client_info=transports.base.DEFAULT_CLIENT_INFO,
265+
always_use_jwt_access=True,
268266
)
269267

270268
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -293,6 +291,7 @@ def test_cloud_memcache_client_client_options(
293291
client_cert_source_for_mtls=None,
294292
quota_project_id="octopus",
295293
client_info=transports.base.DEFAULT_CLIENT_INFO,
294+
always_use_jwt_access=True,
296295
)
297296

298297

@@ -359,6 +358,7 @@ def test_cloud_memcache_client_mtls_env_auto(
359358
client_cert_source_for_mtls=expected_client_cert_source,
360359
quota_project_id=None,
361360
client_info=transports.base.DEFAULT_CLIENT_INFO,
361+
always_use_jwt_access=True,
362362
)
363363

364364
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -392,6 +392,7 @@ def test_cloud_memcache_client_mtls_env_auto(
392392
client_cert_source_for_mtls=expected_client_cert_source,
393393
quota_project_id=None,
394394
client_info=transports.base.DEFAULT_CLIENT_INFO,
395+
always_use_jwt_access=True,
395396
)
396397

397398
# Check the case client_cert_source and ADC client cert are not provided.
@@ -413,6 +414,7 @@ def test_cloud_memcache_client_mtls_env_auto(
413414
client_cert_source_for_mtls=None,
414415
quota_project_id=None,
415416
client_info=transports.base.DEFAULT_CLIENT_INFO,
417+
always_use_jwt_access=True,
416418
)
417419

418420

@@ -443,6 +445,7 @@ def test_cloud_memcache_client_client_options_scopes(
443445
client_cert_source_for_mtls=None,
444446
quota_project_id=None,
445447
client_info=transports.base.DEFAULT_CLIENT_INFO,
448+
always_use_jwt_access=True,
446449
)
447450

448451

@@ -473,6 +476,7 @@ def test_cloud_memcache_client_client_options_credentials_file(
473476
client_cert_source_for_mtls=None,
474477
quota_project_id=None,
475478
client_info=transports.base.DEFAULT_CLIENT_INFO,
479+
always_use_jwt_access=True,
476480
)
477481

478482

@@ -492,6 +496,7 @@ def test_cloud_memcache_client_client_options_from_dict():
492496
client_cert_source_for_mtls=None,
493497
quota_project_id=None,
494498
client_info=transports.base.DEFAULT_CLIENT_INFO,
499+
always_use_jwt_access=True,
495500
)
496501

497502

tests/unit/gapic/memcache_v1beta2/test_cloud_memcache.py

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

128128

129-
@pytest.mark.parametrize(
130-
"client_class", [CloudMemcacheClient, CloudMemcacheAsyncClient,]
131-
)
132-
def test_cloud_memcache_client_service_account_always_use_jwt(client_class):
133-
with mock.patch.object(
134-
service_account.Credentials, "with_always_use_jwt_access", create=True
135-
) as use_jwt:
136-
creds = service_account.Credentials(None, None, None)
137-
client = client_class(credentials=creds)
138-
use_jwt.assert_not_called()
139-
140-
141129
@pytest.mark.parametrize(
142130
"transport_class,transport_name",
143131
[
144132
(transports.CloudMemcacheGrpcTransport, "grpc"),
145133
(transports.CloudMemcacheGrpcAsyncIOTransport, "grpc_asyncio"),
146134
],
147135
)
148-
def test_cloud_memcache_client_service_account_always_use_jwt_true(
136+
def test_cloud_memcache_client_service_account_always_use_jwt(
149137
transport_class, transport_name
150138
):
151139
with mock.patch.object(
@@ -155,6 +143,13 @@ def test_cloud_memcache_client_service_account_always_use_jwt_true(
155143
transport = transport_class(credentials=creds, always_use_jwt_access=True)
156144
use_jwt.assert_called_once_with(True)
157145

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

159154
@pytest.mark.parametrize(
160155
"client_class", [CloudMemcacheClient, CloudMemcacheAsyncClient,]
@@ -235,6 +230,7 @@ def test_cloud_memcache_client_client_options(
235230
client_cert_source_for_mtls=None,
236231
quota_project_id=None,
237232
client_info=transports.base.DEFAULT_CLIENT_INFO,
233+
always_use_jwt_access=True,
238234
)
239235

240236
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -251,6 +247,7 @@ def test_cloud_memcache_client_client_options(
251247
client_cert_source_for_mtls=None,
252248
quota_project_id=None,
253249
client_info=transports.base.DEFAULT_CLIENT_INFO,
250+
always_use_jwt_access=True,
254251
)
255252

256253
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT is
@@ -267,6 +264,7 @@ def test_cloud_memcache_client_client_options(
267264
client_cert_source_for_mtls=None,
268265
quota_project_id=None,
269266
client_info=transports.base.DEFAULT_CLIENT_INFO,
267+
always_use_jwt_access=True,
270268
)
271269

272270
# Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS_ENDPOINT has
@@ -295,6 +293,7 @@ def test_cloud_memcache_client_client_options(
295293
client_cert_source_for_mtls=None,
296294
quota_project_id="octopus",
297295
client_info=transports.base.DEFAULT_CLIENT_INFO,
296+
always_use_jwt_access=True,
298297
)
299298

300299

@@ -361,6 +360,7 @@ def test_cloud_memcache_client_mtls_env_auto(
361360
client_cert_source_for_mtls=expected_client_cert_source,
362361
quota_project_id=None,
363362
client_info=transports.base.DEFAULT_CLIENT_INFO,
363+
always_use_jwt_access=True,
364364
)
365365

366366
# Check the case ADC client cert is provided. Whether client cert is used depends on
@@ -394,6 +394,7 @@ def test_cloud_memcache_client_mtls_env_auto(
394394
client_cert_source_for_mtls=expected_client_cert_source,
395395
quota_project_id=None,
396396
client_info=transports.base.DEFAULT_CLIENT_INFO,
397+
always_use_jwt_access=True,
397398
)
398399

399400
# Check the case client_cert_source and ADC client cert are not provided.
@@ -415,6 +416,7 @@ def test_cloud_memcache_client_mtls_env_auto(
415416
client_cert_source_for_mtls=None,
416417
quota_project_id=None,
417418
client_info=transports.base.DEFAULT_CLIENT_INFO,
419+
always_use_jwt_access=True,
418420
)
419421

420422

@@ -445,6 +447,7 @@ def test_cloud_memcache_client_client_options_scopes(
445447
client_cert_source_for_mtls=None,
446448
quota_project_id=None,
447449
client_info=transports.base.DEFAULT_CLIENT_INFO,
450+
always_use_jwt_access=True,
448451
)
449452

450453

@@ -475,6 +478,7 @@ def test_cloud_memcache_client_client_options_credentials_file(
475478
client_cert_source_for_mtls=None,
476479
quota_project_id=None,
477480
client_info=transports.base.DEFAULT_CLIENT_INFO,
481+
always_use_jwt_access=True,
478482
)
479483

480484

@@ -494,6 +498,7 @@ def test_cloud_memcache_client_client_options_from_dict():
494498
client_cert_source_for_mtls=None,
495499
quota_project_id=None,
496500
client_info=transports.base.DEFAULT_CLIENT_INFO,
501+
always_use_jwt_access=True,
497502
)
498503

499504

0 commit comments

Comments
 (0)