You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/virsh_panel/database.py

988 lines
42 KiB
Python

"""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 logging
import math
import os
import re
import secrets
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
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 .cloud_templates import TemplateError, validate_template_name
from .panel_state import PanelState, SetupError, _USERNAME
from .remote_host import (
LIBVIRT_URI,
SSH_ALIAS,
HostProfile,
RemoteHostError,
RemoteHostManager,
StagedHost,
_fingerprint,
_key_parts,
_quoted_path,
_validate_host,
_validate_network_selector,
_validate_pool_selector,
_validate_port,
_validate_private_key,
_validate_region,
_write_private,
validate_public_pool,
)
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
"""
_METRIC_SCHEMA = """
CREATE TABLE IF NOT EXISTS vm_metric_samples (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
sampled_at BIGINT UNSIGNED NOT NULL,
cpu_percent DOUBLE NULL,
memory_used_mib DOUBLE NULL,
memory_total_mib DOUBLE NULL,
disk_read_bps DOUBLE NULL,
disk_write_bps DOUBLE NULL,
network_rx_bps DOUBLE NULL,
network_tx_bps DOUBLE NULL,
PRIMARY KEY (host_scope, vm_uuid, sampled_at),
KEY vm_metric_retention (sampled_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_USERS_SCHEMA = """
CREATE TABLE IF NOT EXISTS users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(32) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
email VARCHAR(254) CHARACTER SET ascii COLLATE ascii_bin NULL,
password_hash VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
admin TINYINT(1) NOT NULL DEFAULT 0,
approved TINYINT(1) NOT NULL DEFAULT 0,
email_verified TINYINT(1) NOT NULL DEFAULT 0,
created_at BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_username (username),
UNIQUE KEY user_email (email)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_REGISTRATION_CODE_SCHEMA = """
CREATE TABLE IF NOT EXISTS registration_codes (
email VARCHAR(254) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
code_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
sent_at BIGINT UNSIGNED NOT NULL,
expires_at BIGINT UNSIGNED NOT NULL,
failed_attempts TINYINT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (email),
KEY code_expiry (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_VM_OWNERSHIP_SCHEMA = """
CREATE TABLE IF NOT EXISTS vm_ownership (
host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
created_at BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (host_scope, vm_uuid),
KEY vm_owner_user (user_id, host_scope),
CONSTRAINT vm_owner_user_fk FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_USER_SSH_KEYS_SCHEMA = """
CREATE TABLE IF NOT EXISTS user_ssh_keys (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT UNSIGNED NOT NULL,
name VARCHAR(80) NOT NULL,
public_key TEXT CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
fingerprint VARCHAR(80) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
created_at BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_ssh_key_fingerprint (user_id, fingerprint),
KEY user_ssh_key_listing (user_id, created_at),
CONSTRAINT user_ssh_key_user_fk FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
_METRIC_COLUMNS = (
"cpu_percent", "memory_used_mib", "memory_total_mib", "disk_read_bps",
"disk_write_bps", "network_rx_bps", "network_tx_bps",
)
_HOST_SCOPE = re.compile(r"[0-9a-f]{64}\Z")
_METRIC_PRUNE_BATCH_SIZE = 5000
LOG = logging.getLogger(__name__)
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)
cursor.execute(_METRIC_SCHEMA)
cursor.execute(_USERS_SCHEMA)
cursor.execute(_REGISTRATION_CODE_SCHEMA)
cursor.execute(_VM_OWNERSHIP_SCHEMA)
cursor.execute(_USER_SSH_KEYS_SCHEMA)
connection.commit()
except pymysql.MySQLError as exc:
connection.rollback()
raise DatabaseError("无法初始化数据库表,请检查账号建表权限") from exc
finally:
connection.close()
@staticmethod
def _metric_identity(host_scope: str, vm_id: str) -> str:
if not isinstance(host_scope, str) or not _HOST_SCOPE.fullmatch(host_scope):
raise ValueError("Metric host scope is invalid")
try:
canonical = str(uuid.UUID(vm_id))
except (TypeError, ValueError, AttributeError) as exc:
raise ValueError("Metric VM UUID is invalid") from exc
return canonical
def save_metric_samples(self, host_scope: str, points: list[dict[str, Any]]) -> None:
if not points:
return
rows = []
for point in points:
vm_id = self._metric_identity(host_scope, point["vm_id"])
timestamp = point["timestamp"]
if isinstance(timestamp, bool) or not isinstance(timestamp, int) or timestamp < 0:
raise ValueError("Metric timestamp is invalid")
metrics = []
for name in _METRIC_COLUMNS:
value = point.get(name)
if (value is not None and (isinstance(value, bool)
or not isinstance(value, (int, float))
or not math.isfinite(value) or value < 0)):
raise ValueError("Metric value is invalid")
metrics.append(value)
rows.append((host_scope, vm_id, timestamp, *metrics))
connection = self._connect()
try:
with connection.cursor() as cursor:
cursor.executemany(
"INSERT INTO vm_metric_samples (host_scope, vm_uuid, sampled_at, "
+ ", ".join(_METRIC_COLUMNS) + ") VALUES ("
+ ", ".join(["%s"] * (3 + len(_METRIC_COLUMNS))) + ") "
"ON DUPLICATE KEY UPDATE "
+ ", ".join(f"{name}=VALUES({name})" for name in _METRIC_COLUMNS),
rows,
)
connection.commit()
except pymysql.MySQLError as exc:
connection.rollback()
raise DatabaseError("无法保存虚拟机指标") from exc
finally:
connection.close()
def prune_metric_samples(self, cutoff: int) -> int:
if isinstance(cutoff, bool) or not isinstance(cutoff, int) or cutoff < 0:
raise ValueError("Metric retention cutoff is invalid")
connection = self._connect()
try:
with connection.cursor() as cursor:
cursor.execute(
"DELETE FROM vm_metric_samples WHERE sampled_at < %s "
"ORDER BY sampled_at LIMIT %s",
(cutoff, _METRIC_PRUNE_BATCH_SIZE),
)
removed = cursor.rowcount
connection.commit()
return max(0, removed)
except pymysql.MySQLError as exc:
connection.rollback()
raise DatabaseError("无法清理过期虚拟机指标") from exc
finally:
connection.close()
def query_metric_samples(self, host_scope: str, vm_id: str, *,
since: int, until: int, interval_seconds: int,
limit: int = 600) -> list[dict[str, Any]]:
vm_id = self._metric_identity(host_scope, vm_id)
if (any(isinstance(value, bool) or not isinstance(value, int)
for value in (since, until, interval_seconds, limit))
or since < 0 or until < since or interval_seconds < 60
or not 1 <= limit <= 600):
raise ValueError("Metric query range is invalid")
columns = ", ".join(f"AVG({name}) AS {name}" for name in _METRIC_COLUMNS)
connection = self._connect()
try:
with connection.cursor() as cursor:
cursor.execute(
"SELECT MAX(sampled_at) AS timestamp, " + columns
+ " FROM vm_metric_samples "
"WHERE host_scope=%s AND vm_uuid=%s AND sampled_at >= %s "
"AND sampled_at <= %s GROUP BY FLOOR(sampled_at / %s) "
"ORDER BY timestamp ASC LIMIT %s",
(host_scope, vm_id, since, until, interval_seconds, limit),
)
rows = cursor.fetchall()
return [
{"timestamp": int(row["timestamp"]),
**{name: float(row[name]) if row[name] is not None else None
for name in _METRIC_COLUMNS}}
for row in rows
]
except pymysql.MySQLError as exc:
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):
"""Initial user in MariaDB; the one-time token remains a local file."""
def __init__(self, data_dir: Path, store: DatabaseStore, users: Any) -> None:
super().__init__(data_dir)
self.store = store
self.users = users
from .ssh_keys import DatabaseSSHKeyStore
self.ssh_keys = DatabaseSSHKeyStore(store)
def admin(self) -> tuple[str, str] | None:
raise RuntimeError("Database-backed authentication must use state.users")
def setup_required(self) -> bool:
return not self.users.has_users()
def create_admin(self, token: str, username: str, password: str,
email: str | None = None) -> 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 not self.setup_required():
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)
self.users.create_initial_user(username, password, email)
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 any(row.get("id") != record["id"]
and row.get("name", "").casefold() == record["name"].casefold()
for row in self.list(scope)):
raise TemplateError("当前宿主机已有同名云镜像模板", 409)
if not self.store.insert("template", key, json.dumps(
record, ensure_ascii=False, separators=(",", ":"))):
raise TemplateError("此云镜像已导入", 409)
return record
def rename(self, scope: str, template_id: str, name: str) -> dict:
name = validate_template_name(name)
record = self.get(scope, template_id)
if any(row.get("id") != template_id
and row.get("name", "").casefold() == name.casefold()
for row in self.list(scope)):
raise TemplateError("当前宿主机已有同名云镜像模板", 409)
updated = {**record, "name": name}
self.store.set("template", f"{scope}:{template_id}", json.dumps(
updated, ensure_ascii=False, separators=(",", ":")))
return updated
class DatabaseStagedHost(StagedHost):
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:
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()
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")
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 _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, profile.private_key_passphrase)
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
),
"private_key_passphrase": (
staged._private_key_passphrase
if staged.metadata["private_key_encrypted"] else ""
),
}
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")
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:
self.store.set("host", "current", previous)
raise
def _restore_current(self) -> None:
record = self._saved_host_record()
if record is None:
return
try:
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"])
region = _validate_region(metadata.get("region", ""))
public_pool = validate_public_pool(
metadata.get("public_pool_cidr", ""),
metadata.get("public_gateway", ""),
metadata.get("public_dns_servers", []),
metadata.get("public_reserved_ips", []),
host,
metadata.get("public_vm_prefix"),
)
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"]
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")
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, "region": region,
**public_pool},
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()
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
@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["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)
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."""
from .users import DatabaseUserStore
store = DatabaseStore(db_config, master_key)
state = DatabasePanelState(data_dir, store, DatabaseUserStore(store, master_key))
manager = DatabaseRemoteHostManager(store, data_dir, ssh_binary=ssh_binary)
templates = DatabaseTemplateStore(store)
return state, manager, templates, store