Skip to content

Commit d91693a

Browse files
committed
minor fixes and clean up
1 parent 5533e0c commit d91693a

3 files changed

Lines changed: 55 additions & 15 deletions

File tree

packages/google-auth/google/auth/transport/_mtls_helper.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,12 @@ def _get_cert_config_path(certificate_config_path=None, include_context_aware=Tr
437437
certificate_config_path = CERTIFICATE_CONFIGURATION_DEFAULT_PATH
438438

439439
certificate_config_path = path.expanduser(certificate_config_path)
440-
if not path.exists(certificate_config_path):
441-
return None
442440
return certificate_config_path
443441

444442

445443
def _get_workload_cert_and_key_paths(config_path, include_context_aware=True):
446444
absolute_path = _get_cert_config_path(config_path, include_context_aware)
447-
if absolute_path is None:
445+
if absolute_path is None or not os.path.exists(absolute_path):
448446
return None, None
449447

450448
data = _load_json_file(absolute_path)
@@ -722,7 +720,8 @@ def check_use_client_cert():
722720
will default to False.
723721
If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred
724722
as True (auto-enabled) if a workload config file exists (pointed at by
725-
GOOGLE_API_CERTIFICATE_CONFIG) containing a "workload" section.
723+
GOOGLE_API_CERTIFICATE_CONFIG or CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH)
724+
containing a "workload" section.
726725
Otherwise, it returns False.
727726
728727
Returns:

packages/google-auth/google/auth/transport/mtls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ def should_use_client_cert():
138138
If GOOGLE_API_USE_CLIENT_CERTIFICATE is set to true or false, a corresponding
139139
bool value will be returned
140140
If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred by
141-
reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG, and verifying it
141+
reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG or
142+
CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH, and verifying it
142143
contains a "workload" section. If so, the function will return True,
143144
otherwise False.
144145

packages/google-auth/tests/transport/test__mtls_helper.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,16 @@ def test_success_with_context_aware_metadata(
315315
)
316316
@mock.patch("google.auth.transport._mtls_helper._load_json_file", autospec=True)
317317
@mock.patch("google.auth.transport._mtls_helper._check_config_path", autospec=True)
318+
@mock.patch("os.path.exists", autospec=True)
318319
def test_success_with_certificate_config(
319320
self,
321+
mock_path_exists,
320322
mock_check_config_path,
321323
mock_load_json_file,
322324
mock_get_cert_config_path,
323325
mock_read_cert_and_key_files,
324326
):
327+
mock_path_exists.return_value = True
325328
cert_config_path = "/path/to/config"
326329
mock_check_config_path.return_value = cert_config_path
327330
mock_load_json_file.return_value = {
@@ -444,12 +447,15 @@ class TestGetWorkloadCertAndKey(object):
444447
@mock.patch(
445448
"google.auth.transport._mtls_helper._read_cert_and_key_files", autospec=True
446449
)
450+
@mock.patch("os.path.exists", autospec=True)
447451
def test_success(
448452
self,
453+
mock_path_exists,
449454
mock_read_cert_and_key_files,
450455
mock_get_cert_config_path,
451456
mock_load_json_file,
452457
):
458+
mock_path_exists.return_value = True
453459
cert_config_path = "/path/to/cert"
454460
mock_get_cert_config_path.return_value = "/path/to/cert"
455461
mock_load_json_file.return_value = {
@@ -482,7 +488,9 @@ def test_file_not_found_returns_none(self, mock_get_cert_config_path):
482488
@mock.patch(
483489
"google.auth.transport._mtls_helper._get_cert_config_path", autospec=True
484490
)
485-
def test_no_cert_configs(self, mock_get_cert_config_path, mock_load_json_file):
491+
@mock.patch("os.path.exists", autospec=True)
492+
def test_no_cert_configs(self, mock_path_exists, mock_get_cert_config_path, mock_load_json_file):
493+
mock_path_exists.return_value = True
486494
mock_get_cert_config_path.return_value = "/path/to/cert"
487495
mock_load_json_file.return_value = {}
488496

@@ -505,7 +513,9 @@ def test_no_workload(self, mock_get_cert_config_path, mock_load_json_file):
505513
@mock.patch(
506514
"google.auth.transport._mtls_helper._get_cert_config_path", autospec=True
507515
)
508-
def test_no_cert_file(self, mock_get_cert_config_path, mock_load_json_file):
516+
@mock.patch("os.path.exists", autospec=True)
517+
def test_no_cert_file(self, mock_path_exists, mock_get_cert_config_path, mock_load_json_file):
518+
mock_path_exists.return_value = True
509519
mock_get_cert_config_path.return_value = "/path/to/cert"
510520
mock_load_json_file.return_value = {
511521
"cert_configs": {"workload": {"key_path": "path/to/key"}}
@@ -518,7 +528,9 @@ def test_no_cert_file(self, mock_get_cert_config_path, mock_load_json_file):
518528
@mock.patch(
519529
"google.auth.transport._mtls_helper._get_cert_config_path", autospec=True
520530
)
521-
def test_no_key_file(self, mock_get_cert_config_path, mock_load_json_file):
531+
@mock.patch("os.path.exists", autospec=True)
532+
def test_no_key_file(self, mock_path_exists, mock_get_cert_config_path, mock_load_json_file):
533+
mock_path_exists.return_value = True
522534
mock_get_cert_config_path.return_value = "/path/to/cert"
523535
mock_load_json_file.return_value = {
524536
"cert_configs": {"workload": {"cert_path": "path/to/key"}}
@@ -573,7 +585,7 @@ def test_success_with_override(self):
573585
def test_override_does_not_exist(self):
574586
config_path = "fake/file/path"
575587
returned_path = _mtls_helper._get_cert_config_path(config_path)
576-
assert returned_path is None
588+
assert returned_path == config_path
577589

578590
@mock.patch.dict(
579591
os.environ,
@@ -601,12 +613,21 @@ def test_env_variable(self, mock_path_exists):
601613
expected_path = "path/to/config/file"
602614
assert returned_path == expected_path
603615

604-
@mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""})
616+
@mock.patch.dict(
617+
os.environ,
618+
{
619+
"GOOGLE_API_CERTIFICATE_CONFIG": "",
620+
"CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH": "",
621+
},
622+
)
605623
@mock.patch("os.path.exists", autospec=True)
606-
def test_env_variable_file_does_not_exist(self, mock_path_exists):
624+
def test_default_file_does_not_exist(self, mock_path_exists):
607625
mock_path_exists.return_value = False
608626
returned_path = _mtls_helper._get_cert_config_path()
609-
assert returned_path is None
627+
expected_path = os.path.expanduser(
628+
_mtls_helper.CERTIFICATE_CONFIGURATION_DEFAULT_PATH
629+
)
630+
assert returned_path == expected_path
610631

611632
def test_cert_config_path_precedence(self):
612633
# GOOGLE_API_CERTIFICATE_CONFIG takes precedence
@@ -643,10 +664,26 @@ def test_cert_config_path_fallback(self):
643664
os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": "path/to/config/file"}
644665
)
645666
@mock.patch("os.path.exists", autospec=True)
646-
def test_default_file_does_not_exist(self, mock_path_exists):
667+
def test_env_variable_file_does_not_exist(self, mock_path_exists):
647668
mock_path_exists.return_value = False
648669
returned_path = _mtls_helper._get_cert_config_path()
649-
assert returned_path is None
670+
assert returned_path == "path/to/config/file"
671+
672+
@mock.patch.dict(
673+
os.environ,
674+
{
675+
"GOOGLE_API_CERTIFICATE_CONFIG": "",
676+
"CLOUDSDK_CONTEXT_AWARE_CERTIFICATE_CONFIG_FILE_PATH": "path/to/context/aware/config",
677+
},
678+
)
679+
@mock.patch("os.path.exists", autospec=True)
680+
def test_cert_config_path_ignore_context_aware(self, mock_path_exists):
681+
mock_path_exists.return_value = True
682+
returned_path = _mtls_helper._get_cert_config_path(include_context_aware=False)
683+
expected_path = os.path.expanduser(
684+
_mtls_helper.CERTIFICATE_CONFIGURATION_DEFAULT_PATH
685+
)
686+
assert returned_path == expected_path
650687

651688

652689
class TestGetClientCertAndKey(object):
@@ -818,10 +855,13 @@ def test_config_file_not_found(self, mock_exists, mock_file):
818855
mock_file.side_effect = FileNotFoundError
819856
assert _mtls_helper.check_use_client_cert() is False
820857

858+
@mock.patch("builtins.open", autospec=True)
821859
@mock.patch.dict(os.environ, {}, clear=True)
822860
@mock.patch("os.path.exists", autospec=True)
823-
def test_no_env_vars_set(self, mock_exists):
861+
def test_no_env_vars_set(self, mock_exists, mock_open):
862+
_mtls_helper._has_logged_mtls_warning = False
824863
mock_exists.return_value = False
864+
mock_open.side_effect = FileNotFoundError()
825865
assert _mtls_helper.check_use_client_cert() is False
826866

827867
def test_use_client_cert_precedence(self):

0 commit comments

Comments
 (0)