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

699 lines
29 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 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