Repository navigation
feat: developer-entrypoint reads TRENTINA_MCP_TOKEN_FILE #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e844243
88fbe32
6665633
edc0638
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,9 @@ | |
| """ | ||
|
|
||
|
|
||
| class EntrypointTest(unittest.TestCase): | ||
| class EntrypointCase(unittest.TestCase): | ||
| """A temporary home, a stand-in claude, and the helpers every test here uses.""" | ||
|
|
||
| def setUp(self) -> None: | ||
| tmp = tempfile.TemporaryDirectory() | ||
| self.addCleanup(tmp.cleanup) | ||
|
|
@@ -121,6 +123,28 @@ def run_entrypoint( | |
| timeout=60, | ||
| ) | ||
|
|
||
| def git(self, *args: str) -> str: | ||
| env = {"HOME": str(self.home), "PATH": os.environ["PATH"]} | ||
| return subprocess.run( | ||
| ["git", "config", *args], env=env, capture_output=True, text=True, check=True | ||
| ).stdout.strip() | ||
|
|
||
| def token_file(self, body: str) -> dict[str, str]: | ||
| path = self.root / "trentina-token" | ||
| path.write_text(body) | ||
| return {**self.env, "TRENTINA_MCP_TOKEN_FILE": str(path)} | ||
|
|
||
| def assert_refused(self, env: dict[str, str]) -> None: | ||
| result = self.run_entrypoint(env=env) | ||
| self.assertEqual(result.returncode, 1, result.stderr) | ||
| self.assertIn("TRENTINA_MCP_TOKEN_FILE", result.stderr) | ||
| self.assertNotIn("s3cret-token", result.stderr) | ||
| self.assertNotIn("file-token", result.stderr) | ||
| self.assertFalse((self.home / "claude-args").exists()) | ||
| self.assertFalse((self.home / ".claude.json").exists()) | ||
|
|
||
|
|
||
| class EntrypointTest(EntrypointCase): | ||
| def test_assembles_the_home_and_serves_remote_control(self) -> None: | ||
| result = self.run_entrypoint() | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
|
|
@@ -158,12 +182,6 @@ def test_token_is_not_handed_to_the_agent_process(self) -> None: | |
| self.run_entrypoint() | ||
| self.assertNotIn("s3cret-token", (self.home / "claude-env").read_text()) | ||
|
|
||
| def git(self, *args: str) -> str: | ||
| env = {"HOME": str(self.home), "PATH": os.environ["PATH"]} | ||
| return subprocess.run( | ||
| ["git", "config", *args], env=env, capture_output=True, text=True, check=True | ||
| ).stdout.strip() | ||
|
|
||
| def test_git_goes_to_the_gateway_route_with_the_gateway_token(self) -> None: | ||
| self.run_entrypoint() | ||
| self.run_entrypoint() # a restart does not add a second include | ||
|
|
@@ -178,6 +196,58 @@ def test_git_goes_to_the_gateway_route_with_the_gateway_token(self) -> None: | |
| "Authorization: Bearer s3cret-token", | ||
| ) | ||
|
|
||
| def test_a_token_file_wins_over_the_variable(self) -> None: | ||
| result = self.run_entrypoint(env=self.token_file("file-token\n")) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| state = json.loads((self.home / ".claude.json").read_text()) | ||
| self.assertEqual(state["mcpServers"]["trentina"]["headers"]["Authorization"], "Bearer file-token") | ||
| self.assertEqual( | ||
| self.git("--get", "http.http://192.0.2.2:8019/git/.extraHeader"), | ||
| "Authorization: Bearer file-token", | ||
| ) | ||
| self.assertIn( | ||
| "OPENROUTER_API_KEY=file-token\n", (self.home / ".config/mcp-env/gatehouse.env").read_text() | ||
| ) | ||
| claude_env = (self.home / "claude-env").read_text() | ||
| self.assertNotIn("file-token", claude_env) | ||
| self.assertNotIn("TRENTINA_MCP_TOKEN", claude_env) | ||
|
|
||
| def test_a_token_file_works_without_the_variable(self) -> None: | ||
| env = {k: v for k, v in self.token_file("file-token").items() if k != "TRENTINA_MCP_TOKEN"} | ||
| result = self.run_entrypoint(env=env) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| state = json.loads((self.home / ".claude.json").read_text()) | ||
| self.assertEqual(state["mcpServers"]["trentina"]["headers"]["Authorization"], "Bearer file-token") | ||
|
|
||
| def test_a_missing_token_file_stops_the_start(self) -> None: | ||
| self.assert_refused({**self.env, "TRENTINA_MCP_TOKEN_FILE": str(self.root / "nope")}) | ||
|
|
||
| def test_a_token_file_that_is_a_directory_stops_the_start(self) -> None: | ||
| self.assert_refused({**self.env, "TRENTINA_MCP_TOKEN_FILE": str(self.root)}) | ||
|
|
||
| def test_a_token_file_with_a_nul_stops_the_start(self) -> None: | ||
| self.assert_refused(self.token_file("file-token\0extra\n")) | ||
|
|
||
| def test_an_oversized_token_file_stops_the_start(self) -> None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MEDIUM (Test Coverage): The oversized-file test uses only ASCII, so it does not check the documented 4096-byte limit when the shell runs in a multibyte locale. Bash Suggestion: Add a test using a multibyte value whose byte length exceeds 4096 while its character count is below 4097, and run it with a UTF-8 locale to verify the file is refused. Evidence:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6665633 Added |
||
| self.assert_refused(self.token_file("x" * 4097)) | ||
|
|
||
| def test_the_size_limit_is_in_bytes_in_a_multibyte_locale(self) -> None: | ||
| env = {**self.token_file("\u00e9" * 2049), "LC_ALL": "C.UTF-8"} | ||
| self.assert_refused(env) | ||
|
|
||
| def test_a_token_file_at_the_size_limit_is_accepted(self) -> None: | ||
| result = self.run_entrypoint(env=self.token_file("x" * 4095 + "\n")) | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
|
|
||
| def test_an_empty_token_file_stops_the_start(self) -> None: | ||
| self.assert_refused(self.token_file("")) | ||
| self.assert_refused(self.token_file("\n")) | ||
|
|
||
| def test_a_token_file_of_more_than_one_line_stops_the_start(self) -> None: | ||
| self.assert_refused(self.token_file("file-token\n\n")) | ||
| self.assert_refused(self.token_file("file-token\r\n")) | ||
| self.assert_refused(self.token_file("file-token\nsecond")) | ||
|
|
||
| def test_git_url_that_is_not_a_plain_url_is_ignored(self) -> None: | ||
| env = {**self.env, "TRENTINA_GIT_URL": 'http://x"]\n[credential]\n\thelper = !touch /tmp/pwned'} | ||
| result = self.run_entrypoint(env=env) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| """The entrypoint refusing a token file it cannot read. | ||
|
|
||
| Its own file because it is skipped as root, which reads a file whatever its | ||
| mode. Gourmand matches an exception by path alone, so the exception for this | ||
| skip names this file and covers nothing else. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| from test_entrypoint import EntrypointCase | ||
|
|
||
|
|
||
| class UnreadableTokenFileTest(EntrypointCase): | ||
| @unittest.skipIf(os.geteuid() == 0, "root reads a file whatever its mode") | ||
| def test_an_unreadable_token_file_stops_the_start(self) -> None: | ||
| env = self.token_file("file-token\n") | ||
| Path(env["TRENTINA_MCP_TOKEN_FILE"]).chmod(0) | ||
| self.assert_refused(env) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """developer-skills-sync on a checkout it cannot write (#42). | ||
|
|
||
| Its own file because it is skipped as root, which writes through a read-only | ||
| mode. Gourmand matches an exception by path alone, so the exception for this | ||
| skip names this file and covers nothing else. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import unittest | ||
|
|
||
| from test_skills_sync import SkillsSyncCase | ||
|
|
||
|
|
||
| class ReadOnlyCheckoutTest(SkillsSyncCase): | ||
| @unittest.skipIf(os.geteuid() == 0, "root writes through a read-only mode") | ||
| def test_a_checkout_it_cannot_write_is_only_read_and_relinked(self) -> None: | ||
| self.add_skill(self.upstream, "beta") | ||
| before = self.git(self.repo, "rev-parse", "HEAD") | ||
| self.add_skill(self.repo, "gamma") # as if the owner had already pulled it | ||
| gamma = self.git(self.repo, "rev-parse", "HEAD") | ||
| self.assertNotEqual(before, gamma) | ||
| (self.repo / ".git").chmod(0o555) | ||
| self.addCleanup((self.repo / ".git").chmod, 0o755) | ||
|
|
||
| result = self.sync() | ||
|
|
||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| self.assertEqual(self.git(self.repo, "rev-parse", "HEAD"), gamma) | ||
| self.assertEqual(self.linked(), ["alpha", "gamma"]) | ||
| self.assertFalse((self.repo / ".git/skills-sync.status").exists()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOW (Test Coverage): The new unreadable-file failure path is not covered. The tests exercise a missing path and a directory, but neither is a regular file that exists and cannot be read.
Suggestion: Add a test for an unreadable regular token file and verify startup is refused without exposing the token. Ensure the test runs in a context where the file is actually unreadable to the entrypoint.
Evidence:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 88fbe32
Added
test_an_unreadable_token_file_stops_the_start(a 0000 regular file). Root reads through file modes, so it is skipped for root under the same scoped gourmand exception the read-only-checkout test already has.