feat: add settings snapshot rollback backend
这个提交存在于:
父节点
6302ce0589
当前提交
024248ad19
共有 6 个文件被更改,包括 168 次插入 和 1 次删除
|
|
@ -71,6 +71,8 @@ ROUTES: tuple[RouteSpec, ...] = (
|
||||||
RouteSpec("POST", "settings/reload", "backend.routes.settings_routes.reload_settings", "config"),
|
RouteSpec("POST", "settings/reload", "backend.routes.settings_routes.reload_settings", "config"),
|
||||||
RouteSpec("GET", "settings/export", "backend.routes.settings_routes.export_settings", "config"),
|
RouteSpec("GET", "settings/export", "backend.routes.settings_routes.export_settings", "config"),
|
||||||
RouteSpec("POST", "settings/import", "backend.routes.settings_routes.import_settings", "config"),
|
RouteSpec("POST", "settings/import", "backend.routes.settings_routes.import_settings", "config"),
|
||||||
|
RouteSpec("GET", "settings/snapshots", "backend.routes.settings_routes.list_settings_snapshots", "config"),
|
||||||
|
RouteSpec("POST", "settings/rollback", "backend.routes.settings_routes.rollback_settings", "config"),
|
||||||
RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"),
|
RouteSpec("GET", "storage/status", "backend.routes.storage_routes.get_storage_status", "read"),
|
||||||
RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"),
|
RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"),
|
||||||
RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
|
RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,42 @@ def import_settings(
|
||||||
return handle_request(ctx, action)
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def list_settings_snapshots(
|
||||||
|
ctx: RequestContext,
|
||||||
|
query: dict[str, Any] | None = None,
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or _create_settings_service()
|
||||||
|
params = query or {}
|
||||||
|
|
||||||
|
def action() -> list[dict[str, Any]]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
return settings_service.list_snapshots(_int_or_default(params.get("limit"), 20))
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def rollback_settings(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or _create_settings_service()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
return settings_service.rollback(payload, ctx)
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
def _create_settings_service() -> SettingsService:
|
def _create_settings_service() -> SettingsService:
|
||||||
database = Database(load_database_settings())
|
database = Database(load_database_settings())
|
||||||
return SettingsService(audit_service=AuditService(database))
|
return SettingsService(audit_service=AuditService(database))
|
||||||
|
|
||||||
|
|
||||||
|
def _int_or_default(value: Any, default: int) -> int:
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Mapping
|
from typing import Any, Mapping
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from backend.dto.common import ApiError, ErrorCode
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
from core.config_service import ConfigService
|
from core.config_service import ConfigService
|
||||||
|
|
@ -177,6 +178,7 @@ class SettingsService:
|
||||||
if not validation["valid"]:
|
if not validation["valid"]:
|
||||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "config validation failed", validation)
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "config validation failed", validation)
|
||||||
before = self._load_current_mapping()
|
before = self._load_current_mapping()
|
||||||
|
self._record_snapshot(before, ctx, "before_save")
|
||||||
incoming = candidate.get("config", candidate)
|
incoming = candidate.get("config", candidate)
|
||||||
if not isinstance(incoming, Mapping):
|
if not isinstance(incoming, Mapping):
|
||||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
|
||||||
|
|
@ -210,6 +212,75 @@ class SettingsService:
|
||||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "config must be an object", {"field": "config"})
|
||||||
return self.save({"config": config}, ctx)
|
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]:
|
def _load_raw_schema(self) -> dict[str, Any]:
|
||||||
if not self.schema_path.exists():
|
if not self.schema_path.exists():
|
||||||
return {}
|
return {}
|
||||||
|
|
@ -221,6 +292,33 @@ class SettingsService:
|
||||||
return {}
|
return {}
|
||||||
return _load_yaml_subset(path.read_text(encoding="utf-8"))
|
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(
|
def _field(
|
||||||
key: str,
|
key: str,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS config_snapshots (
|
||||||
|
snapshot_id TEXT PRIMARY KEY,
|
||||||
|
reason TEXT NOT NULL,
|
||||||
|
actor_id TEXT,
|
||||||
|
config_json TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_config_snapshots_created
|
||||||
|
ON config_snapshots(created_at);
|
||||||
|
|
@ -25,6 +25,8 @@ class RouteRegistryTests(unittest.TestCase):
|
||||||
self.assertIn(("POST", "workflows/{workflow_id}/archive"), keys)
|
self.assertIn(("POST", "workflows/{workflow_id}/archive"), keys)
|
||||||
self.assertIn(("GET", "nodes/{node_id}/review"), keys)
|
self.assertIn(("GET", "nodes/{node_id}/review"), keys)
|
||||||
self.assertIn(("GET", "workflows/{workflow_id}/review"), keys)
|
self.assertIn(("GET", "workflows/{workflow_id}/review"), keys)
|
||||||
|
self.assertIn(("GET", "settings/snapshots"), keys)
|
||||||
|
self.assertIn(("POST", "settings/rollback"), keys)
|
||||||
self.assertEqual(len(routes), len(ROUTES))
|
self.assertEqual(len(routes), len(ROUTES))
|
||||||
|
|
||||||
def test_webui_adapter_manifest_includes_api_routes(self):
|
def test_webui_adapter_manifest_includes_api_routes(self):
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,8 @@ from backend.routes.settings_routes import (
|
||||||
get_current_settings,
|
get_current_settings,
|
||||||
get_settings_schema,
|
get_settings_schema,
|
||||||
import_settings,
|
import_settings,
|
||||||
|
list_settings_snapshots,
|
||||||
|
rollback_settings,
|
||||||
reload_settings,
|
reload_settings,
|
||||||
save_settings,
|
save_settings,
|
||||||
validate_settings,
|
validate_settings,
|
||||||
|
|
@ -139,7 +141,7 @@ storage:
|
||||||
repository=self.job_repository,
|
repository=self.job_repository,
|
||||||
audit_service=self.audit_service,
|
audit_service=self.audit_service,
|
||||||
)
|
)
|
||||||
self.settings_service = SettingsService(self.config_service)
|
self.settings_service = SettingsService(self.config_service, audit_service=self.audit_service)
|
||||||
self.media_service = MediaService(self.config_service)
|
self.media_service = MediaService(self.config_service)
|
||||||
self.web_hosting_service = WebHostingService()
|
self.web_hosting_service = WebHostingService()
|
||||||
self.ctx = RequestContext.from_astrbot(
|
self.ctx = RequestContext.from_astrbot(
|
||||||
|
|
@ -524,6 +526,23 @@ database:
|
||||||
self.assertTrue(imported["ok"])
|
self.assertTrue(imported["ok"])
|
||||||
self.assertEqual(imported["data"]["config"]["plugin"]["display_name"], "Imported")
|
self.assertEqual(imported["data"]["config"]["plugin"]["display_name"], "Imported")
|
||||||
|
|
||||||
|
snapshots = list_settings_snapshots(self.ctx, {"limit": 10}, self.settings_service)
|
||||||
|
self.assertTrue(snapshots["ok"])
|
||||||
|
self.assertGreaterEqual(len(snapshots["data"]), 2)
|
||||||
|
self.assertNotIn("secret", str(snapshots["data"]))
|
||||||
|
|
||||||
|
old_snapshot = next(
|
||||||
|
item for item in snapshots["data"] if item["config"].get("plugin", {}).get("display_name") == "Old Name"
|
||||||
|
)
|
||||||
|
rollback = rollback_settings(
|
||||||
|
self.ctx,
|
||||||
|
{"snapshot_id": old_snapshot["snapshot_id"]},
|
||||||
|
self.settings_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(rollback["ok"])
|
||||||
|
self.assertEqual(rollback["data"]["config"]["plugin"]["display_name"], "Old Name")
|
||||||
|
self.assertIn("Old Name", self.config_path.read_text(encoding="utf-8"))
|
||||||
|
|
||||||
def test_settings_current_masks_absolute_paths(self):
|
def test_settings_current_masks_absolute_paths(self):
|
||||||
response = get_current_settings(self.ctx, self.settings_service)
|
response = get_current_settings(self.ctx, self.settings_service)
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用