/** * Keeps what Dockhand's vulnerability scans say up to date: every fifteen minutes each Dockhand integration is asked, for each of its Docker hosts, how many findings its stored scans * add up to (and the critical ones, with the containers that use the image), and that is stored. Pages and alerts are built from what is stored, so none of them talks to Dockhand. * * Nothing here starts a scan: Dockhand scans when it is told to (by hand, after a pull, or for a whole host), and this only reads what it kept. So an image that was never scanned is * not known to be clean — the page says how many of a host's images have a scan — and an environment whose summary could not be read is *unknown*, never *none*. * * What is raised is **critical** findings only (a host with one or more is a warning), and only while the setting for it is on (Settings → Notifications). Dockhand can also notify * about vulnerabilities itself; if both are switched on you hear twice. */ import { and, eq, notInArray } from "drizzle-orm"; import { db } from "../db/client.js"; import { dockhandVulnerabilities, integrations } from "../db/schema.js"; import { createDockhandAdapter, type DockhandEnvironmentImages, type DockhandEnvironmentVulns, type DockhandRunningImage, type DockhandVulnFinding } from "../integrations/dockhand/adapter.js"; import { loadIntegrationConfig } from "../integrations/loadIntegration.js"; import { getInternalFlag, setInternalFlag } from "./settingsStore.js"; export const SYNC_INTERVAL_MS = 15 * 60_000; /** After this long without a good read, what is stored is no longer believed. */ export const STALE_AFTER_MS = 45 * 60_000; const STATUS_FLAG = "dockerVulnSync"; export interface DockerVulnSyncStatus { /** The last read that worked (kept when a later one failed). */ at: string; ok: boolean; error: string | null; } async function readStatuses(): Promise> { try { const raw = await getInternalFlag(STATUS_FLAG); return raw ? (JSON.parse(raw) as Record) : {}; } catch { return {}; } } export async function dockerVulnSyncStatuses(): Promise> { return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v])); } export const isDockerVulnFresh = (status: DockerVulnSyncStatus | undefined, nowMs: number) => !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS; export type StateRow = typeof dockhandVulnerabilities.$inferSelect; export const findingsOf = (row: Pick): DockhandVulnFinding[] => { try { const v = JSON.parse(row.findings); return Array.isArray(v) ? (v as DockhandVulnFinding[]) : []; } catch { return []; } }; export const imagesOf = (row: Pick): DockhandRunningImage[] | null => { if (row.images === null) return null; try { const v = JSON.parse(row.images); return Array.isArray(v) ? (v as DockhandRunningImage[]) : null; } catch { return null; } }; /** Writes down which images each host's running containers use and when they were built — onto the host's row (a host whose images could not be read keeps what it had). */ export async function storeImages(integrationId: number, environments: DockhandEnvironmentImages[], now: Date = new Date()): Promise { for (const e of environments) { if (e.images === null) continue; await db .update(dockhandVulnerabilities) .set({ images: JSON.stringify(e.images), imagesAt: now.toISOString() }) .where(and(eq(dockhandVulnerabilities.integrationId, integrationId), eq(dockhandVulnerabilities.environmentId, e.environmentId))); } } /** Writes down one read, replacing the last one for this integration (a host Dockhand no longer lists is dropped). */ export async function storeRead(integrationId: number, environments: DockhandEnvironmentVulns[], now: Date = new Date()): Promise { for (const e of environments) { const values = { environmentName: e.environmentName, critical: e.summary?.critical ?? null, high: e.summary?.high ?? null, medium: e.summary?.medium ?? null, low: e.summary?.low ?? null, imagesScanned: e.summary?.imagesScanned ?? null, totalImages: e.summary?.totalImages ?? null, findings: JSON.stringify(e.critical), updatedAt: now.toISOString(), }; await db.insert(dockhandVulnerabilities).values({ integrationId, environmentId: e.environmentId, ...values }).onConflictDoUpdate({ target: [dockhandVulnerabilities.integrationId, dockhandVulnerabilities.environmentId], set: values }); } const ids = environments.map((e) => e.environmentId); await db.delete(dockhandVulnerabilities).where(ids.length > 0 ? and(eq(dockhandVulnerabilities.integrationId, integrationId), notInArray(dockhandVulnerabilities.environmentId, ids)) : eq(dockhandVulnerabilities.integrationId, integrationId)); } /** Reads one Dockhand's scans and stores them. Never throws: how it went is the answer, and what was stored is left alone when it failed. */ export async function syncDockerVulns( integrationId: number, now: Date = new Date(), read?: (config: Record) => Promise<{ environments: DockhandEnvironmentVulns[] }>, readImages?: (config: Record) => Promise<{ environments: DockhandEnvironmentImages[] }>, ): Promise { let status: DockerVulnSyncStatus; try { const loaded = await loadIntegrationConfig(integrationId); if (!loaded || loaded.integration.type !== "dockhand") throw new Error("not a Dockhand integration"); const result = await (read ? read(loaded.config) : createDockhandAdapter(loaded.config as never).readVulnerabilities()); await storeRead(integrationId, result.environments, now); // The images the containers use (for how old they are) are read beside the scans; a failure here costs only that, not the scans. try { await storeImages(integrationId, (await (readImages ? readImages(loaded.config) : createDockhandAdapter(loaded.config as never).readImages())).environments, now); } catch (err) { console.error(`[docker-vulns] couldn't read the images of integration ${integrationId}:`, err instanceof Error ? err.message : err); } status = { at: now.toISOString(), ok: true, error: null }; } catch (err) { const previous = (await readStatuses())[String(integrationId)]; status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err) }; console.error(`[docker-vulns] couldn't read integration ${integrationId}:`, status.error); } try { const all = await readStatuses(); all[String(integrationId)] = status; await setInternalFlag(STATUS_FLAG, JSON.stringify(all)); } catch (err) { console.error("[docker-vulns] couldn't record the result:", err); } return status; } export async function syncAllDockerVulns(now: Date = new Date()): Promise> { const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "dockhand"), eq(integrations.enabled, true))); const out: Record = {}; for (const r of rows) out[r.id] = await syncDockerVulns(r.id, now); return out; } export async function forgetDockerVulns(integrationId: number): Promise { const all = await readStatuses(); if (String(integrationId) in all) { delete all[String(integrationId)]; await setInternalFlag(STATUS_FLAG, JSON.stringify(all)); } } // ─── What is wrong ────────────────────────────────────────────────────────── export interface DockerVulnProblem { key: string; message: string; severity: "warning"; } /** * Pure: a warning for each Docker host whose stored scans have critical findings — and, when `highThreshold` is above 0, one for each whose scans have at least that many *high* ones. A host * whose summary is unknown says nothing. */ export function dockerVulnProblems(integrationId: number, rows: Pick[], highThreshold = 0): DockerVulnProblem[] { const out: DockerVulnProblem[] = []; for (const r of [...rows].sort((a, b) => a.environmentName.localeCompare(b.environmentName))) { if (highThreshold > 0 && r.high !== null && r.high >= highThreshold) { out.push({ key: `docker-vuln-high:${integrationId}:${r.environmentId}`, severity: "warning", message: `${r.environmentName}: ${r.high} high ${r.high === 1 ? "vulnerability" : "vulnerabilities"} in the scanned images (you asked to hear of ${highThreshold} or more)`, }); } if (r.critical === null || r.critical <= 0) continue; const findings = findingsOf(r); const images = [...new Set(findings.map((f) => f.imageName))]; const fixable = findings.filter((f) => f.fixedVersion).length; const where = images.length === 0 ? "" : ` in ${images.length === 1 ? "1 image" : `${images.length}${findings.length >= r.critical ? "" : "+"} images`}: ${images.slice(0, 3).join(", ")}${images.length > 3 ? ` and ${images.length - 3} more` : ""}`; out.push({ key: `docker-vuln:${integrationId}:${r.environmentId}`, severity: "warning", message: `${r.environmentName}: ${r.critical} critical ${r.critical === 1 ? "vulnerability" : "vulnerabilities"}${where}${findings.length > 0 ? ` (${fixable} of the first ${findings.length} ${fixable === 1 ? "has" : "have"} a fix)` : ""}`, }); } return out; } export interface DockerImageProblem { key: string; message: string; severity: "warning"; } const DAY = 86_400_000; export const imageAgeDays = (created: string, nowMs: number): number => Math.floor((nowMs - Date.parse(created)) / DAY); /** Pure: the running images built more than `thresholdDays` ago, oldest first (an image whose build date is not known is not judged). */ export function oldImages(images: DockhandRunningImage[] | null, thresholdDays: number, nowMs: number): { image: string; ageDays: number; containers: string[] }[] { if (images === null || thresholdDays <= 0) return []; return images .filter((i): i is DockhandRunningImage & { created: string } => i.created !== null) .map((i) => ({ image: i.image, ageDays: imageAgeDays(i.created, nowMs), containers: i.containers })) .filter((i) => i.ageDays > thresholdDays) .sort((a, b) => b.ageDays - a.ageDays || a.image.localeCompare(b.image)); } /** * Pure: a warning for each Docker host that has running images built more than `thresholdDays` ago — however the scans of them came out (an image that was never scanned, or scanned clean, is just as * stale). 0 turns it off. Judged from the stored list; a list older than STALE_AFTER_MS or never read says nothing (the caller holds it). */ export function dockerImageAgeProblems(integrationId: number, rows: Pick[], thresholdDays: number, nowMs: number): DockerImageProblem[] { const out: DockerImageProblem[] = []; if (thresholdDays <= 0) return out; for (const r of [...rows].sort((a, b) => a.environmentName.localeCompare(b.environmentName))) { const old = oldImages(imagesOf(r), thresholdDays, nowMs); if (old.length === 0) continue; const named = old.slice(0, 3).map((i) => `${i.image} (${i.ageDays} days)`); out.push({ key: `docker-image-age:${integrationId}:${r.environmentId}`, severity: "warning", message: `${r.environmentName}: ${old.length} running ${old.length === 1 ? "image was" : "images were"} built more than ${thresholdDays} days ago — ${named.join(", ")}${old.length > 3 ? ` and ${old.length - 3} more` : ""}`, }); } return out; } let timer: ReturnType | null = null; export function initDockerVulnSync(): void { if (timer) return; const pass = () => void syncAllDockerVulns().catch((err) => console.error("[docker-vulns] pass failed:", err)); timer = setInterval(pass, SYNC_INTERVAL_MS); setTimeout(pass, 27_000); console.log(`[docker-vulns] reading Dockhand's vulnerability scans every ${SYNC_INTERVAL_MS / 60000} minutes`); }