- 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
169 lines
7.1 KiB
JavaScript
169 lines
7.1 KiB
JavaScript
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
// Copyright (C) 2026 Bryan Joshua Pedini
|
|
|
|
import { $, ApiError, api, appData, busy, clear, clearErrors, flash, formatTTL, h, handleError, plural, showFieldErrors, toast, zonePath } from "./common.js";
|
|
|
|
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;
|
|
const head = ["Name", "Type", "TTL", "Value"];
|
|
if (withOld) head.push("Old TTL");
|
|
return h("div", { class: "table-wrap" },
|
|
h("table", { class: "table" },
|
|
h("caption", { text: title + " (" + entries.length + ")" }),
|
|
h("thead", null, h("tr", null, head.map((label) => h("th", { scope: "col", text: label })))),
|
|
h("tbody", null, entries.map((entry) => h("tr", null,
|
|
h("th", { scope: "row", class: "mono", text: entry.name === "" ? "@" : entry.name }),
|
|
h("td", { text: entry.type }),
|
|
h("td", { class: "num", text: formatTTL(entry.ttl) }),
|
|
h("td", { class: "mono", text: entry.value }),
|
|
withOld ? h("td", { class: "num", text: formatTTL(entry.old_ttl) }) : null)))));
|
|
}
|
|
|
|
function skippedList(skipped) {
|
|
if (!skipped || skipped.length === 0) return null;
|
|
return h("div", null,
|
|
h("h3", { text: "Skipped (" + skipped.length + ")" }),
|
|
h("ul", { class: "list" }, skipped.map((item) => h("li", null,
|
|
item.line ? "Line " + item.line + ": " : "",
|
|
h("code", { class: "mono", text: item.text || "" }),
|
|
" ",
|
|
h("span", { class: "muted", text: item.reason })))));
|
|
}
|
|
|
|
function showErrors(list) {
|
|
const box = $("#import-errors");
|
|
clear(box);
|
|
box.hidden = !list || list.length === 0;
|
|
if (box.hidden) return;
|
|
box.append(h("div", { class: "alert alert-error", role: "alert" },
|
|
h("p", { text: "The file cannot be imported:" }),
|
|
h("ul", { class: "list" }, list.map((item) => h("li", { text: (item.line ? "Line " + item.line + ": " : "") + item.message })))));
|
|
}
|
|
|
|
function showPreview(data) {
|
|
const result = $("#import-result");
|
|
result.hidden = false;
|
|
showErrors([]);
|
|
const summary = $("#import-summary");
|
|
clear(summary);
|
|
const added = data.added || [];
|
|
const updated = data.updated || [];
|
|
const deleted = data.deleted || [];
|
|
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: "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 }));
|
|
}
|
|
const tables = $("#import-tables");
|
|
clear(tables);
|
|
for (const node of [entryTable("Additions", added, false), entryTable("Updates", updated, true), entryTable("Deletions", deleted, false), skippedList(skipped)]) {
|
|
if (node) tables.append(node);
|
|
}
|
|
state.fingerprint = data.fingerprint;
|
|
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." }));
|
|
}
|
|
|
|
function currentInput() {
|
|
const form = $("#import-form");
|
|
return { mode: form.elements.mode.value, content: form.elements.content.value };
|
|
}
|
|
|
|
async function preview(event) {
|
|
event.preventDefault();
|
|
const form = $("#import-form");
|
|
clearErrors(form);
|
|
const input = currentInput();
|
|
if (input.content.trim() === "") {
|
|
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 });
|
|
state.mode = input.mode;
|
|
state.content = input.content;
|
|
showPreview(data);
|
|
} catch (err) {
|
|
$("#import-apply").hidden = true;
|
|
state.fingerprint = null;
|
|
if (err instanceof ApiError && err.status === 422 && Array.isArray(err.body && err.body.errors)) {
|
|
$("#import-result").hidden = false;
|
|
clear($("#import-summary"));
|
|
clear($("#import-tables"));
|
|
showErrors(err.body.errors);
|
|
return;
|
|
}
|
|
handleError(err, { form });
|
|
}
|
|
});
|
|
}
|
|
|
|
async function apply() {
|
|
if (!state.fingerprint) return;
|
|
await busy($("#import-apply"), async () => {
|
|
try {
|
|
const { data } = await api("POST", base + "/import/apply", { body: { mode: state.mode, content: state.content, fingerprint: state.fingerprint } });
|
|
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) {
|
|
$("#import-apply").hidden = true;
|
|
state.fingerprint = null;
|
|
toast("The zone changed since the preview. Run the preview again.", { kind: "error" });
|
|
return;
|
|
}
|
|
handleError(err);
|
|
}
|
|
});
|
|
}
|
|
|
|
$("#zone-link").href = "/zones/" + encodeURIComponent(zone);
|
|
$("#import-form").addEventListener("submit", preview);
|
|
$("#import-apply").addEventListener("click", apply);
|
|
$("#import-form").addEventListener("input", () => {
|
|
$("#import-apply").hidden = true;
|
|
state.fingerprint = null;
|
|
});
|
|
$("#import-file").addEventListener("change", async (event) => {
|
|
const file = event.target.files[0];
|
|
if (!file) return;
|
|
if (file.size > MAX_REQUEST_BYTES) {
|
|
toast("The file is larger than 2 MiB.", { kind: "error" });
|
|
event.target.value = "";
|
|
return;
|
|
}
|
|
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;
|
|
});
|