diff --git a/backend/routes/settings_routes.py b/backend/routes/settings_routes.py index be2dc16..93d263c 100644 --- a/backend/routes/settings_routes.py +++ b/backend/routes/settings_routes.py @@ -6,8 +6,10 @@ from typing import Any from backend.routes.common import handle_request from backend.services.settings_service import SettingsService +from core.audit_service import AuditService from core.identity import RequestContext from core.security import require_authenticated, require_config_manager +from database.db_helper import Database, load_database_settings def get_settings_schema( @@ -62,3 +64,62 @@ def diff_settings( return settings_service.diff(payload) return handle_request(ctx, action) + + +def save_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.save(payload, ctx) + + return handle_request(ctx, action) + + +def reload_settings( + ctx: RequestContext, + service: SettingsService | None = None, +) -> dict: + settings_service = service or _create_settings_service() + + def action() -> dict[str, Any]: + require_config_manager(ctx) + return settings_service.reload() + + return handle_request(ctx, action) + + +def export_settings( + ctx: RequestContext, + service: SettingsService | None = None, +) -> dict: + settings_service = service or _create_settings_service() + + def action() -> dict[str, Any]: + require_config_manager(ctx) + return settings_service.export() + + return handle_request(ctx, action) + + +def import_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.import_config(payload, ctx) + + return handle_request(ctx, action) + + +def _create_settings_service() -> SettingsService: + database = Database(load_database_settings()) + return SettingsService(audit_service=AuditService(database)) diff --git a/backend/services/settings_service.py b/backend/services/settings_service.py index 3dc779c..acd8eb7 100644 --- a/backend/services/settings_service.py +++ b/backend/services/settings_service.py @@ -8,6 +8,8 @@ from typing import Any, Mapping 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 @@ -20,9 +22,11 @@ class SettingsService: 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 { @@ -159,6 +163,44 @@ class SettingsService: ) 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() + 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=str(self.config_service.config_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 _load_raw_schema(self) -> dict[str, Any]: if not self.schema_path.exists(): return {} @@ -237,3 +279,44 @@ def _resolve_path(path: str | 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 diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index 7804dbd..f73a3f1 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -32,8 +32,12 @@ from backend.routes.profile_routes import ( ) from backend.routes.settings_routes import ( diff_settings, + export_settings, get_current_settings, get_settings_schema, + import_settings, + reload_settings, + save_settings, validate_settings, ) from backend.routes.storage_routes import get_storage_status @@ -435,6 +439,63 @@ security: self.assertFalse(response["ok"]) self.assertEqual(response["error_code"], "FORBIDDEN") + def test_settings_save_reload_export_and_import(self): + self.config_path.write_text( + """ +plugin: + name: astrbot_plugin_imagestudio + display_name: Old Name + version: 3.0.1-test + +database: + backend: auto + postgresql: + enabled: true + dsn: "postgresql://user:secret@example.local/db" +""", + encoding="utf-8", + ) + + saved = save_settings( + self.ctx, + { + "config": { + "plugin": { + "name": "astrbot_plugin_imagestudio", + "display_name": "New Name", + "version": "3.0.1-test", + }, + "database": { + "backend": "auto", + "postgresql": {"enabled": True, "dsn": "***MASKED***"}, + }, + } + }, + self.settings_service, + ) + self.assertTrue(saved["ok"]) + self.assertEqual(saved["data"]["config"]["plugin"]["display_name"], "New Name") + current_raw = self.config_path.read_text(encoding="utf-8") + self.assertIn("New Name", current_raw) + self.assertIn("postgresql://user:secret@example.local/db", current_raw) + + reloaded = reload_settings(self.ctx, self.settings_service) + self.assertTrue(reloaded["ok"]) + self.assertEqual(reloaded["data"]["config"]["database"]["postgresql"]["dsn"], "***MASKED***") + + exported = export_settings(self.ctx, self.settings_service) + self.assertTrue(exported["ok"]) + self.assertTrue(exported["data"]["redacted"]) + self.assertNotIn("secret", str(exported["data"])) + + imported = import_settings( + self.ctx, + {"config": {"plugin": {"name": "astrbot_plugin_imagestudio", "display_name": "Imported"}}}, + self.settings_service, + ) + self.assertTrue(imported["ok"]) + self.assertEqual(imported["data"]["config"]["plugin"]["display_name"], "Imported") + def test_settings_current_masks_absolute_paths(self): response = get_current_settings(self.ctx, self.settings_service)