mcp/server: pre-populated known_hosts for the restricted SSH

The systemd unit's ProtectHome=true blocks ~/.ssh access. SSH then had
no place to write known_hosts (StrictHostKeyChecking=accept-new) and
silently produced empty results. Use /etc/homelab-mcp/known_hosts (which
ProtectSystem=strict still allows reading) and StrictHostKeyChecking=yes.

Operator pre-populates the file via:
  ssh-keyscan -t ed25519 192.168.8.77 > /etc/homelab-mcp/known_hosts

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
root
2026-05-20 20:20:25 +02:00
parent b5dfbb68ae
commit 2d2446b36e

View File

@@ -35,6 +35,8 @@ HOSTS_DIR = CONTEXT_DIR / "hosts"
# hubris validates each command against a strict read-only allowlist. # hubris validates each command against a strict read-only allowlist.
SSH_IDENTITY = os.environ.get("HOMELAB_MCP_SSH_KEY", "/etc/homelab-mcp/mcp-reader.key") SSH_IDENTITY = os.environ.get("HOMELAB_MCP_SSH_KEY", "/etc/homelab-mcp/mcp-reader.key")
SSH_USER = os.environ.get("HOMELAB_MCP_SSH_USER", "root") SSH_USER = os.environ.get("HOMELAB_MCP_SSH_USER", "root")
SSH_KNOWN_HOSTS = os.environ.get("HOMELAB_MCP_SSH_KNOWN_HOSTS",
"/etc/homelab-mcp/known_hosts")
HUBRIS_HOST = os.environ.get("HOMELAB_MCP_HUBRIS_HOST", "192.168.8.77") HUBRIS_HOST = os.environ.get("HOMELAB_MCP_HUBRIS_HOST", "192.168.8.77")
SSH_TIMEOUT = int(os.environ.get("HOMELAB_MCP_SSH_TIMEOUT", "10")) SSH_TIMEOUT = int(os.environ.get("HOMELAB_MCP_SSH_TIMEOUT", "10"))
@@ -89,7 +91,10 @@ def _run_via_hubris(remote_host: str, cmd: list[str],
joined = " ".join(full) joined = " ".join(full)
ssh_args = [ ssh_args = [
"ssh", "-i", SSH_IDENTITY, "-o", "BatchMode=yes", "ssh", "-i", SSH_IDENTITY, "-o", "BatchMode=yes",
"-o", "StrictHostKeyChecking=accept-new", # The systemd unit runs with ProtectHome=true so ~/.ssh is unreachable.
# Use a pre-populated known_hosts in /etc/homelab-mcp/.
"-o", f"UserKnownHostsFile={SSH_KNOWN_HOSTS}",
"-o", "StrictHostKeyChecking=yes",
"-o", f"ConnectTimeout={SSH_TIMEOUT}", "-o", f"ConnectTimeout={SSH_TIMEOUT}",
f"{SSH_USER}@{HUBRIS_HOST}", f"{SSH_USER}@{HUBRIS_HOST}",
joined, joined,