mirror of
https://github.com/Joulenap/joulenap.git
synced 2026-08-11 13:21:43 +02:00
The three silent ones first. A 0.9 config that fails to convert used to boot an empty config that looks exactly like a fresh install: the reason now reaches the UI (GET /api/status.config_error), the activity log and an ERROR line, and the .bak parachute is written on the failure branch too, so a later save from the Advanced tab cannot destroy the original. "PBS left powered on" was wrong in both directions -- every successful run against an always-on PBS warned, and a sync route that left its target awake did not. lease.release() returned one False for four situations, only two of which cost power; it now returns a ReleaseOutcome that names the reason, which becomes both the POWEROFF step's detail and RunContext.left_on. The interrupted-run path keeps a step-derived rule, now paired per device and filtered by managed_power. 422 bodies echoed the whole config, secrets included: a config-level validator raises at loc=(), so pydantic attached every token, the secret key, the password hash, the SMTP and bot tokens as the error's input. One helper with include_input=False now serves all three config-shaped 422 sites. Also: a redaction placeholder with nothing to resolve against is rejected instead of silently clearing the credential (a renamed device id, or a create from a copied body); the ad-hoc "Run verify" asks for outdated_after=0, since None meant "only never-verified" and skipped exactly the snapshots the button exists for; the manual power-off holds the single-run lock so it cannot cut a vzdump that started in the check-then-act gap; _current_run_id is cleared when a run ends, so a stop landing between two runs cannot hit the wrong one; and the pre-migration .bak is chmod 0600 like every other secret-bearing file. Tests: 617 passed, 2 skipped. Every finding was reproduced against the real code before the fix, and each new test confirmed failing on the pre-fix code.
190 lines
7.2 KiB
Python
190 lines
7.2 KiB
Python
"""GET/PUT /api/config — read the redacted config; apply changes and re-arm.
|
|
|
|
This is the design's "Apply changes" action: validate the whole config, persist it, then
|
|
re-arm the scheduler so a new schedule/enabled flag takes effect immediately.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import secrets
|
|
from typing import Any
|
|
|
|
import yaml
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
from ..config import (
|
|
Config,
|
|
RedactionError,
|
|
deep_merge,
|
|
enforce_server_managed,
|
|
redacted_dict,
|
|
restore_secrets,
|
|
)
|
|
from ..connectors.errors import WolError
|
|
from ..connectors.wol import normalize_mac
|
|
from ..core.config_store import ConfigStore
|
|
from ._config_edit import check_route_crons, validation_error
|
|
from .deps import Scheduler, get_config_store, get_scheduler, require_auth
|
|
|
|
router = APIRouter(dependencies=[Depends(require_auth)], tags=["config"])
|
|
|
|
|
|
@router.get("/config")
|
|
def get_config(store: ConfigStore = Depends(get_config_store)) -> dict[str, Any]:
|
|
return redacted_dict(store.config)
|
|
|
|
|
|
@router.put("/config")
|
|
def put_config(
|
|
incoming: dict[str, Any],
|
|
store: ConfigStore = Depends(get_config_store),
|
|
scheduler: Scheduler = Depends(get_scheduler),
|
|
) -> dict[str, Any]:
|
|
return _apply_config(incoming, store, scheduler)
|
|
|
|
|
|
def _apply_config(
|
|
incoming: dict[str, Any], store: ConfigStore, scheduler: Scheduler
|
|
) -> dict[str, Any]:
|
|
"""Validate + persist an incoming (partial, possibly redacted) config and re-arm.
|
|
|
|
Shared by PUT /api/config and PUT /api/config/yaml so the YAML editor goes through the
|
|
exact same validation and secret-restoration path as the settings forms.
|
|
"""
|
|
# Deep-merge over the stored config so PUT means "apply these changes", not "replace
|
|
# everything": an omitted section/field keeps its current value (a partial body can no
|
|
# longer wipe secrets). Then resolve any ***REDACTED*** the client echoed back, and force
|
|
# server-managed secrets (secret_key, password_hash, api_key) to the stored values.
|
|
base = store.config.model_dump(mode="python")
|
|
try:
|
|
merged = restore_secrets(deep_merge(base, incoming), store.config)
|
|
except RedactionError as exc:
|
|
raise HTTPException(status_code=422, detail=str(exc)) from exc
|
|
merged = enforce_server_managed(merged, store.config)
|
|
try:
|
|
new_config = Config.model_validate(merged)
|
|
except ValidationError as exc:
|
|
# 422 to mirror FastAPI's own body-validation responses.
|
|
raise validation_error(exc) from exc
|
|
|
|
# Reject a newly-set unparseable route cron before it can be persisted (BE-B1): the
|
|
# route would silently never fire, which is the failure mode hardest to notice.
|
|
old = store.config
|
|
check_route_crons(new_config, old)
|
|
|
|
# Reject a newly-set malformed WoL MAC before persisting (BE-C2), reusing the exact
|
|
# WoL parser so "fails at save" == "fails at wake time". Changed-only + non-empty: an
|
|
# empty MAC is the wizard's unconfigured state, and a legacy bad MAC on disk carried
|
|
# through an unrelated edit stays saveable (failing later at wake, as today) rather than
|
|
# locking the user out of Settings. Not a pydantic validator, so it never runs at load
|
|
# time and can't brick startup (the BE-B1 lesson).
|
|
old_macs = {p.id: p.mac for p in old.pbss}
|
|
for pbs in new_config.pbss:
|
|
if pbs.mac and pbs.mac != old_macs.get(pbs.id):
|
|
try:
|
|
normalize_mac(pbs.mac)
|
|
except WolError as exc:
|
|
raise HTTPException(
|
|
status_code=422, detail=f"pbs '{pbs.id}': invalid mac {pbs.mac!r}: {exc}"
|
|
) from exc
|
|
|
|
try:
|
|
store.replace(new_config)
|
|
except OSError as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
|
|
) from exc
|
|
|
|
scheduler.rearm(new_config)
|
|
return redacted_dict(new_config)
|
|
|
|
|
|
# --- raw YAML editing (Settings -> Advanced) ---------------------------------
|
|
#
|
|
# The editor is served the *redacted* config re-serialised with the same dumper save_config
|
|
# uses, so the text matches the on-disk file's shape without ever sending secrets to the
|
|
# browser. Saving goes through _apply_config, i.e. the same merge/restore/validate path as
|
|
# the settings forms: a key the user deletes keeps its stored value rather than being wiped.
|
|
|
|
|
|
class YamlBody(BaseModel):
|
|
yaml: str
|
|
|
|
|
|
def _dump_yaml(cfg: Any) -> str:
|
|
return yaml.safe_dump(cfg, sort_keys=False, allow_unicode=True, default_flow_style=False)
|
|
|
|
|
|
def _yaml_error(message: str, line: int | None = None) -> HTTPException:
|
|
return HTTPException(status_code=422, detail={"message": message, "line": line})
|
|
|
|
|
|
def _flatten(detail: Any) -> str:
|
|
"""Turn _apply_config's error detail into text the editor can show.
|
|
|
|
Pydantic's ``errors()`` list becomes one ``field.path: message`` per line; anything else
|
|
(the cron/MAC guards) is already a plain string.
|
|
ponytail: no loc -> line mapping — only YAML syntax errors carry a line number.
|
|
"""
|
|
if isinstance(detail, list):
|
|
return "\n".join(
|
|
f"{'.'.join(str(p) for p in e.get('loc', ()))}: {e.get('msg', '')}".lstrip(": ")
|
|
for e in detail
|
|
)
|
|
return str(detail)
|
|
|
|
|
|
@router.get("/config/yaml")
|
|
def get_config_yaml(store: ConfigStore = Depends(get_config_store)) -> dict[str, str]:
|
|
return {"yaml": _dump_yaml(redacted_dict(store.config))}
|
|
|
|
|
|
@router.put("/config/yaml")
|
|
def put_config_yaml(
|
|
body: YamlBody,
|
|
store: ConfigStore = Depends(get_config_store),
|
|
scheduler: Scheduler = Depends(get_scheduler),
|
|
) -> dict[str, Any]:
|
|
try:
|
|
incoming = yaml.safe_load(body.yaml) or {}
|
|
except yaml.YAMLError as exc:
|
|
mark = getattr(exc, "problem_mark", None)
|
|
raise _yaml_error(str(exc), mark.line + 1 if mark else None) from exc
|
|
if not isinstance(incoming, dict):
|
|
raise _yaml_error(
|
|
f"The document must be a YAML mapping, got {type(incoming).__name__}."
|
|
)
|
|
try:
|
|
return _apply_config(incoming, store, scheduler)
|
|
except HTTPException as exc:
|
|
# Re-shape validation failures into the editor's {message, line} contract; PUT
|
|
# /api/config keeps its own. A 500 (unwritable config.yaml) passes through as-is.
|
|
if exc.status_code != 422:
|
|
raise
|
|
raise _yaml_error(_flatten(exc.detail)) from exc
|
|
|
|
|
|
@router.post("/config/api-key", status_code=status.HTTP_200_OK)
|
|
def generate_api_key(store: ConfigStore = Depends(get_config_store)) -> dict[str, str]:
|
|
"""Generate (or rotate) the dashboard integration key; returns it once."""
|
|
key = secrets.token_urlsafe(32)
|
|
try:
|
|
store.update(lambda c: setattr(c.app, "api_key", key))
|
|
except OSError as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
|
|
) from exc
|
|
return {"api_key": key}
|
|
|
|
|
|
@router.delete("/config/api-key", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_api_key(store: ConfigStore = Depends(get_config_store)) -> None:
|
|
"""Clear the dashboard integration key (disables GET /api/dashboard)."""
|
|
try:
|
|
store.update(lambda c: setattr(c.app, "api_key", ""))
|
|
except OSError as exc:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
|
|
) from exc
|