"""Server configuration, loaded from a flat TOML file.""" from __future__ import annotations import dataclasses import os import tomllib from dataclasses import dataclass DEFAULT_PATH = "/etc/kernelwatch/server.toml" @dataclass(frozen=True) class Config: # Bind to loopback by default; put a TLS reverse proxy in front. listen_host: str = "127.0.0.1" listen_port: int = 8080 db_path: str = "/var/lib/kernelwatch/kernelwatch.db" # A host that hasn't reported for this long is shown as stale. stale_after_hours: float = 3.0 # Package lists older than this make "no updates" untrustworthy. apt_stale_after_hours: float = 48.0 report_retention_days: int = 30 session_hours: int = 12 # Leave on unless you serve the dashboard over plain HTTP on a non-localhost name. secure_cookies: bool = True # Honour X-Forwarded-For (for login rate limiting). Enable only behind a proxy. trust_proxy_headers: bool = False def load(path: str | None = None) -> Config: path = path or os.environ.get("KERNELWATCH_CONFIG") or DEFAULT_PATH if not os.path.exists(path): if path == DEFAULT_PATH: return Config() raise SystemExit(f"config file not found: {path}") with open(path, "rb") as f: raw = tomllib.load(f) fields = {f.name: f for f in dataclasses.fields(Config)} unknown = sorted(set(raw) - set(fields)) if unknown: raise SystemExit(f"{path}: unknown config keys: {', '.join(unknown)}") values = {} for name, value in raw.items(): expected = type(getattr(Config(), name)) if expected is float and isinstance(value, int) and not isinstance(value, bool): value = float(value) if not isinstance(value, expected) or (expected is int and isinstance(value, bool)): raise SystemExit(f"{path}: {name} must be {expected.__name__}") values[name] = value return Config(**values)