AdGuard Home, Grafana, Docker image age, foldable server page cards
New integration kinds adguard (status, statistics, filter lists and a real DNS question over UDP; alerts for DNS down/not running, protection left off, stale lists) and grafana (alert rules firing or failing, data-source health). Optional warning for running Docker images built more than N days ago (off by default). A server's overview cards can be folded and arranged, kept per user. Migrations 0075-0077; docs, metrics and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d9a2310bb
commit
551eb1597a
53 files changed
+27700
-33
No files matched your search
@@ -11,7 +11,7 @@
|
||||
import { and, eq, notInArray } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { dockhandVulnerabilities, integrations } from "../db/schema.js";
|
||||
import { createDockhandAdapter, type DockhandEnvironmentVulns, type DockhandVulnFinding } from "../integrations/dockhand/adapter.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";
|
||||
|
||||
@@ -53,6 +53,27 @@ export const findingsOf = (row: Pick<StateRow, "findings">): DockhandVulnFinding
|
||||
}
|
||||
};
|
||||
|
||||
export const imagesOf = (row: Pick<StateRow, "images">): 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<void> {
|
||||
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<void> {
|
||||
for (const e of environments) {
|
||||
@@ -74,13 +95,24 @@ export async function storeRead(integrationId: number, environments: DockhandEnv
|
||||
}
|
||||
|
||||
/** 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<string, unknown>) => Promise<{ environments: DockhandEnvironmentVulns[] }>): Promise<DockerVulnSyncStatus> {
|
||||
export async function syncDockerVulns(
|
||||
integrationId: number,
|
||||
now: Date = new Date(),
|
||||
read?: (config: Record<string, unknown>) => Promise<{ environments: DockhandEnvironmentVulns[] }>,
|
||||
readImages?: (config: Record<string, unknown>) => Promise<{ environments: DockhandEnvironmentImages[] }>,
|
||||
): Promise<DockerVulnSyncStatus> {
|
||||
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)];
|
||||
@@ -148,6 +180,45 @@ export function dockerVulnProblems(integrationId: number, rows: Pick<StateRow, "
|
||||
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<StateRow, "environmentId" | "environmentName" | "images">[], 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<typeof setInterval> | null = null;
|
||||
|
||||
export function initDockerVulnSync(): void {
|
||||
|
||||
Reference in new issue
Block a user