Skip to content

Personal AI, On Personal Devices

Python

feat: get_tool_credential — scoped credential access without env pollution

Jon Saad-Falcon60317ffcba91
diff --git a/src/openjarvis/core/credentials.py b/src/openjarvis/core/credentials.py
index 04f95d70..6259de7f 100644
--- a/src/openjarvis/core/credentials.py
+++ b/src/openjarvis/core/credentials.py
@@ -108,3 +108,22 @@ def inject_credentials(path: Path | None = None) -> None:
for k, v in kvs.items():
if k not in os.environ:
os.environ[k] = v
+
+
+def get_tool_credential(
+ tool_name: str,
+ key: str,
+ *,
+ path: Path | None = None,
+) -> str | None:
+ """Read a single credential without polluting ``os.environ``.
+
+ Falls back to ``os.environ`` if the key is not in credentials.toml,
+ for backward compatibility with Docker env var workflows.
+ """
+ creds = load_credentials(path=path)
+ tool_creds = creds.get(tool_name, {})
+ value = tool_creds.get(key)
+ if value is not None:
+ return value
+ return os.environ.get(key) or None
diff --git a/tests/security/test_log_sanitization.py b/tests/security/test_log_sanitization.py
index 33acc8a8..46282a09 100644
--- a/tests/security/test_log_sanitization.py
+++ b/tests/security/test_log_sanitization.py
@@ -72,3 +72,45 @@ class TestSanitizingFormatter:
)
result = fmt.format(record)
assert "xoxb-" not in result
+
+
+import os
+import tempfile
+from pathlib import Path
+
+
+class TestScopedCredentialAccess:
+ """get_tool_credential should return values without polluting os.environ."""
+
+ def test_returns_credential_value(self) -> None:
+ from openjarvis.core.credentials import get_tool_credential
+
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
+ f.write('[slack]\nSLACK_BOT_TOKEN = "xoxb-test-token"\n')
+ f.flush()
+ result = get_tool_credential(
+ "slack", "SLACK_BOT_TOKEN", path=Path(f.name)
+ )
+ assert result == "xoxb-test-token"
+ assert os.environ.get("SLACK_BOT_TOKEN") != "xoxb-test-token"
+ os.unlink(f.name)
+
+ def test_returns_none_for_missing(self) -> None:
+ from openjarvis.core.credentials import get_tool_credential
+
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
+ f.write("[slack]\n")
+ f.flush()
+ result = get_tool_credential(
+ "slack", "SLACK_BOT_TOKEN", path=Path(f.name)
+ )
+ assert result is None
+ os.unlink(f.name)
+
+ def test_returns_none_for_missing_file(self) -> None:
+ from openjarvis.core.credentials import get_tool_credential
+
+ result = get_tool_credential(
+ "slack", "SLACK_BOT_TOKEN", path=Path("/nonexistent/file.toml")
+ )
+ assert result is None