Add a consistency report across IPAM, DNS and servers
New Consistency page listing where the three places this app records what lives at an address disagree: - Address conflicts: the same address reported by more than one server. - DNS out of date: a record named after a server (its hostname, or its short name) that points at an address the server doesn't report. - IPAM out of date: an entry labelled with a server's name at an address the server doesn't report. - Not in IPAM: addresses a server reports or DNS points at that IPAM doesn't list, merged into one finding per address, with a one-click "Add to IPAM" that pre-fills a label. - No DNS record: server LAN addresses no cached A/AAAA record resolves to. It compares data the app already holds and fetches nothing when opened, so the page states how many servers had reported addresses and how many DNS zones are synced (and how old the oldest sync is) -- DNS records are only cached for zones that have been synced, and a report that silently treated missing data as "no records" would mislead. Rules chosen to keep it from crying wolf: - Only private addresses are compared; public DNS records aren't expected to be in IPAM. - Servers with no reported addresses are never judged. - Agents report IPv4 only, so records are only compared within an address family (an AAAA record isn't "stale" for lacking an IPv6 address). - Docker bridge networks (172.16/12) are ignored: shared ones aren't conflicts, and they aren't listed unless someone put them in DNS. - Tailscale addresses don't need DNS records (MagicDNS), and IPAM entries kept current by the Tailscale/Proxmox syncs aren't second-guessed. Findings anyone has decided are fine can be ignored (operators) with a reason. An ignore is keyed on the finding's stable identity so it stays ignored across runs, its stored text comes from the finding rather than the request, and it is marked "no longer occurring" once the condition goes away. Ignore/restore are audit-logged. New table consistency_ignores (migration 0012). portScan's private-address helper is now exported and shared. Verified with 36 checks (each rule and its exclusions, address-family and case/trailing-dot handling, IPv6 case, ordering, stable keys, the report route including a garbled agent report, source counts, ignore/unignore rules and audit entries) and by driving the page against the real routers in a browser: Add to IPAM actually created the entry, ignore and restore, severity filter, "show all", the viewer view, and narrow-width layout (which found and fixed a squeezed badge and clipped buttons). Real dev database mtime untouched. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca0fa817f8
commit
2352689fd3
13 files changed
+2246
-1
No files matched your search
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE `consistency_ignores` (
|
||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`key` text NOT NULL,
|
||||
`title` text NOT NULL,
|
||||
`reason` text,
|
||||
`created_by` text,
|
||||
`created_at` text DEFAULT (current_timestamp) NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX `consistency_ignores_key_unique` ON `consistency_ignores` (`key`);
|
||||
File diff suppressed because it is too large.
Load diff
@@ -85,6 +85,13 @@
|
||||
"when": 1790385846612,
|
||||
"tag": "0011_strange_mongoose",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 12,
|
||||
"version": "6",
|
||||
"when": 1790388232273,
|
||||
"tag": "0012_cool_harry_osborn",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -348,3 +348,18 @@ export const domains = sqliteTable("domains", {
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
});
|
||||
|
||||
// ─── Consistency report ─────────────────────────────────────────────────────
|
||||
|
||||
// Findings someone has looked at and decided are fine (a DNS record that intentionally points elsewhere, a Docker bridge
|
||||
// address, ...). Keyed by the finding's stable key so it stays ignored across runs.
|
||||
export const consistencyIgnores = sqliteTable("consistency_ignores", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
key: text("key").notNull().unique(),
|
||||
title: text("title").notNull(), // what the finding said when it was ignored, so the list still makes sense once it's gone
|
||||
reason: text("reason"),
|
||||
createdBy: text("created_by"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
});
|
||||
@@ -25,6 +25,7 @@ import { searchRouter } from "./routes/search.js";
|
||||
import { sessionsRouter } from "./routes/sessions.js";
|
||||
import { maintenanceRouter } from "./routes/maintenance.js";
|
||||
import { domainsRouter } from "./routes/domains.js";
|
||||
import { consistencyRouter } from "./routes/consistency.js";
|
||||
import { initSecretExpiryScheduler } from "./services/secretExpiryScheduler.js";
|
||||
import { initTailscaleKeyExpiryScheduler } from "./services/tailscaleKeyExpiryScheduler.js";
|
||||
import { initLogRetentionScheduler } from "./services/logRetentionScheduler.js";
|
||||
@@ -94,6 +95,7 @@ app.use("/api/search", searchRouter);
|
||||
app.use("/api/sessions", sessionsRouter);
|
||||
app.use("/api/maintenance", maintenanceRouter);
|
||||
app.use("/api/domains", domainsRouter);
|
||||
app.use("/api/consistency", consistencyRouter);
|
||||
|
||||
if (existsSync(webDist)) {
|
||||
app.use(express.static(webDist));
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
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, type Finding } from "../services/consistency.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<string, unknown> }> {
|
||||
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 findings = buildFindings({ servers: serverInputs, ipam: ipamRows, dns: dnsRows });
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
consistencyRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const { findings, sources } = 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,
|
||||
generatedAt: new Date().toISOString(),
|
||||
});
|
||||
}));
|
||||
|
||||
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();
|
||||
}));
|
||||
@@ -0,0 +1,200 @@
|
||||
import * as net from "node:net";
|
||||
import { isPrivateAddress } from "./portScan.js";
|
||||
|
||||
/**
|
||||
* Cross-checks the three places this app records what lives at an IP address — the IPAM inventory, the DNS records
|
||||
* synced from the providers, and what each server's agent reports — and lists where they disagree. Pure: it takes
|
||||
* plain data and returns findings, so every rule can be tested exactly.
|
||||
*/
|
||||
|
||||
export type FindingKind = "ip_conflict" | "dns_stale" | "ipam_stale" | "not_in_ipam" | "no_dns";
|
||||
export type Severity = "error" | "warning" | "info";
|
||||
|
||||
export interface ServerInput {
|
||||
id: number;
|
||||
name: string;
|
||||
hostname: string | null;
|
||||
/** What the agent last reported. Empty for a server with no agent or no report — such a server is never judged. */
|
||||
ips: string[];
|
||||
}
|
||||
export interface IpamInput {
|
||||
id: number;
|
||||
ip: string;
|
||||
label: string | null;
|
||||
/** null = entered by hand; "tailscale"/"proxmox" = kept up to date by a sync. */
|
||||
source: string | null;
|
||||
}
|
||||
export interface DnsInput {
|
||||
name: string;
|
||||
type: string;
|
||||
content: string;
|
||||
providerName: string;
|
||||
}
|
||||
|
||||
export interface Finding {
|
||||
/** Stable identity, so an ignored finding stays ignored across runs. */
|
||||
key: string;
|
||||
kind: FindingKind;
|
||||
severity: Severity;
|
||||
title: string;
|
||||
detail: string;
|
||||
ip: string | null;
|
||||
servers: { id: number; name: string }[];
|
||||
dnsNames: string[];
|
||||
/** For not_in_ipam: a label to pre-fill when adding the address to IPAM. */
|
||||
suggestedLabel: string | null;
|
||||
}
|
||||
|
||||
// ─── helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
const norm = (ip: string) => ip.trim().toLowerCase();
|
||||
const cleanName = (n: string) => n.trim().toLowerCase().replace(/\.$/, "");
|
||||
const firstLabel = (n: string) => cleanName(n).split(".")[0];
|
||||
|
||||
function inRange172(ip: string): boolean {
|
||||
if (net.isIP(ip) !== 4) return false;
|
||||
const [a, b] = ip.split(".").map(Number);
|
||||
return a === 172 && b >= 16 && b <= 31;
|
||||
}
|
||||
|
||||
/** 100.64.0.0/10 — where Tailscale addresses live. MagicDNS names them, so a missing DNS record isn't a gap. */
|
||||
function isCgnat(ip: string): boolean {
|
||||
if (net.isIP(ip) !== 4) return false;
|
||||
const [a, b] = ip.split(".").map(Number);
|
||||
return a === 100 && b >= 64 && b <= 127;
|
||||
}
|
||||
|
||||
/** Agents report IPv4 only, so an IPv6 record can't be judged against them (and vice versa) — only compare within a family. */
|
||||
const sameFamilyAsAny = (ip: string, ips: string[]) => ips.some((o) => net.isIP(o) === net.isIP(ip));
|
||||
|
||||
const SEVERITY_ORDER: Record<Severity, number> = { error: 0, warning: 1, info: 2 };
|
||||
|
||||
/** Which of these servers does a DNS name or an IPAM label refer to? Exact hostname, or the same short name. */
|
||||
function serversNamed(name: string, servers: ServerInput[]): ServerInput[] {
|
||||
const n = cleanName(name);
|
||||
const short = firstLabel(name);
|
||||
return servers.filter((s) => {
|
||||
const host = s.hostname ? cleanName(s.hostname) : null;
|
||||
return (host !== null && n === host) || short === cleanName(s.name);
|
||||
});
|
||||
}
|
||||
|
||||
// ─── the checks ─────────────────────────────────────────────────────────────
|
||||
|
||||
export function buildFindings(input: { servers: ServerInput[]; ipam: IpamInput[]; dns: DnsInput[] }): Finding[] {
|
||||
const findings: Finding[] = [];
|
||||
const servers = input.servers.map((s) => ({ ...s, ips: [...new Set(s.ips.map(norm))] }));
|
||||
const withIps = servers.filter((s) => s.ips.length > 0);
|
||||
const ipamByIp = new Map(input.ipam.map((e) => [norm(e.ip), e]));
|
||||
// Public DNS records are for the outside world, not this inventory — only private addresses are compared.
|
||||
const privateDns = input.dns
|
||||
.filter((r) => (r.type === "A" || r.type === "AAAA") && isPrivateAddress(r.content.trim()))
|
||||
.map((r) => ({ ...r, name: cleanName(r.name), content: norm(r.content) }));
|
||||
const dnsByIp = new Map<string, typeof privateDns>();
|
||||
for (const r of privateDns) dnsByIp.set(r.content, [...(dnsByIp.get(r.content) ?? []), r]);
|
||||
|
||||
const add = (f: Omit<Finding, "servers" | "dnsNames" | "suggestedLabel" | "ip"> & Partial<Pick<Finding, "servers" | "dnsNames" | "suggestedLabel" | "ip">>) =>
|
||||
findings.push({ servers: [], dnsNames: [], suggestedLabel: null, ip: null, ...f });
|
||||
|
||||
// 1. The same address reported by two servers.
|
||||
const byServerIp = new Map<string, ServerInput[]>();
|
||||
for (const s of withIps) for (const ip of s.ips) byServerIp.set(ip, [...(byServerIp.get(ip) ?? []), s]);
|
||||
for (const [ip, owners] of byServerIp) {
|
||||
// Every Docker host has 172.17.0.1 (and similar bridge addresses in 172.16/12) — sharing those is normal.
|
||||
if (owners.length < 2 || inRange172(ip)) continue;
|
||||
add({
|
||||
key: `ip_conflict|${ip}`,
|
||||
kind: "ip_conflict",
|
||||
severity: "error",
|
||||
title: `${ip} is reported by ${owners.length} servers`,
|
||||
detail: `${owners.map((o) => o.name).join(", ")} all claim this address — an IP conflict, or a stale agent report.`,
|
||||
ip,
|
||||
servers: owners.map((o) => ({ id: o.id, name: o.name })),
|
||||
});
|
||||
}
|
||||
|
||||
// 2. DNS names a server's own name, but pointing somewhere the server isn't.
|
||||
const seenDns = new Set<string>();
|
||||
for (const r of privateDns) {
|
||||
for (const s of serversNamed(r.name, withIps)) {
|
||||
if (s.ips.includes(r.content) || !sameFamilyAsAny(r.content, s.ips)) continue;
|
||||
const key = `dns_stale|${s.id}|${r.name}|${r.content}`;
|
||||
if (seenDns.has(key)) continue;
|
||||
seenDns.add(key);
|
||||
add({
|
||||
key,
|
||||
kind: "dns_stale",
|
||||
severity: "warning",
|
||||
title: `${r.name} points to ${r.content}, but ${s.name} reports ${s.ips.join(", ")}`,
|
||||
detail: `The DNS record (${r.providerName}) doesn't match any address ${s.name} reports — likely out of date after an address change.`,
|
||||
ip: r.content,
|
||||
servers: [{ id: s.id, name: s.name }],
|
||||
dnsNames: [r.name],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 3. IPAM labels a server's name, at an address the server doesn't have.
|
||||
for (const e of input.ipam) {
|
||||
if (!e.label || e.source === "tailscale" || e.source === "proxmox") continue; // kept current by their own syncs
|
||||
const ip = norm(e.ip);
|
||||
for (const s of serversNamed(e.label, withIps)) {
|
||||
if (s.ips.includes(ip) || !sameFamilyAsAny(ip, s.ips)) continue;
|
||||
add({
|
||||
key: `ipam_stale|${s.id}|${ip}`,
|
||||
kind: "ipam_stale",
|
||||
severity: "warning",
|
||||
title: `IPAM lists ${e.label} at ${ip}, but ${s.name} reports ${s.ips.join(", ")}`,
|
||||
detail: `The IPAM entry doesn't match any address ${s.name} reports — update IPAM, or the server moved.`,
|
||||
ip,
|
||||
servers: [{ id: s.id, name: s.name }],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 4. In use (a server reports it, or DNS points at it) but not in IPAM.
|
||||
const candidates = new Set<string>([...byServerIp.keys(), ...dnsByIp.keys()]);
|
||||
for (const ip of candidates) {
|
||||
if (ipamByIp.has(ip)) continue;
|
||||
const owners = byServerIp.get(ip) ?? [];
|
||||
const records = dnsByIp.get(ip) ?? [];
|
||||
// A container network on a Docker host that nobody put in DNS isn't something to inventory.
|
||||
if (records.length === 0 && inRange172(ip)) continue;
|
||||
const names = [...new Set(records.map((r) => r.name))];
|
||||
const label = owners.length === 1 ? owners[0].name : names.length > 0 ? firstLabel(names[0]) : null;
|
||||
const who = [...owners.map((o) => `reported by ${o.name}`), ...(names.length > 0 ? [`in DNS as ${names.join(", ")}`] : [])].join(" and ");
|
||||
add({
|
||||
key: `not_in_ipam|${ip}`,
|
||||
kind: "not_in_ipam",
|
||||
severity: records.length > 0 ? "warning" : "info",
|
||||
title: `${ip} isn't in IPAM`,
|
||||
detail: `${who}.`,
|
||||
ip,
|
||||
servers: owners.map((o) => ({ id: o.id, name: o.name })),
|
||||
dnsNames: names,
|
||||
suggestedLabel: label,
|
||||
});
|
||||
}
|
||||
|
||||
// 4b. A server address nothing in DNS points at — only meaningful once there are DNS records to compare against.
|
||||
if (input.dns.length > 0) {
|
||||
for (const s of withIps) {
|
||||
for (const ip of s.ips) {
|
||||
if (dnsByIp.has(ip) || net.isIP(ip) === 0 || !isPrivateAddress(ip) || isCgnat(ip) || inRange172(ip)) continue;
|
||||
add({
|
||||
key: `no_dns|${s.id}|${ip}`,
|
||||
kind: "no_dns",
|
||||
severity: "info",
|
||||
title: `No DNS record points at ${ip} (${s.name})`,
|
||||
detail: `${s.name} reports ${ip}, but no cached A/AAAA record resolves to it.`,
|
||||
ip,
|
||||
servers: [{ id: s.id, name: s.name }],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return findings.sort(
|
||||
(a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity] || a.kind.localeCompare(b.kind) || (a.ip ?? "").localeCompare(b.ip ?? "", undefined, { numeric: true }),
|
||||
);
|
||||
}
|
||||
@@ -76,7 +76,7 @@ function isPrivateIPv6(ip: string): boolean {
|
||||
return (first & 0xfe00) === 0xfc00 || (first & 0xffc0) === 0xfe80; // unique-local fc00::/7, link-local fe80::/10
|
||||
}
|
||||
|
||||
function isPrivateAddress(ip: string): boolean {
|
||||
export function isPrivateAddress(ip: string): boolean {
|
||||
const family = net.isIP(ip);
|
||||
if (family === 4) return isPrivateIPv4(ip);
|
||||
if (family === 6) return isPrivateIPv6(ip);
|
||||
|
||||
Reference in new issue
Block a user