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:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 53444bec7a
commit a7ba01fc77
21 files changed
+1315 -170

No files matched your search

+53 -25
View File
@@ -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 });