astrbot_plugin_imagestudio/backend/services/settings_service.py

433 行
17 KiB
Python

"""Settings schema and current configuration service."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Mapping
from uuid import uuid4
from backend.dto.common import ApiError, ErrorCode
from core.config_service import ConfigService
from core.audit_service import AuditService
from core.identity import RequestContext
from database.db_helper import REPO_ROOT, _load_yaml_subset
SENSITIVE_KEY_PARTS = ("password", "token", "secret", "api_key", "apikey", "key", "dsn")
DATABASE_BACKENDS = {"auto", "sqlite", "postgresql", "postgres", "pg"}
class SettingsService:
def __init__(
self,
config_service: ConfigService | None = None,
schema_path: str | Path = "_conf_schema.json",
audit_service: AuditService | None = None,
):
self.config_service = config_service or ConfigService()
self.schema_path = _resolve_path(schema_path)
self.audit_service = audit_service
def get_schema(self) -> dict[str, Any]:
return {
"schema_version": "3.0",
"raw_schema": self._load_raw_schema(),
"sections": [
{
"key": "plugin",
"title": "Plugin",
"fields": [
_field("name", "string", readonly=True),
_field("display_name", "string"),
_field("version", "string", readonly=True),
],
},
{
"key": "features",
"title": "Features",
"fields": [
_field("quick_profile", "bool"),
_field("workspace", "bool"),
_field("material_library", "bool"),
_field("wardrobe", "bool"),
_field("api_discovery", "bool"),
_field("private_companion_integration", "bool"),
],
},
{
"key": "storage",
"title": "Storage",
"fields": [
_field("images_path", "path"),
_field("thumbnails_path", "path"),
_field("materials_path", "path"),
_field("styles_path", "path"),
_field("archives_path", "path"),
_field("temp_path", "path"),
],
},
{
"key": "database",
"title": "Database",
"fields": [
_field("backend", "select", options=["auto", "sqlite", "postgresql"]),
_field("sqlite.path", "path"),
_field("postgresql.enabled", "bool"),
_field("postgresql.dsn", "string", sensitive=True),
_field("postgresql.host", "string"),
_field("postgresql.port", "number"),
_field("postgresql.database", "string"),
_field("postgresql.user", "string", sensitive=True),
_field("postgresql.password", "string", sensitive=True),
],
},
{
"key": "security",
"title": "Security",
"fields": [
_field("integration_token_set", "bool", readonly=True),
_field("allowed_integration_plugins", "string_list"),
],
},
],
}
def get_current(self) -> dict[str, Any]:
raw = self._load_current_mapping()
return {
"config_path": "***PATH***",
"config": _mask_sensitive(raw),
}
def validate(self, candidate: Mapping[str, Any]) -> dict[str, Any]:
config = candidate.get("config", candidate)
if not isinstance(config, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
errors: list[dict[str, Any]] = []
warnings: list[dict[str, Any]] = []
database = config.get("database", {})
if isinstance(database, Mapping):
backend = str(database.get("backend", "auto")).lower()
if backend not in DATABASE_BACKENDS:
errors.append(
{
"field": "database.backend",
"message": "unsupported database backend",
"allowed": sorted(DATABASE_BACKENDS),
}
)
postgresql = database.get("postgresql", {})
if backend in {"postgresql", "postgres", "pg"} and isinstance(postgresql, Mapping):
if not postgresql.get("dsn") and not (
postgresql.get("host") and postgresql.get("database") and postgresql.get("user")
):
warnings.append(
{
"field": "database.postgresql",
"message": "postgresql backend is selected but connection fields are incomplete",
}
)
return {"valid": not errors, "errors": errors, "warnings": warnings}
def diff(self, candidate: Mapping[str, Any]) -> dict[str, Any]:
current = self._load_current_mapping()
incoming = candidate.get("config", candidate)
if not isinstance(incoming, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
before = _flatten(current)
after = _flatten(dict(incoming))
keys = sorted(set(before) | set(after))
changes = []
for key in keys:
before_value = before.get(key)
after_value = after.get(key)
if before_value == after_value:
continue
if _is_sensitive_key(key):
changes.append(
{
"path": key,
"before_set": bool(before_value),
"after_set": bool(after_value),
"sensitive": True,
}
)
elif _is_path_key(key):
changes.append(
{
"path": key,
"before": _mask_path_value(before_value),
"after": _mask_path_value(after_value),
"sensitive": False,
}
)
else:
changes.append(
{
"path": key,
"before": before_value,
"after": after_value,
"sensitive": False,
}
)
return {"has_changes": bool(changes), "changes": changes}
def save(self, candidate: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
validation = self.validate(candidate)
if not validation["valid"]:
raise ApiError(ErrorCode.VALIDATION_FAILED, "config validation failed", validation)
before = self._load_current_mapping()
self._record_snapshot(before, ctx, "before_save")
incoming = candidate.get("config", candidate)
if not isinstance(incoming, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
merged = _merge_masked(before, incoming)
self.config_service.config_path.parent.mkdir(parents=True, exist_ok=True)
self.config_service.config_path.write_text(_dump_yaml(merged), encoding="utf-8")
after = self._load_current_mapping()
if self.audit_service:
self.audit_service.record(
ctx=ctx,
action="save_settings",
target_type="config",
target_id="***PATH***",
before_snapshot=_mask_sensitive(before),
after_snapshot=_mask_sensitive(after),
)
return {"saved": True, "config": _mask_sensitive(after)}
def reload(self) -> dict[str, Any]:
return {"reloaded": True, "config": self.get_current()["config"]}
def export(self) -> dict[str, Any]:
return {
"redacted": True,
"config": self.get_current()["config"],
}
def import_config(self, payload: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
config = payload.get("config", payload)
if not isinstance(config, Mapping):
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
return self.save({"config": config}, ctx)
def list_snapshots(self, limit: int = 20) -> list[dict[str, Any]]:
if not self.audit_service:
return []
placeholder = self.audit_service.database._placeholder
normalized_limit = min(max(int(limit), 1), 100)
conn = self.audit_service.database.connect()
try:
rows = conn.execute(
f"""
SELECT snapshot_id, reason, actor_id, config_json, created_at
FROM config_snapshots
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(normalized_limit,),
).fetchall()
finally:
conn.close()
snapshots = []
for row in rows:
config = json.loads(row["config_json"] or "{}")
snapshots.append(
{
"snapshot_id": row["snapshot_id"],
"reason": row["reason"],
"actor_id": row["actor_id"],
"config": _mask_sensitive(config),
"created_at": row["created_at"],
}
)
return snapshots
def rollback(self, payload: Mapping[str, Any], ctx: RequestContext) -> dict[str, Any]:
if not self.audit_service:
raise ApiError(ErrorCode.VALIDATION_FAILED, "config snapshots are unavailable", {})
snapshot_id = str(payload.get("snapshot_id") or "").strip()
if not snapshot_id:
raise ApiError(ErrorCode.VALIDATION_FAILED, "snapshot_id is required", {"field": "snapshot_id"})
placeholder = self.audit_service.database._placeholder
conn = self.audit_service.database.connect()
try:
row = conn.execute(
f"SELECT snapshot_id, config_json FROM config_snapshots WHERE snapshot_id = {placeholder}",
(snapshot_id,),
).fetchone()
finally:
conn.close()
if row is None:
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "config snapshot not found", {"snapshot_id": snapshot_id})
config = json.loads(row["config_json"] or "{}")
validation = self.validate({"config": config})
if not validation["valid"]:
raise ApiError(ErrorCode.VALIDATION_FAILED, "snapshot config validation failed", validation)
before = self._load_current_mapping()
self._record_snapshot(before, ctx, "before_rollback")
self.config_service.config_path.parent.mkdir(parents=True, exist_ok=True)
self.config_service.config_path.write_text(_dump_yaml(config), encoding="utf-8")
after = self._load_current_mapping()
self.audit_service.record(
ctx=ctx,
action="rollback_settings",
target_type="config",
target_id="***PATH***",
before_snapshot=_mask_sensitive(before),
after_snapshot=_mask_sensitive(after),
metadata={"snapshot_id": snapshot_id},
)
return {"rolled_back": True, "snapshot_id": snapshot_id, "config": _mask_sensitive(after)}
def _load_raw_schema(self) -> dict[str, Any]:
if not self.schema_path.exists():
return {}
return json.loads(self.schema_path.read_text(encoding="utf-8"))
def _load_current_mapping(self) -> dict[str, Any]:
path = self.config_service.config_path
if not path.exists():
return {}
return _load_yaml_subset(path.read_text(encoding="utf-8"))
def _record_snapshot(self, config: Mapping[str, Any], ctx: RequestContext, reason: str) -> str | None:
if not self.audit_service:
return None
snapshot_id = f"cfgsnap_{uuid4().hex}"
placeholder = self.audit_service.database._placeholder
conn = self.audit_service.database.connect()
try:
conn.execute(
f"""
INSERT INTO config_snapshots (
snapshot_id, reason, actor_id, config_json
) VALUES (
{placeholder}, {placeholder}, {placeholder}, {placeholder}
)
""",
(
snapshot_id,
reason,
ctx.actor_id,
json.dumps(dict(config), ensure_ascii=False, sort_keys=True),
),
)
conn.commit()
finally:
conn.close()
return snapshot_id
def _field(
key: str,
field_type: str,
*,
options: list[str] | None = None,
sensitive: bool = False,
readonly: bool = False,
) -> dict[str, Any]:
return {
"key": key,
"type": field_type,
"options": options or [],
"sensitive": sensitive,
"readonly": readonly,
}
def _mask_sensitive(value: Any, path: str = "") -> Any:
if isinstance(value, Mapping):
masked: dict[str, Any] = {}
for key, child in value.items():
child_path = f"{path}.{key}" if path else str(key)
if _is_sensitive_key(child_path) and child:
masked[str(key)] = "***MASKED***"
elif _is_path_key(child_path) and _is_absolute_path_value(child):
masked[str(key)] = "***PATH***"
else:
masked[str(key)] = _mask_sensitive(child, child_path)
return masked
if isinstance(value, list):
return [_mask_sensitive(item, path) for item in value]
return value
def _flatten(value: Mapping[str, Any], prefix: str = "") -> dict[str, Any]:
flattened: dict[str, Any] = {}
for key, child in value.items():
path = f"{prefix}.{key}" if prefix else str(key)
if isinstance(child, Mapping):
flattened.update(_flatten(child, path))
else:
flattened[path] = child
return flattened
def _is_sensitive_key(path: str) -> bool:
lowered = path.lower()
if lowered.endswith("_set"):
return False
return any(part in lowered for part in SENSITIVE_KEY_PARTS)
def _is_path_key(path: str) -> bool:
lowered = path.lower()
return lowered.endswith("_path") or lowered.endswith(".path")
def _is_absolute_path_value(value: Any) -> bool:
return isinstance(value, str) and Path(value).is_absolute()
def _mask_path_value(value: Any) -> Any:
return "***PATH***" if _is_absolute_path_value(value) else value
def _resolve_path(path: str | Path) -> Path:
resolved = Path(path)
if not resolved.is_absolute():
resolved = REPO_ROOT / resolved
return resolved
def _merge_masked(current: Mapping[str, Any], incoming: Mapping[str, Any]) -> dict[str, Any]:
result: dict[str, Any] = dict(current)
for key, value in incoming.items():
if isinstance(value, str) and value in {"***MASKED***", "***PATH***"}:
continue
if isinstance(value, Mapping) and isinstance(result.get(key), Mapping):
result[str(key)] = _merge_masked(result[key], value)
else:
result[str(key)] = value
return result
def _dump_yaml(value: Mapping[str, Any], indent: int = 0) -> str:
lines: list[str] = []
prefix = " " * indent
for key, child in value.items():
if isinstance(child, Mapping):
lines.append(f"{prefix}{key}:")
lines.append(_dump_yaml(child, indent + 2).rstrip())
elif isinstance(child, list):
lines.append(f"{prefix}{key}:")
for item in child:
lines.append(f"{prefix} - {_format_scalar(item)}")
else:
lines.append(f"{prefix}{key}: {_format_scalar(child)}")
return "\n".join(lines) + "\n"
def _format_scalar(value: Any) -> str:
if isinstance(value, bool):
return "true" if value else "false"
if value is None:
return "null"
if isinstance(value, (int, float)):
return str(value)
text = str(value)
if text == "" or any(char in text for char in [":", "#", "{", "}", "[", "]", ","]) or text.strip() != text:
return '"' + text.replace("\\", "\\\\").replace('"', '\\"') + '"'
return text