"""Settings schema and current configuration service.""" from __future__ import annotations import json from pathlib import Path from typing import Any, Mapping from backend.dto.common import ApiError, ErrorCode from core.config_service import ConfigService 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", ): self.config_service = config_service or ConfigService() self.schema_path = _resolve_path(schema_path) 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": str(self.config_service.config_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, } ) else: changes.append( { "path": key, "before": before_value, "after": after_value, "sensitive": False, } ) return {"has_changes": bool(changes), "changes": changes} 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 _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 _resolve_path(path: str | Path) -> Path: resolved = Path(path) if not resolved.is_absolute(): resolved = REPO_ROOT / resolved return resolved