import { Router } from "express"; import { eq } from "drizzle-orm"; import { z } from "zod"; import { db } from "../db/client.js"; import { consistencyIgnores, dnsProviders, dnsRecordsCache, dnsZonesCache, ipamEntries, servers } from "../db/schema.js"; import { requireAuth, requireRole } from "../auth/middleware.js"; import { recordAudit } from "../services/audit.js"; import { buildFindings, countHiddenAddresses, InvalidRangeError, MAX_EXCLUDED_RANGES, normalizeRange, type Finding, } from "../services/consistency.js"; import { getSettings, updateSettings } from "../services/settingsStore.js"; import { asyncHandler } from "../utils/asyncHandler.js"; export const consistencyRouter = Router(); consistencyRouter.use(requireAuth); function parseIps(stored: string | null): string[] { if (!stored) return []; try { const value = JSON.parse(stored); return Array.isArray(value) ? value.filter((v): v is string => typeof v === "string") : []; } catch { return []; } } async function computeFindings(): Promise<{ findings: Finding[]; sources: Record; excludedRanges: string[]; hiddenAddresses: number }> { const [serverRows, ipamRows, dnsRows, zoneRows] = await Promise.all([ db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ips: servers.ipAddresses }).from(servers), db.select({ id: ipamEntries.id, ip: ipamEntries.ipAddress, label: ipamEntries.label, source: ipamEntries.source }).from(ipamEntries), db .select({ name: dnsRecordsCache.name, type: dnsRecordsCache.type, content: dnsRecordsCache.content, providerName: dnsProviders.name }) .from(dnsRecordsCache) .innerJoin(dnsProviders, eq(dnsRecordsCache.providerId, dnsProviders.id)), db.select({ syncedAt: dnsZonesCache.syncedAt }).from(dnsZonesCache), ]); const serverInputs = serverRows.map((s) => ({ id: s.id, name: s.name, hostname: s.hostname, ips: parseIps(s.ips) })); const { consistency } = await getSettings(); const excludedRanges = consistency.excludedRanges; const findings = buildFindings({ servers: serverInputs, ipam: ipamRows, dns: dnsRows, excludedRanges }); const hiddenAddresses = countHiddenAddresses({ servers: serverInputs, ipam: ipamRows, dns: dnsRows }, excludedRanges); const synced = zoneRows.map((z) => z.syncedAt).filter((t): t is string => !!t).sort(); const sources = { servers: { total: serverInputs.length, withAddresses: serverInputs.filter((s) => s.ips.length > 0).length }, ipam: ipamRows.length, dns: { zones: zoneRows.length, syncedZones: synced.length, records: dnsRows.length, oldestSyncedAt: synced[0] ?? null, newestSyncedAt: synced[synced.length - 1] ?? null, }, }; return { findings, sources, excludedRanges, hiddenAddresses }; } consistencyRouter.get("/", asyncHandler(async (_req, res) => { const { findings, sources, excludedRanges, hiddenAddresses } = await computeFindings(); const ignores = await db.select().from(consistencyIgnores).orderBy(consistencyIgnores.createdAt); const ignoredKeys = new Set(ignores.map((i) => i.key)); const present = new Set(findings.map((f) => f.key)); const active = findings.filter((f) => !ignoredKeys.has(f.key)); const counts = { error: 0, warning: 0, info: 0 }; for (const f of active) counts[f.severity]++; res.json({ findings: active, counts, ignored: ignores.map((i) => ({ ...i, stillPresent: present.has(i.key) })), sources, excludedRanges, hiddenAddresses, generatedAt: new Date().toISOString(), }); })); const rangesSchema = z.object({ ranges: z.array(z.string().max(100)).max(200) }); // Managed here rather than in admin-only Settings: like ignoring a finding, it's a judgement about the network that // whoever is looking at the report is best placed to make. consistencyRouter.put("/excluded-ranges", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = rangesSchema.safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Ranges must be a list of text.", details: parsed.error.flatten() }); let ranges: string[]; try { ranges = [...new Set(parsed.data.ranges.filter((r) => r.trim()).map(normalizeRange))]; } catch (err) { if (err instanceof InvalidRangeError) return res.status(400).json({ error: "invalid_range", message: err.message }); throw err; } if (ranges.length > MAX_EXCLUDED_RANGES) { return res.status(400).json({ error: "too_many", message: `At most ${MAX_EXCLUDED_RANGES} ranges.` }); } const before = (await getSettings()).consistency.excludedRanges; await updateSettings({ consistency: { excludedRanges: ranges } }); await recordAudit({ actor: req.currentUser!, category: "consistency", action: "set_excluded_ranges", targetType: "consistency_settings", detail: { before, after: ranges }, }); res.json({ excludedRanges: ranges }); })); const ignoreSchema = z.object({ key: z.string().min(1).max(500), reason: z.string().trim().max(300).optional() }); // The key must belong to a finding that exists right now, and the stored title comes from that finding rather than the // request — so the ignore list can't be filled with arbitrary text. consistencyRouter.post("/ignore", requireRole("operator"), asyncHandler(async (req, res) => { const parsed = ignoreSchema.safeParse(req.body); if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Missing finding.", details: parsed.error.flatten() }); const { findings } = await computeFindings(); const finding = findings.find((f) => f.key === parsed.data.key); if (!finding) return res.status(404).json({ error: "not_found", message: "That finding no longer exists — refresh the report." }); const [existing] = await db.select({ id: consistencyIgnores.id }).from(consistencyIgnores).where(eq(consistencyIgnores.key, finding.key)).limit(1); if (existing) return res.status(409).json({ error: "already_ignored", message: "That finding is already ignored." }); const [row] = await db .insert(consistencyIgnores) .values({ key: finding.key, title: finding.title, reason: parsed.data.reason || null, createdBy: req.currentUser!.email ?? req.currentUser!.name ?? req.currentUser!.oidcSub }) .returning(); await recordAudit({ actor: req.currentUser!, category: "consistency", action: "ignore", targetType: "consistency_finding", targetId: row.id, detail: { key: finding.key, title: finding.title, reason: row.reason }, }); res.status(201).json({ ignore: row }); })); consistencyRouter.delete("/ignore/:id", requireRole("operator"), asyncHandler(async (req, res) => { const id = Number(req.params.id); if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" }); const deleted = await db.delete(consistencyIgnores).where(eq(consistencyIgnores.id, id)).returning(); if (deleted.length === 0) return res.status(404).json({ error: "not_found" }); await recordAudit({ actor: req.currentUser!, category: "consistency", action: "unignore", targetType: "consistency_finding", targetId: id, detail: { key: deleted[0].key, title: deleted[0].title }, }); res.status(204).end(); }));