Add runbooks, backup coverage, inventory, roles from groups, Prometheus metrics and event webhooks
- 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>
This commit is contained in:
1 parent
c36e2092c5
commit
c17e9aa140
55 files changed
+7177
-27
No files matched your search
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Backup coverage: what the rows say for each kind of guest, and the route and the digest around them.
|
||||
*
|
||||
* 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-coverage-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { buildCoverage, summarize, setCoverageLoaderForTests } = await import("../src/services/backupCoverage.js");
|
||||
const { backupsRouter } = await import("../src/routes/backups.js");
|
||||
const { buildDigest } = await import("../src/services/weeklyDigest.js");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const NOW = new Date("2026-10-17T12:00:00Z");
|
||||
const HOUR = 3600_000;
|
||||
const guest = (vmid: number, name: string, type: "qemu" | "lxc" = "qemu", node = "pve1") => ({ vmid, name, node, type, status: "running", cpu: null, maxmem: null, mem: null, uptime: null });
|
||||
const allJob = { id: "j1", enabled: true, schedule: "02:00", storage: "pbs", node: null, allGuests: true, vmids: null, exclude: null };
|
||||
const task = (guestId: string | null, ago: number, ok = true, status = ok ? "OK" : "job errors") => ({
|
||||
node: "pve1",
|
||||
upid: `upid-${guestId}-${ago}`,
|
||||
guestId,
|
||||
status,
|
||||
ok,
|
||||
startTime: new Date(NOW.getTime() - ago * HOUR).toISOString(),
|
||||
endTime: new Date(NOW.getTime() - ago * HOUR + 600_000).toISOString(),
|
||||
});
|
||||
const snap = (type: string, id: string, ago: number, verification: "ok" | "failed" | null = "ok") => ({ backupType: type, backupId: id, backupTime: Math.floor((NOW.getTime() - ago * HOUR) / 1000), verification });
|
||||
|
||||
const px = (guests: ReturnType<typeof guest>[], jobs: (typeof allJob)[], tasks: ReturnType<typeof task>[]) => [{ integrationId: 1, integrationName: "Felicia", guests, jobs, tasks }];
|
||||
|
||||
describe("what each guest reads as", () => {
|
||||
test("backed up recently to a Backup Server: ok; a while ago: stale", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "web"), guest(101, "db", "lxc")], [allJob], []), pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 5), snap("ct", "101", 100)] }], servers: [], now: NOW });
|
||||
const by = Object.fromEntries(rows.map((r) => [r.name, r]));
|
||||
assert.equal(by.web.status, "ok");
|
||||
assert.equal(by.web.lastSource, "pbs");
|
||||
assert.equal(by.db.status, "stale", "100 hours is more than the 48 by default");
|
||||
assert.equal(by.web.lastBackupAt, new Date(NOW.getTime() - 5 * HOUR).toISOString());
|
||||
});
|
||||
|
||||
test("a task that names the guest counts, and the newer of the two sources wins", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "web")], [allJob], [task("100", 30), task("100", 3, false)]), pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 10)] }], servers: [], now: NOW });
|
||||
assert.equal(rows[0].lastSource, "pbs", "the snapshot (10 h) is newer than the successful task (30 h)");
|
||||
assert.equal(rows[0].status, "failed", "but the latest run, 3 h ago, failed");
|
||||
assert.match(rows[0].failure!, /job errors/);
|
||||
});
|
||||
|
||||
test("a failed run followed by a good backup is not a failure any more", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "web")], [allJob], [task("100", 8, false), task("100", 2, true)]), pbs: [], servers: [], now: NOW });
|
||||
assert.equal(rows[0].status, "ok");
|
||||
assert.equal(rows[0].lastSource, "task");
|
||||
});
|
||||
|
||||
test("a snapshot that failed verification is a failure", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "web")], [allJob], []), pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 2, "failed")] }], servers: [], now: NOW });
|
||||
assert.equal(rows[0].status, "failed");
|
||||
assert.match(rows[0].failure!, /verification/);
|
||||
});
|
||||
|
||||
test("in a job with nothing to say when it last ran is unknown; in no job and no backup is none", () => {
|
||||
const only101 = { ...allJob, allGuests: false, vmids: "100" };
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "covered"), guest(101, "forgotten")], [only101], []), pbs: [], servers: [], now: NOW });
|
||||
const by = Object.fromEntries(rows.map((r) => [r.name, r]));
|
||||
assert.equal(by.covered.status, "unknown");
|
||||
assert.equal(by.covered.inJob, true);
|
||||
assert.equal(by.forgotten.status, "none");
|
||||
assert.equal(by.forgotten.inJob, false);
|
||||
});
|
||||
|
||||
test("a guest in no job that still has an old backup is judged by its age", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(101, "manual")], [], []), pbs: [{ integrationName: "PBS", groups: [snap("vm", "101", 6)] }], servers: [], now: NOW });
|
||||
assert.equal(rows[0].status, "ok");
|
||||
assert.equal(rows[0].inJob, false);
|
||||
});
|
||||
|
||||
test("how long counts as stale is up to the caller", () => {
|
||||
const input = { proxmox: px([guest(100, "web")], [allJob], []), pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 100)] }], servers: [], now: NOW };
|
||||
assert.equal(buildCoverage({ ...input, staleHours: 24 * 3 })[0].status, "stale");
|
||||
assert.equal(buildCoverage({ ...input, staleHours: 24 * 7 })[0].status, "ok");
|
||||
});
|
||||
|
||||
test("a VM and a container with the same number aren't mixed up, and a linked server is named", () => {
|
||||
const rows = buildCoverage({
|
||||
proxmox: px([guest(100, "vm100", "qemu"), guest(100, "ct100", "lxc", "pve2")], [allJob], []),
|
||||
pbs: [{ integrationName: "PBS", groups: [snap("ct", "100", 1)] }],
|
||||
servers: [{ id: 7, name: "elsa", proxmoxIntegrationId: 1, proxmoxVmid: 100 }],
|
||||
now: NOW,
|
||||
});
|
||||
const by = Object.fromEntries(rows.map((r) => [r.name, r]));
|
||||
assert.equal(by.ct100.status, "ok");
|
||||
assert.equal(by.vm100.status, "unknown", "only the container has a snapshot");
|
||||
assert.deepEqual(by.vm100.server, { id: 7, name: "elsa" });
|
||||
});
|
||||
|
||||
test("the counts add up", () => {
|
||||
const rows = buildCoverage({ proxmox: px([guest(100, "a"), guest(101, "b")], [{ ...allJob, allGuests: false, vmids: "100" }], []), pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 1)] }], servers: [], now: NOW });
|
||||
assert.deepEqual(summarize(rows), { ok: 1, stale: 0, failed: 0, none: 1, unknown: 0, total: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("the route and the digest", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values({ oidcSub: "viewer", name: "Vic", role: "viewer" });
|
||||
await db.insert(servers).values({ name: "elsa", apiTokenHash: "a", apiTokenPrefix: "a", lastSeenAt: new Date().toISOString() });
|
||||
setCoverageLoaderForTests(async () => ({
|
||||
proxmox: px([guest(100, "web"), guest(101, "lonely")], [{ ...allJob, allGuests: false, vmids: "100" }], []).map((p) => ({ ...p })),
|
||||
pbs: [{ integrationName: "PBS", groups: [snap("vm", "100", 100)] }],
|
||||
sources: [
|
||||
{ kind: "proxmox" as const, name: "Felicia", ok: true },
|
||||
{ kind: "pbs" as const, name: "PBS", ok: false, error: "couldn't read tank" },
|
||||
],
|
||||
}));
|
||||
const a = express();
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/backups", backupsRouter);
|
||||
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/backups`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
setCoverageLoaderForTests(null);
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("anyone signed in gets the rows, the counts, and which sources couldn't be read", async () => {
|
||||
const body = await (await fetch(`${base}/coverage`)).json();
|
||||
assert.equal(body.staleHours, 48);
|
||||
assert.deepEqual(body.guests.map((g: { name: string }) => g.name), ["lonely", "web"]);
|
||||
assert.equal(body.counts.total, 2);
|
||||
assert.equal(body.sources.find((s: { name: string }) => s.name === "PBS").ok, false);
|
||||
});
|
||||
|
||||
test("the stale threshold is set in days, and nonsense falls back to the default", async () => {
|
||||
// web's newest snapshot is 100 hours old: stale at 2 days, fine at 7.
|
||||
assert.equal((await (await fetch(`${base}/coverage?staleDays=7`)).json()).guests.find((g: { name: string }) => g.name === "web").status, "ok");
|
||||
assert.equal((await (await fetch(`${base}/coverage?staleDays=2`)).json()).guests.find((g: { name: string }) => g.name === "web").status, "stale");
|
||||
assert.equal((await (await fetch(`${base}/coverage?staleDays=banana`)).json()).staleHours, 48);
|
||||
assert.equal((await (await fetch(`${base}/coverage?staleDays=9999`)).json()).staleHours, 48);
|
||||
});
|
||||
|
||||
test("the weekly digest lists what isn't backed up, worst first", async () => {
|
||||
const digest = await buildDigest(new Date());
|
||||
assert.match(digest.message, /BACKUPS\n0 of 2 guests have a recent backup\.\n- lonely \(VM 101\): no backup, and not in any backup job\n- web \(VM 100\): last backup \d+ d( \d+ h)? ago/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* The hardware inventory: warranty and running-cost arithmetic, and the routes.
|
||||
*
|
||||
* 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-inventory-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { inventoryRouter } = await import("../src/routes/inventory.js");
|
||||
const { isRealDate, warrantyDaysLeft, warrantyStatus, yearlyKwh, yearlyCost } = await import("../src/services/inventory.js");
|
||||
const { buildDigest } = await import("../src/services/weeklyDigest.js");
|
||||
const { getSettings } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const NOW = new Date("2026-10-17T12:30:00Z");
|
||||
|
||||
describe("dates and warranties", () => {
|
||||
test("only real calendar dates", () => {
|
||||
assert.equal(isRealDate("2026-10-17"), true);
|
||||
assert.equal(isRealDate("2024-02-29"), true);
|
||||
for (const bad of ["2026-02-30", "2026-13-01", "17/10/2026", "2026-1-5", "", "yesterday"]) assert.equal(isRealDate(bad), false, bad);
|
||||
});
|
||||
|
||||
test("days left count whole days from today, and go negative once it has ended", () => {
|
||||
assert.equal(warrantyDaysLeft("2026-10-17", NOW), 0, "ends today: still covered");
|
||||
assert.equal(warrantyDaysLeft("2026-10-27", NOW), 10);
|
||||
assert.equal(warrantyDaysLeft("2026-10-10", NOW), -7);
|
||||
assert.equal(warrantyDaysLeft(null, NOW), null);
|
||||
assert.equal(warrantyDaysLeft("garbage", NOW), null);
|
||||
});
|
||||
|
||||
test("status: none, ok, expiring within 90 days, expired", () => {
|
||||
assert.equal(warrantyStatus(null, NOW), "none");
|
||||
assert.equal(warrantyStatus("2028-01-01", NOW), "ok");
|
||||
assert.equal(warrantyStatus("2027-01-10", NOW), "expiring", "86 days");
|
||||
assert.equal(warrantyStatus("2027-01-20", NOW), "ok", "95 days");
|
||||
assert.equal(warrantyStatus("2026-10-17", NOW), "expiring");
|
||||
assert.equal(warrantyStatus("2026-10-16", NOW), "expired");
|
||||
});
|
||||
});
|
||||
|
||||
describe("running cost", () => {
|
||||
test("a steady draw over a year, at a price per kWh", () => {
|
||||
assert.equal(yearlyKwh(100), 876);
|
||||
assert.equal(yearlyCost(100, 2), 1752);
|
||||
assert.equal(yearlyCost(45, 1.5), Math.round(0.045 * 8760 * 1.5));
|
||||
});
|
||||
test("no draw or no price is no cost, not zero", () => {
|
||||
assert.equal(yearlyCost(null, 2), null);
|
||||
assert.equal(yearlyCost(0, 2), null);
|
||||
assert.equal(yearlyCost(100, null), null);
|
||||
assert.equal(yearlyCost(100, 0), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the inventory routes", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let who = "operator";
|
||||
let elsa = 0;
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "admin", name: "Ada", role: "admin" },
|
||||
{ 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;
|
||||
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/inventory", inventoryRouter);
|
||||
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/inventory`;
|
||||
});
|
||||
|
||||
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 nasId = 0;
|
||||
|
||||
test("an operator adds hardware, and blanks mean no value", async () => {
|
||||
const res = await send("POST", "/", { name: "Synology DS920+", kind: "storage", manufacturer: "Synology", model: "DS920+", serial: " ", purchaseDate: "2024-03-01", purchasePrice: 5200, warrantyUntil: "2027-03-01", location: "Rack, U12", rackUnit: "U12", powerWatts: 45, notes: "" });
|
||||
assert.equal(res.status, 201);
|
||||
const row = (await res.json()).asset;
|
||||
nasId = row.id;
|
||||
assert.equal(row.serial, null);
|
||||
assert.equal(row.notes, null);
|
||||
assert.equal(row.powerWatts, 45);
|
||||
assert.equal((await send("POST", "/", { name: "elsa's box", kind: "server", serverId: elsa, powerWatts: 120, warrantyUntil: new Date(Date.now() + 20 * 86_400_000).toISOString().slice(0, 10) })).status, 201);
|
||||
});
|
||||
|
||||
test("everyone signed in can read the list, with the linked server's name and the price of power", async () => {
|
||||
who = "viewer";
|
||||
const body = await (await send("GET", "/")).json();
|
||||
assert.equal(body.assets.length, 2);
|
||||
const box = body.assets.find((a: { name: string }) => a.name === "elsa's box");
|
||||
assert.deepEqual(box.server, { id: elsa, name: "elsa" });
|
||||
assert.equal(body.assets.find((a: { name: string }) => a.name.startsWith("Synology")).server, null);
|
||||
assert.deepEqual(body.config, { currency: "", pricePerKwh: null });
|
||||
assert.deepEqual(body.servers, [{ id: elsa, name: "elsa" }]);
|
||||
who = "operator";
|
||||
});
|
||||
|
||||
test("only operators change it, and only an administrator sets the price of power", async () => {
|
||||
who = "viewer";
|
||||
assert.equal((await send("POST", "/", { name: "x", kind: "other" })).status, 403);
|
||||
assert.equal((await send("PUT", `/${nasId}`, { name: "x", kind: "other" })).status, 403);
|
||||
assert.equal((await send("DELETE", `/${nasId}`)).status, 403);
|
||||
who = "operator";
|
||||
assert.equal((await send("PUT", "/config", { currency: "SEK", pricePerKwh: 1.8 })).status, 403);
|
||||
who = "admin";
|
||||
const ok = await send("PUT", "/config", { currency: "SEK", pricePerKwh: 1.8 });
|
||||
assert.equal(ok.status, 200);
|
||||
assert.deepEqual((await getSettings()).inventory, { currency: "SEK", pricePerKwh: 1.8 });
|
||||
assert.equal((await send("PUT", "/config", { currency: "SEK", pricePerKwh: -1 })).status, 400);
|
||||
who = "operator";
|
||||
});
|
||||
|
||||
test("a bad entry is refused: no name, made-up kind, impossible date, negative price, unknown server", async () => {
|
||||
for (const bad of [
|
||||
{ name: "", kind: "other" },
|
||||
{ name: "x", kind: "toaster" },
|
||||
{ name: "x", kind: "other", purchaseDate: "2026-02-30" },
|
||||
{ name: "x", kind: "other", warrantyUntil: "next year" },
|
||||
{ name: "x", kind: "other", purchasePrice: -5 },
|
||||
{ name: "x", kind: "other", powerWatts: 1.5 },
|
||||
{ name: "x", kind: "other", serverId: 99999 },
|
||||
]) {
|
||||
assert.equal((await send("POST", "/", bad)).status, 400, JSON.stringify(bad));
|
||||
}
|
||||
});
|
||||
|
||||
test("editing and removing work, and are in the audit log", async () => {
|
||||
const put = await send("PUT", `/${nasId}`, { name: "Synology DS920+ (hall)", kind: "storage", powerWatts: 50 });
|
||||
assert.equal(put.status, 200);
|
||||
const row = (await put.json()).asset;
|
||||
assert.equal(row.name, "Synology DS920+ (hall)");
|
||||
assert.equal(row.manufacturer, null, "a field left out is cleared, not kept: this replaces the entry");
|
||||
assert.equal((await send("PUT", "/99999", { name: "x", kind: "other" })).status, 404);
|
||||
assert.equal((await send("DELETE", `/${nasId}`)).status, 204);
|
||||
assert.equal((await send("DELETE", `/${nasId}`)).status, 404);
|
||||
const actions = (await db.select().from(auditLog)).filter((e) => e.category === "inventory").map((e) => e.action);
|
||||
for (const a of ["create", "update", "delete", "update_config"]) assert.ok(actions.includes(a), a);
|
||||
});
|
||||
|
||||
test("a warranty about to end is in the weekly digest, with a date", async () => {
|
||||
await db.update(servers).set({ lastSeenAt: new Date().toISOString() }).where(eq(servers.id, elsa));
|
||||
const digest = await buildDigest(new Date());
|
||||
assert.match(digest.message, /EXPIRING\n- Warranty on elsa's box ends in (19|20|21) days \(\d{4}-\d{2}-\d{2}\)/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,322 @@
|
||||
/**
|
||||
* The Prometheus endpoint and its tokens, and the signed event webhooks.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import http from "node:http";
|
||||
import { createHmac } from "node:crypto";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "hm-metrics-"));
|
||||
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
||||
process.env.SESSION_DIR = join(dir, "sessions");
|
||||
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
||||
process.env.HEALTH_STARTUP_GRACE_MS = "0";
|
||||
|
||||
const express = (await import("express")).default;
|
||||
const { db } = await import("../src/db/client.js");
|
||||
const { users, servers, secrets, maintenanceWindows, metricsTokens, eventWebhooks, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { metricsRouter, metricsTokensRouter } = await import("../src/routes/metrics.js");
|
||||
const { eventWebhooksRouter } = await import("../src/routes/eventWebhooks.js");
|
||||
const { escapeLabel } = await import("../src/services/metrics.js");
|
||||
const { deliver, emitEvent, signBody } = await import("../src/services/eventWebhooks.js");
|
||||
const { checkMaintenanceEvents, resetMaintenanceEventsForTests } = await import("../src/services/maintenanceEvents.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
describe("escaping", () => {
|
||||
test("a label value keeps its quotes, backslashes and new lines from breaking the line", () => {
|
||||
assert.equal(escapeLabel('say "hi"'), 'say \\"hi\\"');
|
||||
assert.equal(escapeLabel("C:\\temp"), "C:\\\\temp");
|
||||
assert.equal(escapeLabel("a\nb"), "a\\nb");
|
||||
});
|
||||
});
|
||||
|
||||
describe("metrics and webhooks", () => {
|
||||
let app: http.Server;
|
||||
let base = "";
|
||||
let who = "admin";
|
||||
let receiver: http.Server;
|
||||
const received: { headers: http.IncomingHttpHeaders; body: string }[] = [];
|
||||
let receiverStatus: number[] = []; // statuses to answer with, in turn; 200 once it runs out
|
||||
let receiverUrl = "";
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([
|
||||
{ oidcSub: "admin", name: "Ada", role: "admin" },
|
||||
{ oidcSub: "operator", name: "Olga", role: "operator" },
|
||||
]);
|
||||
const now = new Date();
|
||||
await db.insert(servers).values([
|
||||
{
|
||||
name: 'elsa "main"',
|
||||
apiTokenHash: "a",
|
||||
apiTokenPrefix: "a",
|
||||
lastSeenAt: now.toISOString(),
|
||||
cpuLoadPercent: 12.5,
|
||||
memTotalBytes: 1000,
|
||||
memUsedBytes: 250,
|
||||
disks: JSON.stringify([{ mount: "C:\\", sizeBytes: 100, usedBytes: 40 }]),
|
||||
pendingUpdates: JSON.stringify({ manager: "apt", count: 7, security: 2, rebootRequired: true, reportedAt: now.toISOString() }),
|
||||
agentVersion: 1,
|
||||
},
|
||||
{ name: "anna", apiTokenHash: "b", apiTokenPrefix: "b", lastSeenAt: new Date(now.getTime() - 5 * 3600_000).toISOString() },
|
||||
{ name: "never", apiTokenHash: "c", apiTokenPrefix: "c" },
|
||||
]);
|
||||
await db.insert(secrets).values({ name: "VPN key", type: "api_token", expiryDate: new Date(now.getTime() + 9 * 86_400_000).toISOString().slice(0, 10), warnDays: 30 });
|
||||
|
||||
receiver = await new Promise<http.Server>((resolve) => {
|
||||
const s = http.createServer((req, res) => {
|
||||
let raw = "";
|
||||
req.on("data", (c) => (raw += c));
|
||||
req.on("end", () => {
|
||||
received.push({ headers: req.headers, body: raw });
|
||||
res.statusCode = receiverStatus.shift() ?? 200;
|
||||
res.end("ok");
|
||||
});
|
||||
});
|
||||
s.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
receiverUrl = `http://127.0.0.1:${(receiver.address() as AddressInfo).port}/hook`;
|
||||
|
||||
const a = express();
|
||||
a.use("/metrics", metricsRouter); // no session: it is the bearer token that counts
|
||||
const api = express();
|
||||
api.use(express.json());
|
||||
api.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: who } };
|
||||
next();
|
||||
});
|
||||
api.use("/api/metrics-tokens", metricsTokensRouter);
|
||||
api.use("/api/event-webhooks", eventWebhooksRouter);
|
||||
a.use(api);
|
||||
app = await new Promise<http.Server>((resolve) => {
|
||||
const s = a.listen(0, "127.0.0.1", () => resolve(s));
|
||||
});
|
||||
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
app.close();
|
||||
receiver.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 secret = "";
|
||||
let tokenId = 0;
|
||||
|
||||
test("an administrator makes a token; the secret is shown once and only its hash is kept", async () => {
|
||||
const res = await send("POST", "/api/metrics-tokens", { label: "Prometheus" });
|
||||
assert.equal(res.status, 201);
|
||||
const body = await res.json();
|
||||
secret = body.secret;
|
||||
tokenId = body.token.id;
|
||||
assert.match(secret, /^hlm_m_/);
|
||||
const list = await (await send("GET", "/api/metrics-tokens")).json();
|
||||
assert.ok(!JSON.stringify(list).includes(secret) && !JSON.stringify(list).includes("tokenHash"));
|
||||
const [stored] = await db.select().from(metricsTokens);
|
||||
assert.notEqual(stored.tokenHash, secret);
|
||||
assert.equal(stored.tokenHash.length, 64);
|
||||
who = "operator";
|
||||
assert.equal((await send("POST", "/api/metrics-tokens", { label: "x" })).status, 403);
|
||||
assert.equal((await send("GET", "/api/metrics-tokens")).status, 403);
|
||||
who = "admin";
|
||||
assert.equal((await send("POST", "/api/metrics-tokens", { label: "" })).status, 400);
|
||||
});
|
||||
|
||||
test("/metrics wants the token in a header: nothing, a wrong one, or one in the address all get the same refusal", async () => {
|
||||
const none = await fetch(`${base}/metrics`);
|
||||
assert.equal(none.status, 401);
|
||||
assert.match(none.headers.get("www-authenticate") ?? "", /Bearer/);
|
||||
const refusal = await none.text();
|
||||
assert.equal((await fetch(`${base}/metrics`, { headers: { authorization: "Bearer hlm_m_wrongwrongwrongwrongwrong" } })).status, 401);
|
||||
assert.equal((await fetch(`${base}/metrics?token=${secret}`)).status, 401, "a token in the address is not accepted");
|
||||
assert.equal(await (await fetch(`${base}/metrics`, { headers: { authorization: "Bearer nope" } })).text(), refusal);
|
||||
});
|
||||
|
||||
test("with the token it answers in Prometheus's format, with the servers, alerts and the rest", async () => {
|
||||
const res = await fetch(`${base}/metrics`, { headers: { authorization: `Bearer ${secret}` } });
|
||||
assert.equal(res.status, 200);
|
||||
assert.match(res.headers.get("content-type") ?? "", /^text\/plain; version=0\.0\.4/);
|
||||
assert.equal(res.headers.get("cache-control"), "no-store");
|
||||
const text = await res.text();
|
||||
|
||||
assert.match(text, /# TYPE homelab_server_up gauge/);
|
||||
assert.match(text, /homelab_server_up\{server="elsa \\"main\\""\} 1/, "the name's quotes are escaped");
|
||||
assert.match(text, /homelab_server_up\{server="anna"\} 0/, "five hours silent is past the offline threshold");
|
||||
assert.ok(!text.includes('server="never"'), "a server that never reported has nothing to say");
|
||||
assert.match(text, /homelab_server_cpu_load_percent\{server="elsa \\"main\\""\} 12\.5/);
|
||||
assert.match(text, /homelab_server_memory_used_percent\{server="elsa \\"main\\""\} 25/);
|
||||
assert.match(text, /homelab_server_disk_used_percent\{server="elsa \\"main\\"",mount="C:\\\\"\} 40/, "a backslash in a mount is escaped");
|
||||
assert.match(text, /homelab_server_pending_updates\{server="elsa \\"main\\""\} 7/);
|
||||
assert.match(text, /homelab_server_pending_security_updates\{server="elsa \\"main\\""\} 2/);
|
||||
assert.match(text, /homelab_server_reboot_required\{server="elsa \\"main\\""\} 1/);
|
||||
assert.match(text, /homelab_server_agent_outdated\{server="elsa \\"main\\""\} 1/, "version 1 is behind");
|
||||
assert.match(text, /homelab_alerts\{severity="critical"\} \d+/);
|
||||
assert.match(text, /homelab_secret_expiry_days\{name="VPN key"\} 9/);
|
||||
assert.match(text, /homelab_maintenance_active_windows 0/);
|
||||
assert.match(text, /homelab_scrape_duration_seconds /);
|
||||
});
|
||||
|
||||
test("every line is valid exposition format, and each metric is described once", async () => {
|
||||
const text = await (await fetch(`${base}/metrics`, { headers: { authorization: `Bearer ${secret}` } })).text();
|
||||
const helps: string[] = [];
|
||||
for (const line of text.trim().split("\n")) {
|
||||
if (line.startsWith("# HELP ")) {
|
||||
helps.push(line.split(" ")[2]);
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith("# TYPE ")) continue;
|
||||
assert.match(line, /^[a-zA-Z_:][a-zA-Z0-9_:]*(\{([a-zA-Z_][a-zA-Z0-9_]*="([^"\\]|\\.)*",?)*\})? -?\d+(\.\d+)?$/, line);
|
||||
}
|
||||
assert.equal(new Set(helps).size, helps.length, "no metric is declared twice");
|
||||
});
|
||||
|
||||
test("use is noted, and a removed token stops working at once", async () => {
|
||||
const [row] = await db.select().from(metricsTokens).where(eq(metricsTokens.id, tokenId));
|
||||
assert.ok(row.lastUsedAt);
|
||||
assert.equal((await send("DELETE", `/api/metrics-tokens/${tokenId}`)).status, 204);
|
||||
assert.equal((await fetch(`${base}/metrics`, { headers: { authorization: `Bearer ${secret}` } })).status, 401);
|
||||
assert.equal((await send("DELETE", `/api/metrics-tokens/${tokenId}`)).status, 404);
|
||||
const entries = (await db.select().from(auditLog)).filter((e) => e.category === "metrics_token");
|
||||
assert.deepEqual(entries.map((e) => e.action), ["create", "delete"]);
|
||||
assert.ok(entries.every((e) => !(e.detail ?? "").includes(secret)));
|
||||
});
|
||||
|
||||
// ── event webhooks ──
|
||||
|
||||
let hookId = 0;
|
||||
const HOOK_SECRET = "s3cret-for-signing";
|
||||
const waitFor = async (n: number, ms = 3000) => {
|
||||
for (let t = 0; t < ms && received.length < n; t += 25) await new Promise((r) => setTimeout(r, 25));
|
||||
};
|
||||
|
||||
test("a webhook is made with a secret that is never shown again, and kept encrypted", async () => {
|
||||
const res = await send("POST", "/api/event-webhooks", { label: "Home Assistant", url: receiverUrl, secret: HOOK_SECRET, events: ["alert.opened", "maintenance.started"] });
|
||||
assert.equal(res.status, 201);
|
||||
const hook = (await res.json()).webhook;
|
||||
hookId = hook.id;
|
||||
assert.equal(hook.hasSecret, true);
|
||||
assert.deepEqual(hook.events, ["alert.opened", "maintenance.started"]);
|
||||
assert.ok(!JSON.stringify(await (await send("GET", "/api/event-webhooks")).json()).includes(HOOK_SECRET));
|
||||
const [stored] = await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, hookId));
|
||||
assert.match(stored.secret ?? "", /^enc:v1:/);
|
||||
const audit = (await db.select().from(auditLog)).find((e) => e.category === "webhook" && e.action === "create");
|
||||
assert.ok(!(audit?.detail ?? "").includes(receiverUrl) && !(audit?.detail ?? "").includes(HOOK_SECRET), "neither the address nor the secret goes in the audit log");
|
||||
});
|
||||
|
||||
test("a bad webhook is refused: not http, a password in the address, no events, only admins", async () => {
|
||||
for (const bad of [
|
||||
{ label: "x", url: "ftp://x.example/hook", events: ["alert.opened"] },
|
||||
{ label: "x", url: "https://user:pass@x.example/hook", events: ["alert.opened"] },
|
||||
{ label: "x", url: "not a url", events: ["alert.opened"] },
|
||||
{ label: "x", url: receiverUrl, events: [] },
|
||||
{ label: "x", url: receiverUrl, events: ["alert.exploded"] },
|
||||
{ label: "", url: receiverUrl, events: ["alert.opened"] },
|
||||
]) {
|
||||
assert.equal((await send("POST", "/api/event-webhooks", bad)).status, 400, JSON.stringify(bad));
|
||||
}
|
||||
who = "operator";
|
||||
assert.equal((await send("GET", "/api/event-webhooks")).status, 403);
|
||||
who = "admin";
|
||||
});
|
||||
|
||||
test("an event goes to the webhooks that want it, signed so the receiver can check it", async () => {
|
||||
received.length = 0;
|
||||
emitEvent("alert.opened", { alert: { key: "offline:server:1", severity: "critical", message: "elsa hasn't reported" } });
|
||||
emitEvent("alert.resolved", { alert: { key: "x", message: "not wanted by this webhook" } });
|
||||
await waitFor(1);
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
assert.equal(received.length, 1, "only the event it asked for");
|
||||
const got = received[0];
|
||||
assert.equal(got.headers["x-hlm-event"], "alert.opened");
|
||||
assert.equal(got.headers["x-hlm-signature"], `sha256=${createHmac("sha256", HOOK_SECRET).update(got.body).digest("hex")}`);
|
||||
assert.equal(got.headers["x-hlm-signature"], signBody(HOOK_SECRET, got.body));
|
||||
const body = JSON.parse(got.body);
|
||||
assert.equal(body.event, "alert.opened");
|
||||
assert.equal(body.source, "homelab-manager");
|
||||
assert.equal(body.data.alert.message, "elsa hasn't reported");
|
||||
assert.equal(body.id, got.headers["x-hlm-delivery"]);
|
||||
const [row] = await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, hookId));
|
||||
assert.equal(row.lastResult, "200");
|
||||
assert.ok(row.lastAttemptAt);
|
||||
});
|
||||
|
||||
test("a switched-off webhook hears nothing", async () => {
|
||||
assert.equal((await send("PATCH", `/api/event-webhooks/${hookId}`, { enabled: false })).status, 200);
|
||||
received.length = 0;
|
||||
emitEvent("alert.opened", { alert: { message: "x" } });
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
assert.equal(received.length, 0);
|
||||
await send("PATCH", `/api/event-webhooks/${hookId}`, { enabled: true });
|
||||
});
|
||||
|
||||
test("a delivery that fails is tried once more, and what happened is noted", async () => {
|
||||
const [row] = await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, hookId));
|
||||
received.length = 0;
|
||||
receiverStatus = [500, 200];
|
||||
const ok = await deliver(row, "alert.opened", { n: 1 }, { retryAfterMs: 10 });
|
||||
assert.deepEqual(ok, { ok: true, result: "200" });
|
||||
assert.equal(received.length, 2, "tried twice");
|
||||
assert.equal(received[0].headers["x-hlm-delivery"], received[1].headers["x-hlm-delivery"], "the same message both times");
|
||||
|
||||
receiverStatus = [503, 503];
|
||||
const bad = await deliver(row, "alert.opened", { n: 2 }, { retryAfterMs: 10 });
|
||||
assert.deepEqual(bad, { ok: false, result: "503" });
|
||||
assert.equal((await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, hookId)))[0].lastResult, "503");
|
||||
|
||||
const dead = await deliver({ ...row, url: "http://127.0.0.1:1/nothing" }, "alert.opened", {}, { retryAfterMs: 10 });
|
||||
assert.equal(dead.ok, false);
|
||||
});
|
||||
|
||||
test("a test message goes out whatever the webhook is set to, and says how it went", async () => {
|
||||
received.length = 0;
|
||||
receiverStatus = [];
|
||||
const res = await send("POST", `/api/event-webhooks/${hookId}/test`);
|
||||
assert.deepEqual(await res.json(), { ok: true, result: "200" });
|
||||
assert.equal(received[0].headers["x-hlm-event"], "test");
|
||||
assert.equal((await send("POST", "/api/event-webhooks/99999/test")).status, 404);
|
||||
});
|
||||
|
||||
test("maintenance beginning and ending is noticed, however it came about, and a first look says nothing", async () => {
|
||||
resetMaintenanceEventsForTests();
|
||||
const [elsa] = await db.select().from(servers).where(eq(servers.name, 'elsa "main"'));
|
||||
await db.insert(maintenanceWindows).values({ targetType: "server", targetId: elsa.id, reason: "before", startedAt: new Date(Date.now() - 60_000).toISOString(), endsAt: new Date(Date.now() + 3600_000).toISOString(), createdBy: "Ada" });
|
||||
assert.deepEqual(await checkMaintenanceEvents(), { started: [], ended: [] }, "already running when first looked at: not announced");
|
||||
|
||||
const [w] = await db.insert(maintenanceWindows).values({ targetType: "server", targetId: elsa.id, reason: "patching", startedAt: new Date(Date.now() - 1000).toISOString(), endsAt: new Date(Date.now() + 3600_000).toISOString(), createdBy: "Ada", source: "schedule:1:2026-10-17" }).returning();
|
||||
received.length = 0;
|
||||
const started = await checkMaintenanceEvents();
|
||||
assert.deepEqual(started, { started: [w.id], ended: [] });
|
||||
await waitFor(1);
|
||||
assert.equal(received[0].headers["x-hlm-event"], "maintenance.started");
|
||||
const payload = JSON.parse(received[0].body).data.window;
|
||||
assert.equal(payload.targetName, 'elsa "main"');
|
||||
assert.equal(payload.reason, "patching");
|
||||
assert.equal(payload.source, "schedule:1:2026-10-17");
|
||||
|
||||
// ended early
|
||||
await db.update(maintenanceWindows).set({ endsAt: new Date(Date.now() - 500).toISOString() }).where(eq(maintenanceWindows.id, w.id));
|
||||
assert.deepEqual(await checkMaintenanceEvents(), { started: [], ended: [w.id] });
|
||||
assert.deepEqual(await checkMaintenanceEvents(), { started: [], ended: [] }, "and only once");
|
||||
});
|
||||
|
||||
test("a webhook can be removed", async () => {
|
||||
assert.equal((await send("DELETE", `/api/event-webhooks/${hookId}`)).status, 204);
|
||||
assert.equal((await send("DELETE", `/api/event-webhooks/${hookId}`)).status, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* 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");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* Roles from sign-in groups: reading the claim, the rules, what happens to people at sign-in, and the Users routes around it.
|
||||
*
|
||||
* 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-groups-"));
|
||||
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 } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const { extractGroups, roleFromGroups } = await import("../src/auth/groups.js");
|
||||
const { upsertUserFromLogin } = await import("../src/auth/users.js");
|
||||
const { usersRouter } = await import("../src/routes/users.js");
|
||||
const { updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const rules = { roleFromGroups: true, groupsClaim: "groups", adminGroups: ["homelab-admins"], operatorGroups: ["homelab-ops"], otherwise: "viewer" as const };
|
||||
|
||||
describe("reading the groups out of a sign-in", () => {
|
||||
test("from any place the provider put them, as a list or a single name", () => {
|
||||
assert.deepEqual(extractGroups([{ groups: ["a", "b"] }], "groups"), ["a", "b"]);
|
||||
assert.deepEqual(extractGroups([{ groups: "solo" }], "groups"), ["solo"]);
|
||||
assert.deepEqual(extractGroups([{ sub: "x" }, { groups: ["u"] }], "groups"), ["u"], "the user-info answer counts too");
|
||||
assert.deepEqual(extractGroups([{ groups: ["a", "b"] }, { groups: ["b", "c"] }], "groups"), ["a", "b", "c"], "joined, without repeats");
|
||||
assert.deepEqual(extractGroups([{ roles: ["x"] }], "roles"), ["x"], "a claim of another name");
|
||||
assert.deepEqual(extractGroups([{ groups: [" padded ", "", 5, null, "ok"] }], "groups"), ["padded", "ok"], "only real names");
|
||||
});
|
||||
|
||||
test("not being told is not the same as being in no groups", () => {
|
||||
assert.equal(extractGroups([{ sub: "x" }], "groups"), null);
|
||||
assert.equal(extractGroups([{ groups: null }], "groups"), null);
|
||||
assert.deepEqual(extractGroups([{ groups: [] }], "groups"), [], "an empty list is an answer");
|
||||
assert.equal(extractGroups([], "groups"), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the rules", () => {
|
||||
test("admin beats operator, names ignore case and spaces, and the rest get what 'otherwise' says", () => {
|
||||
assert.equal(roleFromGroups(rules, ["homelab-ops", "homelab-admins"], "viewer"), "admin");
|
||||
assert.equal(roleFromGroups(rules, ["HomeLab-Ops "], "viewer"), "operator");
|
||||
assert.equal(roleFromGroups(rules, ["cooks"], "operator"), "viewer");
|
||||
assert.equal(roleFromGroups(rules, [], "admin"), "viewer");
|
||||
assert.equal(roleFromGroups({ ...rules, otherwise: "keep" }, ["cooks"], "operator"), "operator");
|
||||
assert.equal(roleFromGroups({ ...rules, adminGroups: [], operatorGroups: [] }, ["homelab-admins"], "viewer"), "viewer", "no groups named, no role from them");
|
||||
});
|
||||
});
|
||||
|
||||
describe("at sign-in", () => {
|
||||
const login = (sub: string, groups: string[] | null | undefined) => upsertUserFromLogin({ sub, email: `${sub}@lab`, name: sub, groups });
|
||||
const roleOf = async (sub: string) => (await db.select().from(users).where(eq(users.oidcSub, sub)))[0].role;
|
||||
|
||||
test("with the rules off, groups are remembered but roles stay as they are", async () => {
|
||||
const first = await login("founder", ["homelab-ops"]);
|
||||
assert.equal(first.user.role, "admin", "the first person ever is the administrator");
|
||||
assert.deepEqual(JSON.parse(first.user.groups!), ["homelab-ops"]);
|
||||
const second = await login("sam", ["homelab-admins"]);
|
||||
assert.equal(second.user.role, "viewer");
|
||||
assert.equal(second.roleChange, undefined);
|
||||
assert.equal((await login("sam", ["homelab-admins"])).user.role, "viewer", "and again on the next sign-in");
|
||||
});
|
||||
|
||||
test("with the rules on, a new person's groups decide, and the first person is still the administrator", async () => {
|
||||
await updateSettings({ signIn: rules });
|
||||
assert.equal((await login("olga", ["homelab-ops"])).user.role, "operator");
|
||||
assert.equal((await login("nobody", ["cooks"])).user.role, "viewer");
|
||||
assert.equal((await login("quiet", [])).user.role, "viewer");
|
||||
assert.equal((await login("unknown", null)).user.role, "viewer", "no groups reported: viewer, as before");
|
||||
});
|
||||
|
||||
test("an existing person's role follows their groups, and says what changed", async () => {
|
||||
const up = await login("sam", ["homelab-ops"]);
|
||||
assert.equal(up.user.role, "operator");
|
||||
assert.deepEqual(up.roleChange, { from: "viewer", to: "operator", groups: ["homelab-ops"] });
|
||||
const again = await login("sam", ["homelab-ops"]);
|
||||
assert.equal(again.roleChange, undefined, "nothing to say when nothing changed");
|
||||
const down = await login("sam", ["cooks"]);
|
||||
assert.equal(down.user.role, "viewer");
|
||||
assert.equal(down.roleChange?.from, "operator");
|
||||
});
|
||||
|
||||
test("a sign-in that carries no groups leaves roles alone, so a wrong claim name can't demote anyone", async () => {
|
||||
await login("sam", ["homelab-admins"]);
|
||||
assert.equal(await roleOf("sam"), "admin");
|
||||
const blind = await login("sam", null);
|
||||
assert.equal(blind.roleChange, undefined);
|
||||
assert.equal(await roleOf("sam"), "admin");
|
||||
assert.equal(await roleOf("founder"), "admin");
|
||||
await login("founder", undefined);
|
||||
assert.equal(await roleOf("founder"), "admin");
|
||||
});
|
||||
|
||||
test("the last administrator is never demoted by a group rule", async () => {
|
||||
// sam and founder are both admins now; demoting one is allowed…
|
||||
const d1 = await login("sam", ["cooks"]);
|
||||
assert.equal(d1.user.role, "viewer");
|
||||
// …but not the other, who is the last.
|
||||
const d2 = await login("founder", ["cooks"]);
|
||||
assert.equal(d2.user.role, "admin");
|
||||
assert.equal(d2.roleChange?.blocked, true);
|
||||
assert.equal(await roleOf("founder"), "admin");
|
||||
});
|
||||
|
||||
test("'keep' leaves people in no named group as they are", async () => {
|
||||
await updateSettings({ signIn: { ...rules, otherwise: "keep" } });
|
||||
await login("olga", ["cooks"]);
|
||||
assert.equal(await roleOf("olga"), "operator");
|
||||
await updateSettings({ signIn: { ...rules, otherwise: "viewer" } });
|
||||
});
|
||||
});
|
||||
|
||||
describe("the Users routes", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
|
||||
before(async () => {
|
||||
await db.update(users).set({ role: "admin" }).where(eq(users.oidcSub, "founder"));
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: "founder" } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/users", usersRouter);
|
||||
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/users`;
|
||||
});
|
||||
|
||||
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) });
|
||||
|
||||
test("the list carries each person's groups and says whether roles follow them; nothing else leaks", async () => {
|
||||
const body = await (await send("GET", "/")).json();
|
||||
assert.equal(body.rolesFollowGroups, true);
|
||||
const olga = body.users.find((u: { name: string }) => u.name === "olga");
|
||||
assert.deepEqual(olga.groups, ["cooks"]);
|
||||
assert.deepEqual(Object.keys(olga).sort(), ["createdAt", "email", "groups", "id", "lastLoginAt", "name", "oidcSub", "role"]);
|
||||
});
|
||||
|
||||
test("a role can't be set by hand while groups decide it", async () => {
|
||||
const olga = (await db.select().from(users).where(eq(users.oidcSub, "olga")))[0];
|
||||
const res = await send("PATCH", `/${olga.id}/role`, { role: "viewer" });
|
||||
assert.equal(res.status, 409);
|
||||
assert.equal((await res.json()).error, "managed_by_groups");
|
||||
await updateSettings({ signIn: { roleFromGroups: false } });
|
||||
assert.equal((await send("PATCH", `/${olga.id}/role`, { role: "viewer" })).status, 200, "and it can again once they don't");
|
||||
});
|
||||
|
||||
test("the preview says what each person would be, and warns when nobody would be an administrator", async () => {
|
||||
await db.update(users).set({ groups: JSON.stringify(["homelab-admins"]) }).where(eq(users.oidcSub, "founder"));
|
||||
const ok = await (await send("POST", "/preview-group-roles", { adminGroups: ["homelab-admins"] })).json();
|
||||
assert.equal(ok.people.find((p: { name: string }) => p.name === "founder").wouldBe, "admin");
|
||||
assert.equal(ok.adminsAfter, 1);
|
||||
|
||||
const locked = await (await send("POST", "/preview-group-roles", { adminGroups: ["a-group-nobody-is-in"] })).json();
|
||||
assert.equal(locked.adminsAfter, 0, "that rule would leave no administrator");
|
||||
assert.equal(locked.people.find((p: { name: string }) => p.name === "founder").wouldBe, "viewer");
|
||||
|
||||
const never = locked.people.find((p: { name: string }) => p.name === "unknown");
|
||||
assert.equal(never.groups, null);
|
||||
assert.equal(never.wouldBe, null, "someone who has never signed in with groups can't be told");
|
||||
assert.equal((await send("POST", "/preview-group-roles", { adminGroups: "nope" })).status, 400);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user