Skip to content

Commit 8d40f20

Browse files
committed
chore: fix mtls explicit cert config path existence check
1 parent f95ebd0 commit 8d40f20

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ def _get_cert_config_path(certificate_config_path=None, include_context_aware=Tr
422422
The absolute path of the certificate config file, and None if the file does not exist.
423423
"""
424424

425+
is_explicit = True
425426
if certificate_config_path is None:
426427
env_path = environ.get(environment_vars.GOOGLE_API_CERTIFICATE_CONFIG, None)
427428
if env_path is not None and env_path != "":
@@ -435,14 +436,22 @@ def _get_cert_config_path(certificate_config_path=None, include_context_aware=Tr
435436
certificate_config_path = env_path
436437
else:
437438
certificate_config_path = CERTIFICATE_CONFIGURATION_DEFAULT_PATH
439+
is_explicit = False
438440

439441
certificate_config_path = path.expanduser(certificate_config_path)
442+
if not path.exists(certificate_config_path):
443+
if is_explicit:
444+
_LOGGER.warning(
445+
"Certificate configuration file at %s does not exist",
446+
certificate_config_path,
447+
)
448+
return None
440449
return certificate_config_path
441450

442451

443452
def _get_workload_cert_and_key_paths(config_path, include_context_aware=True):
444453
absolute_path = _get_cert_config_path(config_path, include_context_aware)
445-
if absolute_path is None or not os.path.exists(absolute_path):
454+
if absolute_path is None:
446455
return None, None
447456

448457
data = _load_json_file(absolute_path)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def has_default_client_cert_source(include_context_aware=True):
4343
Returns:
4444
bool: indicating if the default client cert source exists.
4545
"""
46-
if (
47-
_mtls_helper._get_cert_config_path(include_context_aware=include_context_aware)
48-
is not None
49-
):
46+
cert_path = _mtls_helper._get_cert_config_path(
47+
include_context_aware=include_context_aware
48+
)
49+
if cert_path is not None:
5050
return True
5151
if (
5252
include_context_aware

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def test_success_with_override(self):
591591
def test_override_does_not_exist(self):
592592
config_path = "fake/file/path"
593593
returned_path = _mtls_helper._get_cert_config_path(config_path)
594-
assert returned_path == config_path
594+
assert returned_path is None
595595

596596
@mock.patch.dict(
597597
os.environ,
@@ -630,10 +630,7 @@ def test_env_variable(self, mock_path_exists):
630630
def test_default_file_does_not_exist(self, mock_path_exists):
631631
mock_path_exists.return_value = False
632632
returned_path = _mtls_helper._get_cert_config_path()
633-
expected_path = os.path.expanduser(
634-
_mtls_helper.CERTIFICATE_CONFIGURATION_DEFAULT_PATH
635-
)
636-
assert returned_path == expected_path
633+
assert returned_path is None
637634

638635
def test_cert_config_path_precedence(self):
639636
# GOOGLE_API_CERTIFICATE_CONFIG takes precedence
@@ -673,7 +670,7 @@ def test_cert_config_path_fallback(self):
673670
def test_env_variable_file_does_not_exist(self, mock_path_exists):
674671
mock_path_exists.return_value = False
675672
returned_path = _mtls_helper._get_cert_config_path()
676-
assert returned_path == "path/to/config/file"
673+
assert returned_path is None
677674

678675
@mock.patch.dict(
679676
os.environ,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,16 @@ def test_secure_authorized_channel_cert_callback_without_client_cert_env(
404404
@mock.patch("google.auth.transport._mtls_helper._load_json_file", autospec=True)
405405
@mock.patch("google.auth.transport._mtls_helper._check_config_path", autospec=True)
406406
class TestSslCredentials(object):
407+
@mock.patch("os.path.exists", autospec=True)
407408
def test_no_context_aware_metadata(
408409
self,
410+
mock_path_exists,
409411
mock_check_config_path,
410412
mock_load_json_file,
411413
mock_get_client_ssl_credentials,
412414
mock_ssl_channel_credentials,
413415
):
416+
mock_path_exists.return_value = False
414417
# Mock that the metadata file doesn't exist.
415418
mock_check_config_path.return_value = None
416419

0 commit comments

Comments
 (0)