/** * How old the images are that Docker hosts' running containers use (Dockhand's image list and container list), kept beside the vulnerability scans, and the optional warning for a host whose * running images were built long ago — whatever the scans of them said. * * 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-imageage-")); 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, dockhandVulnerabilities } = await import("../src/db/schema.js"); const { runMigrations } = await import("../src/db/migrate.js"); const adapter = await import("../src/integrations/dockhand/adapter.js"); const dv = await import("../src/services/dockerVulnSync.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 { integrationsRouter } = await import("../src/routes/integrations.js"); const { getSettings, updateSettings, setInternalFlag } = await import("../src/services/settingsStore.js"); const { eq } = await import("drizzle-orm"); await runMigrations(); const DAY = 86_400_000; const NOW = Date.parse("2026-06-10T12:00:00Z"); const secs = (daysAgo: number) => Math.floor((Date.now() - daysAgo * DAY) / 1000); describe("what Dockhand says of images", () => { test("Created is seconds since 1970 (Docker's list), milliseconds or a date; Docker's never (0) and anything else is not a date", () => { assert.equal(adapter.parseImageCreated(1719830400), "2024-07-01T10:40:00.000Z"); assert.equal(adapter.parseImageCreated(1719830400000), "2024-07-01T10:40:00.000Z"); assert.equal(adapter.parseImageCreated("2024-07-01T10:40:00Z"), "2024-07-01T10:40:00.000Z"); for (const bad of [0, -5, null, "", "yesterday", {}, NaN]) assert.equal(adapter.parseImageCreated(bad), null, String(bad)); }); test("the image list: id, tags and when built; one with no id is skipped, and an untagged one has no tags", () => { const list = adapter.parseImageList([{ Id: "sha256:aaa", RepoTags: ["nginx:1.25", "nginx:latest"], Created: 1719830400 }, { Id: "sha256:bbb", RepoTags: [":"], Created: 1719830400 }, { RepoTags: ["x:1"] }, "junk"]); assert.deepEqual(list.map((i) => [i.id, i.tags, i.created]), [["sha256:aaa", ["nginx:1.25", "nginx:latest"], "2024-07-01T10:40:00.000Z"], ["sha256:bbb", [], "2024-07-01T10:40:00.000Z"]]); assert.deepEqual(adapter.parseImageList(null), []); }); test("the running containers' images are matched to the list by tag — Docker Hub's prefixes and a missing tag are not a difference — or by id; stopped containers' images do not count", () => { const images = [ { id: "sha256:aaa111", tags: ["nginx:1.25"], created: "2025-01-01T00:00:00.000Z" }, { id: "sha256:bbb222", tags: ["redis:latest"], created: "2024-01-01T00:00:00.000Z" }, { id: "sha256:ccc333", tags: ["ghcr.io/org/app:2.0"], created: "2026-05-01T00:00:00.000Z" }, { id: "sha256:ddd444abcdef", tags: [], created: "2023-03-03T00:00:00.000Z" }, ]; const run = adapter.matchRunningImages( [ { name: "web", image: "nginx:1.25", state: "running" }, { name: "web2", image: "docker.io/library/nginx:1.25", state: "running" }, { name: "cache", image: "redis", state: "running" }, { name: "app", image: "ghcr.io/org/app:2.0", state: "running" }, { name: "pinned", image: "sha256:ddd444abcdef", state: "running" }, { name: "unknown", image: "private/thing:9", state: "running" }, { name: "off", image: "postgres:15", state: "exited" }, ], images, ); assert.deepEqual(run.map((r) => [r.image, r.created, r.containers]), [ ["docker.io/library/nginx:1.25", "2025-01-01T00:00:00.000Z", ["web2"]], ["ghcr.io/org/app:2.0", "2026-05-01T00:00:00.000Z", ["app"]], ["nginx:1.25", "2025-01-01T00:00:00.000Z", ["web"]], ["private/thing:9", null, ["unknown"]], ["redis", "2024-01-01T00:00:00.000Z", ["cache"]], ["sha256:ddd444abcdef", "2023-03-03T00:00:00.000Z", ["pinned"]], ]); }); test("two containers of one image are one entry", () => { const run = adapter.matchRunningImages([{ name: "a", image: "x:1", state: "running" }, { name: "b", image: "x:1", state: "running" }], [{ id: "sha256:1", tags: ["x:1"], created: null }]); assert.deepEqual(run.map((r) => [r.image, r.containers]), [["x:1", ["a", "b"]]]); }); }); describe("the fake Dockhand", () => { let dockhand: Server; let url = ""; const seen: string[] = []; let imagesStatus: Record = {}; before(async () => { dockhand = createHttpServer((req, res) => { const u = new URL(req.url ?? "", "http://x"); seen.push(`${req.method} ${u.pathname}${u.search}`); res.setHeader("content-type", "application/json"); const env = u.searchParams.get("env") ?? ""; if (u.pathname === "/api/environments") return void res.end(JSON.stringify([{ id: 1, name: "Jackie" }, { id: 2, name: "Anna" }])); if (u.pathname === "/api/images") { if (imagesStatus[env]) { res.statusCode = imagesStatus[env]; return void res.end("{}"); } return void res.end(JSON.stringify(env === "1" ? [{ Id: "sha256:a1", RepoTags: ["nginx:1.19"], Created: secs(500) }, { Id: "sha256:a2", RepoTags: ["redis:7"], Created: secs(30) }, { Id: "sha256:a3", RepoTags: ["mariadb:10"], Created: secs(700) }] : [{ Id: "sha256:b1", RepoTags: ["caddy:2"], Created: secs(10) }])); } if (u.pathname === "/api/containers") { return void res.end(JSON.stringify(env === "1" ? [{ id: "1", name: "web", image: "nginx:1.19", state: "running" }, { id: "2", name: "cache", image: "redis:7", state: "running" }, { id: "3", name: "db", image: "mariadb:10", state: "exited" }] : [{ id: "4", name: "proxy", image: "caddy:2", state: "running" }])); } if (u.pathname === "/api/vulnerabilities/count") return void res.end(JSON.stringify({ total: 0, summary: { critical: 0, high: 0, medium: 0, low: 0, imagesScanned: 0, totalImages: 4 } })); res.statusCode = 404; res.end("{}"); }); await new Promise((r) => dockhand.listen(0, "127.0.0.1", r)); url = `http://127.0.0.1:${(dockhand.address() as AddressInfo).port}`; }); after(() => dockhand.close()); test("each host's images and containers are read, only with GET; a host whose list cannot be read is unknown (null), not empty", async () => { seen.length = 0; imagesStatus = { "2": 500 }; const read = await adapter.createDockhandAdapter({ url, token: "t" } as never).readImages(); assert.deepEqual(read.environments.map((e) => [e.environmentName, e.images?.map((i) => [i.image, i.containers])]), [["Jackie", [["nginx:1.19", ["web"]], ["redis:7", ["cache"]]]], ["Anna", undefined]]); assert.ok(seen.every((s) => s.startsWith("GET "))); imagesStatus = {}; }); describe("what is stored and what is wrong", () => { let id = 0; before(async () => { id = (await db.insert(integrations).values({ type: "dockhand", name: "Dockhand", baseUrl: url, config: JSON.stringify({ url, token: "t" }) }).returning({ id: integrations.id }))[0].id; }); const rows = () => db.select().from(dockhandVulnerabilities).where(eq(dockhandVulnerabilities.integrationId, id)); test("a sync stores each host's running images beside its scan counts; a failure to read the images does not fail the scans, and leaves what was stored", async () => { const status = await dv.syncDockerVulns(id, new Date()); assert.equal(status.ok, true); const stored = (await rows()).sort((a, b) => a.environmentId - b.environmentId); assert.deepEqual(dv.imagesOf(stored[0])?.map((i) => i.image), ["nginx:1.19", "redis:7"]); assert.ok(stored[0].imagesAt); const before = stored[0].imagesAt; const failed = await dv.syncDockerVulns(id, new Date(Date.now() + 1000), undefined, async () => { throw new Error("images are down"); }); assert.equal(failed.ok, true, "the scans were read"); assert.equal((await rows()).find((r) => r.environmentId === 1)!.imagesAt, before, "the images kept what they had"); await dv.syncDockerVulns(id, new Date(), undefined, async () => ({ environments: [{ environmentId: 1, environmentName: "Jackie", images: null }] })); assert.equal((await rows()).find((r) => r.environmentId === 1)!.imagesAt, before, "a host whose list was null is left alone"); }); test("only images built longer ago than the number are named, oldest first; an image with no build date is not judged; 0 is off", () => { const images = [ { image: "nginx:1.19", created: new Date(NOW - 500 * DAY).toISOString(), containers: ["web"] }, { image: "redis:7", created: new Date(NOW - 30 * DAY).toISOString(), containers: ["cache"] }, { image: "old:0", created: new Date(NOW - 900 * DAY).toISOString(), containers: ["x", "y"] }, { image: "mystery", created: null, containers: ["z"] }, ]; assert.deepEqual(dv.oldImages(images, 180, NOW).map((i) => [i.image, i.ageDays]), [["old:0", 900], ["nginx:1.19", 500]]); assert.deepEqual(dv.oldImages(images, 0, NOW), []); assert.deepEqual(dv.oldImages(null, 180, NOW), []); const p = dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "Jackie", images: JSON.stringify(images) }], 180, NOW); assert.deepEqual(p.map((x) => [x.key, x.severity]), [["docker-image-age:7:1", "warning"]]); assert.equal(p[0].message, "Jackie: 2 running images were built more than 180 days ago — old:0 (900 days), nginx:1.19 (500 days)"); assert.equal(dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "Jackie", images: JSON.stringify([images[0]]) }], 180, NOW)[0].message, "Jackie: 1 running image was built more than 180 days ago — nginx:1.19 (500 days)"); assert.deepEqual(dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "Jackie", images: JSON.stringify([images[1]]) }], 180, NOW), []); assert.deepEqual(dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "Jackie", images: null }], 180, NOW), []); assert.deepEqual(dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "Jackie", images: JSON.stringify(images) }], 0, NOW), []); const many = Array.from({ length: 5 }, (_, i) => ({ image: `i${i}:1`, created: new Date(NOW - (400 + i) * DAY).toISOString(), containers: [] })); assert.match(dv.dockerImageAgeProblems(7, [{ environmentId: 1, environmentName: "H", images: JSON.stringify(many) }], 180, NOW)[0].message, /— i4:1 \(404 days\), i3:1 \(403 days\), i2:1 \(402 days\) and 2 more$/); }); describe("the health pass, the Alerts page and the Docker page", () => { let app: Server; let base = ""; const fresh = () => setInternalFlag("dockerVulnSync", JSON.stringify({ [id]: { at: new Date().toISOString(), ok: true, error: null } })); const ageIds = async () => { invalidateAlertsCache(); return (await getAlerts(true)).alerts.map((x) => x.id).filter((i) => i.includes("docker-image-age:")); }; before(async () => { await db.insert(users).values([{ oidcSub: "viewer", name: "Vic", role: "viewer" }]); await dv.syncDockerVulns(id, new Date()); await fresh(); const a = express(); a.use(express.json()); a.use((req, _res, next) => { (req as unknown as { session: unknown }).session = { user: { sub: "viewer" } }; next(); }); a.use("/api/integrations", integrationsRouter); app = await new Promise((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/${id}/dockhand`; }); after(() => { app.close(); try { rmSync(dir, { recursive: true, force: true }); } catch { // Windows keeps the database file locked until the process exits. } }); test("off by default: nothing is raised and nothing is held (an old setting without the number is read as 0)", async () => { const checks = (await getSettings()).healthChecks; assert.equal(checks.imageAgeDays, 0); const { imageAgeDays: _dropped, ...older } = checks; await updateSettings({ healthChecks: older as never }); assert.equal((await getSettings()).healthChecks.imageAgeDays, 0); const { snapshot, held } = await collectSnapshot(); assert.deepEqual([snapshot.dockerImages, [...held].filter((h) => h.endsWith(":images"))], [undefined, []]); assert.deepEqual(await ageIds(), []); await updateSettings({ healthChecks: checks }); }); test("with a number the host's old running images are one warning; maintenance holds it; a number the images are younger than raises nothing", async () => { const checks = (await getSettings()).healthChecks; await updateSettings({ healthChecks: { ...checks, imageAgeDays: 180 } }); const { snapshot } = await collectSnapshot(); const list = evaluateHealth(snapshot, { serverOfflineMinutes: 60, diskUsagePercent: 90 }, Date.now(), { skipServers: true }); const c = list.find((x) => x.key === `docker-image-age:${id}:1`)!; assert.deepEqual([c.severity, c.source], ["warning", `dockhand:${id}:images`]); assert.match(c.message, /Jackie: 1 running image was built more than 180 days ago — nginx:1.19 \(\d+ days\)/); assert.equal(list.some((x) => x.key === `docker-image-age:${id}:2`), false, "Anna's only image is young"); assert.equal(subjectOfConditionKey(c.key), `integration:${id}`); assert.deepEqual(await ageIds(), [`services:docker-image-age:${id}:1`]); await updateSettings({ healthChecks: { ...checks, imageAgeDays: 3000 } }); assert.deepEqual(await ageIds(), []); await updateSettings({ healthChecks: checks }); }); test("a list that has not been read lately is held, not cleared", async () => { const checks = (await getSettings()).healthChecks; await updateSettings({ healthChecks: { ...checks, imageAgeDays: 180 } }); await db.update(dockhandVulnerabilities).set({ imagesAt: new Date(Date.now() - 2 * 60 * 60_000).toISOString() }).where(eq(dockhandVulnerabilities.integrationId, id)); const { snapshot, held } = await collectSnapshot(); assert.deepEqual([snapshot.dockerImages, held.has(`dockhand:${id}:images`)], [undefined, true]); await updateSettings({ healthChecks: checks }); await dv.syncDockerVulns(id, new Date()); }); test("the Docker page gets each host's oldest running images and how many are over the number", async () => { const checks = (await getSettings()).healthChecks; await updateSettings({ healthChecks: { ...checks, imageAgeDays: 180 } }); const body = (await (await fetch(`${base}/vulnerabilities`, { headers: { connection: "close" } })).json()) as { imageAgeDays: number; environments: { environmentName: string; images: { running: number; over: number | null; undated: number; oldest: { image: string; ageDays: number }[] } | null }[] }; assert.equal(body.imageAgeDays, 180); const jackie = body.environments.find((e) => e.environmentName === "Jackie")!; assert.deepEqual([jackie.images?.running, jackie.images?.over, jackie.images?.undated, jackie.images?.oldest.map((i) => i.image)], [2, 1, 0, ["nginx:1.19", "redis:7"]]); assert.ok((jackie.images?.oldest[0].ageDays ?? 0) >= 499); await updateSettings({ healthChecks: checks }); const off = (await (await fetch(`${base}/vulnerabilities`, { headers: { connection: "close" } })).json()) as { environments: { environmentName: string; images: { over: number | null } | null }[] }; assert.equal(off.environments.find((e) => e.environmentName === "Jackie")!.images?.over, null, "no number, no count over it"); }); }); }); });