Persist host SSH passphrase for unattended VM metrics

main
admin 2 months ago
parent b58c450abb
commit 17f0226581

@ -42,16 +42,16 @@ PYTHONDONTWRITEBYTECODE=1 .venv/bin/python -m uvicorn virsh_panel.app:create_app
1. 通过你的 HTTPS 域名打开面板,使用启动终端输出中的一次性令牌填写 MariaDB 的地址、端口、已有数据库名、专用账号和密码。数据库账号至少需要在该数据库中建表及读写权限;连接失败时不会保存配置。当前数据库连接尚未提供 TLS 参数,数据库请放在面板本机或可信内网,不要将其端口暴露公网。
2. 使用同一个令牌创建面板管理员。创建完成后令牌失效,管理员密码只保存为密码哈希。
3. 在网页填写 B 的 IP/DNS、SSH 端口、虚拟机地区、现有的 **root 客户端 SSH 私钥**、磁盘目录、ISO 目录和网络名称。地区是这台宿主机上虚拟机共用的展示标签,可留空;它不影响 SSH 连接或已导入的模板。私钥必须与 B 的 root 账号**已经授权的公钥**匹配;仅有 root 密码或 B 的 SSH 主机密钥不能代替它。加密私钥还需填写其口令。口令只在当前面板进程的内存中用于解锁,不写入数据库、磁盘或浏览器存储;面板重启后,登录网页并在“解锁宿主机 SSH 私钥”页面重新输入。
3. 在网页填写 B 的 IP/DNS、SSH 端口、虚拟机地区、现有的 **root 客户端 SSH 私钥**、磁盘目录、ISO 目录和网络名称。地区是这台宿主机上虚拟机共用的展示标签,可留空;它不影响 SSH 连接或已导入的模板。私钥必须与 B 的 root 账号**已经授权的公钥**匹配;仅有 root 密码或 B 的 SSH 主机密钥不能代替它。加密私钥还需填写其口令。保存宿主机设置时,面板会将口令与私钥加密写入 MariaDB,以便面板重启后自动连接和无人登录时继续采样;设置 API 不回显口令,面板代码也不将其写入浏览器存储。**旧版本中只在内存解锁过的口令无法自动恢复**:升级后进入“宿主机设置”,不必重新粘贴原私钥,填写原口令并重新保存一次。只在“解锁宿主机 SSH 私钥”页面输入不会持久保存。
4. 点击“扫描主机指纹”,再通过 B 的可信控制台运行 `ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub` 独立核对;确认后在网页测试、保存。保存前会重扫并测试 SSH 与 libvirt API,失败不会覆盖现有连接。
宿主机私钥和配置加密保存在 MariaDB。OpenSSH 连接时需要读取私钥文件,Linux 上面板会将其临时还原到权限为 `0700` 的 `/dev/shm` 内存文件系统目录;面板关闭时清除。`data/runtime-wrappers/` 只包含不带密钥的启动包装器。面板仅支持单个 Uvicorn worker;多 worker 会导致各自 SSH 解锁状态不一致。
宿主机私钥、已填写的私钥口令和配置加密保存在 MariaDB。数据库备份与 `data/master.key` 同时泄露时,这些凭据可以被解密,因此须分别严密保护;运行中的 root 面板进程同样能使用它们。OpenSSH 连接时需要读取私钥文件,Linux 上面板会将其临时还原到权限为 `0700` 的 `/dev/shm` 内存文件系统目录;面板关闭时清除。`data/runtime-wrappers/` 只包含不带密钥的启动包装器。若 SSH agent 异常退出,后台采样会限频尝试使用已保存口令恢复连接;若保存的口令错误,仍需管理员检查并重新保存。面板仅支持单个 Uvicorn worker。
虚拟机列表只展示状态、地区、系统或安装镜像、IP、规格和磁盘;点击虚拟机名称进入该机管理页,控制台、电源、重装和删除等操作都在管理页。地区取自当前宿主机设置。新建或重装的云镜像 VM 会记录所选模板名称及配置的公网 IP/CIDR;这个 IP 是**配置记录**,不是连通性探测。详情页的公网 IP 不显示 `/24` 等掩码后缀,可点击旁边的按钮复制;磁盘信息只显示总容量,不再显示磁盘块数。ISO 安装 VM 只记录安装镜像文件名,不能据此确认客体最终安装的系统;旧 VM 或面板外创建的 VM 若无可靠记录,会显示“未识别”或“未获取”,不会猜测客体系统与 IP。
云镜像 VM 的详情页显示 SSH 登录名 `root`。面板保存了初始 root 密码时,默认以星号遮盖,管理员可按需点击“查看”或“复制”;不会在虚拟机列表或普通详情数据中直接返回密码。只有本版本以后由面板新建、或成功重装的密码模式云镜像 VM 才能查看其加密保存的初始密码;公钥模式不保存也不显示密码,ISO 安装和面板外创建的 VM 的客体密码则不由面板管理。旧 VM 曾使用但面板未保存的密码无法从密码哈希或虚拟机系统中还原。客体内修改 root 密码后,详情页保存的**初始**密码不会自动更新,可能已无法用于登录;请妥善保护数据库备份和面板主密钥。
虚拟机详情页显示 CPU、客体内存、磁盘 I/O 和网络收发四张历史走势图,可切换最近 1 小时、24 小时、7 天和 30 天。面板后台定时从 [libvirt 域统计](https://libvirt.org/manpages/virsh.html#domstats)采样并写入 MariaDB,即使无人打开详情页也会继续记录。查询**只返回最近 30 天**的数据;运行中的面板会分批清理更早的数据库记录。面板停机或数据库不可用时物理清理会暂停,恢复后继续追赶,因此中断期间数据库中可能暂时留有超期记录。CPU 是按 vCPU 数归一化、最高显示 100% 的使用率,磁盘和网络显示每秒速率而不是累计量或网络带宽上限;网络收发方向以虚拟机为准。磁盘与网卡只汇总 libvirt 实际提供计数的设备,未提供计数的设备无法纳入统计。内存占用依赖客体内存气球统计,客户机不提供或统计过期时会显示无数据,不会把宿主机 QEMU 进程占用误当客体内存。初次采样、虚拟机停机、面板/宿主机不可达或计数器重置期间可能出现空白;面板停机期间不会补录历史。较长时间范围会聚合采样,短暂的停机或采样缺口可能不明显。
虚拟机详情页显示 CPU、客体内存、磁盘 I/O 和网络收发四张历史走势图,可切换最近 1 小时、24 小时、7 天和 30 天。面板后台定时从 [libvirt 域统计](https://libvirt.org/manpages/virsh.html#domstats)采样并写入 MariaDB,即使无人登录也会继续记录,前提是宿主机连接可用且加密私钥能自动解锁。旧配置若只在内存中解锁、没有重新保存口令,重启后后台采样会暂停,直到管理员解锁;按上述步骤保存口令后可避免这种中断。查询**只返回最近 30 天**的数据;运行中的面板会分批清理更早的数据库记录。面板停机或数据库不可用时物理清理会暂停,恢复后继续追赶,因此中断期间数据库中可能暂时留有超期记录。CPU 是按 vCPU 数归一化、最高显示 100% 的使用率,磁盘和网络显示每秒速率而不是累计量或网络带宽上限;网络收发方向以虚拟机为准。磁盘与网卡只汇总 libvirt 实际提供计数的设备,未提供计数的设备无法纳入统计。内存占用依赖客体内存气球统计,客户机不提供或统计过期时会显示无数据,不会把宿主机 QEMU 进程占用误当客体内存。初次采样、虚拟机停机、面板/宿主机不可达或计数器重置期间可能出现空白;面板停机期间不会补录历史。较长时间范围会聚合采样,短暂的停机或采样缺口可能不明显。
磁盘目录和 ISO 目录填写的是 **B 宿主机**上已存在的绝对路径,例如 `/home/kvm`、`/home/iso`,不是 A 面板服务器的路径;不需要提前手动创建 libvirt 存储池。点击“保存”时,面板会将这些现有目录定义为 libvirt `dir` 存储池并启动,作为内部管理机制;不会创建目录,也不会移动或删除现有文件。新建虚拟机的磁盘文件使用配置的磁盘目录下的 `<虚拟机名>.qcow2`,安装镜像使用配置的 ISO 目录下所选的 `.iso`;虚拟机 XML 通过绝对文件路径引用它们。“测试连接”不会修改宿主机,因此此时若目录尚未注册为池,可能仅显示资源警告。也可继续填写已有的 libvirt 存储池名称。若 B 的目录权限或 SELinux 策略阻止 libvirt 访问,保存会给出错误,需在 B 上修正权限或标签。

@ -299,6 +299,14 @@ function showHostView(hostConfig = { configured: false }) {
text("host-key-hint", configured
? "留空则继续使用已保存的私钥;填写或选择文件会替换它。已保存私钥不会回显。"
: "仅通过 HTTPS 传输并保存在面板服务器。请确保这把密钥已在宿主机 root 账号上获得授权。");
const passphraseSaved = Boolean(hostConfig.private_key_passphrase_saved);
byId("host-private-key-passphrase").placeholder = passphraseSaved
? "留空继续使用已保存的口令" : "输入用于解锁私钥的口令";
text("host-passphrase-hint", passphraseSaved
? "口令已加密保存且不会回显;私钥不变时留空可沿用,更换私钥时请填写新口令。"
: configured
? "当前没有保存私钥口令。如私钥需要口令,请在此输入并重新保存,供面板重启后自动连接和后台持续采样。"
: "保存时会加密写入面板数据库,供重启后自动连接和后台持续采样;面板不会回显口令或写入浏览器存储。");
text("host-test-status", configured
? `当前已保存 ${hostConfig.host}:${hostConfig.port},修改前请重新扫描并核对主机指纹。`
: "尚未测试连接");

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#101d2c" />
<title>VirtPilot · 虚拟机管理</title>
<link rel="stylesheet" href="/static/styles.css?v=vm-details-4" />
<script type="module" src="/static/app.js?v=vm-details-4"></script>
<link rel="stylesheet" href="/static/styles.css?v=host-passphrase-1" />
<script type="module" src="/static/app.js?v=host-passphrase-1"></script>
</head>
<body>
<div id="boot-screen" class="boot-screen" role="status" aria-live="polite">
@ -136,7 +136,7 @@
<div class="field"><label for="host-network">默认虚拟网络或网桥</label><input id="host-network" name="default_network" type="text" maxlength="128" value="default" required autocomplete="off" placeholder="例如 default 或 bridge:br0" /><small>libvirt 虚拟网络填名称;宿主机现有 Linux 网桥 br0 填 bridge:br0。</small></div>
<div class="field full"><label for="host-private-key">现有 SSH 私钥 <span id="host-key-required-mark">*</span></label><textarea id="host-private-key" name="private_key" rows="5" maxlength="65536" spellcheck="false" autocomplete="off" placeholder="粘贴 -----BEGIN OPENSSH PRIVATE KEY----- 开头的完整内容,或从下方选择文件"></textarea><small id="host-key-hint">仅通过 HTTPS 传输并保存在面板服务器。请确保这把密钥已在宿主机 root 账号上获得授权。</small></div>
<div class="field full"><label for="host-private-key-file">或从本机选择私钥文件</label><input id="host-private-key-file" type="file" /><small id="host-key-file-label">未选择文件;已保存的密钥不会在这里显示。</small></div>
<div class="field full"><label for="host-private-key-passphrase">SSH 私钥口令(如果设置了)</label><input id="host-private-key-passphrase" name="private_key_passphrase" type="password" maxlength="1024" autocomplete="off" spellcheck="false" placeholder="输入用于解锁私钥的口令" /><small>口令不会回显或写入浏览器存储;保存后仅在当前面板进程中用于连接。面板重启后需要重新输入解锁。</small></div>
<div class="field full"><label for="host-private-key-passphrase">SSH 私钥口令(如果设置了)</label><input id="host-private-key-passphrase" name="private_key_passphrase" type="password" maxlength="1024" autocomplete="off" spellcheck="false" placeholder="输入用于解锁私钥的口令" /><small id="host-passphrase-hint">保存时会加密写入面板数据库,供重启后自动连接和后台持续采样;面板不会回显口令或写入浏览器存储。</small></div>
</div>
</form>
</section>
@ -170,14 +170,14 @@
<section class="host-card unlock-card" aria-labelledby="unlock-title">
<span class="eyebrow">HOST KEY LOCKED</span>
<h1 id="unlock-title">解锁宿主机 SSH 私钥</h1>
<p class="muted">宿主机 <strong id="unlock-host-label"></strong> 的私钥已加密。面板重启后需要重新输入私钥口令,才能管理虚拟机。</p>
<p class="muted">宿主机 <strong id="unlock-host-label"></strong> 的私钥尚未自动解锁。请输入口令以恢复连接;若要重启后自动连接,请随后在“宿主机设置”中重新保存口令。</p>
<form id="unlock-form" class="form-stack">
<label for="unlock-passphrase">私钥口令</label>
<input id="unlock-passphrase" type="password" maxlength="1024" autocomplete="off" spellcheck="false" required autofocus placeholder="输入现有私钥口令" />
<p id="unlock-error" class="form-error" role="alert" hidden></p>
<button id="unlock-submit" class="button button-primary unlock-submit" type="submit">解锁并进入面板</button>
</form>
<p class="host-security-note">口令只用于当前运行中的面板进程,不保存到磁盘或浏览器。请通过 HTTPS 连接面板。</p>
<p class="host-security-note">在这里输入只用于本次解锁;在“宿主机设置”重新保存后,口令会加密写入数据库供后台使用。面板不会将口令写入浏览器存储;请通过 HTTPS 连接面板。</p>
<button id="unlock-settings-button" class="button button-ghost small unlock-settings-button" type="button">修改宿主机设置</button>
<button id="unlock-update-button" class="button button-ghost small unlock-settings-button" type="button">网站更新</button>
</section>

@ -190,6 +190,7 @@ class FakeRemoteManager:
"key_type": "ssh-ed25519", "fingerprint": "SHA256:example",
"private_key_configured": True,
"private_key_encrypted": bool(self.unlock_passphrase), "key_locked": False,
"private_key_passphrase_saved": bool(self.unlock_passphrase),
"storage_pool": staged.profile.storage_pool,
"iso_pool": staged.profile.iso_pool,
"default_network": staged.profile.default_network,
@ -447,6 +448,8 @@ def test_private_key_passphrase_reaches_host_profile_but_is_never_echoed(web):
for profile in manager.staged_profiles)
assert all(secret not in response.text for response in (tested, saved, current))
assert saved.json()["private_key_encrypted"] is True
assert saved.json()["private_key_passphrase_saved"] is True
assert current.json()["private_key_passphrase_saved"] is True
assert "private_key_passphrase" not in manager.current()

@ -23,6 +23,7 @@ from virsh_panel.database import (
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"
@ -52,6 +53,24 @@ class MemoryStore:
if record_type == kind}
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)
@ -137,7 +156,7 @@ def _encrypted_ssh(argv, **kwargs):
class FakeAgent:
def __init__(self, generation: Path, _key_path: Path, passphrase: str) -> None:
if passphrase != "open-sesame":
raise RuntimeError("bad passphrase")
raise SSHAgentError("bad passphrase")
self.socket_path = generation / "fake-agent.sock"
self.public_key = _host_key()
self.alive = True
@ -191,10 +210,40 @@ def test_remote_host_is_rebuilt_from_database_without_durable_private_key(tmp_pa
third.close()
def test_encrypted_host_key_is_locked_again_after_process_restart(tmp_path: Path):
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,
@ -206,16 +255,184 @@ def test_encrypted_host_key_is_locked_again_after_process_restart(tmp_path: Path
host_key=scan["known_hosts_line"], private_key_passphrase="open-sesame",
), scan["fingerprint"])
first.test(staged)
assert first.commit(staged)["key_locked"] is False
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"

@ -332,6 +332,48 @@ def test_collector_prunes_when_ssh_locked_and_starts_after_db_configuration():
collector.close()
def test_background_collector_keeps_sampling_without_an_http_session():
"""The 30-day history must not depend on a logged-in browser polling it."""
saved_twice = threading.Event()
class Store:
def __init__(self):
self.saved = []
def save_metric_samples(self, scope, points):
self.saved.append((scope, points))
if len(self.saved) >= 2:
saved_twice.set()
def prune_metric_samples(self, _cutoff):
return 0
class Service:
calls = 0
def metric_counters(self):
self.calls += 1
return [raw(cpu=self.calls * 1_000_000_000,
block_read=self.calls * 1000,
network_rx=self.calls * 1000)]
store = Store()
service = Service()
profile = {**HOST, "key_locked": False}
collector = MetricsCollector(lambda: store, lambda: profile,
lambda _profile: service,
interval_seconds=0.01)
collector.start()
try:
assert saved_twice.wait(timeout=2)
finally:
collector.close()
assert service.calls >= 2
assert all(scope == host_scope(HOST) for scope, _ in store.saved)
assert all(points[0]["vm_id"] == VM_ID for _, points in store.saved)
def test_metric_cleanup_failure_does_not_discard_current_sample(caplog):
events = []
attempted = threading.Event()

@ -182,7 +182,7 @@ def _public_host(profile: dict | None) -> dict:
keys = (
"host", "port", "key_type", "fingerprint", "storage_pool",
"iso_pool", "default_network", "private_key_configured",
"private_key_encrypted", "key_locked",
"private_key_encrypted", "key_locked", "private_key_passphrase_saved",
)
return {"configured": True, **{key: profile[key] for key in keys if key in profile},
"region": profile.get("region", "")}
@ -391,9 +391,15 @@ def create_app(
login_limiter = LoginLimiter()
setup_limiter = LoginLimiter()
unlock_limiter = LoginLimiter()
def metric_profile():
if manager is None:
return None
background = getattr(manager, "current_for_background", None)
return background() if callable(background) else manager.current()
metric_collector = MetricsCollector(
lambda: data_store,
lambda: manager.current() if manager is not None else None,
metric_profile,
lambda profile: new_service(profile),
) if database_mode else None

@ -10,6 +10,7 @@ from __future__ import annotations
import hmac
import json
import logging
import math
import os
import re
@ -18,7 +19,9 @@ import shutil
import stat
import sys
import threading
import time
import uuid
from dataclasses import replace
from pathlib import Path
from typing import Any, Mapping
from urllib.parse import quote
@ -36,6 +39,7 @@ from .panel_state import PanelState, SetupError, _USERNAME
from .remote_host import (
LIBVIRT_URI,
SSH_ALIAS,
HostProfile,
RemoteHostError,
RemoteHostManager,
StagedHost,
@ -86,6 +90,7 @@ _METRIC_COLUMNS = (
)
_HOST_SCOPE = re.compile(r"[0-9a-f]{64}\Z")
_METRIC_PRUNE_BATCH_SIZE = 5000
LOG = logging.getLogger(__name__)
class DatabaseError(Exception):
@ -496,9 +501,13 @@ class DatabaseTemplateStore:
class DatabaseStagedHost(StagedHost):
def __init__(self, staged: StagedHost, wrapper_path: Path) -> None:
def __init__(self, staged: StagedHost, wrapper_path: Path,
private_key_passphrase: str) -> None:
super().__init__(staged.path, staged.metadata, staged.tested)
self.wrapper_path = wrapper_path
# The staged secret is process-local until a successful save; the
# complete host record is encrypted by DatabaseStore at rest.
self._private_key_passphrase = private_key_passphrase
@property
def libvirt_uri(self) -> str:
@ -586,6 +595,8 @@ class DatabaseRemoteHostManager(RemoteHostManager):
raise RemoteHostError("SSH 包装器目录权限不安全", 500)
self._wrapper_dir = wrapper_dir
self._wrapper_paths: set[Path] = set()
self._passphrase_saved = False
self._last_background_unlock_attempt = float("-inf")
if runtime_parent is None:
if sys.platform == "linux":
runtime_parent = Path("/dev/shm")
@ -657,14 +668,48 @@ class DatabaseRemoteHostManager(RemoteHostManager):
self._wrapper_paths.add(destination)
return destination
def _saved_host_record(self) -> dict[str, Any] | None:
raw = self.store.get("host", "current")
if raw is None:
return None
try:
record = json.loads(raw)
if not isinstance(record, dict):
raise ValueError("Invalid host record")
passphrase = record.get("private_key_passphrase", "")
if not isinstance(passphrase, str):
raise ValueError("Invalid host passphrase")
return record
except (TypeError, ValueError) as exc:
raise RemoteHostError("数据库宿主机配置损坏", 500) from exc
def stage(self, profile, confirmed_fingerprint: str) -> DatabaseStagedHost:
if isinstance(profile, Mapping):
try:
profile = HostProfile(**profile)
except (TypeError, ValueError) as exc:
raise RemoteHostError("宿主机配置字段无效") from exc
if not isinstance(profile, HostProfile):
raise RemoteHostError("宿主机配置字段无效")
if not profile.private_key_passphrase:
saved = self._saved_host_record()
if saved is not None and saved.get("private_key_passphrase"):
try:
saved_key = _validate_private_key(saved["private_key"])
requested_key = (_validate_private_key(profile.private_key)
if profile.private_key else saved_key)
except (KeyError, TypeError, UnicodeError) as exc:
raise RemoteHostError("数据库宿主机配置损坏", 500) from exc
if hmac.compare_digest(requested_key, saved_key):
profile = replace(profile, private_key_passphrase=
saved["private_key_passphrase"])
staged = super().stage(profile, confirmed_fingerprint)
try:
wrapper = self._write_wrapper(staged.path)
except BaseException:
super().abort(staged)
raise
return DatabaseStagedHost(staged, wrapper)
return DatabaseStagedHost(staged, wrapper, profile.private_key_passphrase)
def abort(self, staged: StagedHost) -> None:
super().abort(staged)
@ -682,6 +727,10 @@ class DatabaseRemoteHostManager(RemoteHostManager):
(staged.path / "identity.pub").read_text(encoding="ascii")
if (staged.path / "identity.pub").exists() else None
),
"private_key_passphrase": (
staged._private_key_passphrase
if staged.metadata["private_key_encrypted"] else ""
),
}
except (OSError, UnicodeError) as exc:
raise RemoteHostError("暂存宿主机密钥无法读取", 500) from exc
@ -693,12 +742,18 @@ class DatabaseRemoteHostManager(RemoteHostManager):
# Check the runtime activation preconditions before committing DB.
self._ensure_user_ssh_config()
previous = self.store.get("host", "current")
payload = json.dumps(self._staged_record(staged), ensure_ascii=False,
record = self._staged_record(staged)
next_passphrase_saved = bool(record["metadata"]["private_key_encrypted"]
and record["private_key_passphrase"])
payload = json.dumps(record, ensure_ascii=False,
separators=(",", ":"))
self.store.set("host", "current", payload)
previous_passphrase_saved = self._passphrase_saved
self._passphrase_saved = next_passphrase_saved
try:
return super().commit(staged)
except BaseException:
self._passphrase_saved = previous_passphrase_saved
if previous is None:
self.store.delete("host", "current")
else:
@ -706,11 +761,10 @@ class DatabaseRemoteHostManager(RemoteHostManager):
raise
def _restore_current(self) -> None:
raw = self.store.get("host", "current")
if raw is None:
record = self._saved_host_record()
if record is None:
return
try:
record = json.loads(raw)
metadata = record["metadata"]
host = _validate_host(metadata["host"])
port = _validate_port(metadata["port"])
@ -726,6 +780,7 @@ class DatabaseRemoteHostManager(RemoteHostManager):
raise ValueError("Host key mismatch")
encrypted = bool(metadata["private_key_encrypted"])
identity_public = record["identity_public"]
passphrase = record.get("private_key_passphrase", "")
if encrypted and (not isinstance(identity_public, str)
or len(identity_public.split()) < 2):
raise ValueError("Encrypted host identity is missing")
@ -786,6 +841,14 @@ class DatabaseRemoteHostManager(RemoteHostManager):
temporary_link.symlink_to(generation)
os.replace(temporary_link, self.data_dir / "current")
assert wrapper.exists()
self._passphrase_saved = bool(encrypted and passphrase)
if encrypted and passphrase:
try:
super().unlock_current(passphrase)
except RemoteHostError:
# Keep the panel available for an operator to re-save a
# corrected key/phrase. Never print the secret or SSH output.
LOG.warning("saved remote SSH key could not be auto-unlocked")
except BaseException:
shutil.rmtree(generation, ignore_errors=True)
raise
@ -801,9 +864,33 @@ class DatabaseRemoteHostManager(RemoteHostManager):
profile = super().current()
if profile is None:
return None
profile["private_key_passphrase_saved"] = self._passphrase_saved
profile["libvirt_uri"] = self.libvirt_uri
return profile
def current_for_background(self) -> dict[str, Any] | None:
"""Recover a lost SSH agent so unattended metric sampling can resume."""
profile = self.current()
if profile is None or not profile["key_locked"] or not self._passphrase_saved:
return profile
with self._lock:
profile = self.current()
if profile is None or not profile["key_locked"]:
return profile
now = time.monotonic()
if now - self._last_background_unlock_attempt < 60:
return profile
self._last_background_unlock_attempt = now
record = self._saved_host_record()
passphrase = record.get("private_key_passphrase", "") if record else ""
if not passphrase:
return profile
try:
return super().unlock_current(passphrase)
except RemoteHostError:
LOG.warning("saved remote SSH key could not be unlocked for background metrics")
return self.current()
def close(self) -> None:
super().close()
shutil.rmtree(self._runtime_dir, ignore_errors=True)

Loading…
Cancel
Save