"""Database-backed state behavior without requiring a live MariaDB server.""" from __future__ import annotations import base64 import json import os import stat import struct import subprocess from pathlib import Path from types import SimpleNamespace import pytest from cryptography.hazmat.primitives.ciphers.aead import AESGCM from virsh_panel.auth import hash_password, verify_password from virsh_panel.cloud_templates import TemplateError from virsh_panel.database import ( DatabasePanelState, DatabaseRemoteHostManager, DatabaseStore, DatabaseTemplateStore, ) from virsh_panel.remote_host import HostProfile, RemoteHostError from virsh_panel.ssh_agent import SSHAgentError PRIVATE_KEY = "-----BEGIN OPENSSH PRIVATE KEY-----\nZmFrZQ==\n-----END OPENSSH PRIVATE KEY-----\n" class MemoryStore: def __init__(self) -> None: self.records: dict[tuple[str, str], str] = {} def get(self, kind: str, key: str) -> str | None: return self.records.get((kind, key)) def set(self, kind: str, key: str, value: str) -> None: self.records[(kind, key)] = value def insert(self, kind: str, key: str, value: str) -> bool: if (kind, key) in self.records: return False self.records[(kind, key)] = value return True def delete(self, kind: str, key: str) -> None: self.records.pop((kind, key), None) def list(self, kind: str) -> dict[str, str]: return {key: value for (record_type, key), value in self.records.items() if record_type == kind} class MemoryUsers: def __init__(self, store: MemoryStore) -> None: self.store = store def has_users(self) -> bool: return self.store.get("test_user", "initial") is not None def create_initial_user(self, username: str, password: str, email=None): self.store.set("test_user", "initial", json.dumps({ "id": 1, "username": username, "email": email, "password_hash": hash_password(password), "admin": False, "approved": True, "email_verified": True, "created_at": 100, })) def get_by_username(self, username: str): record = self.store.get("test_user", "initial") user = json.loads(record) if record else None return user if user and user["username"] == username else None class EncryptedMemoryStore(MemoryStore): """Exercise the production record envelope without a MariaDB server.""" def __init__(self) -> None: super().__init__() self.records: dict[tuple[str, str], bytes] = {} self._cipher_store = DatabaseStore.__new__(DatabaseStore) self._cipher_store._cipher = AESGCM(b"a" * 32) def get(self, kind: str, key: str) -> str | None: payload = self.records.get((kind, key)) return (None if payload is None else self._cipher_store._decrypt(kind, key, payload)) def set(self, kind: str, key: str, value: str) -> None: self.records[(kind, key)] = self._cipher_store._encrypt(kind, key, value) def test_database_ciphertext_is_randomized_and_bound_to_record_key(): store = DatabaseStore.__new__(DatabaseStore) store._cipher = AESGCM(b"a" * 32) first = store._encrypt("secret", "gitea_token", "private-token") second = store._encrypt("secret", "gitea_token", "private-token") assert first != second assert b"private-token" not in first assert store._decrypt("secret", "gitea_token", first) == "private-token" with pytest.raises(Exception, match="密文"): store._decrypt("secret", "different_name", first) with pytest.raises(Exception, match="密文"): store._decrypt("secret", "gitea_token", first[:-1] + b"0") def test_linux_tmpfs_agent_socket_path_fits_openssh_limit(): # Linux pid_max cannot exceed 4,194,304; the runtime name is deliberately # short so SSHAgentSession's <100-byte socket guard accepts it. longest = ( "/dev/shm/v4194304-abcd/generations/gen-" + "f" * 32 + "/agent-" + "f" * 16 + ".sock" ) assert len(longest.encode()) < 100 def test_non_linux_rejects_disk_backed_private_key_fallback(tmp_path, monkeypatch): monkeypatch.setattr("virsh_panel.database.sys", SimpleNamespace(platform="darwin")) data_dir = tmp_path / "panel" with pytest.raises(RemoteHostError, match="仅支持 Linux"): DatabaseRemoteHostManager(MemoryStore(), data_dir) assert not list(data_dir.rglob("id_private")) def test_initial_user_hash_is_database_only_and_bootstrap_token_is_consumed(tmp_path: Path): store = MemoryStore() data_dir = tmp_path / "panel" state = DatabasePanelState(data_dir, store, MemoryUsers(store)) token = state.initialize_setup_token() assert token state.create_admin(token, "admin", "a-valid-very-long-password") assert not (data_dir / "state.json").exists() assert not (data_dir / "setup-token").exists() assert stat.S_IMODE(data_dir.stat().st_mode) == 0o700 reopened = DatabasePanelState(data_dir, store, MemoryUsers(store)) user = reopened.users.get_by_username("admin") assert user["admin"] is False and user["approved"] is True assert user["email"] is None assert verify_password("a-valid-very-long-password", user["password_hash"]) assert reopened.initialize_setup_token() is None def test_templates_are_isolated_by_host_scope_and_atomic_insert(): store = MemoryStore() templates = DatabaseTemplateStore(store) record = {"id": "alma10-12345678-abcdef0123456789", "name": "AlmaLinux"} assert templates.add("a" * 64, record) == record assert templates.list("a" * 64) == [record] assert templates.list("b" * 64) == [] with pytest.raises(TemplateError) as duplicate: templates.add("a" * 64, record) assert duplicate.value.status == 409 def test_database_template_rename_persists_only_name_and_rejects_duplicate(): store = MemoryStore() templates = DatabaseTemplateStore(store) scope = "a" * 64 other_scope = "b" * 64 record = { "id": "alma9-12345678-abcdef0123456789", "name": "AlmaLinux 9", "os_family": "AlmaLinux", "os_version": "9", "sha256": "f" * 64, "volume_name": "panel-template-alma9-12345678-abcdef0123456789.qcow2", } templates.add(scope, record) templates.add(scope, {**record, "id": "second", "name": "Second"}) templates.add(other_scope, {**record, "name": "Other host"}) with pytest.raises(TemplateError) as duplicate_import: templates.add(scope, {**record, "id": "third", "name": "ALMALINUX 9"}) assert duplicate_import.value.status == 409 with pytest.raises(TemplateError) as wrong_host: templates.rename(other_scope, "second", "Missing") assert wrong_host.value.status == 404 with pytest.raises(TemplateError) as duplicate_rename: templates.rename(scope, record["id"], "second") assert duplicate_rename.value.status == 409 updated = templates.rename(scope, record["id"], " 自定义名称 ") assert updated == {**record, "name": "自定义名称"} assert DatabaseTemplateStore(store).get(scope, record["id"]) == updated assert templates.get(other_scope, record["id"])["name"] == "Other host" def _host_key() -> str: blob = struct.pack("!I", 11) + b"ssh-ed25519" + struct.pack("!I", 32) + b"a" * 32 return "ssh-ed25519 " + base64.b64encode(blob).decode("ascii") def _fake_ssh(argv, **_kwargs): if argv[0] == "ssh-keyscan": return subprocess.CompletedProcess(argv, 0, "host.example " + _host_key() + "\n", "") if argv[0] == "ssh-keygen": return subprocess.CompletedProcess(argv, 0, "ssh-ed25519 fake-public-key\n", "") if argv[0] == "ssh": return subprocess.CompletedProcess(argv, 0, "ok", "") raise AssertionError(argv) def _encrypted_ssh(argv, **kwargs): if argv[0] == "ssh-keygen": return subprocess.CompletedProcess(argv, 255, "", "passphrase required") return _fake_ssh(argv, **kwargs) class FakeAgent: def __init__(self, generation: Path, _key_path: Path, passphrase: str) -> None: if passphrase != "open-sesame": raise SSHAgentError("bad passphrase") self.socket_path = generation / "fake-agent.sock" self.public_key = _host_key() self.alive = True def close(self) -> None: self.alive = False def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() data_dir = tmp_path / "panel" first = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_fake_ssh, ) scan = first.scan("host.example", 22) profile = HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], storage_pool="/home/kvm", iso_pool="/home/iso", default_network="bridge:br0", region="东京", public_pool_cidr="1.2.3.1/28", public_gateway="1.2.3.1", public_dns_servers=["1.1.1.1", "8.8.8.8"], public_reserved_ips=["1.2.3.10"], public_vm_prefix=24, ) staged = first.stage(profile, scan["fingerprint"]) assert first.test(staged)["libvirt"] == "available" current = first.commit(staged) assert current["host"] == "host.example" assert current["libvirt_uri"].find("runtime-wrappers") >= 0 assert ("host", "current") in store.records assert not list(data_dir.rglob("id_private")) assert not list(data_dir.rglob("profile.json")) first.close() second = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_fake_ssh, ) assert second.current()["storage_pool"] == "/home/kvm" assert second.current()["default_network"] == "bridge:br0" assert second.current()["region"] == "东京" assert second.current()["public_pool_cidr"] == "1.2.3.0/28" assert second.current()["public_vm_prefix"] == 24 assert second.current()["public_gateway"] == "1.2.3.1" assert second.current()["public_dns_servers"] == ["1.1.1.1", "8.8.8.8"] assert second.current()["public_reserved_ips"] == ["1.2.3.10"] assert (second._current_generation() / "id_private").read_text() == PRIVATE_KEY assert not list(data_dir.rglob("id_private")) second.close() assert not list(runtime_parent.rglob("id_private")) legacy = json.loads(store.records[("host", "current")]) legacy["metadata"].pop("region") for key in ("public_pool_cidr", "public_gateway", "public_dns_servers", "public_reserved_ips", "public_vm_prefix"): legacy["metadata"].pop(key) store.records[("host", "current")] = json.dumps(legacy) third = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_fake_ssh, ) assert third.current()["region"] == "" assert third.current()["public_pool_cidr"] == "" assert third.current()["public_gateway"] == "" assert third.current()["public_dns_servers"] == [] assert third.current()["public_vm_prefix"] is None third.close() def test_host_commit_does_not_read_database_after_activation(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() manager = DatabaseRemoteHostManager( store, tmp_path / "panel", runtime_parent=runtime_parent, runner=_fake_ssh, ) scan = manager.scan("host.example", 22) staged = manager.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], ), scan["fingerprint"]) manager.test(staged) original_get = store.get reads = 0 def one_read_only(kind: str, key: str) -> str | None: nonlocal reads reads += 1 if reads > 1: raise RuntimeError("database read unavailable after activation") return original_get(kind, key) store.get = one_read_only assert manager.commit(staged)["host"] == "host.example" assert reads == 1 assert ("host", "current") in store.records manager.close() def test_encrypted_host_key_auto_unlocks_after_process_restart(tmp_path: Path): store = EncryptedMemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() data_dir = tmp_path / "panel" first = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = first.scan("host.example", 22) staged = first.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) first.test(staged) saved = first.commit(staged) assert saved["key_locked"] is False assert saved["private_key_passphrase_saved"] is True ciphertext = store.records[("host", "current")] assert ciphertext.startswith(b"vp1") assert b"open-sesame" not in ciphertext assert b"open-sesame" not in b"".join( path.read_bytes() for path in data_dir.rglob("*") if path.is_file() ) first.close() second = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) assert second.current()["key_locked"] is False assert second.current()["private_key_passphrase_saved"] is True second.close() def test_bad_saved_passphrase_keeps_host_locked_without_blocking_startup(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() data_dir = tmp_path / "panel" first = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = first.scan("host.example", 22) staged = first.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) first.test(staged) first.commit(staged) first.close() record = json.loads(store.get("host", "current")) record["private_key_passphrase"] = "wrong-secret" store.set("host", "current", json.dumps(record)) attempts = [] def counting_agent(*args): attempts.append(1) return FakeAgent(*args) second = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=counting_agent, ) assert len(attempts) == 1 assert second.current()["key_locked"] is True assert second.current()["private_key_passphrase_saved"] is True assert second.current_for_background()["key_locked"] is True assert second.current_for_background()["key_locked"] is True assert len(attempts) == 2 assert second.unlock_current("open-sesame")["key_locked"] is False second.close() def test_background_recovers_ssh_agent_after_it_exits(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() manager = DatabaseRemoteHostManager( store, tmp_path / "panel", runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = manager.scan("host.example", 22) staged = manager.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) manager.test(staged) manager.commit(staged) generation = manager._current_generation() manager._agents[generation].close() assert manager.current()["key_locked"] is True assert manager.current_for_background()["key_locked"] is False manager.close() def test_resaving_same_key_can_reuse_saved_passphrase(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() manager = DatabaseRemoteHostManager( store, tmp_path / "panel", runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = manager.scan("host.example", 22) initial = manager.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) manager.test(initial) manager.commit(initial) for key in ("", PRIVATE_KEY): staged = manager.stage(HostProfile( host="host.example", port=22, private_key=key, host_key=scan["known_hosts_line"], private_key_passphrase="", ), scan["fingerprint"]) manager.test(staged) assert manager.commit(staged)["private_key_passphrase_saved"] is True assert json.loads(store.get("host", "current"))["private_key_passphrase"] == "open-sesame" manager.close() def test_replacing_key_cannot_inherit_previous_passphrase(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() manager = DatabaseRemoteHostManager( store, tmp_path / "panel", runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = manager.scan("host.example", 22) initial = manager.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) manager.test(initial) manager.commit(initial) previous = store.get("host", "current") different_key = PRIVATE_KEY.replace("ZmFrZQ==", "b3RoZXI=") with pytest.raises(RemoteHostError, match="填写私钥口令"): manager.stage(HostProfile( host="host.example", port=22, private_key=different_key, host_key=scan["known_hosts_line"], private_key_passphrase="", ), scan["fingerprint"]) assert store.get("host", "current") == previous manager.close() def test_legacy_saved_host_requiring_password_remains_locked_until_resaved(tmp_path: Path): store = MemoryStore() runtime_parent = tmp_path / "simulated-tmpfs" runtime_parent.mkdir() data_dir = tmp_path / "panel" first = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) scan = first.scan("host.example", 22) initial = first.stage(HostProfile( host="host.example", port=22, private_key=PRIVATE_KEY, host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) first.test(initial) first.commit(initial) record = json.loads(store.get("host", "current")) record.pop("private_key_passphrase") store.set("host", "current", json.dumps(record)) first.close() second = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) assert second.current()["key_locked"] is True assert second.current()["private_key_passphrase_saved"] is False assert second.unlock_current("open-sesame")["key_locked"] is False assert second.current()["private_key_passphrase_saved"] is False staged = second.stage(HostProfile( host="host.example", port=22, private_key="", host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame", ), scan["fingerprint"]) second.test(staged) assert second.commit(staged)["private_key_passphrase_saved"] is True second.close() third = DatabaseRemoteHostManager( store, data_dir, runtime_parent=runtime_parent, runner=_encrypted_ssh, agent_factory=FakeAgent, ) assert third.current()["key_locked"] is False third.close() def test_startup_prunes_only_marked_runtime_of_dead_process(tmp_path: Path, monkeypatch): dead = tmp_path / "v99999999-dead" live = tmp_path / f"v{os.getpid()}-abcd" dead.mkdir(mode=0o700) live.mkdir(mode=0o700) (dead / ".owner").write_text("99999999:123\n") (live / ".owner").write_text(f"{os.getpid()}:456\n") (dead / ".owner").chmod(0o600) (live / ".owner").chmod(0o600) monkeypatch.setattr( DatabaseRemoteHostManager, "_process_start_ticks", staticmethod(lambda pid: "456" if pid == os.getpid() else None), ) DatabaseRemoteHostManager._clean_stale_runtime(tmp_path) assert not dead.exists() assert live.exists()