feat(webui): connect to existing remote nanobot instances (NAN-157)
This commit is contained in:
1 parent
6ecb74aea9
commit
11889c018e
34 files changed
+4074
-14
No files matched your search
@@ -0,0 +1,286 @@
|
||||
"""Connection boundaries: no deployment, copied model keys or remote shutdown."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import socket
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
from websockets.datastructures import Headers
|
||||
from websockets.http11 import Request
|
||||
|
||||
from nanobot.channels.websocket.runtime import WebSocketConfig
|
||||
from nanobot.webui import remote_ssh
|
||||
from nanobot.webui.gateway_services import build_gateway_services
|
||||
from nanobot.webui.remote_instances import RemoteInstances
|
||||
from nanobot.webui.remote_ssh import RemoteError, RemoteProfile, ssh_arguments, ssh_error
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(tmp_path):
|
||||
return RemoteInstances(tmp_path)
|
||||
|
||||
|
||||
async def save(manager, **values):
|
||||
result = await manager.action("save", {"profile": {"name": "Team", "host": "ubuntu@example.test", **values}})
|
||||
return result["id"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ssh(monkeypatch):
|
||||
process = SimpleNamespace(returncode=None)
|
||||
tunnel = SimpleNamespace(process=process, port=23456, close=AsyncMock())
|
||||
probe = AsyncMock(return_value={"port": 8765, "secret": "private-webui-secret", "hostname": "team-host"})
|
||||
monkeypatch.setattr(remote_ssh, "probe", probe)
|
||||
monkeypatch.setattr(remote_ssh, "open_tunnel", AsyncMock(return_value=tunnel))
|
||||
state = {"status": 200, "identity": {"protocolVersion": 1, "gatewayId": "remote-one"}}
|
||||
|
||||
def respond(request):
|
||||
if request.url.path == "/webui/terminal":
|
||||
assert request.headers["X-Nanobot-Auth"] == "private-webui-secret"
|
||||
return httpx.Response(state["status"], json=state["identity"])
|
||||
return httpx.Response(200, text="<!doctype html><title>nanobot</title>", headers={"content-type": "text/html"})
|
||||
|
||||
real_client = httpx.AsyncClient
|
||||
monkeypatch.setattr(httpx, "AsyncClient", lambda **kwargs: real_client(transport=httpx.MockTransport(respond), **kwargs))
|
||||
return SimpleNamespace(tunnel=tunnel, probe=probe, state=state)
|
||||
|
||||
|
||||
async def test_save_survives_restart_without_credentials(manager):
|
||||
key = await save(manager)
|
||||
assert RemoteInstances(manager.path.parent).snapshot()["profiles"][0]["id"] == key
|
||||
assert "secret" not in manager.path.read_text()
|
||||
with pytest.raises(ValidationError):
|
||||
await save(manager, private_key="must-never-save")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("host", ["-oProxyCommand=evil", "user@host;whoami", "a\nb", "$(id)", "host name"])
|
||||
def test_hosts_cannot_be_shell_or_ssh_options(host):
|
||||
with pytest.raises(ValidationError):
|
||||
RemoteProfile(name="Team", host=host)
|
||||
|
||||
|
||||
def test_strict_ssh_without_agent_forwarding(monkeypatch):
|
||||
monkeypatch.setattr(remote_ssh.shutil, "which", lambda _: "/usr/bin/ssh")
|
||||
args = ssh_arguments(RemoteProfile(name="Team", host="my-alias"))
|
||||
for option in ["StrictHostKeyChecking=yes", "BatchMode=yes", "ForwardAgent=no", "ControlPath=none", "PermitLocalCommand=no", "ExitOnForwardFailure=yes"]:
|
||||
assert option in args
|
||||
assert "StrictHostKeyChecking=no" not in args
|
||||
|
||||
|
||||
async def test_connect_checks_protocol_and_only_disconnects_tunnel(manager, ssh):
|
||||
key = await save(manager)
|
||||
connection = await manager.action("connect", {"id": key})
|
||||
assert connection["hostname"] == "team-host"
|
||||
assert connection["gateway_id"] == "remote-one"
|
||||
assert connection["url"].startswith("http://127.0.0.1:23456/#/?bootstrapSecret=")
|
||||
assert "private-webui-secret" not in json.dumps(manager.snapshot())
|
||||
assert "private-webui-secret" not in manager.path.read_text()
|
||||
await manager.action("disconnect", {"id": key})
|
||||
ssh.tunnel.close.assert_awaited_once()
|
||||
assert not manager.snapshot()["profiles"][0]["connected"]
|
||||
assert len(manager.snapshot()["profiles"]) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("status", "identity", "error"), [
|
||||
(401, {}, "remote_auth_failed"), (404, {}, "incompatible_gateway"),
|
||||
(200, {"protocolVersion": 2, "gatewayId": "other"}, "incompatible_gateway"),
|
||||
(200, {"protocolVersion": 1}, "incompatible_gateway"),
|
||||
])
|
||||
async def test_bad_target_closes_tunnel_and_never_returns_launch_url(manager, ssh, status, identity, error):
|
||||
key = await save(manager)
|
||||
ssh.state.update(status=status, identity=identity)
|
||||
with pytest.raises(RemoteError, match=error):
|
||||
await manager.action("connect", {"id": key})
|
||||
ssh.tunnel.close.assert_awaited_once()
|
||||
assert not manager.connections
|
||||
|
||||
|
||||
async def test_server_restart_requires_reconnect_not_local_fallback(manager, ssh):
|
||||
key = await save(manager)
|
||||
await manager.action("connect", {"id": key})
|
||||
ssh.state["identity"]["gatewayId"] = "restarted"
|
||||
assert not (await manager.health())["profiles"][0]["connected"]
|
||||
assert key in manager.connections
|
||||
ssh.tunnel.close.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_connect_to_own_gateway_is_rejected_without_nesting(manager, ssh):
|
||||
manager.local_gateway_id = "remote-one"
|
||||
key = await save(manager)
|
||||
with pytest.raises(RemoteError, match="same_instance"):
|
||||
await manager.action("connect", {"id": key})
|
||||
ssh.tunnel.close.assert_awaited_once()
|
||||
assert not manager.connections
|
||||
|
||||
|
||||
async def test_forget_only_removes_saved_profile(manager, ssh):
|
||||
key = await save(manager)
|
||||
await manager.action("connect", {"id": key})
|
||||
await manager.action("remove", {"id": key})
|
||||
assert manager.snapshot()["profiles"] == []
|
||||
ssh.tunnel.close.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_closing_local_manager_reaps_ssh(manager, ssh):
|
||||
key = await save(manager)
|
||||
await manager.action("connect", {"id": key})
|
||||
await manager.close()
|
||||
ssh.tunnel.close.assert_awaited_once()
|
||||
assert len(manager.snapshot()["profiles"]) == 1
|
||||
|
||||
|
||||
async def test_refresh_reuses_a_healthy_tunnel(manager, ssh):
|
||||
key = await save(manager)
|
||||
first = await manager.action("connect", {"id": key})
|
||||
second = await manager.action("connect", {"id": key})
|
||||
assert first["url"] == second["url"]
|
||||
ssh.probe.assert_awaited_once()
|
||||
ssh.tunnel.close.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_reconnect_keeps_origin_and_changing_target_does_not(manager, ssh):
|
||||
key = await save(manager)
|
||||
await manager.action("connect", {"id": key})
|
||||
assert json.loads(manager.path.read_text())[key]["local_port"] == 23456
|
||||
await manager.action("disconnect", {"id": key})
|
||||
await manager.action("save", {"id": key, "profile": {"name": "Renamed", "host": "ubuntu@example.test"}})
|
||||
assert json.loads(manager.path.read_text())[key]["local_port"] == 23456
|
||||
await manager.action("save", {"id": key, "profile": {"name": "Other", "host": "other.example.test"}})
|
||||
assert json.loads(manager.path.read_text())[key]["local_port"] == 0
|
||||
|
||||
|
||||
async def test_corrupt_store_not_overwritten(manager):
|
||||
manager.path.write_text("corrupt")
|
||||
with pytest.raises(RemoteError, match="profile_store_invalid"):
|
||||
await save(manager)
|
||||
assert manager.path.read_text() == "corrupt"
|
||||
|
||||
|
||||
async def test_new_host_trust_is_explicit_scoped_and_one_use(manager, ssh, monkeypatch):
|
||||
key = await save(manager)
|
||||
ssh.probe.side_effect = RemoteError("host_key_unknown")
|
||||
with pytest.raises(RemoteError):
|
||||
await manager.action("connect", {"id": key})
|
||||
monkeypatch.setattr(remote_ssh, "scan_host_key", AsyncMock(return_value=("nanobot-remote ssh-ed25519 test-key\n", "SHA256:test")))
|
||||
result = await manager.action("fingerprint", {"id": key})
|
||||
assert result["fingerprint"] == "SHA256:test"
|
||||
assert not list(manager.path.parent.glob("remote-hosts/*"))
|
||||
await manager.action("trust", {"id": key, "challenge": result["challenge"]})
|
||||
assert (manager.path.parent / "remote-hosts" / key).read_text() == "nanobot-remote ssh-ed25519 test-key\n"
|
||||
with pytest.raises(RemoteError, match="host_key_changed"):
|
||||
await manager.action("trust", {"id": key, "challenge": result["challenge"]})
|
||||
with pytest.raises(RemoteError, match="host_key_changed"):
|
||||
await manager.action("fingerprint", {"id": key})
|
||||
|
||||
|
||||
async def test_changed_known_host_cannot_be_overridden_in_ui(manager, ssh):
|
||||
key = await save(manager)
|
||||
ssh.probe.side_effect = RemoteError("host_key_changed")
|
||||
with pytest.raises(RemoteError, match="host_key_changed"):
|
||||
await manager.action("connect", {"id": key})
|
||||
with pytest.raises(RemoteError, match="host_key_changed"):
|
||||
await manager.action("fingerprint", {"id": key})
|
||||
|
||||
|
||||
async def test_shutdown_prevents_new_tunnels(manager, ssh):
|
||||
key = await save(manager)
|
||||
await manager.close()
|
||||
with pytest.raises(RemoteError, match="local_io_error"):
|
||||
await manager.action("connect", {"id": key})
|
||||
ssh.probe.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("stderr", "code"), [
|
||||
(b"WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED", "host_key_changed"),
|
||||
(b"Host key verification failed", "host_key_unknown"),
|
||||
(b"Permission denied (publickey). secret-information", "ssh_auth_failed"),
|
||||
(b"timed out with private address", "ssh_unreachable"),
|
||||
])
|
||||
def test_errors_never_echo_raw_ssh_output(stderr, code):
|
||||
assert ssh_error(stderr) == code
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def gateway(tmp_path):
|
||||
return build_gateway_services(
|
||||
config=WebSocketConfig(host="127.0.0.1"), bus=MagicMock(), session_manager=None,
|
||||
static_dist_path=None, workspace_path=tmp_path, config_path=tmp_path / "config.json",
|
||||
default_restrict_to_workspace=False, runtime_model_name=None,
|
||||
runtime_surface="browser", runtime_capabilities_overrides=None,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["save", "discover", "connect", "remove", "trust"])
|
||||
async def test_http_mutations_are_not_gettable_even_with_token(gateway, action):
|
||||
request = Request(f"/api/remote-instances/{action}", Headers())
|
||||
connection = SimpleNamespace(remote_address=("127.0.0.1", 10000))
|
||||
result = await gateway.http.dispatch(connection, request)
|
||||
assert result.status_code == 405
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("peer", "host", "origin", "status"), [
|
||||
("127.0.0.1", "127.0.0.1:8765", "http://127.0.0.1:8765", 200),
|
||||
("203.0.113.1", "127.0.0.1:8765", "http://127.0.0.1:8765", 403),
|
||||
("127.0.0.1", "bot.example.com", "https://bot.example.com", 403),
|
||||
("127.0.0.1", "127.0.0.1:8765", "https://evil.example", 403),
|
||||
])
|
||||
async def test_ssh_access_is_local_and_authenticated(gateway, peer, host, origin, status):
|
||||
token = gateway.tokens.issue_api_token(60)
|
||||
connection = SimpleNamespace(remote_address=(peer, 10000))
|
||||
request = Request("/api/remote-instances", Headers({"Host": host, "Origin": origin, "Authorization": f"Bearer {token}"}))
|
||||
result = await gateway.http.dispatch(connection, request)
|
||||
assert result.status_code == status
|
||||
request = Request("/api/remote-instances", Headers({"Host": host}))
|
||||
assert (await gateway.http.dispatch(connection, request)).status_code == 401
|
||||
|
||||
|
||||
async def test_cancellation_reaps_inflight_probe(monkeypatch):
|
||||
process = MagicMock(returncode=None)
|
||||
process.communicate = AsyncMock(side_effect=asyncio.CancelledError)
|
||||
process.wait = AsyncMock(return_value=0)
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await remote_ssh.probe(RemoteProfile(name="Test", host="example.test"))
|
||||
process.terminate.assert_called_once()
|
||||
|
||||
|
||||
async def test_tunnel_refuses_an_existing_listener(monkeypatch):
|
||||
spawn = AsyncMock()
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)
|
||||
with socket.socket() as listener:
|
||||
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
listener.bind(("127.0.0.1", 0))
|
||||
listener.listen(1)
|
||||
with pytest.raises(RemoteError, match="local_port_in_use"):
|
||||
await remote_ssh.open_tunnel(
|
||||
RemoteProfile(name="Test", host="example.test"), 8765,
|
||||
local_port=listener.getsockname()[1],
|
||||
)
|
||||
spawn.assert_not_awaited()
|
||||
|
||||
|
||||
async def test_tunnel_reuses_closed_port_with_time_wait(monkeypatch):
|
||||
with socket.socket() as listener:
|
||||
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
listener.bind(("127.0.0.1", 0))
|
||||
listener.listen(1)
|
||||
port = listener.getsockname()[1]
|
||||
with socket.create_connection(("127.0.0.1", port), timeout=1) as peer:
|
||||
accepted, _ = listener.accept()
|
||||
accepted.close() # Server-side active close leaves TIME_WAIT after peer closes.
|
||||
assert peer.recv(1) == b""
|
||||
process = MagicMock(returncode=None)
|
||||
process.wait = AsyncMock(return_value=0)
|
||||
writer = MagicMock()
|
||||
writer.wait_closed = AsyncMock()
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))
|
||||
monkeypatch.setattr(asyncio, "open_connection", AsyncMock(return_value=(None, writer)))
|
||||
tunnel = await remote_ssh.open_tunnel(
|
||||
RemoteProfile(name="Test", host="example.test"), 8765, local_port=port,
|
||||
)
|
||||
assert tunnel.port == port
|
||||
await tunnel.close()
|
||||
@@ -0,0 +1,137 @@
|
||||
"""Name discovery is passive; OpenSSH, not our index, interprets connection options."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.webui.remote_instances import RemoteInstances
|
||||
from nanobot.webui.remote_ssh import RemoteError, RemoteProfile, ssh_arguments
|
||||
from nanobot.webui.ssh_config import discover_hosts
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ssh_home(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path)
|
||||
folder = tmp_path / ".ssh"
|
||||
folder.mkdir()
|
||||
return folder
|
||||
|
||||
|
||||
def test_discover_host_names_quotes_equals_multiple_aliases_and_patterns(ssh_home):
|
||||
config = ssh_home / "config"
|
||||
config.write_text('''# Host ignored
|
||||
Host=one two "three" # comment
|
||||
HostName not-returned.example
|
||||
IdentityFile private-key-not-returned
|
||||
Host * !excluded jump? [ab]bad -unsafe one
|
||||
Host TWO
|
||||
Host ipv6:alias
|
||||
''')
|
||||
result = discover_hosts(str(config))
|
||||
assert [host["host"] for host in result["hosts"]] == ["one", "two", "three", "ipv6:alias"]
|
||||
assert {host["ssh_config"] for host in result["hosts"]} == {str(config)}
|
||||
assert "private-key-not-returned" not in json.dumps(result)
|
||||
assert "not-returned.example" not in json.dumps(result)
|
||||
|
||||
|
||||
def test_recursive_includes_are_sorted_relative_to_ssh_root_and_deduplicated(ssh_home):
|
||||
(ssh_home / "hosts").mkdir()
|
||||
(ssh_home / "config").write_text('Include "hosts/*.conf"\nHost final\n')
|
||||
(ssh_home / "hosts" / "b.conf").write_text("Host second\nInclude config\n")
|
||||
(ssh_home / "hosts" / "a.conf").write_text("Host first\nInclude hosts/b.conf\n")
|
||||
result = discover_hosts(str(ssh_home / "config"))
|
||||
assert [host["host"] for host in result["hosts"]] == ["first", "second", "final"]
|
||||
assert len(result["files"]) == 3
|
||||
assert not result["incomplete"]
|
||||
|
||||
|
||||
def test_default_config_is_found_without_pinning_a_custom_file(ssh_home):
|
||||
(ssh_home / "config").write_text("Host default-test\n Port 2222\n")
|
||||
result = discover_hosts()
|
||||
host = next(host for host in result["hosts"] if host["host"] == "default-test")
|
||||
assert host["ssh_config"] == ""
|
||||
assert host["source"] == str(ssh_home / "config")
|
||||
|
||||
|
||||
def test_discovery_never_runs_ssh_match_exec_or_proxy_commands(ssh_home, monkeypatch):
|
||||
def forbidden(*args, **kwargs):
|
||||
pytest.fail("Discovery must not execute anything")
|
||||
|
||||
monkeypatch.setattr(subprocess, "run", forbidden)
|
||||
monkeypatch.setattr(subprocess, "Popen", forbidden)
|
||||
monkeypatch.setattr(asyncio, "create_subprocess_exec", forbidden)
|
||||
config = ssh_home / "config"
|
||||
config.write_text('Host safe\n Match exec "touch dangerous"\nProxyCommand command-not-returned\n')
|
||||
assert [host["host"] for host in discover_hosts(str(config))["hosts"]] == ["safe"]
|
||||
|
||||
|
||||
def test_unresolvable_includes_and_invalid_lines_do_not_hide_other_hosts(ssh_home):
|
||||
config = ssh_home / "config"
|
||||
config.write_text('Host ok\nInclude ${UNSET}/config %h.conf\nHost "unterminated\nHost good\n')
|
||||
result = discover_hosts(str(config))
|
||||
assert [host["host"] for host in result["hosts"]] == ["ok", "good"]
|
||||
assert result["incomplete"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("contents", [b"\xff", b"x" * 262145])
|
||||
def test_unreadable_or_oversized_custom_config_is_an_explicit_error(ssh_home, contents):
|
||||
path = ssh_home / "config"
|
||||
path.write_bytes(contents)
|
||||
with pytest.raises(RemoteError, match="ssh_config_unreadable"):
|
||||
discover_hosts(str(path))
|
||||
|
||||
|
||||
def test_missing_explicit_config_is_not_silently_treated_as_empty(ssh_home):
|
||||
with pytest.raises(RemoteError, match="local_file_not_found"):
|
||||
discover_hosts(str(ssh_home / "missing"))
|
||||
|
||||
|
||||
def test_host_limit_is_bounded_and_reported(ssh_home):
|
||||
config = ssh_home / "config"
|
||||
config.write_text("\n".join(f"Host host-{n}" for n in range(250)))
|
||||
result = discover_hosts(str(config))
|
||||
assert len(result["hosts"]) == 200
|
||||
assert result["incomplete"]
|
||||
|
||||
|
||||
def test_include_read_budget_is_bounded_and_reported(ssh_home):
|
||||
config = ssh_home / "config"
|
||||
config.write_text("Include large-*\nHost final\n")
|
||||
for index in range(6):
|
||||
(ssh_home / f"large-{index}").write_text(f"Host host-{index}\n#" + "x" * 250000)
|
||||
result = discover_hosts(str(config))
|
||||
assert result["incomplete"]
|
||||
assert len(result["files"]) == 5 # Root plus four complete included files.
|
||||
assert result["hosts"][-1]["host"] == "final"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path", ["\nsecret", "\x00bad", "x" * 2049])
|
||||
def test_invalid_paths_rejected_before_reading(path):
|
||||
with pytest.raises(RemoteError, match="invalid_profile"):
|
||||
discover_hosts(path)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("override", "expected"), [(None, "2222"), (2200, "2200")])
|
||||
def test_system_ssh_inherits_config_port_user_and_identity(ssh_home, override, expected):
|
||||
config = ssh_home / "config"
|
||||
config.write_text("Host example-test\n HostName 192.0.2.10\n User tester\n Port 2222\n IdentityFile /tmp/test-key\n")
|
||||
profile = RemoteProfile(name="Test", host="example-test", ssh_config=str(config), port=override)
|
||||
# This controlled fixture has no Match exec; -G makes no network connections.
|
||||
result = subprocess.run([*ssh_arguments(profile), "-G", profile.host],
|
||||
capture_output=True, text=True, check=True)
|
||||
settings = dict(line.split(" ", 1) for line in result.stdout.splitlines() if " " in line)
|
||||
assert settings["port"] == expected
|
||||
assert settings["user"] == "tester"
|
||||
assert settings["identityfile"] == "/tmp/test-key"
|
||||
|
||||
|
||||
async def test_manager_discovery_does_not_save_profiles_or_connect(ssh_home):
|
||||
config = ssh_home / "config"
|
||||
config.write_text("Host one\n")
|
||||
manager = RemoteInstances(ssh_home / "webui")
|
||||
result = await manager.action("discover", {"ssh_config": str(config)})
|
||||
assert result["hosts"][0]["host"] == "one"
|
||||
assert not manager.path.exists()
|
||||
assert not manager.connections
|
||||
Reference in new issue
Block a user