Skip to content

Commit 5bd6dae

Browse files
fix: expand home paths for mtls auth (#72)
Amp-Thread-ID: https://ampcode.com/threads/T-019e76ce-9495-77e0-9e92-c0b6365d47f7 Co-authored-by: Amp <amp@ampcode.com>
1 parent 1590de2 commit 5bd6dae

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/a2a_handler/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ def create_mtls_auth(
250250
ca_cert_path: str | None = None,
251251
) -> AuthCredentials:
252252
"""Create mTLS (mutual TLS) client certificate authentication."""
253+
cert_path = str(Path(cert_path).expanduser())
254+
key_path = str(Path(key_path).expanduser())
255+
ca_cert_path = str(Path(ca_cert_path).expanduser()) if ca_cert_path else None
256+
253257
if not Path(cert_path).is_file():
254258
raise FileNotFoundError(f"Client certificate not found: {cert_path}")
255259
if not Path(key_path).is_file():

tests/auth/test_auth.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Tests for authentication module."""
22

33
import tempfile
4+
from pathlib import Path
45
from unittest.mock import AsyncMock
56

67
import pytest
@@ -166,6 +167,29 @@ def test_create_mtls_auth_with_ca_cert(self) -> None:
166167
creds = create_mtls_auth(cert_file.name, key_file.name, ca_file.name)
167168
assert creds.ca_cert_path == ca_file.name
168169

170+
def test_create_mtls_auth_expands_home_directory_paths(
171+
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
172+
) -> None:
173+
home = tmp_path / "home"
174+
home.mkdir()
175+
cert_path = home / "client.pem"
176+
key_path = home / "client.key"
177+
ca_cert_path = home / "ca.pem"
178+
cert_path.write_text("cert")
179+
key_path.write_text("key")
180+
ca_cert_path.write_text("ca")
181+
182+
import os
183+
184+
os.chmod(key_path, 0o600)
185+
monkeypatch.setenv("HOME", str(home))
186+
187+
creds = create_mtls_auth("~/client.pem", "~/client.key", "~/ca.pem")
188+
189+
assert creds.cert_path == str(cert_path)
190+
assert creds.key_path == str(key_path)
191+
assert creds.ca_cert_path == str(ca_cert_path)
192+
169193
def test_create_mtls_auth_rejects_insecure_key_permissions(self) -> None:
170194
with tempfile.NamedTemporaryFile(suffix=".pem", delete=False) as cert_file:
171195
cert_path = cert_file.name

0 commit comments

Comments
 (0)