- Runbooks: notes per server/integration, shown next to matching alerts - Backup coverage page across Proxmox and PBS, with a digest section - Hardware inventory with warranty, power and running cost - Authentik group to role mapping with preview and safeguards - /metrics (bearer token) and signed outgoing event webhooks - Docs: README, DATABASE, NOTIFICATIONS, ROLES and new API.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
145 lines
6.9 KiB
TypeScript
145 lines
6.9 KiB
TypeScript
/**
|
|
* Runbooks: notes about what to do when something is wrong, attached to a server, integration or DNS provider.
|
|
*
|
|
* Run with: npm test --workspace server
|
|
*/
|
|
import { test, describe, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { AddressInfo } from "node:net";
|
|
import type { Server } from "node:http";
|
|
|
|
const dir = mkdtempSync(join(tmpdir(), "hm-runbooks-"));
|
|
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
|
process.env.SESSION_DIR = join(dir, "sessions");
|
|
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
|
|
|
const express = (await import("express")).default;
|
|
const { db } = await import("../src/db/client.js");
|
|
const { users, servers, integrations, auditLog } = await import("../src/db/schema.js");
|
|
const { runMigrations } = await import("../src/db/migrate.js");
|
|
const { runbooksRouter } = await import("../src/routes/runbooks.js");
|
|
const { eq } = await import("drizzle-orm");
|
|
|
|
await runMigrations();
|
|
|
|
describe("runbooks", () => {
|
|
let app: Server;
|
|
let base = "";
|
|
let who = "operator";
|
|
let elsa = 0;
|
|
let proxmox = 0;
|
|
|
|
before(async () => {
|
|
await db.insert(users).values([
|
|
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
|
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
|
|
]);
|
|
elsa = (await db.insert(servers).values({ name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a" }).returning())[0].id;
|
|
proxmox = (await db.insert(integrations).values({ type: "proxmox", name: "Felicia", baseUrl: "https://pve.lab", config: "{}" }).returning())[0].id;
|
|
const a = express();
|
|
a.use(express.json());
|
|
a.use((req, _res, next) => {
|
|
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
|
next();
|
|
});
|
|
a.use("/api/runbooks", runbooksRouter);
|
|
app = await new Promise<Server>((resolve) => {
|
|
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
|
});
|
|
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/runbooks`;
|
|
});
|
|
|
|
after(() => {
|
|
app.close();
|
|
try {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
} catch {
|
|
// Windows keeps the database file locked until the process exits.
|
|
}
|
|
});
|
|
|
|
const send = (method: string, path: string, body?: unknown) =>
|
|
fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
|
|
let firstId = 0;
|
|
|
|
test("an operator writes one for a server, for everything or for one kind of alert", async () => {
|
|
const any = await send("POST", "/", { targetType: "server", targetId: elsa, title: "If elsa is down", body: "Check the UPS first.\n\n- then the switch" });
|
|
assert.equal(any.status, 201);
|
|
const row = (await any.json()).runbook;
|
|
firstId = row.id;
|
|
assert.equal(row.category, null);
|
|
assert.equal(row.updatedBy, "Olga");
|
|
const disk = await send("POST", "/", { targetType: "server", targetId: elsa, title: "Disk full", body: "Prune docker.", category: "disk" });
|
|
assert.equal(disk.status, 201);
|
|
assert.equal((await disk.json()).runbook.category, "disk");
|
|
assert.equal((await send("POST", "/", { targetType: "integration", targetId: proxmox, title: "Proxmox", body: "Restart pveproxy." })).status, 201);
|
|
});
|
|
|
|
test("anyone signed in can read them, by thing, or as a list of titles", async () => {
|
|
who = "viewer";
|
|
const mine = await (await send("GET", `/?targetType=server&targetId=${elsa}`)).json();
|
|
assert.deepEqual(mine.runbooks.map((r: { title: string }) => r.title), ["Disk full", "If elsa is down"]);
|
|
assert.equal(mine.runbooks[1].body, "Check the UPS first.\n\n- then the switch");
|
|
|
|
const summary = await (await send("GET", "/summary")).json();
|
|
assert.equal(summary.runbooks.length, 3);
|
|
assert.ok(summary.runbooks.every((r: Record<string, unknown>) => !("body" in r)), "titles only: the texts are fetched for the one being read");
|
|
assert.equal((await send("GET", "/?targetType=server")).status, 400);
|
|
assert.equal((await send("GET", "/?targetType=moon&targetId=1")).status, 400);
|
|
who = "operator";
|
|
});
|
|
|
|
test("only operators change them", async () => {
|
|
who = "viewer";
|
|
assert.equal((await send("POST", "/", { targetType: "server", targetId: elsa, title: "x", body: "y" })).status, 403);
|
|
assert.equal((await send("PUT", `/${firstId}`, { title: "x", body: "y" })).status, 403);
|
|
assert.equal((await send("DELETE", `/${firstId}`)).status, 403);
|
|
who = "operator";
|
|
});
|
|
|
|
test("a bad one is refused: no such thing, no title, a made-up kind of alert, too long", async () => {
|
|
for (const bad of [
|
|
{ targetType: "server", targetId: 99999, title: "x", body: "y" },
|
|
{ targetType: "server", targetId: elsa, title: " ", body: "y" },
|
|
{ targetType: "server", targetId: elsa, title: "x", body: "y", category: "weather" },
|
|
{ targetType: "server", targetId: elsa, title: "x", body: "z".repeat(20_001) },
|
|
{ targetType: "toaster", targetId: 1, title: "x", body: "y" },
|
|
]) {
|
|
assert.equal((await send("POST", "/", bad)).status, 400, JSON.stringify(bad).slice(0, 70));
|
|
}
|
|
});
|
|
|
|
test("editing keeps who changed it, and it can be removed; both are in the audit log", async () => {
|
|
const put = await send("PUT", `/${firstId}`, { title: "If elsa is down (updated)", body: "UPS, then switch, then call Bo.", category: "offline" });
|
|
assert.equal(put.status, 200);
|
|
const row = (await put.json()).runbook;
|
|
assert.equal(row.title, "If elsa is down (updated)");
|
|
assert.equal(row.category, "offline");
|
|
assert.ok(row.updatedAt >= row.createdAt);
|
|
assert.equal((await send("PUT", "/99999", { title: "x", body: "y" })).status, 404);
|
|
|
|
assert.equal((await send("DELETE", `/${firstId}`)).status, 204);
|
|
assert.equal((await send("DELETE", `/${firstId}`)).status, 404);
|
|
const actions = (await db.select().from(auditLog)).filter((e) => e.category === "runbook").map((e) => e.action);
|
|
for (const a of ["create", "update", "delete"]) assert.ok(actions.includes(a), a);
|
|
});
|
|
|
|
test("notes for something that has since been removed drop out of the list", async () => {
|
|
await db.delete(integrations).where(eq(integrations.id, proxmox));
|
|
const summary = await (await send("GET", "/summary")).json();
|
|
assert.equal(summary.runbooks.length, 1, "only the remaining server note");
|
|
});
|
|
|
|
test("there is a limit to how many there can be for one thing", async () => {
|
|
const [{ count }] = [{ count: (await (await send("GET", `/?targetType=server&targetId=${elsa}`)).json()).runbooks.length }];
|
|
for (let i = count; i < 20; i++) assert.equal((await send("POST", "/", { targetType: "server", targetId: elsa, title: `Note ${i}`, body: "x" })).status, 201);
|
|
const over = await send("POST", "/", { targetType: "server", targetId: elsa, title: "One too many", body: "x" });
|
|
assert.equal(over.status, 400);
|
|
assert.equal((await over.json()).error, "too_many");
|
|
});
|
|
});
|