feat: add settings api
这个提交存在于:
父节点
1c7a2782d7
当前提交
fe2f52fe06
共有 3 个文件被更改,包括 367 次插入 和 0 次删除
|
|
@ -0,0 +1,64 @@
|
||||||
|
"""Settings route handlers."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.routes.common import handle_request
|
||||||
|
from backend.services.settings_service import SettingsService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
from core.security import require_authenticated, require_config_manager
|
||||||
|
|
||||||
|
|
||||||
|
def get_settings_schema(
|
||||||
|
ctx: RequestContext,
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or SettingsService()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_authenticated(ctx)
|
||||||
|
return settings_service.get_schema()
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_settings(
|
||||||
|
ctx: RequestContext,
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or SettingsService()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_authenticated(ctx)
|
||||||
|
return settings_service.get_current()
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def validate_settings(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or SettingsService()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
return settings_service.validate(payload)
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def diff_settings(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: SettingsService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
settings_service = service or SettingsService()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
return settings_service.diff(payload)
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
225
backend/services/settings_service.py
普通文件
225
backend/services/settings_service.py
普通文件
|
|
@ -0,0 +1,225 @@
|
||||||
|
"""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)
|
||||||
|
masked[str(key)] = "***MASKED***" if _is_sensitive_key(child_path) and child else _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 _resolve_path(path: str | Path) -> Path:
|
||||||
|
resolved = Path(path)
|
||||||
|
if not resolved.is_absolute():
|
||||||
|
resolved = REPO_ROOT / resolved
|
||||||
|
return resolved
|
||||||
|
|
@ -28,6 +28,12 @@ from backend.routes.profile_routes import (
|
||||||
list_profiles,
|
list_profiles,
|
||||||
update_profile,
|
update_profile,
|
||||||
)
|
)
|
||||||
|
from backend.routes.settings_routes import (
|
||||||
|
diff_settings,
|
||||||
|
get_current_settings,
|
||||||
|
get_settings_schema,
|
||||||
|
validate_settings,
|
||||||
|
)
|
||||||
from backend.routes.ui_routes import get_ui_bootstrap
|
from backend.routes.ui_routes import get_ui_bootstrap
|
||||||
from backend.routes.workflow_routes import (
|
from backend.routes.workflow_routes import (
|
||||||
archive_workflow,
|
archive_workflow,
|
||||||
|
|
@ -40,6 +46,7 @@ from backend.services.dashboard_service import DashboardService
|
||||||
from backend.services.image_service import ImageService
|
from backend.services.image_service import ImageService
|
||||||
from backend.services.profile_service import ProfileService
|
from backend.services.profile_service import ProfileService
|
||||||
from backend.services.queue_service import QueueService
|
from backend.services.queue_service import QueueService
|
||||||
|
from backend.services.settings_service import SettingsService
|
||||||
from backend.services.ui_service import UiService
|
from backend.services.ui_service import UiService
|
||||||
from backend.services.workflow_service import WorkflowService
|
from backend.services.workflow_service import WorkflowService
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
|
|
@ -115,6 +122,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.ctx = RequestContext.from_astrbot(
|
self.ctx = RequestContext.from_astrbot(
|
||||||
username="admin",
|
username="admin",
|
||||||
user_id="user_001",
|
user_id="user_001",
|
||||||
|
|
@ -349,6 +357,76 @@ storage:
|
||||||
self.assertFalse(response["ok"])
|
self.assertFalse(response["ok"])
|
||||||
self.assertEqual(response["error_code"], "FORBIDDEN")
|
self.assertEqual(response["error_code"], "FORBIDDEN")
|
||||||
|
|
||||||
|
def test_settings_schema_current_validate_and_diff(self):
|
||||||
|
self.config_path.write_text(
|
||||||
|
"""
|
||||||
|
plugin:
|
||||||
|
name: astrbot_plugin_imagestudio
|
||||||
|
display_name: ImageStudio Test
|
||||||
|
version: 3.0.1-test
|
||||||
|
|
||||||
|
features:
|
||||||
|
quick_profile: true
|
||||||
|
|
||||||
|
database:
|
||||||
|
backend: postgresql
|
||||||
|
postgresql:
|
||||||
|
enabled: true
|
||||||
|
dsn: "postgresql://user:secret@example.local/db"
|
||||||
|
password: "secret"
|
||||||
|
|
||||||
|
security:
|
||||||
|
integration_token_set: true
|
||||||
|
""",
|
||||||
|
encoding="utf-8",
|
||||||
|
)
|
||||||
|
|
||||||
|
schema = get_settings_schema(self.ctx, self.settings_service)
|
||||||
|
self.assertTrue(schema["ok"])
|
||||||
|
self.assertEqual(schema["data"]["schema_version"], "3.0")
|
||||||
|
self.assertIn("database", [section["key"] for section in schema["data"]["sections"]])
|
||||||
|
|
||||||
|
current = get_current_settings(self.ctx, self.settings_service)
|
||||||
|
self.assertTrue(current["ok"])
|
||||||
|
config = current["data"]["config"]
|
||||||
|
self.assertEqual(config["database"]["postgresql"]["dsn"], "***MASKED***")
|
||||||
|
self.assertEqual(config["database"]["postgresql"]["password"], "***MASKED***")
|
||||||
|
self.assertTrue(config["security"]["integration_token_set"])
|
||||||
|
|
||||||
|
validation = validate_settings(
|
||||||
|
self.ctx,
|
||||||
|
{"config": {"database": {"backend": "postgresql", "postgresql": {"enabled": True}}}},
|
||||||
|
self.settings_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(validation["ok"])
|
||||||
|
self.assertTrue(validation["data"]["valid"])
|
||||||
|
self.assertEqual(validation["data"]["warnings"][0]["field"], "database.postgresql")
|
||||||
|
|
||||||
|
diff = diff_settings(
|
||||||
|
self.ctx,
|
||||||
|
{
|
||||||
|
"config": {
|
||||||
|
"database": {
|
||||||
|
"backend": "postgresql",
|
||||||
|
"postgresql": {"dsn": "postgresql://changed", "password": ""},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
self.settings_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(diff["ok"])
|
||||||
|
sensitive_changes = [item for item in diff["data"]["changes"] if item["sensitive"]]
|
||||||
|
self.assertTrue(sensitive_changes)
|
||||||
|
self.assertNotIn("secret", str(diff["data"]["changes"]))
|
||||||
|
|
||||||
|
def test_settings_manage_routes_require_config_permission(self):
|
||||||
|
read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001")
|
||||||
|
|
||||||
|
response = validate_settings(read_only, {"config": {}}, self.settings_service)
|
||||||
|
|
||||||
|
self.assertFalse(response["ok"])
|
||||||
|
self.assertEqual(response["error_code"], "FORBIDDEN")
|
||||||
|
|
||||||
def test_workflow_node_image_routes_return_wrapped_responses(self):
|
def test_workflow_node_image_routes_return_wrapped_responses(self):
|
||||||
profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
|
profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用