"""SSH host configuration is pinned, private, and switched only after test.""" from __future__ import annotations import base64 import hashlib import itertools import json import shutil import stat import struct import subprocess from dataclasses import replace from pathlib import Path from urllib.parse import parse_qs, urlsplit import pytest from virsh_panel.remote_host import ( HostProfile, RemoteHostError, RemoteHostManager, ) from virsh_panel.ssh_agent import SSHAgentError def host_key(seed: bytes) -> str: blob = ( struct.pack("!I", len(b"ssh-ed25519")) + b"ssh-ed25519" + struct.pack("!I", 32) + seed * 32 ) return "ssh-ed25519 " + base64.b64encode(blob).decode("ascii") PRIVATE_KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nZmFrZQ==\n-----END OPENSSH PRIVATE KEY-----\n" class Runner: def __init__(self) -> None: self.key = host_key(b"a") self.ssh_exit = 0 self.encrypted = False self.public_key_type = "ssh-ed25519" self.commands: list[list[str]] = [] def __call__(self, argv, **kwargs): self.commands.append(argv) assert kwargs["timeout"] <= 22 assert kwargs["capture_output"] is True assert kwargs["check"] is False if argv[0] == "ssh-keyscan": return subprocess.CompletedProcess(argv, 0, f"[host.example]:2222 {self.key}\n", "") if argv[0] == "ssh-keygen": if self.encrypted: return subprocess.CompletedProcess(argv, 255, "", "passphrase required") return subprocess.CompletedProcess(argv, 0, f"{self.public_key_type} fake-public-key\n", "") if argv[0] == "ssh": return subprocess.CompletedProcess(argv, self.ssh_exit, "", "permission denied") raise AssertionError(argv) def make_manager(tmp_path: Path, runner: Runner) -> RemoteHostManager: return RemoteHostManager( tmp_path / "service-home", ssh_user_config_path=tmp_path / "service-home" / ".ssh" / "config", runner=runner, ) def make_profile( scanned: dict, *, key: str = PRIVATE_KEY, host: str = "host.example", passphrase: str = "", storage_pool: str = "images", iso_pool: str = "iso", rescue_iso_pool: str = "", default_network: str = "default", region: str = "", ) -> HostProfile: return HostProfile( host=host, port=2222, private_key=key, host_key=scanned["known_hosts_line"], private_key_passphrase=passphrase, storage_pool=storage_pool, iso_pool=iso_pool, rescue_iso_pool=rescue_iso_pool, default_network=default_network, region=region, ) def test_region_survives_host_save_and_legacy_metadata_defaults_empty(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage(make_profile(scanned, region="中国香港"), scanned["fingerprint"]) manager.test(staged) assert manager.commit(staged)["region"] == "中国香港" profile_path = staged.path / "profile.json" legacy = json.loads(profile_path.read_text(encoding="utf-8")) legacy.pop("region") profile_path.write_text(json.dumps(legacy), encoding="utf-8") assert manager.current()["region"] == "" def test_public_pool_survives_host_save_and_legacy_metadata_defaults_empty(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) profile = replace( make_profile(scanned), public_pool_cidr="1.2.3.0/24", public_gateway="1.2.3.1", public_dns_servers=["1.1.1.1", "8.8.8.8"], public_reserved_ips=["1.2.3.10", "1.2.3.10"], ) staged = manager.stage(profile, scanned["fingerprint"]) assert staged.metadata["public_reserved_ips"] == ["1.2.3.10"] manager.test(staged) current = manager.commit(staged) assert current["public_pool_cidr"] == "1.2.3.0/24" assert current["public_gateway"] == "1.2.3.1" assert current["public_dns_servers"] == ["1.1.1.1", "8.8.8.8"] assert current["public_reserved_ips"] == ["1.2.3.10"] metadata_path = staged.path / "profile.json" legacy = json.loads(metadata_path.read_text(encoding="utf-8")) for key in ("public_pool_cidr", "public_gateway", "public_dns_servers", "public_reserved_ips"): legacy.pop(key) metadata_path.write_text(json.dumps(legacy), encoding="utf-8") assert manager.current()["public_pool_cidr"] == "" assert manager.current()["public_dns_servers"] == [] assert manager.current()["public_reserved_ips"] == [] def test_provider_28_gateway_cidr_is_saved_as_canonical_network(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) profile = replace( make_profile(scanned), public_pool_cidr="123.123.123.113/28", public_gateway="123.123.123.113", public_dns_servers=["1.1.1.1"], public_vm_prefix=24, ) staged = manager.stage(profile, scanned["fingerprint"]) assert staged.metadata["public_pool_cidr"] == "123.123.123.112/28" assert staged.metadata["public_vm_prefix"] == 24 manager.test(staged) assert manager.commit(staged)["public_pool_cidr"] == "123.123.123.112/28" assert manager.current()["public_vm_prefix"] == 24 class FakeAgent: sequence = itertools.count() def __init__(self, generation: Path, _key_path: Path, passphrase: str) -> None: if passphrase != "open-sesame": raise SSHAgentError("bad passphrase") self.socket_path = generation / f"agent-{next(self.sequence)}.sock" self.public_key = host_key(b"b") self.alive = True def close(self) -> None: self.alive = False def test_scan_returns_fingerprint_without_trusting_key(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) digest = hashlib.sha256(base64.b64decode(runner.key.split()[1])).digest() assert scanned == { "host": "host.example", "port": 2222, "key_type": "ssh-ed25519", "fingerprint": "SHA256:" + base64.b64encode(digest).decode("ascii").rstrip("="), "host_key": runner.key, "known_hosts_line": "vp-managed " + runner.key, } assert manager.current() is None assert not manager.ssh_user_config_path.exists() assert runner.commands[0] == [ "ssh-keyscan", "-T", "8", "-p", "2222", "-t", "ed25519", "host.example", ] def test_encrypted_key_is_memory_only_and_requires_unlock_after_restart(tmp_path): runner = Runner() runner.encrypted = True data_dir = tmp_path / "panel-data" manager = RemoteHostManager(data_dir, runner=runner, agent_factory=FakeAgent) scanned = manager.scan("host.example", 2222) staged = manager.stage( make_profile(scanned, passphrase="open-sesame"), scanned["fingerprint"], ) config = (staged.path / "config").read_text(encoding="ascii") assert f'IdentityFile "{staged.path / "identity.pub"}"' in config assert f'IdentityAgent "{manager._agents[staged.path].socket_path}"' in config assert all( b"open-sesame" not in path.read_bytes() for path in staged.path.iterdir() if path.is_file() ) manager.test(staged) manager.commit(staged) assert manager.current()["private_key_encrypted"] is True assert manager.current()["key_locked"] is False manager.close() assert manager.current()["key_locked"] is True restarted = RemoteHostManager(data_dir, runner=runner, agent_factory=FakeAgent) assert restarted.current()["key_locked"] is True original_config = (staged.path / "config").read_text(encoding="ascii") with pytest.raises(RemoteHostError, match="口令错误"): restarted.unlock_current("wrong") assert restarted.current()["key_locked"] is True assert (staged.path / "config").read_text(encoding="ascii") == original_config unlocked = restarted.unlock_current("open-sesame") assert unlocked["key_locked"] is False assert str(restarted._agents[staged.path].socket_path) in ( staged.path / "config" ).read_text(encoding="ascii") restarted.close() def test_encrypted_key_needs_passphrase_and_rejects_wrong_one(tmp_path): runner = Runner() runner.encrypted = True manager = RemoteHostManager(tmp_path / "panel-data", runner=runner, agent_factory=FakeAgent) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="填写私钥口令"): manager.stage(make_profile(scanned), scanned["fingerprint"]) with pytest.raises(RemoteHostError, match="口令错误"): manager.stage(make_profile(scanned, passphrase="wrong"), scanned["fingerprint"]) assert not list((manager.data_dir / "generations").iterdir()) def test_existing_host_directories_are_accepted_as_pool_selectors(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage( make_profile(scanned, storage_pool="/home/kvm", iso_pool="/home/iso"), scanned["fingerprint"], ) assert staged.metadata["storage_pool"] == "/home/kvm" assert staged.metadata["iso_pool"] == "/home/iso" manager.abort(staged) def test_rescue_iso_pool_survives_host_save_and_legacy_defaults_unconfigured(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage( make_profile(scanned, iso_pool="/home/iso", rescue_iso_pool="/home/rescue-iso"), scanned["fingerprint"], ) assert staged.metadata["rescue_iso_pool"] == "/home/rescue-iso" manager.test(staged) assert manager.commit(staged)["rescue_iso_pool"] == "/home/rescue-iso" profile_path = staged.path / "profile.json" legacy = json.loads(profile_path.read_text(encoding="utf-8")) legacy.pop("rescue_iso_pool") profile_path.write_text(json.dumps(legacy), encoding="utf-8") assert manager.current()["rescue_iso_pool"] == "" @pytest.mark.parametrize( "rescue_selector,iso_selector", [ ("iso", "iso"), ("/home/iso", "/home/iso"), ], ) def test_rescue_pool_cannot_equal_install_iso_pool( tmp_path, rescue_selector, iso_selector, ): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="不能与安装 ISO 池相同"): manager.stage( make_profile(scanned, iso_pool=iso_selector, rescue_iso_pool=rescue_selector), scanned["fingerprint"], ) @pytest.mark.parametrize("selector", ["/", "/home/../iso", "/home//iso", "/home/iso/", None]) def test_rejects_invalid_rescue_pool_selector(tmp_path, selector): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="救援 ISO 池"): manager.stage( make_profile(scanned, rescue_iso_pool=selector), scanned["fingerprint"], ) def test_explicit_host_bridge_selector_is_accepted(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage( make_profile(scanned, default_network="bridge:br0"), scanned["fingerprint"], ) assert staged.metadata["default_network"] == "bridge:br0" manager.abort(staged) @pytest.mark.parametrize( "selector", ["bridge:", "bridge:br/0", "bridge:br 0", "bridge:-br0", "bridge:" + "a" * 16], ) def test_invalid_host_bridge_selector_is_rejected(tmp_path, selector): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="网桥名称格式无效"): manager.stage(make_profile(scanned, default_network=selector), scanned["fingerprint"]) def test_unencrypted_ecdsa_private_key_does_not_require_passphrase(tmp_path): runner = Runner() runner.public_key_type = "ecdsa-sha2-nistp256" manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage(make_profile(scanned), scanned["fingerprint"]) assert staged.metadata["private_key_encrypted"] is False assert "IdentityAgent none" in (staged.path / "config").read_text() manager.abort(staged) @pytest.mark.parametrize("bad_path", ["/", "/home/../kvm", "/home//kvm", "/home/kvm/"]) def test_rejects_unsafe_pool_paths(tmp_path, bad_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="磁盘池"): manager.stage(make_profile(scanned, storage_pool=bad_path), scanned["fingerprint"]) def test_stage_test_commit_writes_private_pinned_ssh_config(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) stage = manager.stage(make_profile(scanned), scanned["fingerprint"]) assert stage.tested is False assert manager.current() is None for name in ("id_private", "known_hosts", "config", "profile.json"): path = stage.path / name assert stat.S_IMODE(path.stat().st_mode) == 0o600 assert stat.S_IMODE(stage.path.stat().st_mode) == 0o700 config = (stage.path / "config").read_text() assert "Host vp-managed\n" in config assert "HostName host.example\n" in config assert "Port 2222\n" in config assert "User root\n" in config assert "HostKeyAlias vp-managed\n" in config assert "StrictHostKeyChecking yes\n" in config assert "BatchMode yes\n" in config assert "IdentitiesOnly yes\n" in config assert "PasswordAuthentication no\n" in config assert "HostKeyAlgorithms ssh-ed25519\n" in config assert "ServerAliveInterval 15\n" in config assert "ServerAliveCountMax 2\n" in config assert (stage.path / "known_hosts").read_text() == scanned["known_hosts_line"] + "\n" assert (stage.path / "id_private").read_text() == PRIVATE_KEY wrapper = stage.path / "ssh-wrapper" assert stat.S_IMODE(wrapper.stat().st_mode) == 0o700 assert parse_qs(urlsplit(stage.libvirt_uri).query)["command"] == [str(wrapper)] assert manager.test(stage) == {"ok": True, "ssh": "connected", "libvirt": "available"} assert runner.commands[-1][0:4] == ["ssh", "-F", stage.ssh_config_file, "-T"] current = manager.commit(stage) assert current["host"] == "host.example" assert current["port"] == 2222 assert current["fingerprint"] == scanned["fingerprint"] assert current["private_key_configured"] is True assert current["libvirt_uri"].startswith( "qemu+ssh://root@vp-managed/system?no_tty=1&command=" ) assert parse_qs(urlsplit(current["libvirt_uri"]).query)["command"] == [str(wrapper)] assert manager.libvirt_uri == current["libvirt_uri"] assert current["ssh_target"] == "vp-managed" assert "private_key" not in current assert PRIVATE_KEY not in repr(current) assert manager.ssh_user_config_path.is_symlink() assert manager.ssh_user_config_path.resolve() == stage.path / "config" assert stat.S_IMODE(manager.ssh_user_config_path.parent.stat().st_mode) == 0o700 def test_rejects_unconfirmed_or_changed_host_key(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="确认"): manager.stage(make_profile(scanned), "") runner.key = host_key(b"b") with pytest.raises(RemoteHostError) as error: manager.stage(make_profile(scanned), scanned["fingerprint"]) assert error.value.status == 409 assert manager.current() is None def test_failed_test_preserves_old_connection_and_key_can_be_reused(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) first_scan = manager.scan("host.example", 2222) first = manager.stage(make_profile(first_scan), first_scan["fingerprint"]) manager.test(first) manager.commit(first) first_config = manager.ssh_user_config_path.resolve() next_scan = manager.scan("other.example", 2222) second = manager.stage( make_profile(next_scan, key="", host="other.example"), next_scan["fingerprint"], ) assert (second.path / "id_private").read_text() == PRIVATE_KEY runner.ssh_exit = 255 with pytest.raises(RemoteHostError) as error: manager.test(second) assert error.value.status == 502 with pytest.raises(RemoteHostError, match="测试"): manager.commit(second) assert manager.ssh_user_config_path.resolve() == first_config assert manager.current()["host"] == "host.example" manager.abort(second) assert not second.path.exists() def test_first_connection_needs_key_and_does_not_replace_existing_config(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) with pytest.raises(RemoteHostError, match="首次"): manager.stage(make_profile(scanned, key=""), scanned["fingerprint"]) manager.ssh_user_config_path.parent.mkdir(mode=0o700, parents=True) manager.ssh_user_config_path.write_text("Host unrelated\n") staged = manager.stage(make_profile(scanned), scanned["fingerprint"]) manager.test(staged) with pytest.raises(RemoteHostError, match="已有其他 SSH 配置"): manager.commit(staged) assert manager.ssh_user_config_path.read_text() == "Host unrelated\n" assert manager.current() is None @pytest.mark.skipif(shutil.which("ssh") is None, reason="OpenSSH client unavailable") def test_openssh_reads_staged_nondefault_port_and_root_identity(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) staged = manager.stage(make_profile(scanned), scanned["fingerprint"]) result = subprocess.run( ["ssh", "-G", "-F", staged.ssh_config_file, "vp-managed"], check=True, capture_output=True, text=True, timeout=8, ) options = dict(line.split(" ", 1) for line in result.stdout.splitlines() if " " in line) assert options["hostname"] == "host.example" assert options["port"] == "2222" assert options["user"] == "root" assert options["hostkeyalias"] == "vp-managed" assert options["stricthostkeychecking"] == "true" assert options["identityfile"] == str(staged.path / "id_private") assert options["userknownhostsfile"] == str(staged.path / "known_hosts") def test_staged_wrapper_passes_config_to_ssh_without_shell(tmp_path): runner = Runner() manager = RemoteHostManager( tmp_path / "service-home", ssh_binary="/bin/echo", runner=runner ) scanned = manager.scan("host.example", 2222) staged = manager.stage(make_profile(scanned), scanned["fingerprint"]) result = subprocess.run( [str(staged.path / "ssh-wrapper"), "-T", "root@vp-managed"], check=True, capture_output=True, text=True, timeout=8, ) assert result.stdout.strip() == f"-F {staged.ssh_config_file} -T root@vp-managed" def test_only_retired_generations_are_pruned(tmp_path): runner = Runner() manager = make_manager(tmp_path, runner) scanned = manager.scan("host.example", 2222) first = manager.stage(make_profile(scanned), scanned["fingerprint"]) manager.test(first) manager.commit(first) second = manager.stage(make_profile(scanned, key=""), scanned["fingerprint"]) # An uncommitted generation must not be deleted by background cleanup. assert manager.cleanup_inactive(grace_seconds=0) == 0 assert second.path.is_dir() manager.test(second) manager.commit(second) assert manager.cleanup_inactive(grace_seconds=60) == 0 assert first.path.is_dir() assert manager.cleanup_inactive(grace_seconds=0) == 1 assert not first.path.exists() assert second.path.is_dir() assert manager.current()["ssh_config_file"] == second.ssh_config_file def test_symlinked_parent_keeps_active_generation(tmp_path): # A lexical/canonical path mismatch must not classify the active # generation as retired. actual_parent = tmp_path / "private" / "virsh-panel" actual_parent.mkdir(parents=True) public_parent = tmp_path / "public-parent" public_parent.symlink_to(actual_parent, target_is_directory=True) runner = Runner() manager = RemoteHostManager(public_parent / "state", runner=runner) assert manager.data_dir == actual_parent / "state" scanned = manager.scan("host.example", 2222) first = manager.stage(make_profile(scanned), scanned["fingerprint"]) manager.test(first) manager.commit(first) assert manager.current()["ssh_config_file"] == first.ssh_config_file assert manager.cleanup_inactive(grace_seconds=0) == 0 assert first.path.is_dir() restarted = RemoteHostManager(public_parent / "state", runner=runner) assert restarted.current()["ssh_config_file"] == first.ssh_config_file assert restarted.cleanup_inactive(grace_seconds=0) == 0 assert first.path.is_dir() second = restarted.stage(make_profile(scanned, key=""), scanned["fingerprint"]) restarted.test(second) restarted.commit(second) assert restarted.cleanup_inactive(grace_seconds=0) == 1 assert not first.path.exists() assert second.path.is_dir() assert restarted.current()["ssh_config_file"] == second.ssh_config_file def test_rejects_symlink_as_final_data_directory(tmp_path): actual = tmp_path / "actual" actual.mkdir() alias = tmp_path / "alias" alias.symlink_to(actual, target_is_directory=True) with pytest.raises(RemoteHostError, match="符号链接"): RemoteHostManager(alias, runner=Runner()) @pytest.mark.parametrize("host,port", [ ("-oProxyCommand=bad", 22), ("host.example\nUser attacker", 22), ("example.com", 0), ("example.com", 65536), ("example.com", True), ]) def test_rejects_host_and_port_injection(tmp_path, host, port): runner = Runner() manager = make_manager(tmp_path, runner) with pytest.raises(RemoteHostError): manager.scan(host, port) assert runner.commands == []