"""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 hashlib 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 decimal import Decimal 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, host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, user_id BIGINT UNSIGNED 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, network_rx_bytes BIGINT UNSIGNED NULL, network_tx_bytes BIGINT UNSIGNED NULL, PRIMARY KEY (host_scope, vm_uuid, sampled_at), KEY vm_metric_retention (sampled_at), KEY vm_metric_owner (user_id, host_id, sampled_at) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 """ _TRAFFIC_SCHEMA = """ CREATE TABLE IF NOT EXISTS vm_traffic_totals ( host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, user_id BIGINT UNSIGNED NULL, last_observed_at BIGINT UNSIGNED NULL, network_signature CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, network_baseline_json JSON NOT NULL, download_rx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, upload_tx_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0, updated_at BIGINT UNSIGNED NOT NULL, PRIMARY KEY (host_scope, vm_uuid), KEY vm_traffic_owner (user_id, host_id), KEY vm_traffic_host (host_id, vm_uuid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 """ _TRAFFIC_CHECKPOINT_SCHEMA = """ CREATE TABLE IF NOT EXISTS vm_traffic_checkpoints ( host_scope CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, vm_uuid CHAR(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL, checkpoint_hour BIGINT UNSIGNED NOT NULL, host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL, user_id BIGINT UNSIGNED NULL, first_observed_at BIGINT UNSIGNED NOT NULL, download_rx_bytes BIGINT UNSIGNED NOT NULL, upload_tx_bytes BIGINT UNSIGNED NOT NULL, PRIMARY KEY (host_scope, vm_uuid, checkpoint_hour), KEY vm_traffic_checkpoint_owner (user_id, host_id, first_observed_at) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 """ _METRIC_MIGRATIONS = ( "ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS " "host_id VARCHAR(64) CHARACTER SET ascii COLLATE ascii_bin NULL AFTER vm_uuid", "ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS " "user_id BIGINT UNSIGNED NULL AFTER host_id", "ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS " "network_rx_bytes BIGINT UNSIGNED NULL AFTER network_tx_bps", "ALTER TABLE {metric_table} ADD COLUMN IF NOT EXISTS " "network_tx_bytes BIGINT UNSIGNED NULL AFTER network_rx_bytes", "ALTER TABLE {metric_table} ADD INDEX IF NOT EXISTS " "vm_metric_owner (user_id, host_id, sampled_at)", ) _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") _HOST_ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}\Z") _METRIC_PRUNE_BATCH_SIZE = 5000 _MAX_UNSIGNED_BIGINT = (1 << 64) - 1 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. """ RECORD_TABLE = "panel_records" METRIC_TABLE = "vm_metric_samples" TRAFFIC_TABLE = "vm_traffic_totals" TRAFFIC_CHECKPOINT_TABLE = "vm_traffic_checkpoints" 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, } if config.get("ssl_ca"): self._config["ssl"] = {"ca": config["ssl_ca"], "check_hostname": True} self._config["ssl_verify_cert"] = True self._config["ssl_verify_identity"] = True 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(_TRAFFIC_SCHEMA) cursor.execute(_TRAFFIC_CHECKPOINT_SCHEMA) for statement in _METRIC_MIGRATIONS: cursor.execute(statement.format(metric_table=self.METRIC_TABLE)) 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 or timestamp > _MAX_UNSIGNED_BIGINT): 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) host_id = point.get("host_id") if host_id is not None and (not isinstance(host_id, str) or not _HOST_ID.fullmatch(host_id)): raise ValueError("Metric host ID is invalid") user_id = point.get("user_id") if (user_id is not None and (isinstance(user_id, bool) or not isinstance(user_id, int) or user_id < 1 or user_id > _MAX_UNSIGNED_BIGINT)): raise ValueError("Metric user ID is invalid") raw_devices = point.get("network_devices") # None means libvirt did not return a complete NIC counter frame. # Never replace a durable baseline with incomplete data, otherwise # the next healthy sample would look like a brand-new NIC and the # whole gap would become free traffic. An explicit empty mapping # remains a valid observation of a VM with no interfaces. devices = (None if raw_devices is None else self._normalize_traffic_devices(raw_devices)) rows.append((host_scope, vm_id, timestamp, host_id, user_id, metrics, devices)) # Keep accounting deterministic if a caller ever submits more than one # observation for the same VM in a batch. rows.sort(key=lambda row: (row[0], row[1], row[2])) connection = self._connect() try: with connection.cursor() as cursor: for scope, vm_id, timestamp, host_id, user_id, metrics, devices in rows: cursor.execute( f"INSERT IGNORE INTO {self.TRAFFIC_TABLE} " "(host_scope,vm_uuid,host_id,user_id,last_observed_at,network_signature," "network_baseline_json,download_rx_bytes,upload_tx_bytes,updated_at) " "VALUES(%s,%s,%s,%s,NULL,%s,%s,0,0,%s)", (scope, vm_id, host_id, user_id, self._traffic_signature({}), "{}", timestamp), ) cursor.execute( f"SELECT last_observed_at,network_baseline_json," "download_rx_bytes,upload_tx_bytes " f"FROM {self.TRAFFIC_TABLE} WHERE host_scope=%s AND vm_uuid=%s FOR UPDATE", (scope, vm_id), ) state = cursor.fetchone() if not isinstance(state, Mapping): raise DatabaseError("无法锁定虚拟机流量计量状态") previous_at = state.get("last_observed_at") if (previous_at is not None and (isinstance(previous_at, bool) or not isinstance(previous_at, int) or previous_at < 0 or previous_at > _MAX_UNSIGNED_BIGINT)): raise DatabaseError("虚拟机流量计量时间戳损坏") rx_delta = tx_delta = None if devices is not None and (previous_at is None or timestamp > previous_at): previous = self._decode_traffic_baseline( state.get("network_baseline_json")) if previous_at is not None: rx_delta, tx_delta = self._traffic_increment(previous, devices) rx_total = self._traffic_total(state.get("download_rx_bytes")) tx_total = self._traffic_total(state.get("upload_tx_bytes")) rx_total = self._checked_traffic_sum(rx_total, rx_delta or 0) tx_total = self._checked_traffic_sum(tx_total, tx_delta or 0) cursor.execute( f"UPDATE {self.TRAFFIC_TABLE} SET host_id=%s,user_id=%s," "last_observed_at=%s,network_signature=%s,network_baseline_json=%s," "download_rx_bytes=%s,upload_tx_bytes=%s,updated_at=%s " "WHERE host_scope=%s AND vm_uuid=%s", (host_id, user_id, timestamp, self._traffic_signature(devices), json.dumps(devices, sort_keys=True, separators=(",", ":")), rx_total, tx_total, timestamp, scope, vm_id), ) # Full chart samples are retained for 30 days. This # compact hourly ledger is retained independently so a # long PHP lifecycle outage cannot erase the cumulative # counter near a future prepaid-period boundary. The # first observation in each hour is immutable. cursor.execute( f"INSERT IGNORE INTO {self.TRAFFIC_CHECKPOINT_TABLE} " "(host_scope,vm_uuid,checkpoint_hour,host_id,user_id," "first_observed_at,download_rx_bytes,upload_tx_bytes) " "VALUES(%s,%s,%s,%s,%s,%s,%s,%s)", (scope, vm_id, timestamp - timestamp % 3600, host_id, user_id, timestamp, rx_total, tx_total), ) cursor.execute( f"INSERT INTO {self.METRIC_TABLE} " "(host_scope,vm_uuid,host_id,user_id,sampled_at," + ",".join(_METRIC_COLUMNS) + ",network_rx_bytes,network_tx_bytes) VALUES (" + ",".join(["%s"] * (7 + len(_METRIC_COLUMNS))) + ") " "ON DUPLICATE KEY UPDATE " + ",".join(f"{name}=VALUES({name})" for name in _METRIC_COLUMNS), (scope, vm_id, host_id, user_id, timestamp, *metrics, rx_delta, tx_delta), ) connection.commit() except pymysql.MySQLError as exc: connection.rollback() raise DatabaseError("无法保存虚拟机指标") from exc except (DatabaseError, ValueError): connection.rollback() raise finally: connection.close() @staticmethod def _traffic_total(value: Any) -> int: if (isinstance(value, bool) or not isinstance(value, int) or value < 0 or value > _MAX_UNSIGNED_BIGINT): raise DatabaseError("虚拟机流量计量状态损坏") return value @staticmethod def _checked_traffic_sum(total: int, increment: int) -> int: value = total + increment if value > _MAX_UNSIGNED_BIGINT: raise DatabaseError("虚拟机累计流量超过可计量范围") return value @staticmethod def _normalize_traffic_devices(value: Any) -> dict[str, list[int]]: if value is None: return {} if not isinstance(value, Mapping) or len(value) > 1024: raise ValueError("VM network counters are invalid") result: dict[str, list[int]] = {} for name, counters in value.items(): if (not isinstance(name, str) or not 1 <= len(name) <= 255 or any(ord(char) < 32 or ord(char) == 127 for char in name)): raise ValueError("VM network interface name is invalid") if (not isinstance(counters, (list, tuple)) or len(counters) != 2 or any(isinstance(counter, bool) or not isinstance(counter, int) or counter < 0 or counter > _MAX_UNSIGNED_BIGINT for counter in counters)): raise ValueError("VM network counters are invalid") result[name] = [counters[0], counters[1]] return result @classmethod def _decode_traffic_baseline(cls, value: Any) -> dict[str, list[int]]: try: decoded = json.loads(value) if isinstance(value, (str, bytes, bytearray)) else value return cls._normalize_traffic_devices(decoded) except (TypeError, ValueError, UnicodeError, json.JSONDecodeError) as exc: raise DatabaseError("虚拟机流量计量基线损坏") from exc @staticmethod def _traffic_signature(devices: Mapping[str, list[int]]) -> str: names = json.dumps(sorted(devices), ensure_ascii=False, separators=(",", ":")).encode("utf-8") return hashlib.sha256(names).hexdigest() @staticmethod def _traffic_increment(previous: Mapping[str, list[int]], current: Mapping[str, list[int]]) -> tuple[int, int]: """Account stable NICs independently; rx is download and tx is upload.""" rx_total = tx_total = 0 for name, (rx, tx) in current.items(): old = previous.get(name) # A genuinely new NIC establishes a baseline. For an existing # stable identity a lower counter means QEMU/tap counters reset # (normally a VM restart); bytes since zero are still billable. # Treat directions independently because drivers need not reset # both counters in exactly the same observation. if old is None: continue rx_total += rx - old[0] if rx >= old[0] else rx tx_total += tx - old[1] if tx >= old[1] else tx return rx_total, tx_total 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( f"DELETE FROM {self.METRIC_TABLE} 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 + f" FROM {self.METRIC_TABLE} " "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() @staticmethod def _daily_traffic_value(value: Any) -> int | None: if value is None: return None if isinstance(value, bool): raise DatabaseError("虚拟机流量统计数据损坏") if isinstance(value, Decimal): if not value.is_finite() or value != value.to_integral_value(): raise DatabaseError("虚拟机流量统计数据损坏") result = int(value) elif isinstance(value, int): result = value else: raise DatabaseError("虚拟机流量统计数据损坏") if result < 0 or result > _MAX_UNSIGNED_BIGINT: raise DatabaseError("虚拟机流量统计数据损坏") return result def query_daily_traffic(self, host_scope: str, vm_id: str, *, host_id: str | None, user_id: int | None, since: int, until: int, limit: int = 30) -> list[dict[str, Any]]: """Sum per-observation RX/TX byte deltas into UTC calendar days.""" vm_id = self._metric_identity(host_scope, vm_id) if (any(isinstance(value, bool) or not isinstance(value, int) for value in (since, until, limit)) or since < 0 or until < since or until > _MAX_UNSIGNED_BIGINT or until - since > 30 * 24 * 60 * 60 or not 1 <= limit <= 30): raise ValueError("Daily traffic query range is invalid") if host_id is not None and (not isinstance(host_id, str) or not _HOST_ID.fullmatch(host_id)): raise ValueError("Daily traffic host ID is invalid") if user_id is not None and (isinstance(user_id, bool) or not isinstance(user_id, int) or user_id < 1 or user_id > _MAX_UNSIGNED_BIGINT): raise ValueError("Daily traffic user ID is invalid") connection = self._connect() try: with connection.cursor() as cursor: cursor.execute( "SELECT FLOOR(sampled_at / 86400) * 86400 AS day_start, " "SUM(network_rx_bytes) AS download_bytes, " "SUM(network_tx_bytes) AS upload_bytes " f"FROM {self.METRIC_TABLE} " "WHERE host_scope=%s AND vm_uuid=%s " "AND host_id <=> %s AND user_id <=> %s " "AND sampled_at >= %s AND sampled_at <= %s " "GROUP BY FLOOR(sampled_at / 86400) " "ORDER BY day_start ASC LIMIT %s", (host_scope, vm_id, host_id, user_id, since, until, limit), ) rows = cursor.fetchall() result = [] for row in rows: if not isinstance(row, Mapping): raise DatabaseError("虚拟机流量统计数据损坏") day_start = self._daily_traffic_value(row.get("day_start")) if day_start is None or day_start % 86400: raise DatabaseError("虚拟机流量统计数据损坏") download = self._daily_traffic_value(row.get("download_bytes")) upload = self._daily_traffic_value(row.get("upload_bytes")) result.append({ "day_start": day_start, "download_bytes": download, "upload_bytes": upload, "total_bytes": (download + upload if download is not None and upload is not None else None), }) return result 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( f"SELECT encrypted_value FROM {self.RECORD_TABLE} " "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( f"INSERT INTO {self.RECORD_TABLE} (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 compare_and_set( self, record_type: str, record_key: str, expected: str | None, value: str, ) -> bool: """Atomically replace one encrypted record when its plaintext matches. Public-network pools use one small catalog record internally while the HTTP API mutates one independently revisioned pool at a time. A row lock and plaintext comparison prevent two engine workers from both validating against a stale catalog and installing overlapping CIDRs. """ self._validate_key(record_type, record_key) if expected is not None and not isinstance(expected, str): raise ValueError("Expected database value must be text or None") payload = self._encrypt(record_type, record_key, value) connection = self._connect() try: with connection.cursor() as cursor: cursor.execute( f"SELECT encrypted_value FROM {self.RECORD_TABLE} " "WHERE record_type=%s AND record_key=%s FOR UPDATE", (record_type, record_key), ) row = cursor.fetchone() current = None if row is None else self._decrypt( record_type, record_key, row["encrypted_value"], ) matches = ( current is None and expected is None ) or ( isinstance(current, str) and isinstance(expected, str) and hmac.compare_digest(current, expected) ) if not matches: connection.rollback() return False if row is None: cursor.execute( f"INSERT INTO {self.RECORD_TABLE} " "(record_type, record_key, encrypted_value) VALUES (%s, %s, %s)", (record_type, record_key, payload), ) else: cursor.execute( f"UPDATE {self.RECORD_TABLE} SET encrypted_value=%s " "WHERE record_type=%s AND record_key=%s", (payload, record_type, record_key), ) 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 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( f"INSERT INTO {self.RECORD_TABLE} " "(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( f"DELETE FROM {self.RECORD_TABLE} 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( f"SELECT record_key, encrypted_value FROM {self.RECORD_TABLE} 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