Add MariaDB-backed panel state and web updates
parent
4166c3002f
commit
c83e17cd46
@ -0,0 +1,22 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Virsh Panel one-shot update from the pinned private Gitea repository
|
||||||
|
After=network-online.target
|
||||||
|
Wants=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/home/virsh-panel
|
||||||
|
Environment=PANEL_DATA_DIR=/home/virsh-panel/data
|
||||||
|
Environment=PYTHONDONTWRITEBYTECODE=1
|
||||||
|
ExecStart=/home/virsh-panel/.venv/bin/python -m virsh_panel.update_worker
|
||||||
|
ExecStopPost=/home/virsh-panel/.venv/bin/python -m virsh_panel.update_worker --finalize-incomplete
|
||||||
|
TimeoutStartSec=30min
|
||||||
|
UMask=0077
|
||||||
|
LimitCORE=0
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=false
|
||||||
|
ReadWritePaths=/home/virsh-panel
|
||||||
@ -0,0 +1,224 @@
|
|||||||
|
"""Database-backed state behavior without requiring a live MariaDB server."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
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 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
|
||||||
|
|
||||||
|
|
||||||
|
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}
|
||||||
|
|
||||||
|
|
||||||
|
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_admin_hash_is_database_only_and_bootstrap_token_is_consumed(tmp_path: Path):
|
||||||
|
store = MemoryStore()
|
||||||
|
data_dir = tmp_path / "panel"
|
||||||
|
state = DatabasePanelState(data_dir, 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)
|
||||||
|
username, hashed = reopened.admin()
|
||||||
|
assert username == "admin"
|
||||||
|
assert verify_password("a-valid-very-long-password", hashed)
|
||||||
|
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 _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 RuntimeError("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",
|
||||||
|
)
|
||||||
|
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_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"))
|
||||||
|
|
||||||
|
|
||||||
|
def test_encrypted_host_key_is_locked_again_after_process_restart(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)
|
||||||
|
assert first.commit(staged)["key_locked"] is False
|
||||||
|
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.unlock_current("open-sesame")["key_locked"] is False
|
||||||
|
second.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()
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
"""The only local durable configuration is encrypted database bootstrap data."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import stat
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from virsh_panel.database_bootstrap import (
|
||||||
|
DatabaseBootstrap, DatabaseSetupError, validate_database_config,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
CONFIG = {
|
||||||
|
"host": "db.example.test", "port": 3306, "database": "virsh_panel",
|
||||||
|
"username": "panel", "password": "not-a-public-database-password",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_bootstrap_requires_token_and_encrypts_connection_settings(tmp_path):
|
||||||
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
||||||
|
token = bootstrap.ensure_token()
|
||||||
|
assert bootstrap.ensure_token() == token
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def connect(config, key):
|
||||||
|
calls.append((config, key))
|
||||||
|
return "connected"
|
||||||
|
|
||||||
|
with pytest.raises(DatabaseSetupError) as wrong:
|
||||||
|
bootstrap.configure(CONFIG, "wrong-token", connect)
|
||||||
|
assert wrong.value.status == 403
|
||||||
|
assert calls == []
|
||||||
|
assert bootstrap.configure(CONFIG, token, connect) == "connected"
|
||||||
|
assert len(calls[0][1]) == 32
|
||||||
|
assert bootstrap.load_config() == CONFIG
|
||||||
|
assert CONFIG["password"].encode() not in bootstrap.config_file.read_bytes()
|
||||||
|
assert CONFIG["username"].encode() not in bootstrap.config_file.read_bytes()
|
||||||
|
assert stat.S_IMODE(bootstrap.data_dir.stat().st_mode) == 0o700
|
||||||
|
assert stat.S_IMODE(bootstrap.key_file.stat().st_mode) == 0o600
|
||||||
|
assert stat.S_IMODE(bootstrap.config_file.stat().st_mode) == 0o600
|
||||||
|
with pytest.raises(DatabaseSetupError) as repeated:
|
||||||
|
bootstrap.configure(CONFIG, token, connect)
|
||||||
|
assert repeated.value.status == 409
|
||||||
|
|
||||||
|
|
||||||
|
def test_bootstrap_does_not_commit_failed_connection(tmp_path):
|
||||||
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
||||||
|
token = bootstrap.ensure_token()
|
||||||
|
|
||||||
|
def fail(_config, _key):
|
||||||
|
raise ConnectionError("database unavailable")
|
||||||
|
|
||||||
|
with pytest.raises(ConnectionError):
|
||||||
|
bootstrap.configure(CONFIG, token, fail)
|
||||||
|
assert not bootstrap.configured()
|
||||||
|
assert bootstrap.ensure_token() == token
|
||||||
|
|
||||||
|
|
||||||
|
def test_bootstrap_rejects_tampering_and_unsafe_files(tmp_path):
|
||||||
|
bootstrap = DatabaseBootstrap(tmp_path / "data")
|
||||||
|
token = bootstrap.ensure_token()
|
||||||
|
bootstrap.configure(CONFIG, token, lambda *_: None)
|
||||||
|
envelope = json.loads(bootstrap.config_file.read_text())
|
||||||
|
envelope["ciphertext"] = "AAAA"
|
||||||
|
bootstrap.config_file.write_text(json.dumps(envelope))
|
||||||
|
with pytest.raises(ValueError, match="损坏"):
|
||||||
|
bootstrap.load_config()
|
||||||
|
bootstrap.config_file.chmod(0o644)
|
||||||
|
with pytest.raises(ValueError, match="0600"):
|
||||||
|
bootstrap.load_config()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("bad", [
|
||||||
|
{**CONFIG, "host": "https://evil.example"},
|
||||||
|
{**CONFIG, "port": 0},
|
||||||
|
{**CONFIG, "database": "other;DROP TABLE admin"},
|
||||||
|
{**CONFIG, "username": "bad\nname"},
|
||||||
|
{**CONFIG, "password": ""},
|
||||||
|
])
|
||||||
|
def test_database_config_validation_rejects_unsafe_values(bad):
|
||||||
|
with pytest.raises(DatabaseSetupError):
|
||||||
|
validate_database_config(bad)
|
||||||
@ -0,0 +1,317 @@
|
|||||||
|
"""First-run database and update routes with isolated in-memory dependencies."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import stat
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import types
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from pathlib import Path
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
# The HTTP boundary is tested with fake VM services; native libvirt is not
|
||||||
|
# available on ordinary development machines.
|
||||||
|
try:
|
||||||
|
import libvirt # noqa: F401
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
stub = types.ModuleType("libvirt")
|
||||||
|
stub.libvirtError = type("libvirtError", (Exception,), {})
|
||||||
|
sys.modules["libvirt"] = stub
|
||||||
|
|
||||||
|
from virsh_panel import app as app_module
|
||||||
|
from virsh_panel.config import Settings
|
||||||
|
from virsh_panel.database import DatabasePanelState, DatabaseTemplateStore
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryStore:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.records = {}
|
||||||
|
|
||||||
|
def get(self, kind, key):
|
||||||
|
return self.records.get((kind, key))
|
||||||
|
|
||||||
|
def set(self, kind, key, value):
|
||||||
|
self.records[(kind, key)] = value
|
||||||
|
|
||||||
|
def insert(self, kind, key, value):
|
||||||
|
if (kind, key) in self.records:
|
||||||
|
return False
|
||||||
|
self.set(kind, key, value)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def delete(self, kind, key):
|
||||||
|
self.records.pop((kind, key), None)
|
||||||
|
|
||||||
|
def list(self, kind):
|
||||||
|
return {key: value for (record_type, key), value in self.records.items()
|
||||||
|
if record_type == kind}
|
||||||
|
|
||||||
|
def get_secret(self, key):
|
||||||
|
return self.get("secret", key)
|
||||||
|
|
||||||
|
def set_secret(self, key, value):
|
||||||
|
self.set("secret", key, value)
|
||||||
|
|
||||||
|
def get_value(self, key):
|
||||||
|
return self.get("value", key)
|
||||||
|
|
||||||
|
def set_value(self, key, value):
|
||||||
|
self.set("value", key, value)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeManager:
|
||||||
|
def current(self):
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_first_run_requires_database_then_admin_and_ignores_old_json(monkeypatch, tmp_path):
|
||||||
|
data_dir = tmp_path / "data"
|
||||||
|
data_dir.mkdir(mode=0o700)
|
||||||
|
(data_dir / "state.json").write_text(
|
||||||
|
'{"version":1,"admin":{"username":"old","password_hash":"old"}}',
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
store = MemoryStore()
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def make_components(directory, config, key, _ssh_binary):
|
||||||
|
calls.append((config, key))
|
||||||
|
return (DatabasePanelState(directory, store), FakeManager(),
|
||||||
|
DatabaseTemplateStore(store), store)
|
||||||
|
|
||||||
|
monkeypatch.setattr(app_module, "make_components", make_components)
|
||||||
|
settings = Settings(data_dir=data_dir,
|
||||||
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
||||||
|
app = app_module.create_app(settings)
|
||||||
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
||||||
|
config = {
|
||||||
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
||||||
|
"username": "panel", "password": "test-database-password",
|
||||||
|
}
|
||||||
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
||||||
|
assert client.get("/api/v1/database/status").json() == {
|
||||||
|
"configured": False, "setup_required": True,
|
||||||
|
}
|
||||||
|
assert client.get("/api/v1/setup/status").status_code == 409
|
||||||
|
assert client.post("/api/v1/database/setup", json={
|
||||||
|
**config, "setup_token": "invalid",
|
||||||
|
}).status_code == 403
|
||||||
|
assert calls == []
|
||||||
|
response = client.post("/api/v1/database/setup", json={
|
||||||
|
**config, "setup_token": token,
|
||||||
|
})
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json() == {"configured": True}
|
||||||
|
assert calls[0][0] == config and len(calls[0][1]) == 32
|
||||||
|
assert client.get("/api/v1/setup/status").json() == {"setup_required": True}
|
||||||
|
created = client.post("/api/v1/setup/admin", json={
|
||||||
|
"token": token, "username": "admin",
|
||||||
|
"password": "correct-horse-battery-staple",
|
||||||
|
})
|
||||||
|
assert created.status_code == 200
|
||||||
|
assert client.get("/api/v1/setup/status").json() == {"setup_required": False}
|
||||||
|
session_cookie = client.cookies.get("virsh_panel_session")
|
||||||
|
assert ("admin", "singleton") in store.records
|
||||||
|
assert not (data_dir / "setup-token").exists()
|
||||||
|
encrypted = (data_dir / "database-config.json").read_bytes()
|
||||||
|
assert b"test-database-password" not in encrypted
|
||||||
|
assert stat.S_IMODE((data_dir / "master.key").stat().st_mode) == 0o600
|
||||||
|
store.set("import_job", "interrupted-job", json.dumps({
|
||||||
|
"status": "running", "scope": "previous-host",
|
||||||
|
}))
|
||||||
|
restarted = app_module.create_app(settings)
|
||||||
|
assert json.loads(store.get("import_job", "interrupted-job"))["status"] == "failed"
|
||||||
|
with TestClient(restarted, base_url="https://panel.example.test") as client:
|
||||||
|
client.cookies.set("virsh_panel_session", session_cookie)
|
||||||
|
assert client.get("/api/v1/session").json()["authenticated"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_routes_require_auth_and_never_return_token(monkeypatch, tmp_path):
|
||||||
|
data_dir = tmp_path / "data"
|
||||||
|
store = MemoryStore()
|
||||||
|
monkeypatch.setattr(app_module, "make_components", lambda directory, *_: (
|
||||||
|
DatabasePanelState(directory, store), FakeManager(),
|
||||||
|
DatabaseTemplateStore(store), store,
|
||||||
|
))
|
||||||
|
settings = Settings(data_dir=data_dir,
|
||||||
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
||||||
|
app = app_module.create_app(settings)
|
||||||
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
||||||
|
|
||||||
|
class FakeUpdater:
|
||||||
|
def __init__(self, *_args):
|
||||||
|
self.token = None
|
||||||
|
self.job = None
|
||||||
|
|
||||||
|
def settings(self):
|
||||||
|
return {
|
||||||
|
"repository": "https://g.luoli.live/root/virt.git", "branch": "main",
|
||||||
|
"token_configured": self.token is not None, "checkout_ready": True,
|
||||||
|
"current_commit": "a" * 40, "active_job_id": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
def set_token(self, token):
|
||||||
|
self.token = token
|
||||||
|
|
||||||
|
def check(self):
|
||||||
|
return {
|
||||||
|
"repository": "https://g.luoli.live/root/virt.git", "branch": "main",
|
||||||
|
"current_commit": "a" * 40, "remote_commit": "b" * 40,
|
||||||
|
"status": "update_available",
|
||||||
|
}
|
||||||
|
|
||||||
|
def create_job(self, *, expected_current_commit=None, expected_remote_commit=None):
|
||||||
|
assert expected_current_commit == "a" * 40
|
||||||
|
assert expected_remote_commit == "b" * 40
|
||||||
|
self.job = {"job_id": "test-job-00000001", "status": "queued"}
|
||||||
|
return self.job
|
||||||
|
|
||||||
|
def get_job(self, job_id):
|
||||||
|
return self.job if self.job and job_id == self.job["job_id"] else None
|
||||||
|
|
||||||
|
def fail_job(self, job_id, error):
|
||||||
|
self.job = {"job_id": job_id, "status": "failed", "error": error}
|
||||||
|
|
||||||
|
monkeypatch.setattr(app_module, "Updater", FakeUpdater)
|
||||||
|
monkeypatch.setattr(app_module.subprocess, "run", lambda *args, **kwargs:
|
||||||
|
SimpleNamespace(returncode=0, stdout="inactive"))
|
||||||
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
||||||
|
assert client.get("/api/v1/update/settings").status_code == 401
|
||||||
|
client.post("/api/v1/database/setup", json={
|
||||||
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
||||||
|
"username": "panel", "password": "test-database-password",
|
||||||
|
"setup_token": token,
|
||||||
|
})
|
||||||
|
response = client.post("/api/v1/setup/admin", json={
|
||||||
|
"token": token, "username": "admin",
|
||||||
|
"password": "correct-horse-battery-staple",
|
||||||
|
})
|
||||||
|
assert response.status_code == 200
|
||||||
|
csrf = response.json()["csrf_token"]
|
||||||
|
assert client.put("/api/v1/update/settings", json={
|
||||||
|
"access_token": "some-very-private-gitea-token",
|
||||||
|
}).status_code == 403
|
||||||
|
saved = client.put("/api/v1/update/settings", json={
|
||||||
|
"access_token": "some-very-private-gitea-token",
|
||||||
|
}, headers={"X-CSRF-Token": csrf})
|
||||||
|
assert saved.status_code == 200
|
||||||
|
assert saved.json()["configured"] is True
|
||||||
|
assert "some-very-private-gitea-token" not in saved.text
|
||||||
|
checked = client.post("/api/v1/update/check", headers={
|
||||||
|
"X-CSRF-Token": csrf,
|
||||||
|
})
|
||||||
|
assert checked.status_code == 200
|
||||||
|
assert checked.json()["update_available"] is True
|
||||||
|
queued = client.post("/api/v1/update/apply", json={
|
||||||
|
"expected_current_commit": "a" * 40,
|
||||||
|
"expected_remote_commit": "b" * 40,
|
||||||
|
}, headers={
|
||||||
|
"X-CSRF-Token": csrf,
|
||||||
|
})
|
||||||
|
assert queued.status_code == 202
|
||||||
|
job_id = queued.json()["job_id"]
|
||||||
|
assert client.get(f"/api/v1/update/jobs/{job_id}").json()["status"] == "queued"
|
||||||
|
|
||||||
|
|
||||||
|
def test_vm_creation_finishes_before_update_is_queued(monkeypatch, tmp_path):
|
||||||
|
data_dir = tmp_path / "data"
|
||||||
|
store = MemoryStore()
|
||||||
|
vm_started = threading.Event()
|
||||||
|
release_vm = threading.Event()
|
||||||
|
update_received = threading.Event()
|
||||||
|
update_queued = threading.Event()
|
||||||
|
|
||||||
|
class ConfiguredManager(FakeManager):
|
||||||
|
def current(self):
|
||||||
|
return {"key_locked": False}
|
||||||
|
|
||||||
|
class BlockingVMService:
|
||||||
|
def create_vm(self, _spec):
|
||||||
|
vm_started.set()
|
||||||
|
assert release_vm.wait(timeout=5), "test did not release VM creation"
|
||||||
|
return {"uuid": "00000000-0000-4000-8000-000000000001"}
|
||||||
|
|
||||||
|
class FakeUpdater:
|
||||||
|
def __init__(self, _repo_dir, data_store):
|
||||||
|
self.store = data_store
|
||||||
|
self.job = None
|
||||||
|
|
||||||
|
def create_job(self, **_kwargs):
|
||||||
|
self.job = {"job_id": "test-job-00000001", "status": "queued"}
|
||||||
|
self.store.set_value("update.active_job", self.job["job_id"])
|
||||||
|
update_queued.set()
|
||||||
|
return self.job
|
||||||
|
|
||||||
|
def get_job(self, job_id):
|
||||||
|
return self.job if self.job and job_id == self.job["job_id"] else None
|
||||||
|
|
||||||
|
def fail_job(self, _job_id, _message):
|
||||||
|
raise AssertionError("the mocked update service must start")
|
||||||
|
|
||||||
|
monkeypatch.setattr(app_module, "make_components", lambda directory, *_: (
|
||||||
|
DatabasePanelState(directory, store), ConfiguredManager(),
|
||||||
|
DatabaseTemplateStore(store), store,
|
||||||
|
))
|
||||||
|
monkeypatch.setattr(app_module, "Updater", FakeUpdater)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
app_module.subprocess, "run",
|
||||||
|
lambda argv, **_kwargs: SimpleNamespace(
|
||||||
|
returncode=3 if "is-active" in argv else 0,
|
||||||
|
stdout="inactive" if "is-active" in argv else "",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
settings = Settings(data_dir=data_dir,
|
||||||
|
static_dir=Path(__file__).resolve().parents[1] / "static")
|
||||||
|
app = app_module.create_app(settings, service_factory=lambda _profile: BlockingVMService())
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def observe_update(request, call_next):
|
||||||
|
if request.url.path == "/api/v1/update/apply":
|
||||||
|
update_received.set()
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
|
token = (data_dir / "setup-token").read_text(encoding="ascii").strip()
|
||||||
|
with TestClient(app, base_url="https://panel.example.test") as client:
|
||||||
|
configured = client.post("/api/v1/database/setup", json={
|
||||||
|
"host": "127.0.0.1", "port": 3306, "database": "virsh_panel",
|
||||||
|
"username": "panel", "password": "test-database-password",
|
||||||
|
"setup_token": token,
|
||||||
|
})
|
||||||
|
assert configured.status_code == 200
|
||||||
|
admin = client.post("/api/v1/setup/admin", json={
|
||||||
|
"token": token, "username": "admin",
|
||||||
|
"password": "correct-horse-battery-staple",
|
||||||
|
})
|
||||||
|
assert admin.status_code == 200
|
||||||
|
headers = {"X-CSRF-Token": admin.json()["csrf_token"]}
|
||||||
|
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||||
|
creating = executor.submit(client.post, "/api/v1/vms", json={
|
||||||
|
"name": "new-vm", "vcpu": 2, "memory_mib": 2048,
|
||||||
|
"disk_gib": 20, "network": "default", "iso": "AlmaLinux-10.iso",
|
||||||
|
}, headers=headers)
|
||||||
|
assert vm_started.wait(timeout=5)
|
||||||
|
updating = executor.submit(client.post, "/api/v1/update/apply", json={
|
||||||
|
"expected_current_commit": "a" * 40,
|
||||||
|
"expected_remote_commit": "b" * 40,
|
||||||
|
}, headers=headers)
|
||||||
|
try:
|
||||||
|
assert update_received.wait(timeout=5)
|
||||||
|
assert not update_queued.wait(timeout=0.2)
|
||||||
|
assert not updating.done()
|
||||||
|
finally:
|
||||||
|
release_vm.set()
|
||||||
|
assert creating.result(timeout=5).status_code == 201
|
||||||
|
assert updating.result(timeout=5).status_code == 202
|
||||||
|
assert update_queued.is_set()
|
||||||
|
blocked = client.post("/api/v1/vms", json={
|
||||||
|
"name": "another-vm", "vcpu": 2, "memory_mib": 2048,
|
||||||
|
"disk_gib": 20, "network": "default", "iso": "AlmaLinux-10.iso",
|
||||||
|
}, headers=headers)
|
||||||
|
assert blocked.status_code == 409
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
"""The systemd post-stop hook must resolve interrupted update jobs."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from virsh_panel import update_worker
|
||||||
|
|
||||||
|
|
||||||
|
class FakeStore:
|
||||||
|
def __init__(self, job_id: str | None):
|
||||||
|
self.job_id = job_id
|
||||||
|
|
||||||
|
def get_value(self, name: str) -> str | None:
|
||||||
|
if name == "update.active_job":
|
||||||
|
return self.job_id
|
||||||
|
raise AssertionError(name)
|
||||||
|
|
||||||
|
|
||||||
|
class FakeUpdater:
|
||||||
|
def __init__(self, job: dict | None):
|
||||||
|
self.job = job
|
||||||
|
self.failed: list[tuple[str, str]] = []
|
||||||
|
|
||||||
|
def get_job(self, _job_id: str) -> dict | None:
|
||||||
|
return self.job
|
||||||
|
|
||||||
|
def fail_job(self, job_id: str, message: str) -> None:
|
||||||
|
self.failed.append((job_id, message))
|
||||||
|
if self.job is not None:
|
||||||
|
self.job["status"] = "failed"
|
||||||
|
|
||||||
|
|
||||||
|
class FinalizeIncompleteTests(unittest.TestCase):
|
||||||
|
def test_running_job_is_marked_failed_once(self):
|
||||||
|
store = FakeStore("job-1234567890123456")
|
||||||
|
updater = FakeUpdater({"status": "running"})
|
||||||
|
self.assertTrue(update_worker.finalize_incomplete_job(updater, store))
|
||||||
|
self.assertEqual(updater.job["status"], "failed")
|
||||||
|
self.assertEqual(len(updater.failed), 1)
|
||||||
|
self.assertIn("日志", updater.failed[0][1])
|
||||||
|
self.assertFalse(update_worker.finalize_incomplete_job(updater, store))
|
||||||
|
self.assertEqual(len(updater.failed), 1)
|
||||||
|
|
||||||
|
def test_queued_job_is_marked_failed(self):
|
||||||
|
store = FakeStore("job-1234567890123456")
|
||||||
|
updater = FakeUpdater({"status": "queued"})
|
||||||
|
self.assertTrue(update_worker.finalize_incomplete_job(updater, store))
|
||||||
|
self.assertEqual(updater.job["status"], "failed")
|
||||||
|
|
||||||
|
def test_terminal_or_missing_job_is_not_modified(self):
|
||||||
|
for status in ("succeeded", "failed"):
|
||||||
|
with self.subTest(status=status):
|
||||||
|
updater = FakeUpdater({"status": status})
|
||||||
|
self.assertFalse(update_worker.finalize_incomplete_job(
|
||||||
|
updater, FakeStore("job-1234567890123456")))
|
||||||
|
self.assertEqual(updater.failed, [])
|
||||||
|
self.assertFalse(update_worker.finalize_incomplete_job(
|
||||||
|
FakeUpdater(None), FakeStore("job-1234567890123456")))
|
||||||
|
self.assertFalse(update_worker.finalize_incomplete_job(
|
||||||
|
FakeUpdater({"status": "running"}), FakeStore(None)))
|
||||||
|
|
||||||
|
def test_cli_finalize_mode_does_not_run_update(self):
|
||||||
|
store = FakeStore("job-1234567890123456")
|
||||||
|
updater = FakeUpdater({"status": "running"})
|
||||||
|
with patch.object(update_worker, "_load_updater", return_value=(updater, store)):
|
||||||
|
self.assertEqual(update_worker.main(["--finalize-incomplete"]), 0)
|
||||||
|
self.assertEqual(updater.job["status"], "failed")
|
||||||
|
|
||||||
|
def test_service_invokes_post_stop_finalizer(self):
|
||||||
|
unit = (Path(__file__).resolve().parent.parent / "deploy" /
|
||||||
|
"virsh-panel-update.service").read_text(encoding="utf-8")
|
||||||
|
self.assertIn(
|
||||||
|
"ExecStopPost=/home/virsh-panel/.venv/bin/python "
|
||||||
|
"-m virsh_panel.update_worker --finalize-incomplete",
|
||||||
|
unit,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@ -0,0 +1,255 @@
|
|||||||
|
"""Offline checks for the private-repository update safety boundary."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from virsh_panel.updater import REPOSITORY_URL, UpdateError, Updater
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryStore:
|
||||||
|
def __init__(self):
|
||||||
|
self.secrets: dict[str, str] = {}
|
||||||
|
self.values: dict[str, str] = {}
|
||||||
|
|
||||||
|
def get_secret(self, name):
|
||||||
|
return self.secrets.get(name)
|
||||||
|
|
||||||
|
def set_secret(self, name, value):
|
||||||
|
self.secrets[name] = value
|
||||||
|
|
||||||
|
def delete_secret(self, name):
|
||||||
|
self.secrets.pop(name, None)
|
||||||
|
|
||||||
|
def get_value(self, name):
|
||||||
|
return self.values.get(name)
|
||||||
|
|
||||||
|
def set_value(self, name, value):
|
||||||
|
self.values[name] = value
|
||||||
|
|
||||||
|
|
||||||
|
def git(path: Path, *args: str) -> str:
|
||||||
|
result = subprocess.run(
|
||||||
|
["git", *args], cwd=path, capture_output=True, text=True, check=True,
|
||||||
|
)
|
||||||
|
return result.stdout.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def make_repo(tmp_path: Path) -> Path:
|
||||||
|
source = tmp_path / "source"
|
||||||
|
source.mkdir()
|
||||||
|
git(source, "init", "-b", "main")
|
||||||
|
git(source, "config", "user.name", "Updater Test")
|
||||||
|
git(source, "config", "user.email", "updater@example.test")
|
||||||
|
(source / "README.md").write_text("old\n", encoding="utf-8")
|
||||||
|
(source / "requirements.txt").write_text("example-package==1.0\n", encoding="utf-8")
|
||||||
|
git(source, "add", "README.md", "requirements.txt")
|
||||||
|
git(source, "commit", "-m", "initial")
|
||||||
|
checkout = tmp_path / "panel"
|
||||||
|
subprocess.run(
|
||||||
|
["git", "clone", "--branch", "main", str(source), str(checkout)],
|
||||||
|
capture_output=True, text=True, check=True,
|
||||||
|
)
|
||||||
|
git(checkout, "remote", "set-url", "origin", REPOSITORY_URL)
|
||||||
|
return checkout
|
||||||
|
|
||||||
|
|
||||||
|
def advance_source(source: Path, *, filename: str = "README.md") -> str:
|
||||||
|
(source / filename).parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
(source / filename).write_text("new\n", encoding="utf-8")
|
||||||
|
git(source, "add", filename)
|
||||||
|
git(source, "commit", "-m", "next")
|
||||||
|
return git(source, "rev-parse", "HEAD")
|
||||||
|
|
||||||
|
|
||||||
|
def local_fetch(updater: Updater, source: Path):
|
||||||
|
def fetch() -> str:
|
||||||
|
git(updater.repo_dir, "fetch", "--no-tags", str(source), "refs/heads/main")
|
||||||
|
return git(updater.repo_dir, "rev-parse", "FETCH_HEAD")
|
||||||
|
|
||||||
|
updater._fetch_remote = fetch
|
||||||
|
updater._install_requirements = lambda _target: None
|
||||||
|
|
||||||
|
|
||||||
|
class UpdaterTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.temp = tempfile.TemporaryDirectory(prefix="virsh-panel-updater-test-")
|
||||||
|
self.addCleanup(self.temp.cleanup)
|
||||||
|
self.tmp_path = Path(self.temp.name)
|
||||||
|
|
||||||
|
def test_settings_and_token_never_reveal_secret(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
store = MemoryStore()
|
||||||
|
updater = Updater(checkout, store)
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
settings = updater.settings()
|
||||||
|
self.assertEqual(settings["repository"], REPOSITORY_URL)
|
||||||
|
self.assertTrue(settings["token_configured"])
|
||||||
|
self.assertTrue(settings["checkout_ready"])
|
||||||
|
self.assertIsNone(settings["active_job_id"])
|
||||||
|
self.assertIsNone(settings["last_job_id"])
|
||||||
|
self.assertNotIn("a-test-access-token-123", repr(settings))
|
||||||
|
with updater._git_auth() as env:
|
||||||
|
self.assertEqual(env["VIRSH_PANEL_GIT_TOKEN"], "a-test-access-token-123")
|
||||||
|
self.assertNotIn("a-test-access-token-123", Path(env["GIT_ASKPASS"]).read_text())
|
||||||
|
updater.clear_token()
|
||||||
|
self.assertFalse(updater.settings()["token_configured"])
|
||||||
|
|
||||||
|
def test_check_is_read_only_and_reports_new_version(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
current = git(checkout, "rev-parse", "HEAD")
|
||||||
|
updater._remote_head = lambda: current
|
||||||
|
self.assertEqual(updater.check()["status"], "current")
|
||||||
|
updater._remote_head = lambda: "a" * 40
|
||||||
|
self.assertEqual(updater.check()["status"], "update_available")
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), current)
|
||||||
|
|
||||||
|
def test_zip_or_dirty_checkout_is_rejected(self):
|
||||||
|
plain = self.tmp_path / "plain"
|
||||||
|
plain.mkdir()
|
||||||
|
with self.assertRaisesRegex(UpdateError, "Git"):
|
||||||
|
Updater(plain, MemoryStore()).check()
|
||||||
|
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
(checkout / "README.md").write_text("user changes\n", encoding="utf-8")
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
with self.assertRaisesRegex(UpdateError, "未提交"):
|
||||||
|
updater.create_job()
|
||||||
|
with self.assertRaisesRegex(UpdateError, "未提交"):
|
||||||
|
updater.apply()
|
||||||
|
|
||||||
|
def test_fast_forward_job_is_persisted_and_calls_restart(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
old = git(checkout, "rev-parse", "HEAD")
|
||||||
|
target = advance_source(source)
|
||||||
|
store = MemoryStore()
|
||||||
|
updater = Updater(checkout, store)
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
local_fetch(updater, source)
|
||||||
|
queued = updater.create_job()
|
||||||
|
self.assertEqual(updater.settings()["active_job_id"], queued["job_id"])
|
||||||
|
restarted = []
|
||||||
|
result = updater.run_pending_job(restart=lambda: restarted.append(True))
|
||||||
|
self.assertEqual(result["status"], "succeeded")
|
||||||
|
self.assertEqual(result["old_commit"], old)
|
||||||
|
self.assertEqual(result["new_commit"], target)
|
||||||
|
self.assertTrue(result["rollback_ref"].startswith("refs/virsh-panel/rollback/"))
|
||||||
|
self.assertEqual(updater.get_job(queued["job_id"]), result)
|
||||||
|
self.assertEqual(updater.settings()["last_job_id"], queued["job_id"])
|
||||||
|
self.assertIsNone(updater.settings()["active_job_id"])
|
||||||
|
self.assertEqual(restarted, [True])
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), target)
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", result["rollback_ref"]), old)
|
||||||
|
|
||||||
|
def test_remote_history_rewrite_never_overwrites_local_code(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
target = advance_source(source)
|
||||||
|
git(checkout, "config", "user.name", "Updater Test")
|
||||||
|
git(checkout, "config", "user.email", "updater@example.test")
|
||||||
|
(checkout / "README.md").write_text("local fork\n", encoding="utf-8")
|
||||||
|
git(checkout, "add", "README.md")
|
||||||
|
git(checkout, "commit", "-m", "local fork")
|
||||||
|
local = git(checkout, "rev-parse", "HEAD")
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
local_fetch(updater, source)
|
||||||
|
with self.assertRaisesRegex(UpdateError, "分叉"):
|
||||||
|
updater.apply()
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), local)
|
||||||
|
self.assertNotEqual(local, target)
|
||||||
|
|
||||||
|
def test_failed_dependency_sync_leaves_code_unchanged(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
advance_source(source)
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
local_fetch(updater, source)
|
||||||
|
old = git(checkout, "rev-parse", "HEAD")
|
||||||
|
|
||||||
|
def fail_install(_target):
|
||||||
|
raise UpdateError("依赖失败", 502)
|
||||||
|
|
||||||
|
updater._install_requirements = fail_install
|
||||||
|
with self.assertRaisesRegex(UpdateError, "依赖失败"):
|
||||||
|
updater.apply()
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), old)
|
||||||
|
|
||||||
|
def test_git_url_rewrite_is_rejected_before_sending_token(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
git(checkout, "config", "url.https://evil.example/.insteadOf", "https://g.luoli.live/")
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
with self.assertRaisesRegex(UpdateError, "URL 重写"):
|
||||||
|
updater.check()
|
||||||
|
|
||||||
|
def test_new_code_cannot_track_private_data_or_submodules(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
target = advance_source(source, filename="data/master.key")
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
local_fetch(updater, source)
|
||||||
|
old = git(checkout, "rev-parse", "HEAD")
|
||||||
|
with self.assertRaisesRegex(UpdateError, "数据目录"):
|
||||||
|
updater.apply()
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), old)
|
||||||
|
self.assertNotEqual(old, target)
|
||||||
|
|
||||||
|
def test_failed_restart_is_persisted_without_hiding_applied_commit(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
target = advance_source(source)
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
local_fetch(updater, source)
|
||||||
|
job = updater.create_job()
|
||||||
|
|
||||||
|
def fail_restart():
|
||||||
|
raise UpdateError("重启失败", 500)
|
||||||
|
|
||||||
|
with self.assertRaisesRegex(UpdateError, "重启失败"):
|
||||||
|
updater.run_pending_job(restart=fail_restart)
|
||||||
|
stored = updater.get_job(job["job_id"])
|
||||||
|
self.assertEqual(stored["status"], "failed")
|
||||||
|
self.assertEqual(stored["new_commit"], target)
|
||||||
|
self.assertTrue(stored["rollback_ref"])
|
||||||
|
self.assertIn("代码可能已切换", stored["error"])
|
||||||
|
|
||||||
|
def test_checked_remote_commit_is_pinned_through_worker_execution(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
source = self.tmp_path / "source"
|
||||||
|
old = git(checkout, "rev-parse", "HEAD")
|
||||||
|
checked = advance_source(source)
|
||||||
|
store = MemoryStore()
|
||||||
|
updater = Updater(checkout, store)
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
updater._remote_head = lambda: checked
|
||||||
|
local_fetch(updater, source)
|
||||||
|
queued = updater.create_job(
|
||||||
|
expected_current_commit=old,
|
||||||
|
expected_remote_commit=checked,
|
||||||
|
)
|
||||||
|
advance_source(source, filename="VERSION")
|
||||||
|
with self.assertRaisesRegex(UpdateError, "main 分支已变化"):
|
||||||
|
updater.run_pending_job(restart=lambda: None)
|
||||||
|
self.assertEqual(git(checkout, "rev-parse", "HEAD"), old)
|
||||||
|
self.assertEqual(updater.get_job(queued["job_id"])["status"], "failed")
|
||||||
|
|
||||||
|
def test_create_job_rejects_changed_remote_since_check(self):
|
||||||
|
checkout = make_repo(self.tmp_path)
|
||||||
|
updater = Updater(checkout, MemoryStore())
|
||||||
|
updater.set_token("a-test-access-token-123")
|
||||||
|
updater._remote_head = lambda: "b" * 40
|
||||||
|
with self.assertRaisesRegex(UpdateError, "main 分支已变化"):
|
||||||
|
updater.create_job(
|
||||||
|
expected_current_commit=git(checkout, "rev-parse", "HEAD"),
|
||||||
|
expected_remote_commit="a" * 40,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@ -0,0 +1,698 @@
|
|||||||
|
"""MariaDB-backed panel state with authenticated encryption at rest.
|
||||||
|
|
||||||
|
The database is the durable source of truth. OpenSSH still needs ordinary
|
||||||
|
files while it runs, so the remote-host adapter builds those in a private
|
||||||
|
tmpfs directory on Linux and removes them when the panel stops. Only a
|
||||||
|
non-secret executable wrapper is written to the panel's data directory.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hmac
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import secrets
|
||||||
|
import shutil
|
||||||
|
import stat
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Mapping
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pymysql
|
||||||
|
except ImportError: # Allows package tooling to import without optional DB extras.
|
||||||
|
pymysql = None
|
||||||
|
|
||||||
|
from .auth import hash_password
|
||||||
|
from .cloud_templates import TemplateError
|
||||||
|
from .panel_state import PanelState, SetupError, _USERNAME
|
||||||
|
from .remote_host import (
|
||||||
|
LIBVIRT_URI,
|
||||||
|
SSH_ALIAS,
|
||||||
|
RemoteHostError,
|
||||||
|
RemoteHostManager,
|
||||||
|
StagedHost,
|
||||||
|
_fingerprint,
|
||||||
|
_key_parts,
|
||||||
|
_quoted_path,
|
||||||
|
_validate_host,
|
||||||
|
_validate_network_selector,
|
||||||
|
_validate_pool_selector,
|
||||||
|
_validate_port,
|
||||||
|
_validate_private_key,
|
||||||
|
_write_private,
|
||||||
|
)
|
||||||
|
from .ssh_agent import SSHAgentSession, _ASKPASS_SOURCE
|
||||||
|
|
||||||
|
|
||||||
|
_RECORD_NAME = re.compile(r"[A-Za-z0-9_.:-]{1,191}\Z")
|
||||||
|
_RUNTIME_NAME = re.compile(r"v([0-9]+)-[0-9a-f]{4}\Z")
|
||||||
|
_ENVELOPE_VERSION = b"vp1"
|
||||||
|
_SCHEMA = """
|
||||||
|
CREATE TABLE IF NOT EXISTS panel_records (
|
||||||
|
record_type VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
|
||||||
|
record_key VARCHAR(191) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
|
||||||
|
encrypted_value LONGBLOB NOT NULL,
|
||||||
|
PRIMARY KEY (record_type, record_key)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseError(Exception):
|
||||||
|
"""A database failure that does not echo connection credentials."""
|
||||||
|
|
||||||
|
def __init__(self, message: str = "数据库连接或读写失败,请检查数据库配置和服务状态") -> None:
|
||||||
|
super().__init__(message)
|
||||||
|
self.status = 503
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseStore:
|
||||||
|
"""Small encrypted record store backed by a single InnoDB table.
|
||||||
|
|
||||||
|
A unique `(record_type, record_key)` key makes insert-only operations
|
||||||
|
atomic across panel processes. Each record uses a fresh AES-GCM nonce and
|
||||||
|
binds the ciphertext to its database key through associated data.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, config: Mapping[str, Any], master_key: bytes) -> None:
|
||||||
|
if pymysql is None:
|
||||||
|
raise DatabaseError("缺少 PyMySQL 依赖,请安装面板 requirements.txt")
|
||||||
|
if not isinstance(master_key, bytes) or len(master_key) != 32:
|
||||||
|
raise ValueError("Database master key must be 32 bytes")
|
||||||
|
if not isinstance(config, Mapping):
|
||||||
|
raise ValueError("Database configuration is invalid")
|
||||||
|
try:
|
||||||
|
host = config["host"]
|
||||||
|
port = config["port"]
|
||||||
|
database = config["database"]
|
||||||
|
username = config["username"]
|
||||||
|
password = config["password"]
|
||||||
|
except KeyError as exc:
|
||||||
|
raise ValueError("Database configuration is incomplete") from exc
|
||||||
|
if (not isinstance(host, str) or not 1 <= len(host) <= 253
|
||||||
|
or any(char in host for char in "\r\n\x00")):
|
||||||
|
raise ValueError("Database host is invalid")
|
||||||
|
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
|
||||||
|
raise ValueError("Database port is invalid")
|
||||||
|
if (not isinstance(database, str) or not re.fullmatch(r"[A-Za-z0-9_]{1,64}", database)
|
||||||
|
or not isinstance(username, str) or not 1 <= len(username) <= 128
|
||||||
|
or any(char in username for char in "\r\n\x00")
|
||||||
|
or not isinstance(password, str)):
|
||||||
|
raise ValueError("Database credentials are invalid")
|
||||||
|
self._config = {
|
||||||
|
"host": host,
|
||||||
|
"port": port,
|
||||||
|
"database": database,
|
||||||
|
"user": username,
|
||||||
|
"password": password,
|
||||||
|
"charset": "utf8mb4",
|
||||||
|
"cursorclass": pymysql.cursors.DictCursor,
|
||||||
|
"connect_timeout": 5,
|
||||||
|
"read_timeout": 10,
|
||||||
|
"write_timeout": 10,
|
||||||
|
"autocommit": False,
|
||||||
|
}
|
||||||
|
self._cipher = AESGCM(master_key)
|
||||||
|
self._initialize_schema()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_key(record_type: str, record_key: str) -> None:
|
||||||
|
if (not isinstance(record_type, str) or len(record_type) > 32
|
||||||
|
or not _RECORD_NAME.fullmatch(record_type)
|
||||||
|
or not isinstance(record_key, str) or not _RECORD_NAME.fullmatch(record_key)):
|
||||||
|
raise ValueError("Database record name is invalid")
|
||||||
|
|
||||||
|
def _connect(self):
|
||||||
|
try:
|
||||||
|
return pymysql.connect(**self._config)
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
|
||||||
|
def _initialize_schema(self) -> None:
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(_SCHEMA)
|
||||||
|
connection.commit()
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
connection.rollback()
|
||||||
|
raise DatabaseError("无法初始化数据库表,请检查账号建表权限") from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def _encrypt(self, record_type: str, record_key: str, value: str) -> bytes:
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise TypeError("Database value must be a string")
|
||||||
|
nonce = secrets.token_bytes(12)
|
||||||
|
associated_data = f"{record_type}\x00{record_key}".encode("ascii")
|
||||||
|
return _ENVELOPE_VERSION + nonce + self._cipher.encrypt(
|
||||||
|
nonce, value.encode("utf-8"), associated_data,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _decrypt(self, record_type: str, record_key: str, payload: bytes) -> str:
|
||||||
|
if not isinstance(payload, (bytes, bytearray)) or not payload.startswith(_ENVELOPE_VERSION):
|
||||||
|
raise DatabaseError("数据库密文格式无效或主密钥不匹配")
|
||||||
|
nonce = bytes(payload[3:15])
|
||||||
|
ciphertext = bytes(payload[15:])
|
||||||
|
if len(nonce) != 12 or len(ciphertext) < 16:
|
||||||
|
raise DatabaseError("数据库密文格式无效或主密钥不匹配")
|
||||||
|
try:
|
||||||
|
return self._cipher.decrypt(
|
||||||
|
nonce, ciphertext, f"{record_type}\x00{record_key}".encode("ascii"),
|
||||||
|
).decode("utf-8")
|
||||||
|
except (ValueError, UnicodeError) as exc:
|
||||||
|
raise DatabaseError("数据库密文格式无效或主密钥不匹配") from exc
|
||||||
|
except Exception as exc:
|
||||||
|
# InvalidTag does not reveal whether the key or ciphertext is wrong.
|
||||||
|
raise DatabaseError("数据库密文格式无效或主密钥不匹配") from exc
|
||||||
|
|
||||||
|
def get(self, record_type: str, record_key: str) -> str | None:
|
||||||
|
self._validate_key(record_type, record_key)
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT encrypted_value FROM panel_records "
|
||||||
|
"WHERE record_type=%s AND record_key=%s",
|
||||||
|
(record_type, record_key),
|
||||||
|
)
|
||||||
|
row = cursor.fetchone()
|
||||||
|
return None if row is None else self._decrypt(
|
||||||
|
record_type, record_key, row["encrypted_value"],
|
||||||
|
)
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def set(self, record_type: str, record_key: str, value: str) -> None:
|
||||||
|
self._validate_key(record_type, record_key)
|
||||||
|
payload = self._encrypt(record_type, record_key, value)
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO panel_records (record_type, record_key, encrypted_value) "
|
||||||
|
"VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE encrypted_value=VALUES(encrypted_value)",
|
||||||
|
(record_type, record_key, payload),
|
||||||
|
)
|
||||||
|
connection.commit()
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
connection.rollback()
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def insert(self, record_type: str, record_key: str, value: str) -> bool:
|
||||||
|
"""Insert once; return False if another process already inserted it."""
|
||||||
|
self._validate_key(record_type, record_key)
|
||||||
|
payload = self._encrypt(record_type, record_key, value)
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"INSERT INTO panel_records "
|
||||||
|
"(record_type, record_key, encrypted_value) VALUES (%s, %s, %s)",
|
||||||
|
(record_type, record_key, payload),
|
||||||
|
)
|
||||||
|
connection.commit()
|
||||||
|
return True
|
||||||
|
except pymysql.err.IntegrityError as exc:
|
||||||
|
connection.rollback()
|
||||||
|
if exc.args and exc.args[0] == 1062:
|
||||||
|
return False
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
connection.rollback()
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def delete(self, record_type: str, record_key: str) -> None:
|
||||||
|
self._validate_key(record_type, record_key)
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"DELETE FROM panel_records WHERE record_type=%s AND record_key=%s",
|
||||||
|
(record_type, record_key),
|
||||||
|
)
|
||||||
|
connection.commit()
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
connection.rollback()
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def list(self, record_type: str) -> dict[str, str]:
|
||||||
|
self._validate_key(record_type, "x")
|
||||||
|
connection = self._connect()
|
||||||
|
try:
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
cursor.execute(
|
||||||
|
"SELECT record_key, encrypted_value FROM panel_records WHERE record_type=%s",
|
||||||
|
(record_type,),
|
||||||
|
)
|
||||||
|
rows = cursor.fetchall()
|
||||||
|
return {
|
||||||
|
row["record_key"]: self._decrypt(
|
||||||
|
record_type, row["record_key"], row["encrypted_value"],
|
||||||
|
) for row in rows
|
||||||
|
}
|
||||||
|
except pymysql.MySQLError as exc:
|
||||||
|
raise DatabaseError() from exc
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def get_secret(self, name: str) -> str | None:
|
||||||
|
return self.get("secret", name)
|
||||||
|
|
||||||
|
def set_secret(self, name: str, value: str) -> None:
|
||||||
|
self.set("secret", name, value)
|
||||||
|
|
||||||
|
def delete_secret(self, name: str) -> None:
|
||||||
|
self.delete("secret", name)
|
||||||
|
|
||||||
|
def get_value(self, name: str) -> str | None:
|
||||||
|
return self.get("value", name)
|
||||||
|
|
||||||
|
def set_value(self, name: str, value: str) -> None:
|
||||||
|
self.set("value", name, value)
|
||||||
|
|
||||||
|
def delete_value(self, name: str) -> None:
|
||||||
|
self.delete("value", name)
|
||||||
|
|
||||||
|
|
||||||
|
class DatabasePanelState(PanelState):
|
||||||
|
"""Admin in MariaDB; the one-time bootstrap token remains a local file."""
|
||||||
|
|
||||||
|
def __init__(self, data_dir: Path, store: DatabaseStore) -> None:
|
||||||
|
super().__init__(data_dir)
|
||||||
|
self.store = store
|
||||||
|
|
||||||
|
def admin(self) -> tuple[str, str] | None:
|
||||||
|
raw = self.store.get("admin", "singleton")
|
||||||
|
if raw is None:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
record = json.loads(raw)
|
||||||
|
except (TypeError, ValueError) as exc:
|
||||||
|
raise DatabaseError("数据库管理员记录格式无效") from exc
|
||||||
|
if (not isinstance(record, dict) or not isinstance(record.get("username"), str)
|
||||||
|
or not isinstance(record.get("password_hash"), str)):
|
||||||
|
raise DatabaseError("数据库管理员记录格式无效")
|
||||||
|
return record["username"], record["password_hash"]
|
||||||
|
|
||||||
|
def create_admin(self, token: str, username: str, password: str) -> str:
|
||||||
|
if not isinstance(username, str) or not _USERNAME.fullmatch(username):
|
||||||
|
raise SetupError("用户名应为 3 至 32 位字母、数字、点、下划线或连字符")
|
||||||
|
if not isinstance(password, str) or len(password) < 12 or len(password) > 1024:
|
||||||
|
raise SetupError("管理员密码长度须在 12 至 1024 位之间")
|
||||||
|
with self._setup_lock():
|
||||||
|
if self.admin() is not None:
|
||||||
|
raise SetupError("管理员已创建", 409)
|
||||||
|
try:
|
||||||
|
expected = self._read_token()
|
||||||
|
except FileNotFoundError as exc:
|
||||||
|
raise SetupError("初始化口令不可用,请重新启动面板并查看启动终端输出", 503) from exc
|
||||||
|
if not isinstance(token, str) or not hmac.compare_digest(token, expected):
|
||||||
|
raise SetupError("初始化口令无效", 403)
|
||||||
|
record = json.dumps(
|
||||||
|
{"username": username, "password_hash": hash_password(password)},
|
||||||
|
ensure_ascii=False, separators=(",", ":"),
|
||||||
|
)
|
||||||
|
if not self.store.insert("admin", "singleton", record):
|
||||||
|
raise SetupError("管理员已创建", 409)
|
||||||
|
self.token_file.unlink(missing_ok=True)
|
||||||
|
self._fsync_directory()
|
||||||
|
return username
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseTemplateStore:
|
||||||
|
"""Cloud-image catalog records stored independently in MariaDB."""
|
||||||
|
|
||||||
|
def __init__(self, store: DatabaseStore) -> None:
|
||||||
|
self.store = store
|
||||||
|
|
||||||
|
def list(self, scope: str) -> list[dict]:
|
||||||
|
prefix = f"{scope}:"
|
||||||
|
records = []
|
||||||
|
for key, payload in self.store.list("template").items():
|
||||||
|
if not key.startswith(prefix):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
record = json.loads(payload)
|
||||||
|
except (TypeError, ValueError) as exc:
|
||||||
|
raise TemplateError("数据库模板记录损坏", 500) from exc
|
||||||
|
if not isinstance(record, dict):
|
||||||
|
raise TemplateError("数据库模板记录损坏", 500)
|
||||||
|
records.append(record)
|
||||||
|
return records
|
||||||
|
|
||||||
|
def get(self, scope: str, template_id: str) -> dict:
|
||||||
|
for record in self.list(scope):
|
||||||
|
if record.get("id") == template_id:
|
||||||
|
return record
|
||||||
|
raise TemplateError("模板不存在或不属于当前宿主机", 404)
|
||||||
|
|
||||||
|
def add(self, scope: str, record: dict) -> dict:
|
||||||
|
if not isinstance(record, dict) or not isinstance(record.get("id"), str):
|
||||||
|
raise TemplateError("模板记录无效")
|
||||||
|
key = f"{scope}:{record['id']}"
|
||||||
|
if not self.store.insert("template", key, json.dumps(
|
||||||
|
record, ensure_ascii=False, separators=(",", ":"))):
|
||||||
|
raise TemplateError("此云镜像已导入", 409)
|
||||||
|
return record
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseStagedHost(StagedHost):
|
||||||
|
def __init__(self, staged: StagedHost, wrapper_path: Path) -> None:
|
||||||
|
super().__init__(staged.path, staged.metadata, staged.tested)
|
||||||
|
self.wrapper_path = wrapper_path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def libvirt_uri(self) -> str:
|
||||||
|
return f"{LIBVIRT_URI}&command={quote(str(self.wrapper_path), safe='/')}"
|
||||||
|
|
||||||
|
|
||||||
|
class _DatabaseSSHAgentSession(SSHAgentSession):
|
||||||
|
"""Keep the no-secret askpass executable off possibly-noexec tmpfs."""
|
||||||
|
|
||||||
|
def __init__(self, generation: Path, key_path: Path, passphrase: str,
|
||||||
|
helper_dir: Path) -> None:
|
||||||
|
self._helper_dir = helper_dir
|
||||||
|
super().__init__(generation, key_path, passphrase)
|
||||||
|
|
||||||
|
def _create_helper(self) -> Path:
|
||||||
|
helper = self._helper_dir / ("askpass-" + secrets.token_hex(8))
|
||||||
|
_write_private(helper, _ASKPASS_SOURCE)
|
||||||
|
helper.chmod(0o700)
|
||||||
|
self._helper_path = helper
|
||||||
|
return helper
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseRemoteHostManager(RemoteHostManager):
|
||||||
|
"""Existing SSH workflow, with encrypted DB persistence and tmpfs keys."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _process_start_ticks(pid: int) -> str | None:
|
||||||
|
"""Linux process birth marker, to distinguish a recycled PID."""
|
||||||
|
try:
|
||||||
|
content = Path(f"/proc/{pid}/stat").read_text(encoding="ascii")
|
||||||
|
fields = content[content.rfind(")") + 2:].split()
|
||||||
|
return fields[19] # /proc stat field 22; tail starts at field 3.
|
||||||
|
except (FileNotFoundError, IndexError, OSError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _clean_stale_runtime(cls, parent: Path) -> None:
|
||||||
|
"""Remove only our private, marked dirs whose exact owner process died."""
|
||||||
|
try:
|
||||||
|
entries = list(parent.iterdir())
|
||||||
|
except OSError:
|
||||||
|
return
|
||||||
|
for entry in entries:
|
||||||
|
match = _RUNTIME_NAME.fullmatch(entry.name)
|
||||||
|
if match is None:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
info = entry.lstat()
|
||||||
|
if (not stat.S_ISDIR(info.st_mode) or info.st_uid != os.geteuid()
|
||||||
|
or stat.S_IMODE(info.st_mode) != 0o700):
|
||||||
|
continue
|
||||||
|
marker = entry / ".owner"
|
||||||
|
marker_info = marker.lstat()
|
||||||
|
if (not stat.S_ISREG(marker_info.st_mode)
|
||||||
|
or marker_info.st_uid != os.geteuid()
|
||||||
|
or stat.S_IMODE(marker_info.st_mode) != 0o600):
|
||||||
|
continue
|
||||||
|
owner = marker.read_text(encoding="ascii").strip().split(":")
|
||||||
|
pid = int(match.group(1))
|
||||||
|
if len(owner) != 2 or owner[0] != str(pid) or not owner[1].isdigit():
|
||||||
|
continue
|
||||||
|
birth = cls._process_start_ticks(pid)
|
||||||
|
if birth is None or birth != owner[1]:
|
||||||
|
shutil.rmtree(entry)
|
||||||
|
except (OSError, UnicodeError, ValueError):
|
||||||
|
# Never prune a directory whose ownership cannot be proven.
|
||||||
|
continue
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, store: DatabaseStore, data_dir: Path, *, ssh_binary: str = "ssh",
|
||||||
|
runtime_parent: Path | None = None, **kwargs: Any,
|
||||||
|
) -> None:
|
||||||
|
self.store = store
|
||||||
|
if Path(data_dir).is_symlink():
|
||||||
|
raise RemoteHostError("面板数据目录不能是符号链接", 500)
|
||||||
|
self._persistent_dir = Path(data_dir).resolve()
|
||||||
|
self._persistent_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||||
|
if stat.S_IMODE(self._persistent_dir.stat().st_mode) & 0o077:
|
||||||
|
raise RemoteHostError("面板数据目录权限不安全", 500)
|
||||||
|
wrapper_dir = self._persistent_dir / "runtime-wrappers"
|
||||||
|
if wrapper_dir.is_symlink():
|
||||||
|
raise RemoteHostError("SSH 包装器目录不能是符号链接", 500)
|
||||||
|
wrapper_dir.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
if stat.S_IMODE(wrapper_dir.stat().st_mode) & 0o077:
|
||||||
|
raise RemoteHostError("SSH 包装器目录权限不安全", 500)
|
||||||
|
self._wrapper_dir = wrapper_dir
|
||||||
|
self._wrapper_paths: set[Path] = set()
|
||||||
|
if runtime_parent is None:
|
||||||
|
if sys.platform == "linux":
|
||||||
|
runtime_parent = Path("/dev/shm")
|
||||||
|
if not runtime_parent.is_dir() or not os.access(runtime_parent, os.W_OK):
|
||||||
|
raise RemoteHostError("/dev/shm 不可用,无法安全存放临时 SSH 密钥", 500)
|
||||||
|
else:
|
||||||
|
# A disk-backed fallback would leave the decrypted SSH key in
|
||||||
|
# data/ after an unclean shutdown. Tests may supply an
|
||||||
|
# explicit isolated runtime parent, but production requires
|
||||||
|
# the Linux tmpfs path above.
|
||||||
|
raise RemoteHostError("数据库模式仅支持 Linux 的 /dev/shm 临时密钥目录", 500)
|
||||||
|
if sys.platform == "linux":
|
||||||
|
self._clean_stale_runtime(Path(runtime_parent))
|
||||||
|
# SSHAgentSession requires a socket path under 100 bytes. The fixed
|
||||||
|
# `generations/gen-.../agent-...sock` suffix leaves only a tiny prefix
|
||||||
|
# budget under /dev/shm on Linux, so mkdtemp's eight-byte suffix is
|
||||||
|
# too long for systems with seven-digit PIDs.
|
||||||
|
self._runtime_dir = None
|
||||||
|
for _attempt in range(16):
|
||||||
|
candidate = Path(runtime_parent) / f"v{os.getpid()}-{secrets.token_hex(2)}"
|
||||||
|
try:
|
||||||
|
candidate.mkdir(mode=0o700)
|
||||||
|
except FileExistsError:
|
||||||
|
continue
|
||||||
|
self._runtime_dir = candidate
|
||||||
|
break
|
||||||
|
if self._runtime_dir is None:
|
||||||
|
raise RemoteHostError("无法创建独立的 SSH 临时目录", 500)
|
||||||
|
self._runtime_dir.chmod(0o700)
|
||||||
|
if sys.platform == "linux":
|
||||||
|
birth = self._process_start_ticks(os.getpid())
|
||||||
|
if birth is None:
|
||||||
|
shutil.rmtree(self._runtime_dir, ignore_errors=True)
|
||||||
|
raise RemoteHostError("无法确认面板进程身份,拒绝存放 SSH 密钥", 500)
|
||||||
|
_write_private(self._runtime_dir / ".owner",
|
||||||
|
f"{os.getpid()}:{birth}\n".encode("ascii"))
|
||||||
|
try:
|
||||||
|
kwargs.setdefault(
|
||||||
|
"agent_factory",
|
||||||
|
lambda generation, key_path, passphrase: _DatabaseSSHAgentSession(
|
||||||
|
generation, key_path, passphrase, self._wrapper_dir,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
super().__init__(
|
||||||
|
self._runtime_dir, ssh_binary=ssh_binary,
|
||||||
|
ssh_user_config_path=self._runtime_dir / ".ssh" / "config",
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
self._restore_current()
|
||||||
|
except BaseException:
|
||||||
|
shutil.rmtree(self._runtime_dir, ignore_errors=True)
|
||||||
|
for wrapper in self._wrapper_paths:
|
||||||
|
wrapper.unlink(missing_ok=True)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _wrapper_for(self, generation: Path) -> Path:
|
||||||
|
return self._wrapper_dir / generation.name
|
||||||
|
|
||||||
|
def _write_wrapper(self, generation: Path) -> Path:
|
||||||
|
destination = self._wrapper_for(generation)
|
||||||
|
source = (
|
||||||
|
"#!/usr/bin/env python3\n"
|
||||||
|
"import os\nimport sys\n"
|
||||||
|
f"os.execvp({self.ssh_binary!r}, "
|
||||||
|
f"[{self.ssh_binary!r}, '-F', {str(generation / 'config')!r}, *sys.argv[1:]])\n"
|
||||||
|
)
|
||||||
|
_write_private(destination, source.encode("ascii"))
|
||||||
|
destination.chmod(0o700)
|
||||||
|
self._wrapper_paths.add(destination)
|
||||||
|
return destination
|
||||||
|
|
||||||
|
def stage(self, profile, confirmed_fingerprint: str) -> DatabaseStagedHost:
|
||||||
|
staged = super().stage(profile, confirmed_fingerprint)
|
||||||
|
try:
|
||||||
|
wrapper = self._write_wrapper(staged.path)
|
||||||
|
except BaseException:
|
||||||
|
super().abort(staged)
|
||||||
|
raise
|
||||||
|
return DatabaseStagedHost(staged, wrapper)
|
||||||
|
|
||||||
|
def abort(self, staged: StagedHost) -> None:
|
||||||
|
super().abort(staged)
|
||||||
|
wrapper = self._wrapper_for(staged.path)
|
||||||
|
wrapper.unlink(missing_ok=True)
|
||||||
|
self._wrapper_paths.discard(wrapper)
|
||||||
|
|
||||||
|
def _staged_record(self, staged: StagedHost) -> dict:
|
||||||
|
try:
|
||||||
|
return {
|
||||||
|
"metadata": staged.metadata,
|
||||||
|
"private_key": (staged.path / "id_private").read_text(encoding="ascii"),
|
||||||
|
"known_hosts": (staged.path / "known_hosts").read_text(encoding="ascii"),
|
||||||
|
"identity_public": (
|
||||||
|
(staged.path / "identity.pub").read_text(encoding="ascii")
|
||||||
|
if (staged.path / "identity.pub").exists() else None
|
||||||
|
),
|
||||||
|
}
|
||||||
|
except (OSError, UnicodeError) as exc:
|
||||||
|
raise RemoteHostError("暂存宿主机密钥无法读取", 500) from exc
|
||||||
|
|
||||||
|
def commit(self, staged: StagedHost) -> dict[str, Any]:
|
||||||
|
self._check_staged(staged)
|
||||||
|
if not staged.tested:
|
||||||
|
raise RemoteHostError("保存前必须先测试宿主机连接", 409)
|
||||||
|
# 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,
|
||||||
|
separators=(",", ":"))
|
||||||
|
self.store.set("host", "current", payload)
|
||||||
|
try:
|
||||||
|
return super().commit(staged)
|
||||||
|
except BaseException:
|
||||||
|
if previous is None:
|
||||||
|
self.store.delete("host", "current")
|
||||||
|
else:
|
||||||
|
self.store.set("host", "current", previous)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _restore_current(self) -> None:
|
||||||
|
raw = self.store.get("host", "current")
|
||||||
|
if raw is None:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
record = json.loads(raw)
|
||||||
|
metadata = record["metadata"]
|
||||||
|
host = _validate_host(metadata["host"])
|
||||||
|
port = _validate_port(metadata["port"])
|
||||||
|
storage_pool = _validate_pool_selector(metadata["storage_pool"], "磁盘池")
|
||||||
|
iso_pool = _validate_pool_selector(metadata["iso_pool"], "ISO 池")
|
||||||
|
default_network = _validate_network_selector(metadata["default_network"])
|
||||||
|
private_key = _validate_private_key(record["private_key"])
|
||||||
|
known_hosts = record["known_hosts"].strip()
|
||||||
|
key_type, encoded = _key_parts(known_hosts)
|
||||||
|
if (not known_hosts.startswith(SSH_ALIAS + " ")
|
||||||
|
or metadata["fingerprint"] != _fingerprint(f"{key_type} {encoded}")):
|
||||||
|
raise ValueError("Host key mismatch")
|
||||||
|
encrypted = bool(metadata["private_key_encrypted"])
|
||||||
|
identity_public = record["identity_public"]
|
||||||
|
if encrypted and (not isinstance(identity_public, str)
|
||||||
|
or len(identity_public.split()) < 2):
|
||||||
|
raise ValueError("Encrypted host identity is missing")
|
||||||
|
except (KeyError, TypeError, ValueError, RemoteHostError, UnicodeError) as exc:
|
||||||
|
raise RemoteHostError("数据库宿主机配置损坏", 500) from exc
|
||||||
|
|
||||||
|
self._ensure_storage()
|
||||||
|
generation = self.data_dir / "generations" / ("gen-" + secrets.token_hex(16))
|
||||||
|
generation.mkdir(mode=0o700)
|
||||||
|
try:
|
||||||
|
_write_private(generation / "id_private", private_key)
|
||||||
|
_write_private(generation / "known_hosts", (known_hosts + "\n").encode("ascii"))
|
||||||
|
identity_path = generation / "id_private"
|
||||||
|
if encrypted:
|
||||||
|
_write_private(generation / "identity.pub", identity_public.encode("ascii"))
|
||||||
|
identity_path = generation / "identity.pub"
|
||||||
|
config_text = (
|
||||||
|
f"Host {SSH_ALIAS}\n"
|
||||||
|
f" HostName {host}\n"
|
||||||
|
f" Port {port}\n"
|
||||||
|
" User root\n"
|
||||||
|
f" IdentityFile {_quoted_path(identity_path)}\n"
|
||||||
|
f" UserKnownHostsFile {_quoted_path(generation / 'known_hosts')}\n"
|
||||||
|
" GlobalKnownHostsFile /dev/null\n"
|
||||||
|
f" HostKeyAlias {SSH_ALIAS}\n"
|
||||||
|
" HostKeyAlgorithms ssh-ed25519\n"
|
||||||
|
" StrictHostKeyChecking yes\n"
|
||||||
|
" VerifyHostKeyDNS no\n"
|
||||||
|
" BatchMode yes\n"
|
||||||
|
" IdentitiesOnly yes\n"
|
||||||
|
" IdentityAgent none\n"
|
||||||
|
" PreferredAuthentications publickey\n"
|
||||||
|
" PasswordAuthentication no\n"
|
||||||
|
" KbdInteractiveAuthentication no\n"
|
||||||
|
" GSSAPIAuthentication no\n"
|
||||||
|
" ForwardAgent no\n"
|
||||||
|
" ProxyCommand none\n"
|
||||||
|
" ProxyJump none\n"
|
||||||
|
" CanonicalizeHostname no\n"
|
||||||
|
" ControlMaster no\n"
|
||||||
|
" ControlPath none\n"
|
||||||
|
" UpdateHostKeys no\n"
|
||||||
|
" RequestTTY no\n"
|
||||||
|
" ServerAliveInterval 15\n"
|
||||||
|
" ServerAliveCountMax 2\n"
|
||||||
|
" ConnectTimeout 10\n"
|
||||||
|
)
|
||||||
|
_write_private(generation / "config", config_text.encode("ascii"))
|
||||||
|
_write_private(generation / "profile.json", json.dumps(
|
||||||
|
{**metadata, "host": host, "port": port,
|
||||||
|
"storage_pool": storage_pool, "iso_pool": iso_pool,
|
||||||
|
"default_network": default_network},
|
||||||
|
ensure_ascii=False,
|
||||||
|
).encode("utf-8"))
|
||||||
|
wrapper = self._write_wrapper(generation)
|
||||||
|
self._ensure_user_ssh_config()
|
||||||
|
temporary_link = self.data_dir / (".current-" + secrets.token_hex(8))
|
||||||
|
temporary_link.symlink_to(generation)
|
||||||
|
os.replace(temporary_link, self.data_dir / "current")
|
||||||
|
assert wrapper.exists()
|
||||||
|
except BaseException:
|
||||||
|
shutil.rmtree(generation, ignore_errors=True)
|
||||||
|
raise
|
||||||
|
|
||||||
|
@property
|
||||||
|
def libvirt_uri(self) -> str:
|
||||||
|
generation = self._current_generation()
|
||||||
|
if generation is None:
|
||||||
|
return LIBVIRT_URI
|
||||||
|
return f"{LIBVIRT_URI}&command={quote(str(self._wrapper_for(generation)), safe='/')}"
|
||||||
|
|
||||||
|
def current(self) -> dict[str, Any] | None:
|
||||||
|
profile = super().current()
|
||||||
|
if profile is None:
|
||||||
|
return None
|
||||||
|
profile["libvirt_uri"] = self.libvirt_uri
|
||||||
|
return profile
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
super().close()
|
||||||
|
shutil.rmtree(self._runtime_dir, ignore_errors=True)
|
||||||
|
for wrapper in self._wrapper_paths:
|
||||||
|
wrapper.unlink(missing_ok=True)
|
||||||
|
self._wrapper_paths.clear()
|
||||||
|
|
||||||
|
|
||||||
|
def make_components(
|
||||||
|
data_dir: Path, db_config: Mapping[str, Any], master_key: bytes,
|
||||||
|
ssh_binary: str = "ssh",
|
||||||
|
) -> tuple[DatabasePanelState, DatabaseRemoteHostManager, DatabaseTemplateStore, DatabaseStore]:
|
||||||
|
"""Create ready-to-use DB-backed application components."""
|
||||||
|
store = DatabaseStore(db_config, master_key)
|
||||||
|
state = DatabasePanelState(data_dir, store)
|
||||||
|
manager = DatabaseRemoteHostManager(store, data_dir, ssh_binary=ssh_binary)
|
||||||
|
templates = DatabaseTemplateStore(store)
|
||||||
|
return state, manager, templates, store
|
||||||
@ -0,0 +1,109 @@
|
|||||||
|
"""Entry point for the separate virsh-panel-update.service one-shot unit."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
from urllib.error import URLError
|
||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
|
from .config import PROJECT_DIR, Settings
|
||||||
|
from .database import DatabaseError, DatabaseStore
|
||||||
|
from .database_bootstrap import DatabaseBootstrap
|
||||||
|
from .updater import UpdateError, Updater
|
||||||
|
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
_WEB_UNIT = "virsh-panel.service"
|
||||||
|
_HEALTH_URL = "http://127.0.0.1:8000/api/v1/health"
|
||||||
|
|
||||||
|
|
||||||
|
def restart_web_service() -> None:
|
||||||
|
"""Restart the main unit and wait for its local health endpoint."""
|
||||||
|
try:
|
||||||
|
result = subprocess.run(
|
||||||
|
["/usr/bin/systemctl", "restart", _WEB_UNIT],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
timeout=90,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
except (OSError, subprocess.TimeoutExpired) as exc:
|
||||||
|
raise UpdateError("面板服务重启失败,请检查 systemd 日志", 500) from exc
|
||||||
|
if result.returncode != 0:
|
||||||
|
# systemctl output is intentionally not returned: it may contain
|
||||||
|
# environment details from another unit or configuration.
|
||||||
|
raise UpdateError("面板服务重启失败,请检查 systemd 日志", 500)
|
||||||
|
|
||||||
|
deadline = time.monotonic() + 45
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
try:
|
||||||
|
with urlopen(_HEALTH_URL, timeout=2) as response:
|
||||||
|
payload = json.load(response)
|
||||||
|
if response.status == 200 and isinstance(payload, dict) and payload.get("status") == "ok":
|
||||||
|
return
|
||||||
|
except (OSError, URLError, ValueError):
|
||||||
|
pass
|
||||||
|
time.sleep(1)
|
||||||
|
raise UpdateError("新版本服务未通过本机健康检查,请检查面板日志", 500)
|
||||||
|
|
||||||
|
|
||||||
|
def _load_updater() -> tuple[Updater, DatabaseStore]:
|
||||||
|
settings = Settings.from_env()
|
||||||
|
bootstrap = DatabaseBootstrap(settings.data_dir)
|
||||||
|
config = bootstrap.load_config()
|
||||||
|
if config is None:
|
||||||
|
raise UpdateError("尚未完成数据库配置,无法运行在线更新", 409)
|
||||||
|
store = DatabaseStore(config, bootstrap.master_key())
|
||||||
|
return Updater(PROJECT_DIR, store), store
|
||||||
|
|
||||||
|
|
||||||
|
def finalize_incomplete_job(updater: Updater, store: DatabaseStore) -> bool:
|
||||||
|
"""Mark an interrupted job failed after the one-shot unit has exited.
|
||||||
|
|
||||||
|
This is invoked by systemd ExecStopPost regardless of the main worker's
|
||||||
|
exit code. Successful/failed jobs are untouched. It is intentionally
|
||||||
|
idempotent so operators may rerun it while diagnosing a crashed worker.
|
||||||
|
"""
|
||||||
|
job_id = store.get_value("update.active_job")
|
||||||
|
if not job_id:
|
||||||
|
return False
|
||||||
|
job = updater.get_job(job_id)
|
||||||
|
if job is None or job.get("status") not in {"queued", "running"}:
|
||||||
|
return False
|
||||||
|
updater.fail_job(
|
||||||
|
job_id,
|
||||||
|
"独立更新服务异常退出或超时;请检查更新服务日志和当前 Git 版本,勿直接重复提交",
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] | None = None) -> int:
|
||||||
|
parser = argparse.ArgumentParser(description="Run or finalize the Virsh Panel update job")
|
||||||
|
parser.add_argument("--finalize-incomplete", action="store_true")
|
||||||
|
options = parser.parse_args(argv)
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
||||||
|
try:
|
||||||
|
updater, store = _load_updater()
|
||||||
|
if options.finalize_incomplete:
|
||||||
|
if finalize_incomplete_job(updater, store):
|
||||||
|
LOG.warning("interrupted panel update marked failed")
|
||||||
|
return 0
|
||||||
|
result = updater.run_pending_job(restart=restart_web_service)
|
||||||
|
except (UpdateError, DatabaseError, ValueError) as exc:
|
||||||
|
# Public errors are deliberately credential-free. Never log the DB
|
||||||
|
# config, Gitea PAT, subprocess output or captured environment.
|
||||||
|
LOG.error("panel update failed: %s", exc)
|
||||||
|
return 1
|
||||||
|
LOG.info(
|
||||||
|
"panel update finished: job=%s old=%s new=%s updated=%s",
|
||||||
|
result["job_id"], result.get("old_commit"), result.get("new_commit"), result.get("updated"),
|
||||||
|
)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
Loading…
Reference in New Issue