Hub v1: install API, licences, health dashboard, alerts

- Protocol 1 (docs/hub-protocol.md): public key, register, signed heartbeats
- Ed25519 licence tokens (30-day validity, status, expiry, grace, limits)
- Dashboard: installs, install detail, customers and licences, alerts,
  settings and audit log; two-factor login required
- Alert checks every 5 minutes, emailed once; heartbeat roll-up after 30 days
- Unit tests for encryption, tokens, signatures, TOTP and alert rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
This commit is contained in:
2026-10-26 12:12:51 +02:00
co-authored by Claude Opus 5.5
commit e72330893a
56 changed files with 16952 additions and 0 deletions
+117
View File
@@ -0,0 +1,117 @@
"use client";
import React, { useEffect, useState } from "react";
import { api, errorText, setToken } from "@/lib/api";
import { fmtDate } from "@/lib/format";
import { Loading, Message, PageTitle } from "@/components/ui";
type Settings = Record<string, string>;
type AuditRow = { id: string; action: string; target: string | null; metadata: unknown; createdAt: string; actor: { name: string } | null };
type Msg = { type: "ok" | "err"; text: string } | null;
const FIELDS: { key: string; label: string; hint: string; type?: string }[] = [
{ key: "latest_version", label: "Latest Hope Events release", hint: "e.g. 1.41.0 — installs on an older version show as behind, and see \"Update available\"." },
{ key: "alert_email", label: "Alert email", hint: "Where new alerts are sent.", type: "email" },
{ key: "offline_minutes", label: "Offline after (minutes without a heartbeat)", hint: "Default 60.", type: "number" },
{ key: "backup_max_age_hours", label: "Backup too old after (hours)", hint: "Default 48.", type: "number" },
{ key: "disk_min_free_percent", label: "Disk low below (% free)", hint: "Default 10.", type: "number" },
{ key: "errors_per_day", label: "Errors spiking above (per 24 h)", hint: "Default 50.", type: "number" },
{ key: "licence_expiry_warning_days", label: "Warn about licence expiry (days before)", hint: "Default 14.", type: "number" },
];
export default function SettingsPage() {
const [settings, setSettings] = useState<Settings | null>(null);
const [publicKey, setPublicKey] = useState("");
const [smtp, setSmtp] = useState(false);
const [audit, setAudit] = useState<AuditRow[]>([]);
const [pw, setPw] = useState({ currentPassword: "", newPassword: "" });
const [msg, setMsg] = useState<Msg>(null);
useEffect(() => {
api<{ settings: Settings; publicKey: string; smtp: boolean }>("/api/admin/settings").then(r => { setSettings(r.settings); setPublicKey(r.publicKey); setSmtp(r.smtp); }).catch(e => setMsg({ type: "err", text: errorText(e) }));
api<AuditRow[]>("/api/admin/audit").then(setAudit).catch(() => {});
}, []);
if (!settings) return msg ? <Message msg={msg} /> : <Loading />;
const save = async (e: React.FormEvent) => {
e.preventDefault();
try {
const r = await api<{ settings: Settings }>("/api/admin/settings", { method: "PUT", body: { settings } });
setSettings(r.settings);
setMsg({ type: "ok", text: "Settings saved." });
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
const changePassword = async (e: React.FormEvent) => {
e.preventDefault();
try {
const r = await api<{ token: string }>("/api/auth/password", { method: "POST", body: pw });
setToken(r.token);
setPw({ currentPassword: "", newPassword: "" });
setMsg({ type: "ok", text: "Password changed. Other devices are signed out." });
} catch (err) { setMsg({ type: "err", text: errorText(err) }); }
};
const signOutEverywhere = async () => {
if (!confirm("Sign out on every device, including this one?")) return;
await api("/api/auth/logout-everywhere", { method: "POST" }).catch(() => {});
setToken(null);
window.location.href = "/login";
};
return (
<>
<PageTitle title="Settings" />
<div className="mb-4"><Message msg={msg} /></div>
<div className="grid gap-6 lg:grid-cols-2">
<form onSubmit={save} className="card space-y-4">
<h2 className="font-semibold">Releases and alerts</h2>
{!smtp && <p className="rounded-lg bg-amber-50 p-3 text-sm text-amber-900">Email isn&apos;t set up on this server (SMTP_* in .env), so alerts are only logged.</p>}
{FIELDS.map(f => (
<div key={f.key}>
<label className="label" htmlFor={f.key}>{f.label}</label>
<input id={f.key} className="input" type={f.type || "text"} value={settings[f.key] ?? ""} onChange={e => setSettings({ ...settings, [f.key]: e.target.value })} />
<p className="mt-1 text-xs text-slate-500">{f.hint}</p>
</div>
))}
<button className="btn-primary">Save settings</button>
</form>
<div className="space-y-6">
<div className="card space-y-2">
<h2 className="font-semibold">Licence signing key</h2>
<p className="text-sm text-slate-600">Installs check licences with this public key (built into Hope Events: <code>backend/src/hub/publicKey.js</code>). The private key stays on this server only — keep a backup.</p>
<pre className="overflow-x-auto rounded-lg bg-slate-50 p-3 text-xs">{publicKey}</pre>
</div>
<form onSubmit={changePassword} className="card space-y-3">
<h2 className="font-semibold">Your login</h2>
<div><label className="label">Current password</label><input type="password" className="input" autoComplete="current-password" value={pw.currentPassword} onChange={e => setPw({ ...pw, currentPassword: e.target.value })} required /></div>
<div><label className="label">New password (12+ characters)</label><input type="password" className="input" autoComplete="new-password" minLength={12} value={pw.newPassword} onChange={e => setPw({ ...pw, newPassword: e.target.value })} required /></div>
<div className="flex flex-wrap gap-2">
<button className="btn-secondary">Change password</button>
<button type="button" className="btn-danger" onClick={signOutEverywhere}>Sign out everywhere</button>
</div>
</form>
</div>
</div>
<div className="card mt-6">
<h2 className="mb-3 font-semibold">Audit log</h2>
{audit.length === 0 ? <p className="text-sm text-slate-500">Nothing yet.</p> : (
<ul className="space-y-1 text-sm">
{audit.map(a => (
<li key={a.id} className="flex flex-wrap gap-2">
<span className="w-40 shrink-0 text-xs text-slate-500">{fmtDate(a.createdAt)}</span>
<span className="font-medium">{a.actor?.name || "—"}</span>
<span>{a.action.replace(/_/g, " ")}</span>
{a.metadata ? <span className="text-xs text-slate-400 break-all">{JSON.stringify(a.metadata)}</span> : null}
</li>
))}
</ul>
)}
</div>
</>
);
}