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
This commit is contained in:
1 parent
53444bec7a
commit
a7ba01fc77
21 files changed
+1315
-170
No files matched your search
+29
-3
@@ -20,9 +20,25 @@ function show(value) {
|
||||
return value === undefined ? "(none)" : JSON.stringify(value);
|
||||
}
|
||||
|
||||
export function diffRows(before, after) {
|
||||
const left = flatten(before === undefined ? null : before, "", new Map());
|
||||
const right = flatten(after === undefined ? null : after, "", new Map());
|
||||
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);
|
||||
@@ -35,6 +51,16 @@ export function diffRows(before, after) {
|
||||
});
|
||||
}
|
||||
|
||||
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." });
|
||||
|
||||
+27
-5
@@ -6,6 +6,15 @@ import { $, ApiError, api, appData, busy, clear, clearErrors, flash, formatTTL,
|
||||
const { zone } = appData();
|
||||
const base = zonePath(zone);
|
||||
const state = { fingerprint: null, mode: "merge", content: "" };
|
||||
const MAX_REQUEST_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
function requestBytes(mode, content) {
|
||||
return new TextEncoder().encode(JSON.stringify({ mode, content, fingerprint: "0".repeat(64) })).length;
|
||||
}
|
||||
|
||||
function tooLarge(mode, content) {
|
||||
return requestBytes(mode, content) > MAX_REQUEST_BYTES;
|
||||
}
|
||||
|
||||
function entryTable(title, entries, withOld) {
|
||||
if (!entries || entries.length === 0) return null;
|
||||
@@ -56,7 +65,7 @@ function showPreview(data) {
|
||||
const skipped = data.skipped || [];
|
||||
summary.append(h("p", { text: plural(added.length, "addition", "additions") + ", " + plural(updated.length, "update", "updates") + ", " + plural(deleted.length, "deletion", "deletions") + ", " + (data.unchanged || 0) + " unchanged, " + plural(skipped.length, "skipped entry", "skipped entries") + "." }));
|
||||
if (data.soa) {
|
||||
summary.append(h("p", { text: "The SOA record is replaced: " + data.soa.ns + " " + data.soa.mbox + " refresh " + data.soa.refresh + " retry " + data.soa.retry + " expire " + data.soa.expire + " minimum " + data.soa.minttl + " TTL " + formatTTL(data.soa.ttl) + "." }));
|
||||
summary.append(h("p", { text: "One of the updates replaces the SOA record: " + data.soa.ns + " " + data.soa.mbox + " refresh " + data.soa.refresh + " retry " + data.soa.retry + " expire " + data.soa.expire + " minimum " + data.soa.minttl + " TTL " + formatTTL(data.soa.ttl) + "." }));
|
||||
}
|
||||
for (const warning of data.warnings || []) {
|
||||
summary.append(h("p", { class: "alert alert-warn", text: (warning.line ? "Line " + warning.line + ": " : "") + warning.message }));
|
||||
@@ -67,7 +76,7 @@ function showPreview(data) {
|
||||
if (node) tables.append(node);
|
||||
}
|
||||
state.fingerprint = data.fingerprint;
|
||||
const total = added.length + updated.length + deleted.length + (data.soa ? 1 : 0);
|
||||
const total = added.length + updated.length + deleted.length;
|
||||
$("#import-apply").hidden = total === 0;
|
||||
if (total === 0) summary.append(h("p", { class: "muted", text: "Nothing to apply." }));
|
||||
}
|
||||
@@ -86,6 +95,10 @@ async function preview(event) {
|
||||
showFieldErrors(form, { content: "Paste a zone file or load one." });
|
||||
return;
|
||||
}
|
||||
if (tooLarge(input.mode, input.content)) {
|
||||
showFieldErrors(form, { content: "The zone file is too large: an import request is limited to 2 MiB." });
|
||||
return;
|
||||
}
|
||||
await busy($("#import-preview"), async () => {
|
||||
try {
|
||||
const { data } = await api("POST", base + "/import/preview", { body: input });
|
||||
@@ -112,7 +125,9 @@ async function apply() {
|
||||
await busy($("#import-apply"), async () => {
|
||||
try {
|
||||
const { data } = await api("POST", base + "/import/apply", { body: { mode: state.mode, content: state.content, fingerprint: state.fingerprint } });
|
||||
flash("Import applied: " + plural(data.added || 0, "addition", "additions") + ", " + plural(data.updated || 0, "update", "updates") + ", " + plural(data.deleted || 0, "deletion", "deletions") + ".");
|
||||
const warnings = Array.isArray(data.warnings) ? data.warnings : [];
|
||||
const notes = warnings.map((warning) => (warning.line ? "line " + warning.line + ": " : "") + warning.message);
|
||||
flash("Import applied: " + plural(data.added || 0, "addition", "additions") + ", " + plural(data.updated || 0, "update", "updates") + ", " + plural(data.deleted || 0, "deletion", "deletions") + "." + (notes.length ? " Warnings: " + notes.join("; ") + "." : ""));
|
||||
location.assign("/zones/" + encodeURIComponent(zone));
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 409) {
|
||||
@@ -136,11 +151,18 @@ $("#import-form").addEventListener("input", () => {
|
||||
$("#import-file").addEventListener("change", async (event) => {
|
||||
const file = event.target.files[0];
|
||||
if (!file) return;
|
||||
if (file.size > 2 * 1024 * 1024) {
|
||||
if (file.size > MAX_REQUEST_BYTES) {
|
||||
toast("The file is larger than 2 MiB.", { kind: "error" });
|
||||
event.target.value = "";
|
||||
return;
|
||||
}
|
||||
$("#import-content").value = await file.text();
|
||||
const content = await file.text();
|
||||
if (tooLarge($("#import-form").elements.mode.value, content)) {
|
||||
toast("The file is too large: an import request, which encodes the file as JSON, is limited to 2 MiB.", { kind: "error" });
|
||||
event.target.value = "";
|
||||
return;
|
||||
}
|
||||
$("#import-content").value = content;
|
||||
$("#import-apply").hidden = true;
|
||||
state.fingerprint = null;
|
||||
});
|
||||
+1
-37
@@ -56,7 +56,7 @@ export function presentValue(record) {
|
||||
case "CAA":
|
||||
return data.flag + " " + data.tag + ' "' + data.value + '"';
|
||||
case "SOA":
|
||||
return [data.ns, data.MBox !== undefined ? data.MBox : data.mbox, data.serial === null || data.serial === undefined ? "dynamic" : data.serial, data.refresh, data.retry, data.expire, data.minttl].join(" ");
|
||||
return [data.ns, data.MBox !== undefined ? data.MBox : data.mbox, data.serial == null || data.serial === 0 ? "dynamic (Unix time)" : data.serial, data.refresh, data.retry, data.expire, data.minttl].join(" ");
|
||||
default:
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
@@ -66,42 +66,6 @@ export function displayName(record) {
|
||||
return record.name === "" ? "@" : record.name;
|
||||
}
|
||||
|
||||
function nameLabels(name) {
|
||||
return name === "" ? [] : name.toLowerCase().split(".").reverse();
|
||||
}
|
||||
|
||||
function compareLabels(a, b) {
|
||||
const left = nameLabels(a);
|
||||
const right = nameLabels(b);
|
||||
const length = Math.min(left.length, right.length);
|
||||
for (let i = 0; i < length; i += 1) {
|
||||
if (left[i] < right[i]) return -1;
|
||||
if (left[i] > right[i]) return 1;
|
||||
}
|
||||
return left.length - right.length;
|
||||
}
|
||||
|
||||
function typeRank(type) {
|
||||
if (type === "SOA") return 0;
|
||||
if (type === "NS") return 1;
|
||||
return 2;
|
||||
}
|
||||
|
||||
export function compareRecords(a, b) {
|
||||
const byName = compareLabels(a.name, b.name);
|
||||
if (byName !== 0) return byName;
|
||||
const byRank = typeRank(a.type) - typeRank(b.type);
|
||||
if (byRank !== 0) return byRank;
|
||||
if (a.type !== b.type) return a.type < b.type ? -1 : 1;
|
||||
const left = presentValue(a);
|
||||
const right = presentValue(b);
|
||||
return left.localeCompare(right, "en", { numeric: true });
|
||||
}
|
||||
|
||||
export function sortRecords(records) {
|
||||
return [...records].sort(compareRecords);
|
||||
}
|
||||
|
||||
export function matchesFilter(record, type, text) {
|
||||
if (type && record.type !== type) return false;
|
||||
if (!text) return true;
|
||||
|
||||
+53
-25
@@ -2,7 +2,7 @@
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, appData, busy, clear, clearErrors, confirmDialog, flash, formatTTL, handleError, h, icon, openDialog, option, parseNumber, plural, setupDialogs, showFieldErrors, showFlash, toast, zonePath } from "./common.js";
|
||||
import { EDITABLE_TYPES, FIELD_SPECS, displayName, matchesFilter, presentValue, previewName, sortRecords, stripDot } from "./records.js";
|
||||
import { EDITABLE_TYPES, FIELD_SPECS, displayName, matchesFilter, presentValue, previewName, stripDot } from "./records.js";
|
||||
|
||||
const PAGE_SIZE = 100;
|
||||
const { zone: zoneParam, isAdmin } = appData();
|
||||
@@ -22,8 +22,22 @@ function zoneName() {
|
||||
return state.summary ? state.summary.name : zoneParam;
|
||||
}
|
||||
|
||||
function soaRows() {
|
||||
return state.records.filter((record) => record.type === "SOA");
|
||||
}
|
||||
|
||||
function soaRow() {
|
||||
return state.records.find((record) => record.type === "SOA" && record.name === "") || null;
|
||||
const rows = soaRows();
|
||||
const summary = state.summary && state.summary.soa ? rows.find((record) => record.id === state.summary.soa.id) : null;
|
||||
return summary || rows.find((record) => record.name === "") || rows[0] || null;
|
||||
}
|
||||
|
||||
function hasErrors(record) {
|
||||
return (record.problems || []).some((problem) => problem.severity === "error");
|
||||
}
|
||||
|
||||
function isBroken(record) {
|
||||
return record.data === null || record.data === undefined || hasErrors(record);
|
||||
}
|
||||
|
||||
function problemBadge(problem) {
|
||||
@@ -43,12 +57,13 @@ function renderSoa() {
|
||||
clear(summary);
|
||||
managed.hidden = true;
|
||||
const soa = state.summary ? state.summary.soa : null;
|
||||
edit.disabled = !(soa || soaRow());
|
||||
const count = soaRows().length;
|
||||
edit.disabled = count === 0;
|
||||
if (!soa) {
|
||||
status.textContent = soaRow() ? "The SOA record cannot be read. Edit it to replace it." : "This zone has no SOA record.";
|
||||
status.textContent = count === 0 ? "This zone has no SOA record." : count > 1 ? "This zone has " + count + " SOA records. Delete the extra ones in the records table and edit the one to keep." : "The SOA record has errors. Edit it to repair it.";
|
||||
return;
|
||||
}
|
||||
status.textContent = "";
|
||||
status.textContent = count > 1 ? "This zone has " + count + " SOA records. Delete the extra ones in the records table." : "";
|
||||
const dynamic = soa.serial === null || soa.serial === undefined;
|
||||
const items = [
|
||||
["Primary name server", soa.ns],
|
||||
@@ -90,7 +105,7 @@ function typeOptions() {
|
||||
}
|
||||
|
||||
function visibleRecords() {
|
||||
return sortRecords(state.records).filter((record) => matchesFilter(record, state.type, state.text));
|
||||
return state.records.filter((record) => matchesFilter(record, state.type, state.text));
|
||||
}
|
||||
|
||||
function problemsCell(record) {
|
||||
@@ -121,7 +136,7 @@ function renderRecords() {
|
||||
|
||||
function recordRow(record) {
|
||||
const label = displayName(record) + " " + record.type;
|
||||
const broken = record.data === null || record.data === undefined;
|
||||
const broken = isBroken(record);
|
||||
const isSoa = record.type === "SOA";
|
||||
const checkbox = h("input", {
|
||||
type: "checkbox",
|
||||
@@ -134,14 +149,14 @@ function recordRow(record) {
|
||||
updateSelection();
|
||||
},
|
||||
});
|
||||
const hasError = (record.problems || []).some((problem) => problem.severity === "error");
|
||||
const hasError = hasErrors(record);
|
||||
const hasWarning = (record.problems || []).length > 0;
|
||||
const value = h("td", { class: "value" },
|
||||
h("code", { class: broken ? "mono raw" : "mono", text: presentValue(record) }),
|
||||
h("code", { class: broken ? "mono raw" : "mono", text: broken ? (record.raw ?? "") : presentValue(record) }),
|
||||
(record.problems || []).length > 0 ? problemsCell(record) : null);
|
||||
const edit = h("button", { type: "button", class: "btn btn-small", "aria-label": "Edit " + label, onclick: () => (isSoa ? openSoa() : openRecord(record)) }, icon("edit"), " Edit");
|
||||
const edit = h("button", { type: "button", class: "btn btn-small", "aria-label": "Edit " + label, onclick: () => (isSoa ? openSoa(record) : openRecord(record)) }, icon("edit"), " Edit");
|
||||
const actions = h("td", { class: "actions-cell" }, edit);
|
||||
if (!isSoa) {
|
||||
if (!isSoa || soaRows().length > 1) {
|
||||
actions.append(" ", h("button", { type: "button", class: "btn btn-small btn-danger", "aria-label": "Delete " + label, onclick: () => deleteRecord(record) }, icon("trash"), " Delete"));
|
||||
}
|
||||
return h("tr", { class: hasError ? "row-error" : hasWarning ? "row-warning" : null },
|
||||
@@ -206,24 +221,37 @@ function soaBody(form) {
|
||||
};
|
||||
}
|
||||
|
||||
function openSoa() {
|
||||
const soaForm = { etag: null };
|
||||
|
||||
function soaValues(record) {
|
||||
const summary = state.summary ? state.summary.soa : null;
|
||||
if (summary && record && summary.id === record.id && !hasErrors(record)) return summary;
|
||||
const ttl = record && Number.isInteger(record.ttl) && record.ttl >= 0 ? record.ttl : 3600;
|
||||
if (record && !isBroken(record)) {
|
||||
const data = record.data;
|
||||
return { ns: data.ns, mbox: data.MBox !== undefined ? data.MBox : data.mbox, ttl, refresh: data.refresh, retry: data.retry, expire: data.expire, minttl: data.minttl };
|
||||
}
|
||||
return { ns: "", mbox: "", ttl, refresh: 3600, retry: 600, expire: 604800, minttl: 300 };
|
||||
}
|
||||
|
||||
function openSoa(selected) {
|
||||
const form = $("#soa-form");
|
||||
const soa = state.summary ? state.summary.soa : null;
|
||||
const row = soaRow();
|
||||
$("#soa-broken").hidden = Boolean(soa);
|
||||
const values = soa || { ns: "", mbox: "", ttl: row && Number.isInteger(row.ttl) && row.ttl >= 0 ? row.ttl : 3600, refresh: 3600, retry: 600, expire: 604800, minttl: 300 };
|
||||
for (const name of ["ns", "mbox", "ttl", "refresh", "retry", "expire", "minttl"]) form.elements[name].value = String(values[name]);
|
||||
const record = selected || soaRow();
|
||||
soaForm.etag = record ? record.etag : null;
|
||||
$("#soa-broken").hidden = !record || !isBroken(record);
|
||||
const values = soaValues(record);
|
||||
for (const name of ["ns", "mbox", "ttl", "refresh", "retry", "expire", "minttl"]) form.elements[name].value = values[name] === undefined || values[name] === null ? "" : String(values[name]);
|
||||
openDialog($("#soa-dialog"));
|
||||
}
|
||||
|
||||
function setupSoa() {
|
||||
const dialog = $("#soa-dialog");
|
||||
const form = $("#soa-form");
|
||||
$("#soa-edit").addEventListener("click", openSoa);
|
||||
$("#soa-edit").addEventListener("click", () => openSoa(null));
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
clearErrors(form);
|
||||
const etag = (state.summary && state.summary.soa ? state.summary.soa.etag : null) || (soaRow() ? soaRow().etag : null);
|
||||
const etag = soaForm.etag;
|
||||
await busy(form.querySelector("button[type=submit]"), async () => {
|
||||
try {
|
||||
const { data } = await api("PUT", base + "/soa", { body: soaBody(form), ifMatch: etag });
|
||||
@@ -322,7 +350,7 @@ function applyRrsetTtl(result) {
|
||||
|
||||
function openRecord(record) {
|
||||
const form = $("#record-form");
|
||||
const broken = Boolean(record) && (record.data === null || record.data === undefined);
|
||||
const broken = Boolean(record) && isBroken(record);
|
||||
const known = Boolean(record) && EDITABLE_TYPES.includes(record.type);
|
||||
recordForm.editing = record || null;
|
||||
recordForm.ttlTouched = Boolean(record);
|
||||
@@ -348,7 +376,7 @@ function recordBody(form) {
|
||||
const data = {};
|
||||
for (const spec of FIELD_SPECS[type] || []) {
|
||||
const value = form.elements[spec.key].value;
|
||||
data[spec.key] = spec.numeric || spec.options ? parseNumber(value) : spec.multiline ? value : value.trim();
|
||||
data[spec.key] = spec.numeric || spec.options ? parseNumber(value) : spec.multiline || (type === "CAA" && spec.key === "value") ? value : value.trim();
|
||||
}
|
||||
return { name: form.elements.name.value.trim(), type, ttl: parseNumber(form.elements.ttl.value), data };
|
||||
}
|
||||
@@ -394,9 +422,9 @@ async function deleteRecord(record) {
|
||||
const ok = await confirmDialog({ title: "Delete record", message: "Delete " + label + "?", confirm: "Delete" });
|
||||
if (!ok) return;
|
||||
try {
|
||||
await api("DELETE", base + "/records/" + record.id, { ifMatch: record.etag });
|
||||
const { data } = await api("DELETE", base + "/records/" + record.id, { ifMatch: record.etag });
|
||||
state.selected.delete(record.id);
|
||||
toast("Record deleted.");
|
||||
reportWarnings("Record deleted.", data);
|
||||
await load();
|
||||
} catch (err) {
|
||||
handleError(err, { reload: load });
|
||||
@@ -409,9 +437,9 @@ async function bulkDelete() {
|
||||
const ok = await confirmDialog({ title: "Delete records", message: "Delete " + plural(items.length, "selected record", "selected records") + "? Either all of them are deleted or none.", confirm: "Delete" });
|
||||
if (!ok) return;
|
||||
try {
|
||||
await api("POST", base + "/records/delete", { body: { records: items.map((record) => ({ id: record.id, etag: record.etag })) } });
|
||||
const { data } = await api("POST", base + "/records/delete", { body: { records: items.map((record) => ({ id: record.id, etag: record.etag })) } });
|
||||
state.selected.clear();
|
||||
toast(plural(items.length, "record", "records") + " deleted.");
|
||||
reportWarnings(plural(data && Number.isInteger(data.deleted) ? data.deleted : items.length, "record", "records") + " deleted.", data);
|
||||
await load();
|
||||
} catch (err) {
|
||||
handleError(err, { reload: load });
|
||||
|
||||
+4
-1
@@ -104,8 +104,9 @@ function setupCreate() {
|
||||
event.preventDefault();
|
||||
clearErrors(form);
|
||||
await busy(form.querySelector("button[type=submit]"), async () => {
|
||||
let created;
|
||||
try {
|
||||
await api("POST", "/zones", { body: zoneBody(form) });
|
||||
created = await api("POST", "/zones", { body: zoneBody(form) });
|
||||
} catch (err) {
|
||||
handleError(err, { form });
|
||||
return;
|
||||
@@ -113,6 +114,8 @@ function setupCreate() {
|
||||
dialog.close();
|
||||
form.reset();
|
||||
toast("Zone created. The DNS server serves a new zone after its next zone list reload, which happens every 10 minutes by default.");
|
||||
const warnings = created.data && Array.isArray(created.data.warnings) ? created.data.warnings : [];
|
||||
for (const warning of warnings) toast("Warning: " + warning.message, { kind: "warning" });
|
||||
await load();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user