Files
coredns-web-ui/static/js/audit.js
T
bryanpedini a7ba01fc77 refactor(api): use audit operation constants and the shared zone not found error in zone handlers
- refactor(api): use audit operation constants and the shared zone not found error in zone handlers
- fix(api): return the stored content of rows with check errors
- fix(api): return warnings from record deletes, zone creation and import apply
- fix(api): allow repairing rows stored with a non canonical zone and invalid SOA sets
- fix(check): accept JSON column formatting and an absent SOA serial as canonical content
- fix(ui): match audit diff items by identity instead of array index
- fix(ui): count the SOA change once, report import warnings and check the request size
- fix(ui): edit the selected SOA row, keep the server order, show error rows raw and report write warnings
- test(check): cover triggering and clean pairs with exact codes and record ids
- test(records): cover SRV port, SOA timer and CAA value boundaries
- test(cli): check the exact check output and the exit code of warning-only zones
- refactor(import): order current rows with cmp.Compare
- test(check): cover malformed stored content and a mailbox stored as an address
- test(api): cover zone write failure paths, ambiguous SOA updates and unchanged record detection
2026-11-19 15:46:12 +01:00

159 lines
6.4 KiB
JavaScript

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
import { $, api, busy, formatTime, h, handleError, icon } from "./common.js";
const state = { nextBeforeId: null, count: 0, filters: {}, counter: 0 };
function flatten(value, prefix, out) {
if (Array.isArray(value)) {
value.forEach((item, index) => flatten(item, prefix + "[" + index + "]", out));
} else if (value !== null && typeof value === "object") {
for (const [key, item] of Object.entries(value)) flatten(item, prefix ? prefix + "." + key : key, out);
} else {
out.set(prefix, value);
}
return out;
}
function show(value) {
return value === undefined ? "(none)" : JSON.stringify(value);
}
function itemLabel(item, index) {
if (item !== null && typeof item === "object" && !Array.isArray(item)) {
if ("record_type" in item && "id" in item) return "record " + item.id;
if ("zone" in item && "username" in item && !("id" in item)) return "delegation " + item.zone + " " + item.username;
if ("username" in item && "id" in item) return "user " + item.id;
}
return "[" + index + "]";
}
function keyed(value) {
const out = new Map();
const items = Array.isArray(value) ? value : value === null || value === undefined ? [] : [value];
items.forEach((item, index) => out.set(itemLabel(item, index), item));
return out;
}
function diffValues(before, after, prefix) {
const left = before === undefined ? new Map() : flatten(before, prefix, new Map());
const right = after === undefined ? new Map() : flatten(after, prefix, new Map());
const paths = [...new Set([...left.keys(), ...right.keys()])].filter((path) => path !== "");
return paths.map((path) => {
const old = left.get(path);
const now = right.get(path);
let kind = "same";
if (!left.has(path)) kind = "added";
else if (!right.has(path)) kind = "removed";
else if (JSON.stringify(old) !== JSON.stringify(now)) kind = "changed";
return { path, kind, before: left.has(path) ? old : undefined, after: right.has(path) ? now : undefined };
});
}
export function diffRows(before, after) {
if (!Array.isArray(before) && !Array.isArray(after)) {
return diffValues(before === undefined ? null : before, after === undefined ? null : after, "");
}
const left = keyed(before);
const right = keyed(after);
const labels = [...new Set([...left.keys(), ...right.keys()])];
return labels.flatMap((label) => diffValues(left.has(label) ? left.get(label) : undefined, right.has(label) ? right.get(label) : undefined, label));
}
function diffTable(entry) {
const rows = diffRows(entry.before, entry.after);
if (rows.length === 0) return h("p", { class: "muted", text: "No field-level data." });
return h("table", { class: "table diff" },
h("thead", null, h("tr", null, h("th", { scope: "col", text: "Field" }), h("th", { scope: "col", text: "Before" }), h("th", { scope: "col", text: "After" }))),
h("tbody", null, rows.map((row) => h("tr", { class: "diff-" + row.kind },
h("th", { scope: "row", class: "mono", text: row.path }),
h("td", { class: "mono", text: show(row.before) }),
h("td", { class: "mono", text: show(row.after) })))));
}
function entryRows(entry) {
state.counter += 1;
const detailId = "audit-detail-" + state.counter;
const hasDiff = entry.before !== null && entry.before !== undefined || entry.after !== null && entry.after !== undefined;
const target = [];
if (entry.zone) target.push(entry.zone);
if (entry.record_id !== null && entry.record_id !== undefined) target.push("record " + entry.record_id);
if (entry.target_user) target.push("user " + entry.target_user);
const failed = typeof entry.status === "number" && entry.status >= 400;
const detail = h("tr", { class: "detail-row", id: detailId, hidden: true }, h("td", { colspan: 7 }));
const toggle = h("button", {
type: "button",
class: "btn btn-small",
"aria-expanded": "false",
"aria-controls": detailId,
onclick: () => {
const open = toggle.getAttribute("aria-expanded") !== "true";
toggle.setAttribute("aria-expanded", String(open));
detail.hidden = !open;
if (open && detail.firstChild.childNodes.length === 0) detail.firstChild.append(diffTable(entry));
},
}, icon("chevron"), " Changes");
const row = h("tr", { class: failed ? "row-error" : null },
h("td", { class: "mono", text: formatTime(entry.time) }),
h("td", { text: entry.actor }),
h("td", { class: "mono", text: entry.origin }),
h("td", { class: "mono", text: entry.operation }),
h("td", null,
entry.status === null || entry.status === undefined ? "-" : h("span", { class: "badge " + (failed ? "badge-error" : "badge-ok"), text: String(entry.status) }),
entry.error ? h("div", { class: "muted", text: entry.error }) : null),
h("td", { text: target.length ? target.join(", ") : "-" }),
h("td", null, hasDiff ? toggle : "-"));
return [row, detail];
}
async function load(append) {
const body = $("#audit-body");
const status = $("#audit-status");
const query = { ...state.filters };
if (append && state.nextBeforeId !== null) query.before_id = state.nextBeforeId;
if (!append) {
body.replaceChildren();
state.count = 0;
status.textContent = "Loading...";
}
try {
const { data } = await api("GET", "/audit", { query });
const entries = data.entries || [];
for (const entry of entries) body.append(...entryRows(entry));
state.count += entries.length;
state.nextBeforeId = data.next_before_id === undefined ? null : data.next_before_id;
status.textContent = state.count === 0 ? "No audit rows match." : state.count + " rows shown.";
$("#audit-more").hidden = state.nextBeforeId === null;
} catch (err) {
status.textContent = "The audit could not be loaded.";
handleError(err);
}
}
function readFilters() {
const form = $("#audit-filters");
const filters = {};
for (const name of ["zone", "actor", "operation", "kind", "outcome"]) {
const value = form.elements[name].value.trim();
if (value) filters[name] = value;
}
return filters;
}
$("#audit-filters").addEventListener("submit", (event) => {
event.preventDefault();
state.filters = readFilters();
state.nextBeforeId = null;
load(false);
});
$("#audit-filters").addEventListener("reset", () => {
setTimeout(() => {
state.filters = {};
state.nextBeforeId = null;
load(false);
}, 0);
});
$("#audit-more").addEventListener("click", (event) => busy(event.target, () => load(true)));
load(false);