Lidarr/Readarr, integration inspector, app versions, grouped alerts
Lidarr and Readarr join the shared *arr reader. Integrations get an Inspect button that runs the reader once and shows what each request returned as shapes (never values) and where the result is empty. Operations -> App versions compares each application's version with its newest GitHub release (lookup off until an admin switches it on; optional alert). The Alerts page can group by source, kind of problem or type of alert. The shared HTTP helper no longer reuses connections (stale-socket resets). Migration 0078; docs and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
551eb1597a
commit
93aaf5476d
45 files changed
+10033
-65
No files matched your search
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Grouping the alerts on the Alerts page (by source, by kind of problem, by type of alert): the pure function the page uses, run here against the web's own source file — the page has no test
|
||||
* runner of its own — and the words for a type of alert.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { join } from "node:path";
|
||||
|
||||
const web = (file: string) => pathToFileURL(join(import.meta.dirname, "../../web/src/utils", file)).href;
|
||||
const { groupAlerts, isGroup, GROUP_BY_LABELS } = (await import(web("alertGroups.ts"))) as typeof import("../../web/src/utils/alertGroups");
|
||||
const { kindWords } = (await import(web("alertKinds.ts"))) as typeof import("../../web/src/utils/alertKinds");
|
||||
|
||||
type A = Parameters<typeof groupAlerts>[0][number];
|
||||
const alert = (id: string, source: string, severity: A["severity"], extra: Partial<A> = {}): A => ({ id, source, severity, silenced: false, category: "services", kind: "disk", ...extra });
|
||||
const label = (c: string) => ({ services: "Services", disk: "Disk", backup: "Backups" })[c] ?? c;
|
||||
const none = new Set<string>();
|
||||
const shape = (list: ReturnType<typeof groupAlerts>) => list.map((x) => (isGroup(x) ? `# ${x.label} (${x.count}: ${x.critical}c ${x.warning}w ${x.info}i ${x.silenced}s)` : (x as A).id));
|
||||
|
||||
describe("the words for a type of alert", () => {
|
||||
test("a type that has words says them; one that has not is its own name", () => {
|
||||
assert.equal(kindWords("hetzner-cert"), "a Hetzner certificate about to end");
|
||||
assert.equal(kindWords("sonarr-queue"), "stuck downloads in Sonarr");
|
||||
assert.equal(kindWords("lidarr-queue"), "stuck downloads in Lidarr");
|
||||
assert.equal(kindWords("something-new"), "something new alerts");
|
||||
});
|
||||
});
|
||||
|
||||
describe("grouping alerts", () => {
|
||||
const list = [
|
||||
alert("1", "Hetzner", "warning", { category: "services", kind: "hetzner-cert" }),
|
||||
alert("2", "Sonarr", "critical", { category: "disk", kind: "sonarr-disk" }),
|
||||
alert("3", "Hetzner", "warning", { category: "backup", kind: "hetzner-backup" }),
|
||||
alert("4", "Sonarr", "warning", { category: "services", kind: "sonarr-queue" }),
|
||||
alert("5", "Hetzner", "info", { category: "services", kind: "hetzner-cert" }),
|
||||
alert("6", "Fragnet", "warning", { category: "backup", kind: "fragnet-backup", silenced: true }),
|
||||
];
|
||||
|
||||
test("not grouped is the list as it was", () => {
|
||||
assert.deepEqual(groupAlerts(list, "none", none, label), list);
|
||||
assert.deepEqual(Object.keys(GROUP_BY_LABELS), ["none", "source", "category", "kind"]);
|
||||
});
|
||||
|
||||
test("by source: the worst group first, then the biggest; each group a header with its counts and then its alerts in the order given; a group of only held-back alerts last", () => {
|
||||
assert.deepEqual(shape(groupAlerts(list, "source", none, label)), ["# Sonarr (2: 1c 1w 0i 0s)", "2", "4", "# Hetzner (3: 0c 2w 1i 0s)", "1", "3", "5", "# Fragnet (1: 0c 0w 0i 1s)", "6"]);
|
||||
});
|
||||
|
||||
test("by kind of problem it is the category's name; by type of alert the words for the type", () => {
|
||||
assert.deepEqual(shape(groupAlerts(list, "category", none, label)).filter((x) => x.startsWith("#")), ["# Disk (1: 1c 0w 0i 0s)", "# Services (3: 0c 2w 1i 0s)", "# Backups (2: 0c 1w 0i 1s)"]);
|
||||
assert.deepEqual(
|
||||
shape(groupAlerts(list, "kind", none, label)).filter((x) => x.startsWith("#")),
|
||||
["# a disk on Sonarr (1: 1c 0w 0i 0s)".replace("a disk on Sonarr", kindWords("sonarr-disk")), `# ${kindWords("hetzner-cert")} (2: 0c 1w 1i 0s)`, `# ${kindWords("hetzner-backup")} (1: 0c 1w 0i 0s)`, `# ${kindWords("sonarr-queue")} (1: 0c 1w 0i 0s)`, `# ${kindWords("fragnet-backup")} (1: 0c 0w 0i 1s)`],
|
||||
);
|
||||
});
|
||||
|
||||
test("a folded group keeps its header, with the count, and shows none of its alerts", () => {
|
||||
const folded = groupAlerts(list, "source", new Set(["source:Hetzner"]), label);
|
||||
assert.deepEqual(shape(folded), ["# Sonarr (2: 1c 1w 0i 0s)", "2", "4", "# Hetzner (3: 0c 2w 1i 0s)", "# Fragnet (1: 0c 0w 0i 1s)", "6"]);
|
||||
assert.equal((folded.find((x) => isGroup(x) && x.label === "Hetzner") as { folded: boolean }).folded, true);
|
||||
});
|
||||
|
||||
test("what can be picked from a group is what is not held back; ids are by grouping, so folding one grouping does not fold another", () => {
|
||||
const g = groupAlerts(list, "source", none, label).filter(isGroup);
|
||||
assert.deepEqual(g.map((x) => [x.id, x.pickable]), [["source:Sonarr", ["2", "4"]], ["source:Hetzner", ["1", "3", "5"]], ["source:Fragnet", []]]);
|
||||
assert.equal(groupAlerts(list, "category", new Set(["source:Hetzner"]), label).filter((x) => !isGroup(x)).length, 6, "nothing folded");
|
||||
});
|
||||
|
||||
test("no alerts is no groups", () => {
|
||||
assert.deepEqual(groupAlerts([], "source", none, label), []);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* App versions: comparing what each integration last read with the newest release (kept from GitHub, only while switched on), the page's data and its switch, the optional alert and /metrics.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import type { Server } from "node:http";
|
||||
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-versions-"));
|
||||
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, integrations, arrState, grafanaState, adguardState, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const av = await import("../src/services/appVersions.js");
|
||||
const { versionsRouter } = await import("../src/routes/versions.js");
|
||||
const { evaluateHealth, collectSnapshot } = await import("../src/services/healthMonitor.js");
|
||||
const { subjectOfConditionKey } = await import("../src/services/maintenance.js");
|
||||
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
|
||||
const { buildMetrics } = await import("../src/services/metrics.js");
|
||||
const { getSettings, updateSettings } = await import("../src/services/settingsStore.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
const DAY = 86_400_000;
|
||||
const iso = (ms: number) => new Date(ms).toISOString();
|
||||
|
||||
describe("reading a version", () => {
|
||||
test("the first three numbers, after the prefixes projects put on theirs", () => {
|
||||
assert.deepEqual(av.parseVersion("v4.0.9.2244"), [4, 0, 9]);
|
||||
assert.deepEqual(av.parseVersion("4.0.9.2244"), [4, 0, 9]);
|
||||
assert.deepEqual(av.parseVersion("n8n@1.60.0"), [1, 60, 0]);
|
||||
assert.deepEqual(av.parseVersion("version/2024.8.3"), [2024, 8, 3]);
|
||||
assert.deepEqual(av.parseVersion("v0.107.52"), [0, 107, 52]);
|
||||
assert.deepEqual(av.parseVersion("2.12.0-rc1"), [2, 12, 0]);
|
||||
assert.deepEqual(av.parseVersion("v10"), [10, 0, 0]);
|
||||
for (const none of [null, undefined, "", "latest", "nightly"]) assert.equal(av.parseVersion(none), null, String(none));
|
||||
});
|
||||
|
||||
test("how far behind: the first number is major, the second minor, the third a patch; a build number is not a difference; newer than the newest is ahead", () => {
|
||||
assert.equal(av.lagOf("4.0.9.2244", "v4.0.9.2300"), "same");
|
||||
assert.equal(av.lagOf("4.0.9.2244", "v4.0.10.2300"), "patch");
|
||||
assert.equal(av.lagOf("11.2.0", "v11.3.1"), "minor");
|
||||
assert.equal(av.lagOf("10.9.11", "v11.0.0"), "major");
|
||||
assert.equal(av.lagOf("2024.8.3", "version/2025.2.0"), "major", "a new year of Authentik");
|
||||
assert.equal(av.lagOf("v0.107.52", "v0.107.51"), "ahead");
|
||||
assert.equal(av.lagOf("1.0.0", "v1.0.0"), "same");
|
||||
assert.equal(av.lagOf(null, "v1.0.0"), "unknown");
|
||||
assert.equal(av.lagOf("1.0.0", ""), "unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("what is kept and shown", () => {
|
||||
const ids: Record<string, number> = {};
|
||||
const mk = async (type: string, name: string, enabled = true) => (await db.insert(integrations).values({ type: type as "sonarr", name, baseUrl: "http://x", enabled, config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
|
||||
const asked: string[] = [];
|
||||
let failing = new Set<string>();
|
||||
const published = (daysAgo: number) => iso(Date.now() - daysAgo * DAY);
|
||||
|
||||
before(async () => {
|
||||
ids.sonarr = await mk("sonarr", "TV");
|
||||
ids.sonarr2 = await mk("sonarr", "TV 4K");
|
||||
ids.grafana = await mk("grafana", "Dashboards");
|
||||
ids.adguard = await mk("adguard", "DNS");
|
||||
ids.radarr = await mk("radarr", "Films"); // never read: no version
|
||||
ids.proxmox = await mk("proxmox", "Cluster"); // no release to compare
|
||||
ids.off = await mk("grafana", "Old grafana", false);
|
||||
const at = new Date().toISOString();
|
||||
await db.insert(arrState).values([{ integrationId: ids.sonarr, version: "4.0.9.2244", health: "[]", updatedAt: at }, { integrationId: ids.sonarr2, version: "4.0.9.2244", health: "[]", updatedAt: at }]);
|
||||
await db.insert(grafanaState).values({ integrationId: ids.grafana, version: "11.2.0", updatedAt: at });
|
||||
await db.insert(adguardState).values({ integrationId: ids.adguard, version: "v0.107.52", updatedAt: at });
|
||||
av.setReleaseFetcherForTests(async (repo) => {
|
||||
asked.push(repo);
|
||||
if (failing.has(repo)) throw new Error("GitHub's limit on requests without a sign-in was reached — it is tried again later");
|
||||
const tags: Record<string, [string, number]> = { "Sonarr/Sonarr": ["v4.0.10.2400", 90], "grafana/grafana": ["v11.2.0", 200], "AdguardTeam/AdGuardHome": ["v0.107.57", 10] };
|
||||
const t = tags[repo];
|
||||
if (!t) throw new Error("GitHub lists no releases for it");
|
||||
return { tag: t[0], publishedAt: published(t[1]), url: `https://github.com/${repo}/releases/tag/${t[0]}` };
|
||||
});
|
||||
});
|
||||
after(() => av.setReleaseFetcherForTests(null));
|
||||
|
||||
test("nothing is listed that has no release to compare with, no version read, or is switched off; before any lookup the newest is not known", async () => {
|
||||
const rows = await av.versionRows();
|
||||
assert.deepEqual(rows.map((r) => [r.label, r.name, r.running, r.latest, r.lag]), [["AdGuard Home", "DNS", "v0.107.52", null, "unknown"], ["Grafana", "Dashboards", "11.2.0", null, "unknown"], ["Sonarr", "TV", "4.0.9.2244", null, "unknown"], ["Sonarr", "TV 4K", "4.0.9.2244", null, "unknown"]]);
|
||||
});
|
||||
|
||||
test("a lookup asks once for each repository — two Sonarrs are one question — and keeps what it learns", async () => {
|
||||
asked.length = 0;
|
||||
const r = await av.refreshReleases(new Date(), true);
|
||||
assert.deepEqual([r.checked, r.failed], [3, 0]);
|
||||
assert.deepEqual([...asked].sort(), ["AdguardTeam/AdGuardHome", "Sonarr/Sonarr", "grafana/grafana"]);
|
||||
const rows = await av.versionRows();
|
||||
const by = (n: string) => rows.find((x) => x.name === n)!;
|
||||
assert.deepEqual([by("TV").lag, by("TV").latest?.tag, by("TV").daysOut], ["patch", "v4.0.10.2400", 90]);
|
||||
assert.deepEqual([by("Dashboards").lag, by("Dashboards").daysOut], ["same", null]);
|
||||
assert.deepEqual([by("DNS").lag, by("DNS").daysOut], ["patch", 10], "0.107.52 against 0.107.57");
|
||||
});
|
||||
|
||||
test("a repository looked up lately is not asked again; with force it is", async () => {
|
||||
asked.length = 0;
|
||||
await av.refreshReleases(new Date());
|
||||
assert.deepEqual(asked, []);
|
||||
await av.refreshReleases(new Date(), true);
|
||||
assert.equal(asked.length, 3);
|
||||
});
|
||||
|
||||
test("a lookup that fails keeps the release it had and says why; a later pass tries it again at once", async () => {
|
||||
failing = new Set(["grafana/grafana"]);
|
||||
const r = await av.refreshReleases(new Date(), true);
|
||||
assert.deepEqual([r.checked, r.failed], [2, 1]);
|
||||
const g = (await av.versionRows()).find((x) => x.name === "Dashboards")!;
|
||||
assert.deepEqual([g.latest?.tag, g.lookupError], ["v11.2.0", "GitHub's limit on requests without a sign-in was reached — it is tried again later"]);
|
||||
failing = new Set();
|
||||
asked.length = 0;
|
||||
await av.refreshReleases(new Date());
|
||||
assert.deepEqual(asked, ["grafana/grafana"], "only the one that had failed");
|
||||
assert.equal((await av.versionRows()).find((x) => x.name === "Dashboards")!.lookupError, null);
|
||||
});
|
||||
|
||||
test("the alert: a newer release out for the number of days or more; not before; never at 0", async () => {
|
||||
const rows = await av.versionRows();
|
||||
assert.deepEqual(av.versionProblems(rows, 0), []);
|
||||
const p = av.versionProblems(rows, 60);
|
||||
assert.deepEqual(p.map((x) => x.key), [`version-behind:${ids.sonarr}`, `version-behind:${ids.sonarr2}`]);
|
||||
assert.equal(p[0].message, "TV: Sonarr 4.0.9.2244 is running; v4.0.10.2400 came out 90 days ago (a patch)");
|
||||
assert.deepEqual(av.versionProblems(rows, 91), []);
|
||||
assert.equal(av.versionProblems(rows, 5).length, 3, "the two Sonarrs and AdGuard Home (Grafana is up to date)");
|
||||
});
|
||||
|
||||
describe("the page, its switch, the health pass, the Alerts page and /metrics", () => {
|
||||
let app: Server;
|
||||
let base = "";
|
||||
const call = (sub: string, method: string, path: string, body?: unknown) => fetch(`${base}${path}`, { method, headers: { "content-type": "application/json", "x-sub": sub }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }, { oidcSub: "admin", name: "Ada", role: "admin" }]);
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: String(req.headers["x-sub"]) } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/versions", versionsRouter);
|
||||
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/versions`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("it is off by default; everyone signed in sees the list, only an administrator switches it, and a refresh while it is off is refused", async () => {
|
||||
const off = (await (await call("viewer", "GET", "")).json()) as { enabled: boolean; alertAfterDays: number; rows: unknown[] };
|
||||
assert.deepEqual([off.enabled, off.alertAfterDays, off.rows.length], [false, 0, 4]);
|
||||
assert.equal((await call("viewer", "PUT", "/settings", { enabled: true, alertAfterDays: 0 })).status, 403);
|
||||
assert.equal((await call("admin", "POST", "/refresh")).status, 400);
|
||||
assert.equal((await call("viewer", "POST", "/refresh")).status, 403);
|
||||
});
|
||||
|
||||
test("it must be given the whole of what it asks; switching on looks the releases up at once, and is in the audit log", async () => {
|
||||
assert.equal((await call("admin", "PUT", "/settings", { enabled: true })).status, 400);
|
||||
assert.equal((await call("admin", "PUT", "/settings", { enabled: true, alertAfterDays: 999 })).status, 400);
|
||||
asked.length = 0;
|
||||
const on = (await (await call("admin", "PUT", "/settings", { enabled: true, alertAfterDays: 60 })).json()) as { enabled: boolean; alertAfterDays: number; lastCheckedAt: string | null; rows: { name: string; lag: string }[] };
|
||||
assert.deepEqual([on.enabled, on.alertAfterDays, on.lastCheckedAt !== null], [true, 60, true]);
|
||||
assert.equal(on.rows.find((r) => r.name === "TV")!.lag, "patch");
|
||||
assert.equal(asked.length, 3, "looked up on switching on");
|
||||
assert.equal((await db.select().from(auditLog)).filter((e) => e.targetType === "versionChecks").length, 1);
|
||||
});
|
||||
|
||||
test("with the lookup and a number set, the health pass raises it, the Alerts page puts it under Updates with a link, and maintenance on the integration holds it", async () => {
|
||||
const { snapshot } = await collectSnapshot();
|
||||
assert.deepEqual(snapshot.versions?.map((p) => p.key), [`version-behind:${ids.sonarr}`, `version-behind:${ids.sonarr2}`]);
|
||||
const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true });
|
||||
assert.equal(list.find((x) => x.key === `version-behind:${ids.sonarr}`)!.source, "versions");
|
||||
assert.equal(subjectOfConditionKey(`version-behind:${ids.sonarr}`), `integration:${ids.sonarr}`);
|
||||
invalidateAlertsCache();
|
||||
const a = (await getAlerts(true)).alerts.find((x) => x.id.endsWith(`version-behind:${ids.sonarr}`))!;
|
||||
assert.deepEqual([a.category, a.source, a.link, a.target?.type, a.kind], ["updates", "Versions", "/versions", "integration", "version-behind"]);
|
||||
});
|
||||
|
||||
test("/metrics says which have a newer release — only with the lookup on — and with a number of 0 no alert is raised", async () => {
|
||||
const text = await buildMetrics(new Date());
|
||||
assert.match(text, new RegExp(`homelab_app_update_available\\{app="sonarr",name="TV",running="4\\.0\\.9\\.2244",latest="v4\\.0\\.10\\.2400"\\} 1`));
|
||||
assert.match(text, /homelab_app_update_available\{app="grafana",name="Dashboards",running="11\.2\.0",latest="v11\.2\.0"\} 0/);
|
||||
const checks = (await getSettings()).versionChecks;
|
||||
await updateSettings({ versionChecks: { ...checks, alertAfterDays: 0 } });
|
||||
assert.equal((await collectSnapshot()).snapshot.versions, undefined);
|
||||
await updateSettings({ versionChecks: { ...checks, enabled: false } });
|
||||
assert.doesNotMatch(await buildMetrics(new Date()), /homelab_app_update_available/);
|
||||
assert.equal((await collectSnapshot()).snapshot.versions, undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -128,6 +128,26 @@ describe("what the *arr API says", () => {
|
||||
assert.ok(!seen.some((s) => s.path.includes("/queue")));
|
||||
});
|
||||
|
||||
test("Lidarr and Readarr are under v1, ask for their own unknown items, and have a queue and the missing like Sonarr (and no indexers)", async () => {
|
||||
for (const [kind, include] of [["lidarr", "includeUnknownArtistItems=true"], ["readarr", "includeUnknownAuthorItems=true"]] as const) {
|
||||
seen.length = 0;
|
||||
const r = await adapter.createArrAdapter(kind, { url, apiKey: "k" }).read();
|
||||
assert.deepEqual([r.queue?.total, r.missing, r.indexers], [1, 7, null], kind);
|
||||
assert.ok(seen.every((s) => s.path.startsWith("/api/v1/") && s.method === "GET"), kind);
|
||||
assert.ok(seen.some((s) => s.path.includes(include)), kind);
|
||||
assert.ok(!seen.some((s) => s.path.includes("/indexer")), kind);
|
||||
}
|
||||
assert.deepEqual(adapter.ARR_KINDS, ["sonarr", "radarr", "prowlarr", "lidarr", "readarr"]);
|
||||
});
|
||||
|
||||
test("an address that is another one of the family is said, for the new kinds too", async () => {
|
||||
appName = "Sonarr";
|
||||
assert.match((await adapter.createArrAdapter("lidarr", { url, apiKey: "k" }).ping()).error ?? "", /That is Sonarr, not Lidarr/);
|
||||
appName = "Lidarr";
|
||||
assert.equal((await adapter.createArrAdapter("lidarr", { url, apiKey: "k" }).ping()).ok, true);
|
||||
appName = "Sonarr";
|
||||
});
|
||||
|
||||
test("a part that cannot be read is null, not empty; the status and the health are what must work", async () => {
|
||||
queueStatus = 500;
|
||||
const r = await adapter.createArrAdapter("sonarr", { url, apiKey: "k" }).read();
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* The integration inspector: what an answer looks like (names and kinds, never values), where what a reader made of it is empty, the requests written down while a read runs, and the route that
|
||||
* runs one for an administrator.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
*/
|
||||
import { test, describe, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer as createHttpServer, type Server } from "node:http";
|
||||
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-inspect-"));
|
||||
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, integrations, auditLog } = await import("../src/db/schema.js");
|
||||
const { runMigrations } = await import("../src/db/migrate.js");
|
||||
const insp = await import("../src/integrations/inspector.js");
|
||||
const { requestJson } = await import("../src/integrations/httpJson.js");
|
||||
const { integrationsRouter } = await import("../src/routes/integrations.js");
|
||||
const { eq } = await import("drizzle-orm");
|
||||
|
||||
await runMigrations();
|
||||
|
||||
describe("what an answer looks like", () => {
|
||||
test("keys and the kinds of what they hold — and never a value", () => {
|
||||
const s = insp.shapeOf({ id: 7, name: "super-secret-name", ok: true, nothing: null, tags: ["a", "b"], nested: { when: "2026-01-01", n: 1.5 } });
|
||||
assert.equal(s, "{ id: number, name: string, ok: boolean, nothing: null, tags: array(2) of string, nested: { when: string, n: number } }");
|
||||
assert.doesNotMatch(s, /super-secret|2026|7\b/);
|
||||
});
|
||||
|
||||
test("a list of objects shows the keys of them all, and marks a key that only some have", () => {
|
||||
const s = insp.shapeOf([{ id: 1, name: "a" }, { id: 2, name: "b", extra: 1 }, { id: 3, name: "c" }]);
|
||||
assert.equal(s, "array(3) of { id: number, name: string, extra?: number }");
|
||||
assert.equal(insp.shapeOf([]), "array(0)");
|
||||
assert.equal(insp.shapeOf([1, "x", null]), "array(3) of number | string | null");
|
||||
assert.equal(insp.shapeOf("text"), "string");
|
||||
});
|
||||
|
||||
test("it goes three levels down and no further, caps the keys and the length", () => {
|
||||
assert.equal(insp.shapeOf({ a: { b: { c: { d: { e: 1 } } } } }), "{ a: { b: { c: object } } }");
|
||||
const many = Object.fromEntries(Array.from({ length: 40 }, (_, i) => [`k${i}`, i]));
|
||||
assert.match(insp.shapeOf(many), /, … \}$/);
|
||||
assert.ok(insp.shapeOf(Object.fromEntries(Array.from({ length: 30 }, (_, i) => [`key${i}_${"x".repeat(80)}`, "v"]))).length <= 1810);
|
||||
});
|
||||
|
||||
test("a path is reported with the names of its query, not what they were set to", () => {
|
||||
assert.equal(insp.pathForReport(new URL("http://x/api/v3/queue?page=1&pageSize=100&apikey=SECRET")), "/api/v3/queue?page&pageSize&apikey");
|
||||
assert.equal(insp.pathForReport(new URL("http://x/api/health")), "/api/health");
|
||||
});
|
||||
});
|
||||
|
||||
describe("where it is empty", () => {
|
||||
test("null, empty text, empty lists — and a field null in every item of a list", () => {
|
||||
const e = insp.emptyFields({ version: null, name: "", queue: { total: 0, items: [] }, servers: [{ id: 1, usage: null, name: "a" }, { id: 2, usage: null, name: "b" }], fine: "yes" });
|
||||
assert.deepEqual(e.map((x) => [x.path, x.what]), [["version", "null"], ["name", "text"], ["queue.items", "list"], ["servers[].usage", "every-item"]]);
|
||||
});
|
||||
|
||||
test("a field that is null in some items only is not reported; numbers such as 0 are not empty", () => {
|
||||
assert.deepEqual(insp.emptyFields({ servers: [{ usage: null }, { usage: 5 }], count: 0, ok: false }), []);
|
||||
});
|
||||
|
||||
test("a result that is itself empty, and a limit", () => {
|
||||
assert.deepEqual(insp.emptyFields(null), [{ path: "(the result)", what: "null" }]);
|
||||
assert.equal(insp.emptyFields(Object.fromEntries(Array.from({ length: 80 }, (_, i) => [`f${i}`, null])), 10).length, 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the requests written down while a read runs", () => {
|
||||
let http: Server;
|
||||
let url = "";
|
||||
before(async () => {
|
||||
http = createHttpServer((req, res) => {
|
||||
res.setHeader("content-type", "application/json");
|
||||
if (req.url?.startsWith("/json")) return void res.end(JSON.stringify({ items: [{ id: 1, secret: "hidden-value" }], total: 1 }));
|
||||
if (req.url?.startsWith("/html")) return void res.end("<html>nope</html>");
|
||||
res.statusCode = 404;
|
||||
res.end("{}");
|
||||
});
|
||||
await new Promise<void>((r) => http.listen(0, "127.0.0.1", r));
|
||||
url = `http://127.0.0.1:${(http.address() as AddressInfo).port}`;
|
||||
});
|
||||
after(() => http.close());
|
||||
|
||||
test("each request: method, path (no values), status, time, size and the shape of the answer; a failed one too", async () => {
|
||||
const { calls, value, error } = await insp.inspectCalls(async () => {
|
||||
await requestJson(`${url}/json?token=hidden&page=2`);
|
||||
await requestJson(`${url}/html`);
|
||||
await requestJson(`${url}/missing`);
|
||||
await requestJson("http://127.0.0.1:1/refused").catch(() => undefined);
|
||||
return "done";
|
||||
});
|
||||
assert.deepEqual([value, error], ["done", null]);
|
||||
assert.deepEqual(calls.map((c) => [c.method, c.path, c.status]), [["GET", "/json?token&page", 200], ["GET", "/html", 200], ["GET", "/missing", 404], ["GET", "/refused", 0]]);
|
||||
assert.equal(calls[0].shape, "{ items: array(1) of { id: number, secret: string }, total: number }");
|
||||
assert.equal(calls[1].shape, null, "not JSON");
|
||||
assert.ok(calls[0].bytes > 10 && calls[0].ms >= 0);
|
||||
assert.ok(calls[3].error);
|
||||
assert.doesNotMatch(JSON.stringify(calls), /hidden-value|hidden/);
|
||||
});
|
||||
|
||||
test("a read that throws is reported with what it asked before it did; and nothing is written down outside an inspection", async () => {
|
||||
const { calls, value, error } = await insp.inspectCalls(async () => {
|
||||
await requestJson(`${url}/json`);
|
||||
throw new Error("the reader gave up");
|
||||
});
|
||||
assert.deepEqual([calls.length, value, error], [1, undefined, "the reader gave up"]);
|
||||
assert.equal(insp.isInspecting(), false);
|
||||
await requestJson(`${url}/json`);
|
||||
const second = await insp.inspectCalls(async () => 1);
|
||||
assert.deepEqual(second.calls, [], "the earlier request was not kept");
|
||||
});
|
||||
|
||||
test("two inspections at once keep their own requests", async () => {
|
||||
const [a, b] = await Promise.all([insp.inspectCalls(async () => requestJson(`${url}/json?a=1`)), insp.inspectCalls(async () => requestJson(`${url}/html?b=1`))]);
|
||||
assert.deepEqual([a.calls.map((c) => c.path), b.calls.map((c) => c.path)], [["/json?a"], ["/html?b"]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the route", () => {
|
||||
let grafana: Server;
|
||||
let app: Server;
|
||||
let base = "";
|
||||
let grafanaId = 0;
|
||||
let tailscaleId = 0;
|
||||
let offId = 0;
|
||||
const call = (sub: string, id: number) => fetch(`${base}/${id}/inspect`, { method: "POST", headers: { "content-type": "application/json", "x-sub": sub } });
|
||||
|
||||
before(async () => {
|
||||
await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }, { oidcSub: "admin", name: "Ada", role: "admin" }]);
|
||||
grafana = createHttpServer((req, res) => {
|
||||
res.setHeader("content-type", "application/json");
|
||||
const u = new URL(req.url ?? "", "http://x");
|
||||
if (u.pathname === "/api/health") return void res.end(JSON.stringify({ database: "ok", version: "11.2.0" }));
|
||||
if (u.pathname === "/api/prometheus/grafana/api/v1/rules") return void res.end(JSON.stringify({ status: "success", data: { groups: [{ name: "g", file: "f", rules: [{ name: "r", state: "inactive", health: "ok" }] }] } }));
|
||||
if (u.pathname === "/api/datasources") return void res.end(JSON.stringify([]));
|
||||
res.statusCode = 404;
|
||||
res.end("{}");
|
||||
});
|
||||
await new Promise<void>((r) => grafana.listen(0, "127.0.0.1", r));
|
||||
const gurl = `http://127.0.0.1:${(grafana.address() as AddressInfo).port}`;
|
||||
grafanaId = (await db.insert(integrations).values({ type: "grafana", name: "Dashboards", baseUrl: gurl, config: JSON.stringify({ url: gurl, token: "glsa_x" }) }).returning({ id: integrations.id }))[0].id;
|
||||
tailscaleId = (await db.insert(integrations).values({ type: "tailscale", name: "Tailnet", baseUrl: "http://x", config: JSON.stringify({}) }).returning({ id: integrations.id }))[0].id;
|
||||
offId = (await db.insert(integrations).values({ type: "grafana", name: "Off", baseUrl: gurl, enabled: false, config: JSON.stringify({ url: gurl, token: "t" }) }).returning({ id: integrations.id }))[0].id;
|
||||
const a = express();
|
||||
a.use(express.json());
|
||||
a.use((req, _res, next) => {
|
||||
(req as unknown as { session: unknown }).session = { user: { sub: String(req.headers["x-sub"]) } };
|
||||
next();
|
||||
});
|
||||
a.use("/api/integrations", integrationsRouter);
|
||||
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/integrations`;
|
||||
});
|
||||
after(() => {
|
||||
app.close();
|
||||
grafana.close();
|
||||
try {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Windows keeps the database file locked until the process exits.
|
||||
}
|
||||
});
|
||||
|
||||
test("an administrator gets what it asked, what came back (as shapes) and where the result is empty; the token is nowhere in it", async () => {
|
||||
const res = await call("admin", grafanaId);
|
||||
assert.equal(res.status, 200);
|
||||
const text = await res.text();
|
||||
assert.doesNotMatch(text, /glsa_x/);
|
||||
const body = JSON.parse(text) as { ok: boolean; type: string; calls: { path: string; status: number; shape: string | null }[]; resultShape: string; empty: { path: string; what: string }[] };
|
||||
assert.deepEqual([body.ok, body.type], [true, "grafana"]);
|
||||
assert.ok(body.calls.some((c) => c.path === "/api/health" && c.status === 200 && c.shape === "{ database: string, version: string }"));
|
||||
assert.ok(body.calls.some((c) => c.path === "/api/prometheus/grafana/api/v1/rules"));
|
||||
assert.match(body.resultShape, /^\{ version: string, rules: \{ total: number, items: array\(0\) \}, datasources: array\(0\) \}$/);
|
||||
assert.ok(body.empty.some((e) => e.path === "rules.items" && e.what === "list"));
|
||||
assert.ok(body.empty.some((e) => e.path === "datasources" && e.what === "list"));
|
||||
});
|
||||
|
||||
test("a failed read is reported with what it asked, not as an error of the route", async () => {
|
||||
await db.update(integrations).set({ config: JSON.stringify({ url: "http://127.0.0.1:1", token: "t" }) }).where(eq(integrations.id, grafanaId));
|
||||
const body = (await (await call("admin", grafanaId)).json()) as { ok: boolean; error: string; calls: { status: number; error?: string }[]; resultShape: string | null };
|
||||
assert.deepEqual([body.ok, body.resultShape, body.calls.some((c) => c.status === 0)], [false, null, true]);
|
||||
assert.ok(body.error);
|
||||
});
|
||||
|
||||
test("only an administrator; a kind with no reader, a switched-off integration and an unknown one are refused; each inspection is in the audit log", async () => {
|
||||
assert.equal((await call("viewer", grafanaId)).status, 403);
|
||||
assert.equal((await call("admin", tailscaleId)).status, 400);
|
||||
assert.equal(((await (await call("admin", tailscaleId)).json()) as { error: string }).error, "not_inspectable");
|
||||
assert.equal(((await (await call("admin", offId)).json()) as { error: string }).error, "integration_disabled");
|
||||
assert.equal((await call("admin", 99999)).status, 404);
|
||||
const entries = (await db.select().from(auditLog)).filter((e) => e.action === "inspect");
|
||||
assert.equal(entries.length, 2, "the two that ran");
|
||||
assert.deepEqual(entries.map((e) => [e.category, e.targetType]), [["integration", "integration"], ["integration", "integration"]]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user