322 行
12 KiB
Python
322 行
12 KiB
Python
"""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 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": 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 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 {}
|
|
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
|
|
|
|
|
|
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
|