|
|
|
|
|
"""Private, encrypted bootstrap configuration for the panel database.
|
|
|
|
|
|
|
|
|
|
|
|
The database cannot contain its own connection password. The only durable
|
|
|
|
|
|
local state is therefore a 0600 encryption key, an encrypted connection
|
|
|
|
|
|
document and the one-time setup token (until an administrator is created).
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
|
import fcntl
|
|
|
|
|
|
import hmac
|
|
|
|
|
|
import ipaddress
|
|
|
|
|
|
import json
|
|
|
|
|
|
import os
|
|
|
|
|
|
import re
|
|
|
|
|
|
import secrets
|
|
|
|
|
|
import stat
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
import threading
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from typing import Callable, Mapping, TypeVar
|
|
|
|
|
|
|
|
|
|
|
|
from cryptography.exceptions import InvalidTag
|
|
|
|
|
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_AAD = b"virsh-panel/database-bootstrap/v1"
|
|
|
|
|
|
_DATABASE_NAME = re.compile(r"[A-Za-z0-9_]{1,64}\Z")
|
|
|
|
|
|
_DNS_LABEL = re.compile(r"[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\Z")
|
|
|
|
|
|
_T = TypeVar("_T")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseSetupError(Exception):
|
|
|
|
|
|
def __init__(self, message: str, status: int = 400) -> None:
|
|
|
|
|
|
super().__init__(message)
|
|
|
|
|
|
self.status = status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_database_config(config: Mapping[str, object]) -> dict[str, object]:
|
|
|
|
|
|
"""Normalize user-entered connection parameters without SQL interpolation."""
|
|
|
|
|
|
host = config.get("host")
|
|
|
|
|
|
port = config.get("port")
|
|
|
|
|
|
database = config.get("database")
|
|
|
|
|
|
username = config.get("username")
|
|
|
|
|
|
password = config.get("password")
|
|
|
|
|
|
if not isinstance(host, str) or not host or len(host) > 253 or host != host.strip():
|
|
|
|
|
|
raise DatabaseSetupError("数据库地址格式无效")
|
|
|
|
|
|
try:
|
|
|
|
|
|
ipaddress.ip_address(host)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
if not all(_DNS_LABEL.fullmatch(label) for label in host.split(".")):
|
|
|
|
|
|
raise DatabaseSetupError("数据库地址必须是 IP 或 DNS 名称") from None
|
|
|
|
|
|
host = host.lower()
|
|
|
|
|
|
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
|
|
|
|
|
|
raise DatabaseSetupError("数据库端口必须介于 1 到 65535")
|
|
|
|
|
|
if not isinstance(database, str) or not _DATABASE_NAME.fullmatch(database):
|
|
|
|
|
|
raise DatabaseSetupError("数据库名仅支持 1 至 64 位字母、数字和下划线")
|
|
|
|
|
|
if (not isinstance(username, str) or not 1 <= len(username) <= 128
|
|
|
|
|
|
or any(ord(char) < 33 or ord(char) == 127 for char in username)):
|
|
|
|
|
|
raise DatabaseSetupError("数据库用户名格式无效")
|
|
|
|
|
|
if not isinstance(password, str) or not 1 <= len(password) <= 1024 or "\x00" in password:
|
|
|
|
|
|
raise DatabaseSetupError("数据库密码不能为空或格式无效")
|
|
|
|
|
|
return {
|
|
|
|
|
|
"host": host, "port": port, "database": database,
|
|
|
|
|
|
"username": username, "password": password,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseBootstrap:
|
|
|
|
|
|
def __init__(self, data_dir: Path) -> None:
|
|
|
|
|
|
requested = Path(data_dir)
|
|
|
|
|
|
if requested.is_symlink():
|
|
|
|
|
|
raise ValueError("面板数据目录不能是符号链接")
|
|
|
|
|
|
self.data_dir = requested.resolve()
|
|
|
|
|
|
self.data_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
|
|
|
|
info = self.data_dir.lstat()
|
|
|
|
|
|
if not stat.S_ISDIR(info.st_mode) or info.st_mode & 0o077:
|
|
|
|
|
|
raise ValueError("面板数据目录权限必须为 0700")
|
|
|
|
|
|
self.config_file = self.data_dir / "database-config.json"
|
|
|
|
|
|
self.key_file = self.data_dir / "master.key"
|
|
|
|
|
|
self.token_file = self.data_dir / "setup-token"
|
|
|
|
|
|
self.lock_file = self.data_dir / ".database.lock"
|
|
|
|
|
|
self._lock = threading.RLock()
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _read_private(path: Path) -> bytes:
|
|
|
|
|
|
info = path.lstat()
|
|
|
|
|
|
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
|
|
|
|
|
|
raise ValueError(f"敏感配置文件权限必须为 0600:{path.name}")
|
|
|
|
|
|
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
|
|
|
|
|
|
descriptor = os.open(path, flags)
|
|
|
|
|
|
try:
|
|
|
|
|
|
return os.read(descriptor, 1024 * 1024 + 1)
|
|
|
|
|
|
finally:
|
|
|
|
|
|
os.close(descriptor)
|
|
|
|
|
|
|
|
|
|
|
|
def _sync_directory(self) -> None:
|
|
|
|
|
|
descriptor = os.open(self.data_dir, os.O_RDONLY)
|
|
|
|
|
|
try:
|
|
|
|
|
|
os.fsync(descriptor)
|
|
|
|
|
|
finally:
|
|
|
|
|
|
os.close(descriptor)
|
|
|
|
|
|
|
|
|
|
|
|
def _write_private(self, path: Path, content: bytes) -> None:
|
|
|
|
|
|
descriptor, raw_path = tempfile.mkstemp(prefix=f".{path.name}-", dir=self.data_dir)
|
|
|
|
|
|
try:
|
|
|
|
|
|
os.fchmod(descriptor, 0o600)
|
|
|
|
|
|
with os.fdopen(descriptor, "wb") as stream:
|
|
|
|
|
|
stream.write(content)
|
|
|
|
|
|
stream.flush()
|
|
|
|
|
|
os.fsync(stream.fileno())
|
|
|
|
|
|
os.replace(raw_path, path)
|
|
|
|
|
|
self._sync_directory()
|
|
|
|
|
|
finally:
|
|
|
|
|
|
if os.path.exists(raw_path):
|
|
|
|
|
|
os.unlink(raw_path)
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
|
def _exclusive(self):
|
|
|
|
|
|
with self._lock:
|
|
|
|
|
|
flags = os.O_RDWR | os.O_CREAT | getattr(os, "O_NOFOLLOW", 0)
|
|
|
|
|
|
descriptor = os.open(self.lock_file, flags, 0o600)
|
|
|
|
|
|
try:
|
|
|
|
|
|
info = os.fstat(descriptor)
|
|
|
|
|
|
if not stat.S_ISREG(info.st_mode) or info.st_mode & 0o077:
|
|
|
|
|
|
raise ValueError("数据库初始化锁文件权限必须为 0600")
|
|
|
|
|
|
fcntl.flock(descriptor, fcntl.LOCK_EX)
|
|
|
|
|
|
try:
|
|
|
|
|
|
yield
|
|
|
|
|
|
finally:
|
|
|
|
|
|
fcntl.flock(descriptor, fcntl.LOCK_UN)
|
|
|
|
|
|
finally:
|
|
|
|
|
|
os.close(descriptor)
|
|
|
|
|
|
|
|
|
|
|
|
def configured(self) -> bool:
|
|
|
|
|
|
return self.config_file.exists()
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_token(self) -> str:
|
|
|
|
|
|
"""Create once; database and administrator setup share the same token."""
|
|
|
|
|
|
with self._exclusive():
|
|
|
|
|
|
try:
|
|
|
|
|
|
token = self._read_private(self.token_file).decode("ascii").strip()
|
|
|
|
|
|
if len(token) < 32:
|
|
|
|
|
|
raise ValueError("初始化令牌文件无效")
|
|
|
|
|
|
return token
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
token = secrets.token_urlsafe(32)
|
|
|
|
|
|
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
|
|
|
|
|
|
descriptor = os.open(self.token_file, flags, 0o600)
|
|
|
|
|
|
try:
|
|
|
|
|
|
with os.fdopen(descriptor, "wb") as stream:
|
|
|
|
|
|
stream.write((token + "\n").encode("ascii"))
|
|
|
|
|
|
stream.flush()
|
|
|
|
|
|
os.fsync(stream.fileno())
|
|
|
|
|
|
self._sync_directory()
|
|
|
|
|
|
except BaseException:
|
|
|
|
|
|
self.token_file.unlink(missing_ok=True)
|
|
|
|
|
|
raise
|
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
def master_key(self, *, create: bool = False) -> bytes:
|
|
|
|
|
|
try:
|
|
|
|
|
|
key = self._read_private(self.key_file)
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
if not create:
|
|
|
|
|
|
raise ValueError("数据库加密主密钥不存在") from None
|
|
|
|
|
|
key = secrets.token_bytes(32)
|
|
|
|
|
|
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0)
|
|
|
|
|
|
try:
|
|
|
|
|
|
descriptor = os.open(self.key_file, flags, 0o600)
|
|
|
|
|
|
except FileExistsError:
|
|
|
|
|
|
return self.master_key()
|
|
|
|
|
|
try:
|
|
|
|
|
|
with os.fdopen(descriptor, "wb") as stream:
|
|
|
|
|
|
stream.write(key)
|
|
|
|
|
|
stream.flush()
|
|
|
|
|
|
os.fsync(stream.fileno())
|
|
|
|
|
|
self._sync_directory()
|
|
|
|
|
|
except BaseException:
|
|
|
|
|
|
self.key_file.unlink(missing_ok=True)
|
|
|
|
|
|
raise
|
|
|
|
|
|
if len(key) != 32:
|
|
|
|
|
|
raise ValueError("数据库加密主密钥无效")
|
|
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(self) -> dict[str, object] | None:
|
|
|
|
|
|
try:
|
|
|
|
|
|
content = self._read_private(self.config_file)
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
return None
|
|
|
|
|
|
try:
|
|
|
|
|
|
envelope = json.loads(content)
|
|
|
|
|
|
if envelope.get("version") != 1:
|
|
|
|
|
|
raise ValueError("版本无效")
|
|
|
|
|
|
nonce = base64.b64decode(envelope["nonce"], validate=True)
|
|
|
|
|
|
ciphertext = base64.b64decode(envelope["ciphertext"], validate=True)
|
|
|
|
|
|
if len(nonce) != 12 or len(ciphertext) > 4096:
|
|
|
|
|
|
raise ValueError("密文无效")
|
|
|
|
|
|
raw = AESGCM(self.master_key()).decrypt(nonce, ciphertext, _AAD)
|
|
|
|
|
|
return validate_database_config(json.loads(raw))
|
|
|
|
|
|
except (KeyError, TypeError, ValueError, InvalidTag) as exc:
|
|
|
|
|
|
raise ValueError("数据库连接配置损坏或加密主密钥不匹配") from exc
|
|
|
|
|
|
|
|
|
|
|
|
def configure(
|
|
|
|
|
|
self, config: Mapping[str, object], setup_token: str,
|
|
|
|
|
|
connect: Callable[[dict[str, object], bytes], _T],
|
|
|
|
|
|
) -> _T:
|
|
|
|
|
|
"""Verify token and DB access, then atomically commit encrypted config."""
|
|
|
|
|
|
normalized = validate_database_config(config)
|
|
|
|
|
|
with self._exclusive():
|
|
|
|
|
|
if self.configured():
|
|
|
|
|
|
raise DatabaseSetupError("数据库已经配置", 409)
|
|
|
|
|
|
try:
|
|
|
|
|
|
expected = self._read_private(self.token_file).decode("ascii").strip()
|
|
|
|
|
|
except FileNotFoundError as exc:
|
|
|
|
|
|
raise DatabaseSetupError("初始化令牌不可用,请重启面板后查看终端输出", 503) from exc
|
|
|
|
|
|
if (not isinstance(setup_token, str)
|
|
|
|
|
|
or not hmac.compare_digest(setup_token, expected)):
|
|
|
|
|
|
raise DatabaseSetupError("初始化令牌无效", 403)
|
|
|
|
|
|
key = self.master_key(create=True)
|
|
|
|
|
|
result = connect(normalized, key)
|
|
|
|
|
|
nonce = secrets.token_bytes(12)
|
|
|
|
|
|
plaintext = json.dumps(normalized, ensure_ascii=False,
|
|
|
|
|
|
separators=(",", ":")).encode("utf-8")
|
|
|
|
|
|
encrypted = AESGCM(key).encrypt(nonce, plaintext, _AAD)
|
|
|
|
|
|
envelope = {
|
|
|
|
|
|
"version": 1,
|
|
|
|
|
|
"nonce": base64.b64encode(nonce).decode("ascii"),
|
|
|
|
|
|
"ciphertext": base64.b64encode(encrypted).decode("ascii"),
|
|
|
|
|
|
}
|
|
|
|
|
|
self._write_private(
|
|
|
|
|
|
self.config_file,
|
|
|
|
|
|
(json.dumps(envelope, separators=(",", ":")) + "\n").encode("ascii"),
|
|
|
|
|
|
)
|
|
|
|
|
|
return result
|