From 900e31dce918a00fe73843406406dbdf05144b9f Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:10:44 +0200 Subject: [PATCH 01/51] Add scoped obsolete transit peer removal --- app.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/app.py b/app.py index 37b94c6..5a99cdc 100644 --- a/app.py +++ b/app.py @@ -1274,6 +1274,85 @@ async def network_wireguard_status(host: str, _=Depends(_verify)): return {"host": host, **result} +class WireGuardPeerRemoveRequest(BaseModel): + public_key: str + expected_allowed_ip: str + dry_run: bool = True + + +def _wireguard_remove_peer_command(public_key: str, expected_allowed_ip: str, dry_run: bool) -> str: + script = f'''import json, os, re, shutil, subprocess, time +from pathlib import Path + +public_key = {public_key!r} +expected = {expected_allowed_ip!r} +dry_run = {dry_run!r} +config = Path("/etc/wireguard/wg0.conf") +text = config.read_text() +sections = re.split(r"(?=^\\[Peer\\]\\s*$)", text, flags=re.M) +matches = [] +for index, section in enumerate(sections): + key_match = re.search(r"^PublicKey\\s*=\\s*(\\S+)\\s*$", section, re.M) + allowed_match = re.search(r"^AllowedIPs\\s*=\\s*(.+?)\\s*$", section, re.M) + allowed = [item.strip() for item in allowed_match.group(1).split(",")] if allowed_match else [] + if key_match and key_match.group(1) == public_key and expected in allowed: + matches.append((index, allowed)) +if len(matches) != 1: + print(json.dumps({{"error": "expected exactly one matching peer", "matches": len(matches)}})); raise SystemExit(2) +index, allowed = matches[0] +result = {{"status": "would_remove" if dry_run else "removed", "allowed_ips": allowed, "removed_routes": [], "backup": None}} +if dry_run: + print(json.dumps(result)); raise SystemExit(0) +backup = config.with_name("wg0.conf.butler-" + time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())) +shutil.copy2(config, backup) +result["backup"] = str(backup) +new_text = "".join(section for number, section in enumerate(sections) if number != index) +tmp = config.with_name("wg0.conf.butler-tmp") +tmp.write_text(new_text) +os.chmod(tmp, config.stat().st_mode) +os.chown(tmp, config.stat().st_uid, config.stat().st_gid) +os.replace(tmp, config) +try: + subprocess.run(["wg", "set", "wg0", "peer", public_key, "remove"], check=True, text=True, capture_output=True) + for route in allowed: + proc = subprocess.run(["ip", "route", "del", route, "dev", "wg0"], text=True, capture_output=True) + if proc.returncode == 0: result["removed_routes"].append(route) + peers = subprocess.run(["wg", "show", "wg0", "peers"], check=True, text=True, capture_output=True).stdout.split() + if public_key in peers: raise RuntimeError("peer still active") +except Exception: + shutil.copy2(backup, config) + raise +print(json.dumps(result)) +''' + encoded = base64.b64encode(script.encode()).decode() + return f'sudo -n python3 -c "import base64;exec(base64.b64decode(\'{encoded}\'))"' + + +@app.delete("/network/wireguard/{host}/peer") +async def network_wireguard_remove_peer(host: str, req: WireGuardPeerRemoveRequest, _=Depends(_verify)): + allowed = {"guck-vps": "10.7.1.0/24", "pfannkuchen": "10.200.200.60/32"} + if host not in allowed: + raise HTTPException(403, "Peer removal is restricted to the obsolete OVH-Hetzner transit") + if req.expected_allowed_ip != allowed[host]: + raise HTTPException(400, "Unexpected AllowedIP for this host") + if not re.fullmatch(r"[A-Za-z0-9+/]{43}=", req.public_key): + raise HTTPException(400, "Invalid WireGuard public key") + inventory = await asyncio.to_thread(_find_inventory_host, host) + if not inventory: + raise HTTPException(404, f"Host {host} not found") + target = f'{inventory["user"]}@{inventory["ip"]}' + command = _wireguard_remove_peer_command(req.public_key, req.expected_allowed_ip, req.dry_run) + rc, out, err = await asyncio.to_thread(_ssh, target, command, 45) + if rc != 0: + raise HTTPException(502, (err or out).strip()[-500:] or "WireGuard peer removal failed") + try: + result = json.loads(out) + except json.JSONDecodeError as exc: + raise HTTPException(502, "WireGuard peer removal returned invalid JSON") from exc + _audit(f"/network/wireguard/{host}/peer", "DELETE", 200, f"dry_run={req.dry_run}") + return {"host": host, **result} + + SYSCTL_AUDIT_KEYS = ( "net.core.default_qdisc", "net.core.rmem_default", From f39a72681cc2aac7b6c969a46c0352a155a5cd61 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:10:44 +0200 Subject: [PATCH 02/51] Add scoped obsolete transit peer removal --- tests/test_app.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index c49a918..726d11d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -104,6 +104,37 @@ def test_wireguard_status_command_uses_sudo_and_accepts_off_keepalive(): assert '0 if fields[7] == "off" else int(fields[7])' in script +def test_wireguard_peer_remove_is_scoped_and_audited(monkeypatch): + calls = [] + monkeypatch.setattr(app, "_find_inventory_host", lambda host: {"user": "debian", "ip": "141.94.237.199"}) + monkeypatch.setattr(app, "_ssh", lambda host, command, timeout=30: (calls.append((host, command, timeout)) or (0, json.dumps({"status": "removed", "removed_routes": ["10.7.1.0/24"]}), ""))) + + with TestClient(app.app) as client: + response = client.request( + "DELETE", + "/network/wireguard/guck-vps/peer", + headers={"Authorization": "Bearer test-token"}, + json={"public_key": "A" * 43 + "=", "expected_allowed_ip": "10.7.1.0/24", "dry_run": False}, + ) + + assert response.status_code == 200 + assert response.json()["status"] == "removed" + assert calls[0][0] == "debian@141.94.237.199" + assert calls[0][2] == 45 + + +def test_wireguard_peer_remove_rejects_non_allowlisted_host(monkeypatch): + monkeypatch.setattr(app, "_ssh", lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("SSH must not run"))) + with TestClient(app.app) as client: + response = client.request( + "DELETE", + "/network/wireguard/node7/peer", + headers={"Authorization": "Bearer test-token"}, + json={"public_key": "A" * 43 + "=", "expected_allowed_ip": "10.7.1.0/24", "dry_run": True}, + ) + assert response.status_code == 403 + + def test_tts_generate_returns_cloned_wav(monkeypatch): captured = {} From f2c5fa705175bd33e47ab628c716bbf28c9ff5f8 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:10:46 +0200 Subject: [PATCH 03/51] Add scoped removal for obsolete OVH-Hetzner peer (#39) --- app.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_app.py | 31 +++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/app.py b/app.py index 37b94c6..5a99cdc 100644 --- a/app.py +++ b/app.py @@ -1274,6 +1274,85 @@ async def network_wireguard_status(host: str, _=Depends(_verify)): return {"host": host, **result} +class WireGuardPeerRemoveRequest(BaseModel): + public_key: str + expected_allowed_ip: str + dry_run: bool = True + + +def _wireguard_remove_peer_command(public_key: str, expected_allowed_ip: str, dry_run: bool) -> str: + script = f'''import json, os, re, shutil, subprocess, time +from pathlib import Path + +public_key = {public_key!r} +expected = {expected_allowed_ip!r} +dry_run = {dry_run!r} +config = Path("/etc/wireguard/wg0.conf") +text = config.read_text() +sections = re.split(r"(?=^\\[Peer\\]\\s*$)", text, flags=re.M) +matches = [] +for index, section in enumerate(sections): + key_match = re.search(r"^PublicKey\\s*=\\s*(\\S+)\\s*$", section, re.M) + allowed_match = re.search(r"^AllowedIPs\\s*=\\s*(.+?)\\s*$", section, re.M) + allowed = [item.strip() for item in allowed_match.group(1).split(",")] if allowed_match else [] + if key_match and key_match.group(1) == public_key and expected in allowed: + matches.append((index, allowed)) +if len(matches) != 1: + print(json.dumps({{"error": "expected exactly one matching peer", "matches": len(matches)}})); raise SystemExit(2) +index, allowed = matches[0] +result = {{"status": "would_remove" if dry_run else "removed", "allowed_ips": allowed, "removed_routes": [], "backup": None}} +if dry_run: + print(json.dumps(result)); raise SystemExit(0) +backup = config.with_name("wg0.conf.butler-" + time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())) +shutil.copy2(config, backup) +result["backup"] = str(backup) +new_text = "".join(section for number, section in enumerate(sections) if number != index) +tmp = config.with_name("wg0.conf.butler-tmp") +tmp.write_text(new_text) +os.chmod(tmp, config.stat().st_mode) +os.chown(tmp, config.stat().st_uid, config.stat().st_gid) +os.replace(tmp, config) +try: + subprocess.run(["wg", "set", "wg0", "peer", public_key, "remove"], check=True, text=True, capture_output=True) + for route in allowed: + proc = subprocess.run(["ip", "route", "del", route, "dev", "wg0"], text=True, capture_output=True) + if proc.returncode == 0: result["removed_routes"].append(route) + peers = subprocess.run(["wg", "show", "wg0", "peers"], check=True, text=True, capture_output=True).stdout.split() + if public_key in peers: raise RuntimeError("peer still active") +except Exception: + shutil.copy2(backup, config) + raise +print(json.dumps(result)) +''' + encoded = base64.b64encode(script.encode()).decode() + return f'sudo -n python3 -c "import base64;exec(base64.b64decode(\'{encoded}\'))"' + + +@app.delete("/network/wireguard/{host}/peer") +async def network_wireguard_remove_peer(host: str, req: WireGuardPeerRemoveRequest, _=Depends(_verify)): + allowed = {"guck-vps": "10.7.1.0/24", "pfannkuchen": "10.200.200.60/32"} + if host not in allowed: + raise HTTPException(403, "Peer removal is restricted to the obsolete OVH-Hetzner transit") + if req.expected_allowed_ip != allowed[host]: + raise HTTPException(400, "Unexpected AllowedIP for this host") + if not re.fullmatch(r"[A-Za-z0-9+/]{43}=", req.public_key): + raise HTTPException(400, "Invalid WireGuard public key") + inventory = await asyncio.to_thread(_find_inventory_host, host) + if not inventory: + raise HTTPException(404, f"Host {host} not found") + target = f'{inventory["user"]}@{inventory["ip"]}' + command = _wireguard_remove_peer_command(req.public_key, req.expected_allowed_ip, req.dry_run) + rc, out, err = await asyncio.to_thread(_ssh, target, command, 45) + if rc != 0: + raise HTTPException(502, (err or out).strip()[-500:] or "WireGuard peer removal failed") + try: + result = json.loads(out) + except json.JSONDecodeError as exc: + raise HTTPException(502, "WireGuard peer removal returned invalid JSON") from exc + _audit(f"/network/wireguard/{host}/peer", "DELETE", 200, f"dry_run={req.dry_run}") + return {"host": host, **result} + + SYSCTL_AUDIT_KEYS = ( "net.core.default_qdisc", "net.core.rmem_default", diff --git a/tests/test_app.py b/tests/test_app.py index c49a918..726d11d 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -104,6 +104,37 @@ def test_wireguard_status_command_uses_sudo_and_accepts_off_keepalive(): assert '0 if fields[7] == "off" else int(fields[7])' in script +def test_wireguard_peer_remove_is_scoped_and_audited(monkeypatch): + calls = [] + monkeypatch.setattr(app, "_find_inventory_host", lambda host: {"user": "debian", "ip": "141.94.237.199"}) + monkeypatch.setattr(app, "_ssh", lambda host, command, timeout=30: (calls.append((host, command, timeout)) or (0, json.dumps({"status": "removed", "removed_routes": ["10.7.1.0/24"]}), ""))) + + with TestClient(app.app) as client: + response = client.request( + "DELETE", + "/network/wireguard/guck-vps/peer", + headers={"Authorization": "Bearer test-token"}, + json={"public_key": "A" * 43 + "=", "expected_allowed_ip": "10.7.1.0/24", "dry_run": False}, + ) + + assert response.status_code == 200 + assert response.json()["status"] == "removed" + assert calls[0][0] == "debian@141.94.237.199" + assert calls[0][2] == 45 + + +def test_wireguard_peer_remove_rejects_non_allowlisted_host(monkeypatch): + monkeypatch.setattr(app, "_ssh", lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("SSH must not run"))) + with TestClient(app.app) as client: + response = client.request( + "DELETE", + "/network/wireguard/node7/peer", + headers={"Authorization": "Bearer test-token"}, + json={"public_key": "A" * 43 + "=", "expected_allowed_ip": "10.7.1.0/24", "dry_run": True}, + ) + assert response.status_code == 403 + + def test_tts_generate_returns_cloned_wav(monkeypatch): captured = {} From c7dd2ea97230add71a156d918fe298cc2cc1d43a Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:18:29 +0200 Subject: [PATCH 04/51] Add live capabilities and safety map (#40) --- app.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++ tests/test_app.py | 27 +++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/app.py b/app.py index 5a99cdc..aa94ed4 100644 --- a/app.py +++ b/app.py @@ -264,6 +264,7 @@ async def root(): "openapi": "/openapi.json", "services": svc_list, "endpoints": { + "capabilities": "GET /capabilities - live machine-readable operation and safety map", "proxy": "GET/POST/PUT/DELETE /{service}/{path} - proxy to backend with auto-auth", "vm_list": "GET /vm/list", "vm_create": "POST /vm/create {node, ip, hostname, cores?, memory?, disk?}", @@ -288,6 +289,80 @@ async def root(): async def health(): return {"status": "ok", "vault_items": len(_vault_cache), "services": len(SERVICES), "version": VERSION} + +def _schema_contains_property(node, property_name: str, components: dict, seen: set[str] | None = None) -> bool: + """Resolve local OpenAPI refs and look for a request property.""" + seen = seen or set() + if isinstance(node, list): + return any(_schema_contains_property(item, property_name, components, seen) for item in node) + if not isinstance(node, dict): + return False + if node.get("name") == property_name or property_name in node.get("properties", {}): + return True + ref = node.get("$ref", "") + if ref.startswith("#/components/schemas/"): + name = ref.rsplit("/", 1)[-1] + if name in seen: + return False + return _schema_contains_property(components.get(name, {}), property_name, components, seen | {name}) + return any( + _schema_contains_property(value, property_name, components, seen) + for key, value in node.items() + if key != "properties" + ) + + +@app.get("/capabilities") +async def capabilities(_=Depends(_verify)): + """Live operation catalog with safety metadata for AI agents.""" + schema = app.openapi() + components = schema.get("components", {}).get("schemas", {}) + operations = [] + for path, methods in schema.get("paths", {}).items(): + if path == "/{service}/{path}" or path in {"/", "/health", "/openapi.json", "/docs", "/redoc"}: + continue + for method, operation in methods.items(): + if method.upper() not in {"GET", "POST", "PUT", "PATCH", "DELETE"}: + continue + mode = "read_only" if method.upper() == "GET" else "mutation" + serialized = {"parameters": operation.get("parameters", []), "requestBody": operation.get("requestBody", {})} + dry_run = _schema_contains_property(serialized, "dry_run", components) + critical = path.startswith(("/network/wireguard", "/caddy/")) + destructive = method.upper() == "DELETE" or any( + marker in path for marker in ("/destroy/", "/cleanup/", "/break-lock/", "/restore/") + ) + operations.append({ + "method": method.upper(), + "path": path, + "summary": operation.get("summary", ""), + "description": operation.get("description", ""), + "mode": mode, + "dry_run": dry_run, + "critical": critical, + "destructive": destructive, + "confirmation_required": mode == "mutation", + }) + operations.sort(key=lambda item: (item["path"], item["method"])) + counts = { + "total": len(operations), + "read_only": sum(item["mode"] == "read_only" for item in operations), + "mutations": sum(item["mode"] == "mutation" for item in operations), + "destructive": sum(item["destructive"] for item in operations), + } + return { + "schema_version": 1, + "service": "homelab-butler", + "version": VERSION, + "generated": datetime.now(timezone.utc).isoformat(), + "counts": counts, + "operations": operations, + "proxy": {"path": "/{service}/{path}", "note": "Generic backend proxy; inspect /info services and OpenAPI before use"}, + "model_contract": { + "instruction": "Prefer read_only operations. Before every mutation inspect its schema, use dry_run when available, and obtain confirmation for critical or destructive actions.", + "source_of_truth": "/openapi.json", + }, + } + def _classify_http_status(status_code: int, expected: set[int]) -> str: """Return a deterministic service state suitable for small models.""" if status_code in expected: @@ -408,6 +483,7 @@ async def info(_=Depends(_verify)): for name, cfg in SERVICES.items() }, "endpoints": { + "capabilities": "/capabilities", "status": "/status", "overview": "/overview?details=false", "audit": "/audit", diff --git a/tests/test_app.py b/tests/test_app.py index 726d11d..a9a0ae1 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -46,6 +46,33 @@ def test_health_exposes_current_version(): assert response.json()["version"] == app.VERSION == "2.3.5" +def test_capabilities_is_live_machine_readable_safety_map(): + with TestClient(app.app) as client: + response = client.get( + "/capabilities", + headers={"Authorization": "Bearer test-token"}, + ) + assert response.status_code == 200 + payload = response.json() + by_operation = {(item["method"], item["path"]): item for item in payload["operations"]} + assert ("GET", "/network/wireguard/{host}") in by_operation + assert by_operation[("GET", "/network/wireguard/{host}")]["mode"] == "read_only" + removal = by_operation[("DELETE", "/network/wireguard/{host}/peer")] + assert removal["mode"] == "mutation" + assert removal["dry_run"] is True + assert removal["critical"] is True + assert by_operation[("DELETE", "/vm/destroy/{vmid}")]["dry_run"] is True + assert all(item["path"] != "/{service}/{path}" for item in payload["operations"]) + assert payload["model_contract"]["instruction"].startswith("Prefer read_only") + + +def test_info_advertises_capabilities_endpoint(): + with TestClient(app.app) as client: + response = client.get("/info", headers={"Authorization": "Bearer test-token"}) + assert response.status_code == 200 + assert response.json()["endpoints"]["capabilities"] == "/capabilities" + + def test_wireguard_status_returns_redacted_live_state(monkeypatch): payload = { "interface": "wg0", From 5058ddf0a11ba2acb2ccd15130c12dabaed8e5ce Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:36:22 +0200 Subject: [PATCH 05/51] Add Butler operational safety suite (#41) --- .gitignore | 1 + app.py | 195 +++++++++++++++++++++++++++++++++++++++++++++- butler.yaml | 4 + compose.yaml | 2 + tests/test_app.py | 111 +++++++++++++++++++++++++- 5 files changed, 308 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d5b4207..47bca58 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vault-sync.log __pycache__/ *.pyc +state/ diff --git a/app.py b/app.py index aa94ed4..91be060 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ """Homelab Butler v2.1 – Unified API proxy for Pfannkuchen homelab. Reads service config from butler.yaml, credentials from Vaultwarden cache with flat-file fallback.""" -import os, json, asyncio, logging, time, base64, re, subprocess, ipaddress, secrets +import os, json, asyncio, logging, time, base64, re, subprocess, ipaddress, secrets, sqlite3 from datetime import datetime, timezone import httpx, yaml from typing import Literal @@ -46,6 +46,33 @@ _load_config() _audit_log: list[dict] = [] MAX_AUDIT = 500 +AUDIT_DB_PATH = os.environ.get("AUDIT_DB_PATH", "/data/state/audit.sqlite3") + + +def _redact_audit_detail(detail: str) -> str: + return re.sub( + r"(?i)\b(token|password|api[_-]?key|secret)=([^\s]+)", + lambda match: f"{match.group(1)}=[REDACTED]", + detail, + )[:200] + + +def _init_audit_db() -> bool: + try: + os.makedirs(os.path.dirname(AUDIT_DB_PATH), exist_ok=True) + with sqlite3.connect(AUDIT_DB_PATH) as db: + db.execute("""CREATE TABLE IF NOT EXISTS audit ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ts TEXT NOT NULL, + endpoint TEXT NOT NULL, + method TEXT NOT NULL, + status INTEGER NOT NULL, + detail TEXT NOT NULL, + dry_run INTEGER NOT NULL + )""") + return True + except (OSError, sqlite3.Error): + return False def _audit(endpoint: str, method: str, status: int, detail: str = "", dry_run: bool = False): entry = { @@ -53,12 +80,22 @@ def _audit(endpoint: str, method: str, status: int, detail: str = "", dry_run: b "endpoint": endpoint, "method": method, "status": status, - "detail": detail[:200], + "detail": _redact_audit_detail(detail), "dry_run": dry_run, } _audit_log.append(entry) if len(_audit_log) > MAX_AUDIT: _audit_log.pop(0) + try: + if _init_audit_db(): + with sqlite3.connect(AUDIT_DB_PATH) as db: + db.execute( + "INSERT INTO audit (ts, endpoint, method, status, detail, dry_run) VALUES (?, ?, ?, ?, ?, ?)", + (entry["ts"], entry["endpoint"], entry["method"], entry["status"], entry["detail"], int(entry["dry_run"])), + ) + db.execute("DELETE FROM audit WHERE id NOT IN (SELECT id FROM audit ORDER BY id DESC LIMIT ?)", (MAX_AUDIT,)) + except (OSError, sqlite3.Error): + pass API_DIR = os.environ.get("API_KEY_DIR", "/data/api") VAULT_CACHE_DIR = os.environ.get("VAULT_CACHE_DIR", "/data/vault-cache") @@ -92,6 +129,7 @@ async def _periodic_cache_reload(): async def lifespan(app: FastAPI): _load_config() _load_vault_cache() + _init_audit_db() task = asyncio.create_task(_periodic_cache_reload()) yield task.cancel() @@ -265,6 +303,9 @@ async def root(): "services": svc_list, "endpoints": { "capabilities": "GET /capabilities - live machine-readable operation and safety map", + "doctor": "GET /doctor/{target} - correlated service/host/backup/disk diagnosis", + "drift": "GET /drift - inventory coverage gaps", + "maintenance_preflight": "GET /maintenance/preflight?action=general&target=HOST - read-only safety gate", "proxy": "GET/POST/PUT/DELETE /{service}/{path} - proxy to backend with auto-auth", "vm_list": "GET /vm/list", "vm_create": "POST /vm/create {node, ip, hostname, cores?, memory?, disk?}", @@ -457,6 +498,17 @@ async def status(_=Depends(_verify)): @app.get("/audit") async def audit(_=Depends(_verify), limit: int = Query(50, le=MAX_AUDIT)): """Recent API calls (newest first).""" + try: + if os.path.exists(AUDIT_DB_PATH): + with sqlite3.connect(AUDIT_DB_PATH) as db: + db.row_factory = sqlite3.Row + rows = db.execute( + "SELECT ts, endpoint, method, status, detail, dry_run FROM audit ORDER BY id DESC LIMIT ?", + (limit,), + ).fetchall() + return [dict(row) | {"dry_run": bool(row["dry_run"])} for row in rows] + except (OSError, sqlite3.Error): + pass return list(reversed(_audit_log[-limit:])) @app.post("/config/reload") @@ -484,6 +536,9 @@ async def info(_=Depends(_verify)): }, "endpoints": { "capabilities": "/capabilities", + "doctor": "/doctor/{target}", + "drift": "/drift", + "maintenance_preflight": "/maintenance/preflight", "status": "/status", "overview": "/overview?details=false", "audit": "/audit", @@ -599,8 +654,11 @@ async def _collect_backup_status(concurrency: int = 10) -> dict: """Query VM backups concurrently; one slow host no longer blocks all others serially.""" semaphore = asyncio.Semaphore(concurrency) hosts = [host for host in await _get_inventory_hosts_async() if not host["name"].startswith("node")] + exempt_hosts = set(_config.get("backup", {}).get("exempt_hosts", [])) async def inspect_backup(host: dict): + if host["name"] in exempt_hosts: + return host["name"], {"state": "exempt", "ok": True, "reason": "backup policy exemption"} async with semaphore: rc, out, err = await asyncio.to_thread( _ssh, @@ -612,7 +670,7 @@ async def _collect_backup_status(concurrency: int = 10) -> dict: pairs = await asyncio.gather(*(inspect_backup(host) for host in hosts)) results = dict(pairs) - summary = {"total": len(results), "healthy": 0, "warning": 0, "critical": 0, "unknown": 0} + summary = {"total": len(results), "healthy": 0, "warning": 0, "critical": 0, "unknown": 0, "exempt": 0} for item in results.values(): summary[item["state"]] += 1 return {"summary": summary, "hosts": results} @@ -780,6 +838,135 @@ def _add_component(summary: dict, bucket: dict, state: str): bucket[normalized] += 1 +async def _collect_operational_snapshot() -> dict: + services, hosts, backups, disks = await asyncio.gather( + _collect_service_status(), _collect_health_all(), _collect_backup_status(), _collect_disk_usage() + ) + return {"services": services, "hosts": hosts, "backups": backups, "disks": disks} + + +@app.get("/doctor/{target}") +async def doctor(target: str, _=Depends(_verify)): + """Correlate service, host, backup and disk layers for one known target.""" + if not re.fullmatch(r"[A-Za-z0-9_.-]+", target): + raise HTTPException(400, "Invalid target") + snapshot = await _collect_operational_snapshot() + layers = {} + findings = [] + if target in snapshot["services"]: + service = snapshot["services"][target] + layers["service"] = service + if service.get("status") != "healthy": + findings.append({"severity": "critical" if service.get("status") in ("offline", "auth_failed", "misconfigured") else "warning", "code": "service_unhealthy", "message": service.get("message", "Service probe failed")}) + if target in snapshot["hosts"]: + host = snapshot["hosts"][target] + layers["host"] = host + if not host.get("reachable"): + findings.append({"severity": "critical", "code": "host_unreachable", "message": "Host is not reachable over SSH"}) + bad = [line for line in host.get("containers", []) if "unhealthy" in line.lower() or "restarting" in line.lower()] + if bad: + findings.append({"severity": "critical", "code": "container_unhealthy", "message": bad[0][:200]}) + backup = snapshot["backups"].get("hosts", {}).get(target) + if backup is not None: + layers["backup"] = backup + if backup.get("state") not in ("healthy", "exempt"): + findings.append({"severity": "critical" if backup.get("state") in ("critical", "unknown") else "warning", "code": "backup_unhealthy", "message": "Backup is stale or could not be verified"}) + disk = snapshot["disks"].get(target) + if disk is not None: + layers["disk"] = disk + pct = int(str(disk.get("pct", "0")).rstrip("%") or 0) + if pct >= 80: + findings.append({"severity": "critical" if pct >= 90 else "warning", "code": "disk_high", "message": f"Root filesystem usage is {pct}%"}) + if not layers: + raise HTTPException(404, "Target not found") + state = "critical" if any(item["severity"] == "critical" for item in findings) else "warning" if findings else "healthy" + return {"target": target, "state": state, "findings": findings, "layers": layers, "next_checks": [f"/logs/{target}/{{container}}", f"/system/forensics/{target}"] if "host" in layers else []} + + +@app.get("/drift") +async def drift(_=Depends(_verify)): + """Report coverage drift between inventory, backup and disk collectors.""" + snapshot = await _collect_operational_snapshot() + inventory = set(snapshot["hosts"]) + managed_hosts = {name for name in inventory if not name.startswith("node")} + backups = set(snapshot["backups"].get("hosts", {})) + disks = set(snapshot["disks"]) + findings = [] + for name in sorted(managed_hosts - backups): + findings.append({"severity": "warning", "code": "inventory_missing_backup", "target": name}) + for name in sorted(inventory - disks): + findings.append({"severity": "warning", "code": "inventory_missing_disk", "target": name}) + for name in sorted(backups - inventory): + findings.append({"severity": "warning", "code": "backup_without_inventory", "target": name}) + return { + "state": "warning" if findings else "healthy", + "findings": findings, + "coverage": {"inventory": len(inventory), "backups": len(backups), "disks": len(disks)}, + "model_contract": {"instruction": "Treat findings as coverage gaps, not proof that the target is offline."}, + } + + +async def _collect_active_backups(concurrency: int = 10) -> dict: + hosts = [host for host in await _get_inventory_hosts_async() if not host["name"].startswith("node")] + exempt = set(_config.get("backup", {}).get("exempt_hosts", [])) + semaphore = asyncio.Semaphore(concurrency) + + async def check(host: dict): + if host["name"] in exempt: + return host["name"], "exempt" + async with semaphore: + rc, out, _err = await asyncio.to_thread( + _ssh, + f'{host["user"]}@{host["ip"]}', + "sudo -n systemctl is-active borg-backup.service 2>/dev/null || true", + 10, + ) + state = out.strip().splitlines()[-1] if out.strip() else "unknown" + return host["name"], state if rc == 0 else "unknown" + + return dict(await asyncio.gather(*(check(host) for host in hosts))) + + +@app.get("/maintenance/preflight") +async def maintenance_preflight( + action: Literal["general", "docker", "network", "vm"] = Query("general"), + target: str | None = Query(None), + _=Depends(_verify), +): + """Read-only safety gate before maintenance or mutations.""" + if target and not re.fullmatch(r"[A-Za-z0-9_.-]+", target): + raise HTTPException(400, "Invalid target") + snapshot, active_backups = await asyncio.gather(_collect_operational_snapshot(), _collect_active_backups()) + if target and target not in snapshot["hosts"] and target not in snapshot["services"]: + raise HTTPException(404, "Target not found") + selected = {target} if target else set(snapshot["hosts"]) + blockers = [] + warnings = [] + for name in sorted(selected): + host = snapshot["hosts"].get(name) + if host and not host.get("reachable"): + blockers.append({"code": "host_unreachable", "target": name}) + if host and any("unhealthy" in line.lower() or "restarting" in line.lower() for line in host.get("containers", [])): + blockers.append({"code": "container_unhealthy", "target": name}) + if active_backups.get(name) in ("active", "activating"): + blockers.append({"code": "backup_active", "target": name}) + backup = snapshot["backups"].get("hosts", {}).get(name, {}) + if backup.get("state") not in (None, "healthy", "exempt"): + warnings.append({"code": "backup_unhealthy", "target": name}) + disk = snapshot["disks"].get(name, {}) + pct = int(str(disk.get("pct", "0")).rstrip("%") or 0) + if pct >= 80: + warnings.append({"code": "disk_high", "target": name, "pct": pct}) + return { + "safe": not blockers, + "action": action, + "target": target, + "blockers": blockers, + "warnings": warnings, + "model_contract": {"instruction": "Do not start the requested maintenance while safe is false."}, + } + + @app.get("/overview", response_model=OverviewResponse, response_model_exclude_none=True) async def overview(details: bool = Query(False), _=Depends(_verify)): """Compact deterministic homelab verdict designed for small language models.""" @@ -843,7 +1030,7 @@ async def overview(details: bool = Query(False), _=Depends(_verify)): for name in sorted(backups.get("hosts", {})): item = backups["hosts"][name] raw_state = item.get("state", "unknown") - state = "healthy" if raw_state == "healthy" else "critical" if raw_state in ("critical", "unknown") else "warning" + state = "healthy" if raw_state in ("healthy", "exempt") else "critical" if raw_state in ("critical", "unknown") else "warning" _add_component(summary, component_summary["backups"], state) if state != "healthy": findings.append({ diff --git a/butler.yaml b/butler.yaml index 4624d82..0f8ca9e 100644 --- a/butler.yaml +++ b/butler.yaml @@ -105,6 +105,10 @@ services: health_path: "/" timeout: 300 +backup: + # guck-vps contains Git-managed edge configuration and has no Borgmatic installation. + exempt_hosts: [guck-vps] + # VM lifecycle settings vm: automation_host: "sascha@10.5.85.5" diff --git a/compose.yaml b/compose.yaml index b593f71..e0d1f56 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,10 +11,12 @@ services: - /home/sascha/.ssh:/root/.ssh:ro - ./butler.yaml:/data/butler.yaml:ro - ./app.py:/app/app.py:ro + - ./state:/data/state environment: - API_KEY_DIR=/data/api - VAULT_CACHE_DIR=/data/vault-cache - BUTLER_TOKEN=${BUTLER_TOKEN} + - AUDIT_DB_PATH=/data/state/audit.sqlite3 volumes: vault-cache: diff --git a/tests/test_app.py b/tests/test_app.py index a9a0ae1..645220a 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -71,6 +71,28 @@ def test_info_advertises_capabilities_endpoint(): response = client.get("/info", headers={"Authorization": "Bearer test-token"}) assert response.status_code == 200 assert response.json()["endpoints"]["capabilities"] == "/capabilities" + assert response.json()["endpoints"]["doctor"] == "/doctor/{target}" + assert response.json()["endpoints"]["drift"] == "/drift" + assert response.json()["endpoints"]["maintenance_preflight"] == "/maintenance/preflight" + + +def test_audit_persists_and_redacts_secrets(tmp_path, monkeypatch): + db = tmp_path / "audit.sqlite3" + monkeypatch.setattr(app, "AUDIT_DB_PATH", str(db)) + app._init_audit_db() + app._audit("/danger", "POST", 200, "host=x token=abc password=hunter2 api_key=secret") + app._audit_log.clear() + + with TestClient(app.app) as client: + response = client.get("/audit", headers={"Authorization": "Bearer test-token"}) + + assert response.status_code == 200 + entry = response.json()[0] + assert entry["endpoint"] == "/danger" + assert "abc" not in entry["detail"] + assert "hunter2" not in entry["detail"] + assert "secret" not in entry["detail"] + assert entry["detail"].count("[REDACTED]") == 3 def test_wireguard_status_returns_redacted_live_state(monkeypatch): @@ -633,6 +655,7 @@ def test_backup_collection_runs_hosts_concurrently(monkeypatch): {"name": f"vm-{index}", "user": "sascha", "ip": f"10.1.1.{index}"} for index in range(1, 5) ]) + monkeypatch.setattr(app, "_config", {}) def fake_ssh(*_args, **_kwargs): nonlocal active, max_active @@ -647,7 +670,24 @@ def test_backup_collection_runs_hosts_concurrently(monkeypatch): assert max_active > 1 assert result["summary"] == { - "total": 4, "healthy": 4, "warning": 0, "critical": 0, "unknown": 0 + "total": 4, "healthy": 4, "warning": 0, "critical": 0, "unknown": 0, "exempt": 0 + } + + +def test_backup_policy_exempts_host_without_borg_call(monkeypatch): + monkeypatch.setattr(app, "_get_inventory_hosts", lambda: [ + {"name": "guck-vps", "user": "debian", "ip": "141.94.237.199"} + ]) + monkeypatch.setattr(app, "_config", {"backup": {"exempt_hosts": ["guck-vps"]}}) + monkeypatch.setattr(app, "_ssh", lambda *_args, **_kwargs: (_ for _ in ()).throw(AssertionError("must not call borg"))) + + result = asyncio.run(app._collect_backup_status()) + + assert result["summary"] == { + "total": 1, "healthy": 0, "warning": 0, "critical": 0, "unknown": 0, "exempt": 1 + } + assert result["hosts"]["guck-vps"] == { + "state": "exempt", "ok": True, "reason": "backup policy exemption" } @@ -661,6 +701,75 @@ def test_overview_openapi_has_stable_enums_and_schema(): assert finding_schema["properties"]["severity"]["enum"] == ["healthy", "warning", "critical"] +def test_doctor_correlates_host_layers(monkeypatch): + async def snapshot(): + return { + "services": {}, + "hosts": {"guck-vps": {"reachable": True, "containers": ["caddy: Up 3 days"]}}, + "backups": {"summary": {}, "hosts": {"guck-vps": {"state": "exempt", "ok": True}}}, + "disks": {"guck-vps": {"pct": "8%"}}, + } + + monkeypatch.setattr(app, "_collect_operational_snapshot", snapshot) + with TestClient(app.app) as client: + response = client.get("/doctor/guck-vps", headers={"Authorization": "Bearer test-token"}) + + assert response.status_code == 200 + result = response.json() + assert result["state"] == "healthy" + assert result["layers"]["host"]["reachable"] is True + assert result["layers"]["backup"]["state"] == "exempt" + assert result["layers"]["disk"]["pct"] == "8%" + assert result["findings"] == [] + + +def test_drift_reports_inventory_coverage_gaps(monkeypatch): + async def snapshot(): + return { + "services": {}, + "hosts": {"vm-a": {"reachable": True}, "vm-b": {"reachable": True}, "node1": {"reachable": True}}, + "backups": {"summary": {}, "hosts": {"vm-a": {"state": "healthy"}, "orphan": {"state": "healthy"}}}, + "disks": {"vm-a": {"pct": "10%"}, "node1": {"pct": "20%"}}, + } + + monkeypatch.setattr(app, "_collect_operational_snapshot", snapshot) + with TestClient(app.app) as client: + response = client.get("/drift", headers={"Authorization": "Bearer test-token"}) + + assert response.status_code == 200 + result = response.json() + assert result["state"] == "warning" + assert {item["code"] for item in result["findings"]} == { + "inventory_missing_backup", "inventory_missing_disk", "backup_without_inventory" + } + + +def test_maintenance_preflight_blocks_active_target_backup(monkeypatch): + async def snapshot(): + return { + "services": {}, + "hosts": {"emby-chris": {"reachable": True, "containers": ["emby: Up 2 days"]}}, + "backups": {"summary": {}, "hosts": {"emby-chris": {"state": "healthy"}}}, + "disks": {"emby-chris": {"pct": "30%"}}, + } + + async def active_backups(): + return {"emby-chris": "active"} + + monkeypatch.setattr(app, "_collect_operational_snapshot", snapshot) + monkeypatch.setattr(app, "_collect_active_backups", active_backups) + with TestClient(app.app) as client: + response = client.get( + "/maintenance/preflight?action=docker&target=emby-chris", + headers={"Authorization": "Bearer test-token"}, + ) + + assert response.status_code == 200 + result = response.json() + assert result["safe"] is False + assert result["blockers"] == [{"code": "backup_active", "target": "emby-chris"}] + + def test_overview_is_compact_deterministic_and_light_model_friendly(monkeypatch): async def service_data(): return { From a1a69567d4c851ea4fb5c78556b91084b02ccdae Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:56:23 +0200 Subject: [PATCH 06/51] feat(ui): update app.py --- app.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 91be060..2b6da2e 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,7 @@ import httpx, yaml from typing import Literal from pydantic import BaseModel, Field from fastapi import FastAPI, Request, HTTPException, Depends, Query -from fastapi.responses import JSONResponse, RedirectResponse, Response +from fastapi.responses import JSONResponse, RedirectResponse, Response, HTMLResponse from contextlib import asynccontextmanager log = logging.getLogger("butler") @@ -17,6 +17,7 @@ API_DIR = os.environ.get("API_KEY_DIR", "/data/api") VAULT_CACHE_DIR = os.environ.get("VAULT_CACHE_DIR", "/data/vault-cache") BUTLER_TOKEN = os.environ.get("BUTLER_TOKEN", "") CONFIG_PATH = os.environ.get("BUTLER_CONFIG", "/data/butler.yaml") +UI_PATH = os.environ.get("BUTLER_UI_PATH", os.path.join(os.path.dirname(__file__), "ui.html")) # --- Config loading --- @@ -227,12 +228,37 @@ async def _dockhand_login(client): # --- Auth --- +_ui_sessions: dict[str, dict] = {} +UI_SESSION_TTL = 8 * 60 * 60 + + +class UiLoginRequest(BaseModel): + token: str + + +def _ui_session(request: Request) -> dict | None: + session_id = request.cookies.get("butler_session", "") + session = _ui_sessions.get(session_id) + if not session: + return None + if session["expires"] <= time.time(): + _ui_sessions.pop(session_id, None) + return None + return session + def _verify(request: Request): if not BUTLER_TOKEN: return auth = request.headers.get("authorization", "") - if auth != f"Bearer {BUTLER_TOKEN}": + if secrets.compare_digest(auth, f"Bearer {BUTLER_TOKEN}"): + return + session = _ui_session(request) + if not session: raise HTTPException(401, "Invalid token") + if request.method not in {"GET", "HEAD", "OPTIONS"}: + csrf = request.headers.get("x-csrf-token", "") + if not csrf or not secrets.compare_digest(csrf, session["csrf"]): + raise HTTPException(403, "Invalid CSRF token") def _get_key(cfg): vault_key = cfg.get("vault_key") @@ -290,6 +316,45 @@ def _inventory_hosts(text: str) -> list[dict]: # --- Routes --- +@app.get("/ui", response_class=HTMLResponse) +async def ui(): + try: + return HTMLResponse(open(UI_PATH, encoding="utf-8").read()) + except FileNotFoundError: + raise HTTPException(503, "Butler UI asset is missing") + + +@app.post("/ui/login") +async def ui_login(payload: UiLoginRequest): + if not BUTLER_TOKEN or not secrets.compare_digest(payload.token, BUTLER_TOKEN): + raise HTTPException(401, "Invalid token") + session_id = secrets.token_urlsafe(32) + csrf = secrets.token_urlsafe(24) + _ui_sessions[session_id] = {"csrf": csrf, "expires": time.time() + UI_SESSION_TTL} + response = JSONResponse({"authenticated": True, "expires_in": UI_SESSION_TTL}) + response.set_cookie("butler_session", session_id, max_age=UI_SESSION_TTL, httponly=True, samesite="strict", path="/") + response.set_cookie("butler_csrf", csrf, max_age=UI_SESSION_TTL, httponly=False, samesite="strict", path="/") + return response + + +@app.get("/ui/session") +async def ui_session(request: Request): + return {"authenticated": _ui_session(request) is not None} + + +@app.post("/ui/logout") +async def ui_logout(request: Request): + session = _ui_session(request) + if session: + csrf = request.headers.get("x-csrf-token", "") + if not csrf or not secrets.compare_digest(csrf, session["csrf"]): + raise HTTPException(403, "Invalid CSRF token") + _ui_sessions.pop(request.cookies.get("butler_session", ""), None) + response = JSONResponse({"authenticated": False}) + response.delete_cookie("butler_session", path="/") + response.delete_cookie("butler_csrf", path="/") + return response + @app.get("/") async def root(): """AI self-onboarding: returns all available endpoints and services.""" @@ -536,6 +601,7 @@ async def info(_=Depends(_verify)): }, "endpoints": { "capabilities": "/capabilities", + "ui": "/ui", "doctor": "/doctor/{target}", "drift": "/drift", "maintenance_preflight": "/maintenance/preflight", From 5723eebfb47d657265a553ac908c80065db87a30 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:56:24 +0200 Subject: [PATCH 07/51] feat(ui): update tests/test_app.py --- tests/test_app.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index 645220a..82356fd 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -46,6 +46,46 @@ def test_health_exposes_current_version(): assert response.json()["version"] == app.VERSION == "2.3.5" +def test_ui_serves_self_contained_operator_console(): + with TestClient(app.app) as client: + response = client.get("/ui") + assert response.status_code == 200 + assert "Pfannkuchen Butler" in response.text + assert 'id="operations-grid"' in response.text + assert 'id="doctor-form"' in response.text + assert 'id="preflight-form"' in response.text + assert "localStorage" not in response.text + + +def test_ui_asset_is_mounted_read_only_in_compose(): + from pathlib import Path + import yaml + compose = yaml.safe_load(Path(app.__file__).with_name("compose.yaml").read_text(encoding="utf-8")) + mounts = compose["services"]["homelab-butler"]["volumes"] + assert "./ui.html:/app/ui.html:ro" in mounts + + +def test_ui_session_login_uses_httponly_cookie_and_csrf(): + app._ui_sessions.clear() + with TestClient(app.app) as client: + denied = client.post("/ui/login", json={"token": "wrong"}) + assert denied.status_code == 401 + + login = client.post("/ui/login", json={"token": "test-token"}) + assert login.status_code == 200 + assert "HttpOnly" in login.headers.get("set-cookie", "") + csrf = client.cookies.get("butler_csrf") + assert csrf + + capabilities = client.get("/capabilities") + assert capabilities.status_code == 200 + + blocked = client.post("/config/reload") + assert blocked.status_code == 403 + allowed = client.post("/config/reload", headers={"X-CSRF-Token": csrf}) + assert allowed.status_code == 200 + + def test_capabilities_is_live_machine_readable_safety_map(): with TestClient(app.app) as client: response = client.get( @@ -74,6 +114,7 @@ def test_info_advertises_capabilities_endpoint(): assert response.json()["endpoints"]["doctor"] == "/doctor/{target}" assert response.json()["endpoints"]["drift"] == "/drift" assert response.json()["endpoints"]["maintenance_preflight"] == "/maintenance/preflight" + assert response.json()["endpoints"]["ui"] == "/ui" def test_audit_persists_and_redacts_secrets(tmp_path, monkeypatch): From 6d05c16f589250059cfe3bf9043c44f655daf69d Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:56:24 +0200 Subject: [PATCH 08/51] feat(ui): update compose.yaml --- compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/compose.yaml b/compose.yaml index e0d1f56..a07a30f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -11,6 +11,7 @@ services: - /home/sascha/.ssh:/root/.ssh:ro - ./butler.yaml:/data/butler.yaml:ro - ./app.py:/app/app.py:ro + - ./ui.html:/app/ui.html:ro - ./state:/data/state environment: - API_KEY_DIR=/data/api From 94f6c6b241178c248eb323430c0a835e4b282f1d Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 20:56:25 +0200 Subject: [PATCH 09/51] feat(ui): add operator console asset --- ui.html | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 ui.html diff --git a/ui.html b/ui.html new file mode 100644 index 0000000..9ad3d27 --- /dev/null +++ b/ui.html @@ -0,0 +1,124 @@ + + + + + + + Pfannkuchen Butler + + + + + + +
+ + + + From d23fdc7896a258ffbd5b89777feb5ef22c1a34c6 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 21:28:56 +0200 Subject: [PATCH 10/51] Add Emby account-sharing analysis and read-only UI access (app.py) --- app.py | 280 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 271 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 2b6da2e..bac1a05 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ """Homelab Butler v2.1 – Unified API proxy for Pfannkuchen homelab. Reads service config from butler.yaml, credentials from Vaultwarden cache with flat-file fallback.""" -import os, json, asyncio, logging, time, base64, re, subprocess, ipaddress, secrets, sqlite3 +import os, json, asyncio, logging, time, base64, re, subprocess, ipaddress, secrets, sqlite3, math from datetime import datetime, timezone import httpx, yaml from typing import Literal @@ -246,6 +246,24 @@ def _ui_session(request: Request) -> dict | None: return None return session + +def _issue_ui_session(read_only: bool) -> JSONResponse: + session_id = secrets.token_urlsafe(32) + csrf = secrets.token_urlsafe(24) + _ui_sessions[session_id] = { + "csrf": csrf, + "expires": time.time() + UI_SESSION_TTL, + "read_only": read_only, + } + response = JSONResponse({ + "authenticated": True, + "read_only": read_only, + "expires_in": UI_SESSION_TTL, + }) + response.set_cookie("butler_session", session_id, max_age=UI_SESSION_TTL, httponly=True, samesite="strict", path="/") + response.set_cookie("butler_csrf", csrf, max_age=UI_SESSION_TTL, httponly=False, samesite="strict", path="/") + return response + def _verify(request: Request): if not BUTLER_TOKEN: return @@ -256,10 +274,253 @@ def _verify(request: Request): if not session: raise HTTPException(401, "Invalid token") if request.method not in {"GET", "HEAD", "OPTIONS"}: + if session.get("read_only", False): + raise HTTPException(403, "Anonymous UI session is read-only") csrf = request.headers.get("x-csrf-token", "") if not csrf or not secrets.compare_digest(csrf, session["csrf"]): raise HTTPException(403, "Invalid CSRF token") +def _emby_network_identity(endpoint: str) -> dict: + """Normalize an Emby endpoint without treating IPv6 privacy addresses as new households.""" + raw = str(endpoint or "").strip() + if raw.startswith("[") and "]" in raw: + raw = raw[1:raw.index("]")] + try: + address = ipaddress.ip_address(raw) + except ValueError: + if raw.count(":") == 1: + raw = raw.rsplit(":", 1)[0] + try: + address = ipaddress.ip_address(raw) + except ValueError as exc: + raise ValueError("Invalid Emby remote endpoint") from exc + if address.version == 4: + network = ipaddress.ip_network(f"{address}/32", strict=False) + parent = network + else: + network = ipaddress.ip_network(f"{address}/64", strict=False) + parent = ipaddress.ip_network(f"{address}/48", strict=False) + return { + "ip": str(address), + "version": address.version, + "network": str(network), + "parent": str(parent), + "identity": str(parent if address.version == 6 else network), + "public": address.is_global, + } + + +def _emby_location(metric: dict) -> dict: + def coordinate(name): + try: + return float(metric.get(name, 0)) + except (TypeError, ValueError): + return 0.0 + return { + "city": metric.get("city", ""), + "region": metric.get("region", ""), + "country": metric.get("countryCode", ""), + "latitude": coordinate("latitude"), + "longitude": coordinate("longitude"), + } + + +def _analyze_emby_sharing(series: list[dict], step_seconds: int) -> dict: + observations: dict[str, dict[int, dict[str, dict]]] = {} + tracks: dict[str, dict[str, dict]] = {} + identities_by_user: dict[str, set[str]] = {} + servers_by_user: dict[str, set[str]] = {} + for item in series: + metric = item.get("metric", {}) + username = str(metric.get("username", "")).strip() + if not username: + continue + try: + network = _emby_network_identity(metric.get("remoteEndPoint", "")) + except ValueError: + continue + if not network["public"]: + continue + evidence = { + **network, + "server": metric.get("job", ""), + "location": _emby_location(metric), + } + identities_by_user.setdefault(username, set()).add(network["identity"]) + servers_by_user.setdefault(username, set()).add(str(metric.get("job", ""))) + track = tracks.setdefault(username, {}).setdefault(network["identity"], {"evidence": evidence, "timestamps": []}) + for value in item.get("values", []): + if not isinstance(value, list) or len(value) < 2 or str(value[1]).lower() in {"0", "nan"}: + continue + timestamp = int(float(value[0])) + track["timestamps"].append(timestamp) + observations.setdefault(username, {}).setdefault(timestamp, {}).setdefault(network["identity"], evidence) + + raw_events = [] + for username, timeline in observations.items(): + buckets = [] + for timestamp in sorted(timeline): + evidence = timeline[timestamp] + if len(evidence) >= 2: + buckets.append((timestamp, tuple(sorted(evidence)), evidence)) + current = None + for timestamp, identity_key, evidence in buckets: + if current and current["identity_key"] == identity_key and timestamp - current["end_ts"] <= step_seconds * 2: + current["end_ts"] = timestamp + current["samples"] += 1 + continue + if current and current["samples"] >= 2 and current["end_ts"] - current["start_ts"] + step_seconds >= 360: + raw_events.append(current) + current = { + "username": username, "identity_key": identity_key, "start_ts": timestamp, + "end_ts": timestamp, "samples": 1, "evidence": list(evidence.values()), + } + if current and current["samples"] >= 2 and current["end_ts"] - current["start_ts"] + step_seconds >= 360: + raw_events.append(current) + + events = [{ + "type": "concurrent_networks", + "severity": "high", + "username": item["username"], + "start": datetime.fromtimestamp(item["start_ts"], timezone.utc).isoformat(), + "end": datetime.fromtimestamp(item["end_ts"], timezone.utc).isoformat(), + "duration_seconds": item["end_ts"] - item["start_ts"] + step_seconds, + "samples": item["samples"], + "evidence": item["evidence"], + "reason": "Zeitgleiche Nutzung desselben Emby-Benutzers aus unterschiedlichen öffentlichen Netzen", + } for item in raw_events] + + def distance_km(first: dict, second: dict) -> float: + lat1, lon1 = first["latitude"], first["longitude"] + lat2, lon2 = second["latitude"], second["longitude"] + if not all((-90 <= lat <= 90 and -180 <= lon <= 180) for lat, lon in ((lat1, lon1), (lat2, lon2))): + return 0.0 + phi1, phi2 = math.radians(lat1), math.radians(lat2) + dphi, dlambda = math.radians(lat2 - lat1), math.radians(lon2 - lon1) + value = math.sin(dphi / 2) ** 2 + math.cos(phi1) * math.cos(phi2) * math.sin(dlambda / 2) ** 2 + return 6371.0 * 2 * math.atan2(math.sqrt(value), math.sqrt(max(0.0, 1 - value))) + + travel_events = [] + for username, user_tracks in tracks.items(): + intervals = [] + for identity, track in user_tracks.items(): + current = None + for timestamp in sorted(set(track["timestamps"])): + if current and timestamp - current["end"] <= step_seconds * 2: + current["end"] = timestamp + current["samples"] += 1 + else: + if current and current["samples"] >= 2: + intervals.append(current) + current = {"identity": identity, "start": timestamp, "end": timestamp, "samples": 1, "evidence": track["evidence"]} + if current and current["samples"] >= 2: + intervals.append(current) + intervals.sort(key=lambda item: item["start"]) + for previous, current in zip(intervals, intervals[1:]): + if previous["identity"] == current["identity"] or current["start"] <= previous["end"]: + continue + distance = distance_km(previous["evidence"]["location"], current["evidence"]["location"]) + gap_hours = max((current["start"] - previous["end"]) / 3600, 1 / 60) + speed = distance / gap_hours + if distance < 300 or speed <= 1000: + continue + travel_events.append({ + "type": "impossible_travel", "severity": "medium", "username": username, + "start": datetime.fromtimestamp(previous["end"], timezone.utc).isoformat(), + "end": datetime.fromtimestamp(current["start"], timezone.utc).isoformat(), + "duration_seconds": current["start"] - previous["end"], + "samples": previous["samples"] + current["samples"], + "distance_km": round(distance, 1), "required_speed_kmh": round(speed, 1), + "evidence": [previous["evidence"], current["evidence"]], + "reason": "Geografischer Wechsel zwischen öffentlichen Netzen wäre in der verfügbaren Zeit nicht plausibel", + }) + events.extend(travel_events) + events.sort(key=lambda item: item["start"], reverse=True) + flagged = {item["username"] for item in events} + users = [{ + "username": username, + "risk": "high" if any(item["username"] == username and item["type"] == "concurrent_networks" for item in events) else ("medium" if username in flagged else "none"), + "events": sum(item["username"] == username for item in events), + "network_identities": len(identities_by_user.get(username, set())), + "servers": sorted(servers_by_user.get(username, set())), + } for username in sorted(observations)] + return { + "summary": { + "users_analyzed": len(observations), + "flagged_users": len(flagged), + "concurrent_events": len(raw_events), + "impossible_travel_events": len(travel_events), + }, + "users": users, + "events": events, + } + + +async def _fetch_emby_session_history(days: int, server: str) -> tuple[list[dict], int]: + cfg = SERVICES.get("grafana") + if not cfg: + raise HTTPException(503, "Grafana service is not configured") + request_data = _service_auth(cfg) + datasource_uid = os.environ.get("EMBY_PROMETHEUS_UID", "bdpu4276997nkc") + labels = "job,username,remoteEndPoint,city,region,countryCode,latitude,longitude" + selector = 'emby_sessions{username!=""}' + if server != "all": + selector = f'emby_sessions{{username!="",job="{server}"}}' + query = f"max by ({labels}) ({selector})" + end = int(time.time()) + start = end - days * 86400 + step = max(60, math.ceil(((end - start) / 30000) / 60) * 60) + url = f"{request_data['base_url'].rstrip('/')}/api/datasources/proxy/uid/{datasource_uid}/api/v1/query_range" + try: + async with httpx.AsyncClient(timeout=90) as client: + response = await client.get( + url, + params={"query": query, "start": start, "end": end, "step": step}, + headers=request_data["headers"], cookies=request_data["cookies"], + ) + response.raise_for_status() + payload = response.json() + except (httpx.HTTPError, ValueError) as exc: + log.warning("Emby sharing history query failed: %s", type(exc).__name__) + raise HTTPException(502, "Emby session history is temporarily unavailable") + if payload.get("status") != "success": + raise HTTPException(502, "Prometheus rejected the Emby session history query") + return payload.get("data", {}).get("result", []), step + + +@app.get("/emby/account-sharing") +async def emby_account_sharing( + days: int = Query(30, ge=1, le=90), + username: str | None = Query(None, min_length=1, max_length=100), + server: Literal["all", "emby-sascha", "emby-chris"] = "all", + _=Depends(_verify), +): + """Conservative read-only analysis of concurrent networks and geographically impossible changes.""" + series, step = await _fetch_emby_session_history(days, server) + if username: + wanted = username.casefold() + series = [item for item in series if str(item.get("metric", {}).get("username", "")).casefold() == wanted] + result = _analyze_emby_sharing(series, step) + return { + "generated": datetime.now(timezone.utc).isoformat(), + "period": {"days": days, "server": server, "step_seconds": step, "series": len(series)}, + "policy": { + "mode": "conservative", + "ipv4_detection_identity": "/32", + "ipv6_display_network": "/64", + "ipv6_detection_identity": "/48", + "minimum_samples": 2, + "minimum_concurrent_seconds": 360, + "prometheus_staleness_guard": True, + "impossible_travel_minimum_km": 300, + "impossible_travel_speed_kmh": 1000, + "private_networks_excluded": True, + "automatic_enforcement": False, + }, + **result, + } + + def _get_key(cfg): vault_key = cfg.get("vault_key") if vault_key and vault_key in _vault_cache: @@ -328,18 +589,19 @@ async def ui(): async def ui_login(payload: UiLoginRequest): if not BUTLER_TOKEN or not secrets.compare_digest(payload.token, BUTLER_TOKEN): raise HTTPException(401, "Invalid token") - session_id = secrets.token_urlsafe(32) - csrf = secrets.token_urlsafe(24) - _ui_sessions[session_id] = {"csrf": csrf, "expires": time.time() + UI_SESSION_TTL} - response = JSONResponse({"authenticated": True, "expires_in": UI_SESSION_TTL}) - response.set_cookie("butler_session", session_id, max_age=UI_SESSION_TTL, httponly=True, samesite="strict", path="/") - response.set_cookie("butler_csrf", csrf, max_age=UI_SESSION_TTL, httponly=False, samesite="strict", path="/") - return response + return _issue_ui_session(read_only=False) @app.get("/ui/session") async def ui_session(request: Request): - return {"authenticated": _ui_session(request) is not None} + session = _ui_session(request) + if session: + return { + "authenticated": True, + "read_only": session.get("read_only", False), + "expires_in": max(0, int(session["expires"] - time.time())), + } + return _issue_ui_session(read_only=True) @app.post("/ui/logout") From 1ae67e158b42200a3cd7926584d7eaeeb1ef4df4 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 21:28:57 +0200 Subject: [PATCH 11/51] Add Emby account-sharing analysis and read-only UI access (ui.html) --- ui.html | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/ui.html b/ui.html index 9ad3d27..f837d9f 100644 --- a/ui.html +++ b/ui.html @@ -11,47 +11,33 @@ .shell{display:grid;grid-template-columns:248px minmax(0,1fr);min-height:100vh}.sidebar{position:sticky;top:0;height:100vh;padding:22px 16px;border-right:1px solid var(--border2);background:rgba(15,16,17,.92);backdrop-filter:blur(18px);z-index:20}.brand{display:flex;align-items:center;gap:11px;padding:0 8px 24px}.logo{width:34px;height:34px;border:1px solid rgba(113,112,255,.35);border-radius:10px;display:grid;place-items:center;background:linear-gradient(145deg,rgba(113,112,255,.22),rgba(255,255,255,.03));box-shadow:inset 0 0 16px rgba(113,112,255,.12)}.brand strong{font-size:14px;font-weight:590}.brand small{display:block;color:var(--muted);font-size:11px;margin-top:2px}.nav-label{font-size:10px;text-transform:uppercase;letter-spacing:.1em;color:var(--dim);padding:15px 10px 7px}.nav button{width:100%;display:flex;align-items:center;gap:10px;border:0;background:transparent;color:var(--muted);padding:9px 10px;border-radius:7px;text-align:left;font-size:13px;font-weight:510}.nav button:hover,.nav button.active{background:rgba(255,255,255,.05);color:var(--text)}.nav .icon{width:18px;text-align:center;color:var(--dim)}.sidebar-foot{position:absolute;left:16px;right:16px;bottom:18px}.session-pill{display:flex;align-items:center;justify-content:space-between;border:1px solid var(--border);border-radius:8px;padding:9px 10px;color:var(--muted);font-size:11px;background:rgba(255,255,255,.02)}.dot{width:7px;height:7px;border-radius:50%;background:var(--green);box-shadow:0 0 10px rgba(16,185,129,.65)} main{min-width:0}.topbar{height:68px;position:sticky;top:0;z-index:15;display:flex;align-items:center;justify-content:space-between;padding:0 30px;border-bottom:1px solid var(--border2);background:rgba(8,9,10,.78);backdrop-filter:blur(18px)}.topbar h1{font-size:15px;margin:0;font-weight:510}.top-actions{display:flex;gap:8px}.btn{border:1px solid var(--border);background:rgba(255,255,255,.03);color:var(--secondary);border-radius:7px;padding:8px 12px;font-size:12px;font-weight:510}.btn:hover{background:rgba(255,255,255,.07);color:var(--text)}.btn.primary{background:var(--accent2);border-color:transparent;color:white}.btn.danger{color:#fca5a5}.mobile-menu{display:none}.content{max-width:1440px;margin:0 auto;padding:30px}.view{display:none}.view.active{display:block}.eyebrow{font-size:11px;color:var(--accent);text-transform:uppercase;letter-spacing:.11em;font-weight:590}.hero{display:flex;justify-content:space-between;align-items:flex-end;gap:24px;margin:4px 0 26px}.hero h2{font-size:31px;letter-spacing:-.7px;font-weight:510;margin:8px 0 6px}.hero p{margin:0;color:var(--muted);font-size:14px;line-height:1.55}.updated{font:11px ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--dim)} .metrics{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin-bottom:18px}.metric,.panel,.operation{border:1px solid var(--border);background:rgba(255,255,255,.025);border-radius:var(--radius)}.metric{padding:17px}.metric-label{font-size:11px;color:var(--muted);margin-bottom:12px}.metric-value{font-size:25px;letter-spacing:-.45px;font-weight:510}.metric-meta{font-size:11px;color:var(--dim);margin-top:7px}.metric.good .metric-value{color:#a7f3d0}.metric.warn .metric-value{color:#fcd34d}.metric.bad .metric-value{color:#fca5a5}.grid-2{display:grid;grid-template-columns:minmax(0,1.4fr) minmax(300px,.6fr);gap:14px}.panel{padding:18px;min-width:0}.panel-head{display:flex;align-items:center;justify-content:space-between;margin-bottom:15px}.panel-title{font-size:13px;font-weight:590}.panel-sub{font-size:11px;color:var(--dim)}.empty{border:1px dashed var(--border);border-radius:8px;color:var(--dim);padding:26px;text-align:center;font-size:12px}.finding{display:grid;grid-template-columns:9px 1fr auto;gap:10px;align-items:start;padding:11px 0;border-bottom:1px solid var(--border2)}.finding:last-child{border-bottom:0}.finding-dot{width:7px;height:7px;border-radius:50%;background:var(--yellow);margin-top:5px}.finding.critical .finding-dot{background:var(--red)}.finding strong{font-size:12px}.finding p{font-size:11px;color:var(--muted);margin:4px 0 0}.badge{display:inline-flex;align-items:center;border:1px solid var(--border);border-radius:999px;padding:3px 7px;font:10px ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--muted);white-space:nowrap}.badge.get,.badge.read_only{color:#a7f3d0;border-color:rgba(16,185,129,.25);background:rgba(16,185,129,.06)}.badge.post,.badge.put,.badge.patch{color:#c4b5fd;border-color:rgba(113,112,255,.3);background:rgba(113,112,255,.07)}.badge.delete,.badge.destructive{color:#fca5a5;border-color:rgba(239,68,68,.25);background:rgba(239,68,68,.06)} - .form-row{display:grid;grid-template-columns:1fr auto;gap:9px}.form-row.triple{grid-template-columns:160px 1fr auto}.input{width:100%;border:1px solid var(--border);background:rgba(255,255,255,.025);color:var(--text);border-radius:7px;padding:10px 12px;outline:0;font-size:13px}.input:focus{border-color:rgba(113,112,255,.6);box-shadow:0 0 0 3px rgba(113,112,255,.1)}select.input{appearance:none}.result{margin-top:14px;min-height:110px}.layer-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:9px}.layer{border:1px solid var(--border2);background:rgba(255,255,255,.02);border-radius:8px;padding:12px}.layer h4{font-size:11px;margin:0 0 8px;text-transform:uppercase;color:var(--muted);letter-spacing:.07em}.layer pre,.json{white-space:pre-wrap;word-break:break-word;margin:0;color:var(--secondary);font:11px/1.55 ui-monospace,SFMono-Regular,Menlo,monospace}.safe-banner{display:flex;align-items:center;gap:12px;border-radius:9px;padding:14px;border:1px solid rgba(16,185,129,.25);background:rgba(16,185,129,.06)}.safe-banner.blocked{border-color:rgba(239,68,68,.25);background:rgba(239,68,68,.06)}.safe-icon{font-size:21px} + .form-row{display:grid;grid-template-columns:1fr auto;gap:9px}.form-row.triple{grid-template-columns:160px 1fr auto}.sharing-form{grid-template-columns:140px 170px minmax(180px,1fr) auto}.input{width:100%;border:1px solid var(--border);background:rgba(255,255,255,.025);color:var(--text);border-radius:7px;padding:10px 12px;outline:0;font-size:13px}.input:focus{border-color:rgba(113,112,255,.6);box-shadow:0 0 0 3px rgba(113,112,255,.1)}select.input{appearance:none}.result{margin-top:14px;min-height:110px}.layer-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(190px,1fr));gap:9px}.layer{border:1px solid var(--border2);background:rgba(255,255,255,.02);border-radius:8px;padding:12px}.layer h4{font-size:11px;margin:0 0 8px;text-transform:uppercase;color:var(--muted);letter-spacing:.07em}.layer pre,.json{white-space:pre-wrap;word-break:break-word;margin:0;color:var(--secondary);font:11px/1.55 ui-monospace,SFMono-Regular,Menlo,monospace}.safe-banner{display:flex;align-items:center;gap:12px;border-radius:9px;padding:14px;border:1px solid rgba(16,185,129,.25);background:rgba(16,185,129,.06)}.safe-banner.blocked{border-color:rgba(239,68,68,.25);background:rgba(239,68,68,.06)}.safe-icon{font-size:21px} .toolbar{display:flex;align-items:center;gap:9px;flex-wrap:wrap;margin-bottom:15px}.toolbar .input{max-width:360px}.chips{display:flex;gap:6px;flex-wrap:wrap}.chip{border:1px solid var(--border);background:transparent;color:var(--muted);border-radius:999px;padding:6px 10px;font-size:11px}.chip.active,.chip:hover{color:var(--text);background:rgba(255,255,255,.05)}.operations{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px}.operation{padding:14px;display:flex;flex-direction:column;gap:10px;min-height:145px;transition:.16s ease}.operation:hover{border-color:rgba(113,112,255,.35);transform:translateY(-1px);background:rgba(255,255,255,.035)}.operation-top{display:flex;justify-content:space-between;gap:8px}.operation-path{font:11px/1.45 ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--secondary);word-break:break-all}.operation h3{font-size:12px;font-weight:590;margin:0}.operation p{font-size:11px;color:var(--muted);line-height:1.45;margin:0;flex:1}.operation-badges{display:flex;gap:5px;flex-wrap:wrap}.table-wrap{overflow:auto;border:1px solid var(--border);border-radius:9px}table{width:100%;border-collapse:collapse;min-width:760px}th,td{text-align:left;padding:10px 12px;border-bottom:1px solid var(--border2);font-size:11px}th{color:var(--dim);text-transform:uppercase;letter-spacing:.07em;font-size:9px;background:rgba(255,255,255,.02)}td{color:var(--secondary)}td.mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace}.status-code.ok{color:#a7f3d0}.status-code.err{color:#fca5a5} .login{position:fixed;inset:0;z-index:100;background:radial-gradient(circle at 50% 15%,rgba(113,112,255,.18),transparent 30%),#08090a;display:grid;place-items:center;padding:20px}.login-card{width:min(420px,100%);border:1px solid var(--border);background:#0f1011;border-radius:14px;padding:28px;box-shadow:0 28px 90px rgba(0,0,0,.5)}.login-card .logo{margin-bottom:22px}.login-card h1{font-size:25px;letter-spacing:-.5px;font-weight:510;margin:0 0 8px}.login-card p{font-size:13px;color:var(--muted);line-height:1.5;margin:0 0 20px}.login-card form{display:grid;gap:10px}.error{color:#fca5a5;font-size:11px;min-height:16px}.toast{position:fixed;right:20px;bottom:20px;z-index:120;background:#191a1b;border:1px solid var(--border);border-radius:9px;padding:11px 14px;font-size:12px;color:var(--secondary);box-shadow:0 12px 40px rgba(0,0,0,.35);transform:translateY(20px);opacity:0;pointer-events:none;transition:.2s}.toast.show{transform:none;opacity:1}.spinner{width:15px;height:15px;border:2px solid rgba(255,255,255,.15);border-top-color:var(--accent);border-radius:50%;animation:spin .7s linear infinite;display:inline-block}@keyframes spin{to{transform:rotate(360deg)}} @media(max-width:1050px){.operations{grid-template-columns:repeat(2,minmax(0,1fr))}.metrics{grid-template-columns:repeat(2,minmax(0,1fr))}.grid-2{grid-template-columns:1fr}} - @media(max-width:760px){.shell{display:block}.sidebar{position:fixed;transform:translateX(-100%);transition:.2s;width:260px}.sidebar.open{transform:none}.mobile-menu{display:inline-flex}.topbar{padding:0 15px}.content{padding:20px 14px}.hero{align-items:flex-start;flex-direction:column}.hero h2{font-size:25px}.operations{grid-template-columns:1fr}.metrics{grid-template-columns:repeat(2,1fr)}.form-row,.form-row.triple{grid-template-columns:1fr}.btn,.input{min-height:44px}.top-actions .desktop-only{display:none}} + @media(max-width:760px){.shell{display:block}.sidebar{position:fixed;transform:translateX(-100%);transition:.2s;width:260px}.sidebar.open{transform:none}.mobile-menu{display:inline-flex}.topbar{padding:0 15px}.content{padding:20px 14px}.hero{align-items:flex-start;flex-direction:column}.hero h2{font-size:25px}.operations{grid-template-columns:1fr}.metrics{grid-template-columns:repeat(2,1fr)}.form-row,.form-row.triple,.sharing-form{grid-template-columns:1fr}.btn,.input{min-height:44px}.top-actions .desktop-only{display:none}} @media(max-width:420px){.metrics{grid-template-columns:1fr}.content{padding:18px 10px}.panel{padding:14px}.metric{padding:14px}} - - -