Skip to content

Commit fbf1452

Browse files
committed
fix: address PR feedback for agent identity utils
1 parent d43f6de commit fbf1452

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/google-auth/google/auth/_agent_identity_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,15 @@ def get_agent_identity_certificate_path():
9999
# For all other paths, we return early to avoid introducing unnecessary startup
100100
# delays.
101101
well_known_dir = os.path.dirname(_WELL_KNOWN_CERT_PATH)
102-
should_poll = cert_config_path.startswith(well_known_dir)
102+
try:
103+
abs_cert_path = os.path.abspath(cert_config_path)
104+
abs_well_known_dir = os.path.abspath(well_known_dir)
105+
should_poll = (
106+
os.path.commonpath([abs_well_known_dir, abs_cert_path])
107+
== abs_well_known_dir
108+
)
109+
except ValueError:
110+
should_poll = False
103111

104112
return _get_cert_path_with_optional_polling(cert_config_path, should_poll)
105113

@@ -197,7 +205,7 @@ def _parse_cert_path_from_config(cert_config_path):
197205
"""
198206
import json
199207

200-
with open(cert_config_path, "r") as f:
208+
with open(cert_config_path, "r", encoding="utf-8") as f:
201209
cert_config = json.load(f)
202210

203211
cert_configs = (

packages/google-auth/tests/test_agent_identity_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ def test_get_agent_identity_certificate_path_empty_env(self, monkeypatch):
7676
result = _agent_identity_utils.get_agent_identity_certificate_path()
7777
assert result is None
7878

79+
@mock.patch("google.auth._agent_identity_utils.os.path.commonpath")
80+
@mock.patch(
81+
"google.auth._agent_identity_utils._get_cert_path_with_optional_polling"
82+
)
83+
def test_get_agent_identity_certificate_path_value_error(
84+
self, mock_get_cert, mock_commonpath, monkeypatch
85+
):
86+
monkeypatch.setenv(
87+
environment_vars.GOOGLE_API_CERTIFICATE_CONFIG, "/path/to/config.json"
88+
)
89+
mock_commonpath.side_effect = ValueError("Different drives")
90+
mock_get_cert.return_value = "cert_path"
91+
92+
result = _agent_identity_utils.get_agent_identity_certificate_path()
93+
assert result == "cert_path"
94+
mock_get_cert.assert_called_once_with("/path/to/config.json", False)
95+
7996
@mock.patch("google.auth._agent_identity_utils.os.stat")
8097
def test_is_certificate_file_ready_permission_error(self, mock_stat):
8198
mock_stat.side_effect = PermissionError("Permission denied")

0 commit comments

Comments
 (0)