@@ -111,15 +111,15 @@ def test_key(self):
111
111
)
112
112
113
113
114
- class TestCheckaMetadataPath (object ):
114
+ class TestCheckConfigPath (object ):
115
115
def test_success (self ):
116
116
metadata_path = os .path .join (pytest .data_dir , "context_aware_metadata.json" )
117
- returned_path = _mtls_helper ._check_dca_metadata_path (metadata_path )
117
+ returned_path = _mtls_helper ._check_config_path (metadata_path )
118
118
assert returned_path is not None
119
119
120
120
def test_failure (self ):
121
121
metadata_path = os .path .join (pytest .data_dir , "not_exists.json" )
122
- returned_path = _mtls_helper ._check_dca_metadata_path (metadata_path )
122
+ returned_path = _mtls_helper ._check_config_path (metadata_path )
123
123
assert returned_path is None
124
124
125
125
@@ -275,54 +275,92 @@ def test_popen_raise_exception(self, mock_popen):
275
275
276
276
class TestGetClientSslCredentials (object ):
277
277
@mock .patch (
278
- "google.auth.transport._mtls_helper._run_cert_provider_command " , autospec = True
278
+ "google.auth.transport._mtls_helper._get_workload_cert_and_key " , autospec = True
279
279
)
280
- @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
281
280
@mock .patch (
282
- "google.auth.transport._mtls_helper._check_dca_metadata_path " , autospec = True
281
+ "google.auth.transport._mtls_helper._run_cert_provider_command " , autospec = True
283
282
)
284
- def test_success (
283
+ @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
284
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
285
+ def test_success_with_context_aware_metadata (
285
286
self ,
286
- mock_check_dca_metadata_path ,
287
+ mock_check_config_path ,
287
288
mock_load_json_file ,
288
289
mock_run_cert_provider_command ,
290
+ mock_get_workload_cert_and_key ,
289
291
):
290
- mock_check_dca_metadata_path .return_value = True
292
+ mock_check_config_path .return_value = "/path/to/config"
291
293
mock_load_json_file .return_value = {"cert_provider_command" : ["command" ]}
292
294
mock_run_cert_provider_command .return_value = (b"cert" , b"key" , None )
295
+ mock_get_workload_cert_and_key .return_value = (None , None )
293
296
has_cert , cert , key , passphrase = _mtls_helper .get_client_ssl_credentials ()
294
297
assert has_cert
295
298
assert cert == b"cert"
296
299
assert key == b"key"
297
300
assert passphrase is None
298
301
299
302
@mock .patch (
300
- "google.auth.transport._mtls_helper._check_dca_metadata_path " , autospec = True
303
+ "google.auth.transport._mtls_helper._read_cert_and_key_files " , autospec = True
301
304
)
302
- def test_success_without_metadata (self , mock_check_dca_metadata_path ):
303
- mock_check_dca_metadata_path .return_value = False
305
+ @mock .patch (
306
+ "google.auth.transport._mtls_helper._get_cert_config_path" , autospec = True
307
+ )
308
+ @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
309
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
310
+ def test_success_with_certificate_config (
311
+ self ,
312
+ mock_check_config_path ,
313
+ mock_load_json_file ,
314
+ mock_get_cert_config_path ,
315
+ mock_read_cert_and_key_files ,
316
+ ):
317
+ cert_config_path = "/path/to/config"
318
+ mock_check_config_path .return_value = cert_config_path
319
+ mock_load_json_file .return_value = {
320
+ "cert_configs" : {
321
+ "workload" : {"cert_path" : "cert/path" , "key_path" : "key/path" }
322
+ }
323
+ }
324
+ mock_get_cert_config_path .return_value = cert_config_path
325
+ mock_read_cert_and_key_files .return_value = (
326
+ pytest .public_cert_bytes ,
327
+ pytest .private_key_bytes ,
328
+ )
329
+
330
+ has_cert , cert , key , passphrase = _mtls_helper .get_client_ssl_credentials ()
331
+ assert has_cert
332
+ assert cert == pytest .public_cert_bytes
333
+ assert key == pytest .private_key_bytes
334
+ assert passphrase is None
335
+
336
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
337
+ def test_success_without_metadata (self , mock_check_config_path ):
338
+ mock_check_config_path .return_value = False
304
339
has_cert , cert , key , passphrase = _mtls_helper .get_client_ssl_credentials ()
305
340
assert not has_cert
306
341
assert cert is None
307
342
assert key is None
308
343
assert passphrase is None
309
344
310
345
@mock .patch (
311
- "google.auth.transport._mtls_helper._run_cert_provider_command " , autospec = True
346
+ "google.auth.transport._mtls_helper._get_workload_cert_and_key " , autospec = True
312
347
)
313
- @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
314
348
@mock .patch (
315
- "google.auth.transport._mtls_helper._check_dca_metadata_path " , autospec = True
349
+ "google.auth.transport._mtls_helper._run_cert_provider_command " , autospec = True
316
350
)
351
+ @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
352
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
317
353
def test_success_with_encrypted_key (
318
354
self ,
319
- mock_check_dca_metadata_path ,
355
+ mock_check_config_path ,
320
356
mock_load_json_file ,
321
357
mock_run_cert_provider_command ,
358
+ mock_get_workload_cert_and_key ,
322
359
):
323
- mock_check_dca_metadata_path .return_value = True
360
+ mock_check_config_path .return_value = "/path/to/config"
324
361
mock_load_json_file .return_value = {"cert_provider_command" : ["command" ]}
325
362
mock_run_cert_provider_command .return_value = (b"cert" , b"key" , b"passphrase" )
363
+ mock_get_workload_cert_and_key .return_value = (None , None )
326
364
has_cert , cert , key , passphrase = _mtls_helper .get_client_ssl_credentials (
327
365
generate_encrypted_key = True
328
366
)
@@ -334,33 +372,36 @@ def test_success_with_encrypted_key(
334
372
["command" , "--with_passphrase" ], expect_encrypted_key = True
335
373
)
336
374
337
- @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
338
375
@mock .patch (
339
- "google.auth.transport._mtls_helper._check_dca_metadata_path " , autospec = True
376
+ "google.auth.transport._mtls_helper._get_workload_cert_and_key " , autospec = True
340
377
)
378
+ @mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
379
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
341
380
def test_missing_cert_command (
342
- self , mock_check_dca_metadata_path , mock_load_json_file
381
+ self ,
382
+ mock_check_config_path ,
383
+ mock_load_json_file ,
384
+ mock_get_workload_cert_and_key ,
343
385
):
344
- mock_check_dca_metadata_path .return_value = True
386
+ mock_check_config_path .return_value = "/path/to/config"
345
387
mock_load_json_file .return_value = {}
388
+ mock_get_workload_cert_and_key .return_value = (None , None )
346
389
with pytest .raises (exceptions .ClientCertError ):
347
390
_mtls_helper .get_client_ssl_credentials ()
348
391
349
392
@mock .patch (
350
393
"google.auth.transport._mtls_helper._run_cert_provider_command" , autospec = True
351
394
)
352
395
@mock .patch ("google.auth.transport._mtls_helper._load_json_file" , autospec = True )
353
- @mock .patch (
354
- "google.auth.transport._mtls_helper._check_dca_metadata_path" , autospec = True
355
- )
396
+ @mock .patch ("google.auth.transport._mtls_helper._check_config_path" , autospec = True )
356
397
def test_customize_context_aware_metadata_path (
357
398
self ,
358
- mock_check_dca_metadata_path ,
399
+ mock_check_config_path ,
359
400
mock_load_json_file ,
360
401
mock_run_cert_provider_command ,
361
402
):
362
403
context_aware_metadata_path = "/path/to/metata/data"
363
- mock_check_dca_metadata_path .return_value = context_aware_metadata_path
404
+ mock_check_config_path .return_value = context_aware_metadata_path
364
405
mock_load_json_file .return_value = {"cert_provider_command" : ["command" ]}
365
406
mock_run_cert_provider_command .return_value = (b"cert" , b"key" , None )
366
407
@@ -372,7 +413,7 @@ def test_customize_context_aware_metadata_path(
372
413
assert cert == b"cert"
373
414
assert key == b"key"
374
415
assert passphrase is None
375
- mock_check_dca_metadata_path .assert_called_with (context_aware_metadata_path )
416
+ mock_check_config_path .assert_called_with (context_aware_metadata_path )
376
417
mock_load_json_file .assert_called_with (context_aware_metadata_path )
377
418
378
419
@@ -520,7 +561,7 @@ def test_default(self, mock_path_exists):
520
561
mock_path_exists .return_value = True
521
562
returned_path = _mtls_helper ._get_cert_config_path ()
522
563
expected_path = os .path .expanduser (
523
- _mtls_helper ._CERTIFICATE_CONFIGURATION_DEFAULT_PATH
564
+ _mtls_helper .CERTIFICATE_CONFIGURATION_DEFAULT_PATH
524
565
)
525
566
assert returned_path == expected_path
526
567
0 commit comments