Delete Technitium zones from the DNS page
A Delete zone button on the open zone of a Technitium provider deletes the zone and every record in it: the name has to be typed, and the server checks it against the zone as the provider lists it now (so a stale page, a wrong id or an internal zone cannot be deleted by mistake). What this app cached of the zone and its records goes with it. Technitium's own refusal is shown and nothing is changed. Administrators only, audited with how many records were cached; the adapter gets an optional deleteZone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0244f5a8ab
commit
082c524125
10 files changed
+197
-16
No files matched your search
@@ -6,7 +6,8 @@
|
||||
* Record ID format: "type||name||content||priority"
|
||||
*
|
||||
* Making a zone is `/api/zones/create` (permission "Zones: Modify"): the name and the type, and for a Secondary zone its primary servers, for a Forwarder zone the address to pass questions to, the
|
||||
* protocol to use and whether to validate DNSSEC. It answers with the name of the zone it made (`response.domain` — for a reverse zone the …arpa name). Not checked against a live server.
|
||||
* protocol to use and whether to validate DNSSEC. It answers with the name of the zone it made (`response.domain` — for a reverse zone the …arpa name). Deleting one is `/api/zones/delete` with the
|
||||
* zone's name (permission "Zones: Delete", which is not the one for changing records): the zone and every record in it go. Neither is checked against a live server.
|
||||
*/
|
||||
import type { DnsAdapter, DnsRecord, DnsRecordInput, DnsZone, NewDnsZone } from "../types.js";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
@@ -113,6 +114,10 @@ export function createTechnitiumAdapter(config: TechnitiumConfig): DnsAdapter {
|
||||
return { id: made, name: made };
|
||||
}
|
||||
|
||||
async function deleteZone(zoneId: string): Promise<void> {
|
||||
await apiPost("/api/zones/delete", { zone: zoneId });
|
||||
}
|
||||
|
||||
async function listZones(): Promise<DnsZone[]> {
|
||||
const resp = await apiGet("/api/zones/list");
|
||||
const zones = resp?.zones ?? [];
|
||||
@@ -166,5 +171,5 @@ export function createTechnitiumAdapter(config: TechnitiumConfig): DnsAdapter {
|
||||
await apiPost("/api/zones/records/delete", { domain: name, zone, type, ...typeParams });
|
||||
}
|
||||
|
||||
return withDiagLogging("technitium", { listZones, createZone, listRecords, addRecord, updateRecord, deleteRecord });
|
||||
return withDiagLogging("technitium", { listZones, createZone, deleteZone, listRecords, addRecord, updateRecord, deleteRecord });
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import { createTechnitiumAdapter } from "./adapters/technitium.js";
|
||||
/** The kinds of provider whose adapter can make a zone: the page offers "New zone" for these, and the route refuses it for the others. */
|
||||
export const ZONE_CREATING_PROVIDERS: ReadonlySet<DnsProviderType> = new Set<DnsProviderType>(["technitium"]);
|
||||
|
||||
/** The kinds of provider whose adapter can delete a zone: the page offers "Delete zone" for these, and the route refuses it for the others. */
|
||||
export const ZONE_DELETING_PROVIDERS: ReadonlySet<DnsProviderType> = new Set<DnsProviderType>(["technitium"]);
|
||||
|
||||
export function createDnsAdapter(providerType: DnsProviderType, config: DnsProviderConfig): DnsAdapter {
|
||||
switch (providerType) {
|
||||
case "cloudflare":
|
||||
|
||||
@@ -32,6 +32,8 @@ export interface DnsAdapter {
|
||||
listZones(): Promise<DnsZone[]>;
|
||||
/** Only for a provider that can make a zone (see ZONE_CREATING_PROVIDERS in registry.ts); the zone it made, by the name the provider gave it. */
|
||||
createZone?(zone: NewDnsZone): Promise<DnsZone>;
|
||||
/** Only for a provider that can delete a zone (ZONE_DELETING_PROVIDERS in registry.ts): the zone and every record in it are gone. */
|
||||
deleteZone?(zoneId: string): Promise<void>;
|
||||
listRecords(zoneId: string): Promise<DnsRecord[]>;
|
||||
addRecord(zoneId: string, record: DnsRecordInput): Promise<DnsRecord>;
|
||||
updateRecord(zoneId: string, recordId: string, record: DnsRecordInput): Promise<DnsRecord>;
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
splitProviderConfig,
|
||||
validateProviderConfig,
|
||||
} from "../dns/providerSchemas.js";
|
||||
import { createDnsAdapter, ZONE_CREATING_PROVIDERS } from "../dns/registry.js";
|
||||
import { createDnsAdapter, ZONE_CREATING_PROVIDERS, ZONE_DELETING_PROVIDERS } from "../dns/registry.js";
|
||||
import { normaliseZoneName } from "../dns/zoneNames.js";
|
||||
import { loadDnsProviderConfig, getDnsAdapterForProvider } from "../dns/loadProvider.js";
|
||||
import { getDnsStats } from "../dns/stats.js";
|
||||
@@ -56,7 +56,7 @@ dnsRouter.get("/providers", asyncHandler(async (_req, res) => {
|
||||
createdAt: dnsProviders.createdAt,
|
||||
})
|
||||
.from(dnsProviders);
|
||||
res.json({ providers: rows.map((r) => ({ ...r, canCreateZones: ZONE_CREATING_PROVIDERS.has(r.providerType) })) });
|
||||
res.json({ providers: rows.map((r) => ({ ...r, canCreateZones: ZONE_CREATING_PROVIDERS.has(r.providerType), canDeleteZones: ZONE_DELETING_PROVIDERS.has(r.providerType) })) });
|
||||
}));
|
||||
|
||||
const configValueSchema = z.union([z.string(), z.boolean()]);
|
||||
@@ -331,6 +331,40 @@ dnsRouter.post("/providers/:id/zones", requireRole("admin"), asyncHandler(async
|
||||
}
|
||||
}));
|
||||
|
||||
const deleteZoneSchema = z.object({ name: z.string().min(1).max(300) });
|
||||
|
||||
/**
|
||||
* Deletes a zone, and every record in it, on a provider that can (Technitium). An administrator's alone. The zone's name has to be sent along and has to be the one the zone has now, and the
|
||||
* provider has to list the zone: a page that is minutes old, a wrong id or one of the server's own internal zones (not listed) cannot be deleted by mistake. What this app has cached of the zone
|
||||
* and its records goes with it. What the provider says when it refuses (a token that may not delete zones) is passed on, and nothing is changed here.
|
||||
*/
|
||||
dnsRouter.delete("/providers/:id/zones/:zoneId", requireRole("admin"), asyncHandler(async (req, res) => {
|
||||
const providerId = /^\d+$/.test(req.params.id) ? Number(req.params.id) : Number.NaN;
|
||||
const zoneId = req.params.zoneId;
|
||||
if (!Number.isInteger(providerId) || normaliseZoneName(zoneId) !== zoneId) return res.status(400).json({ error: "invalid_id" });
|
||||
const parsed = deleteZoneSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "The name of the zone has to be sent along to delete it." });
|
||||
const found = await getDnsAdapterForProvider(providerId);
|
||||
if (!found) return res.status(404).json({ error: "not_found" });
|
||||
if (!found.provider.enabled) return res.status(400).json({ error: "provider_disabled", message: "This provider is switched off." });
|
||||
if (!ZONE_DELETING_PROVIDERS.has(found.provider.providerType) || !found.adapter.deleteZone) {
|
||||
return res.status(400).json({ error: "not_supported", message: "Zones can't be deleted from here on this kind of provider." });
|
||||
}
|
||||
try {
|
||||
const zone = (await found.adapter.listZones()).find((z) => z.id === zoneId);
|
||||
if (!zone) return res.status(404).json({ error: "zone_not_found", message: "The provider has no such zone." });
|
||||
if (zone.name !== parsed.data.name) return res.status(400).json({ error: "name_mismatch", message: "That is not the name of that zone." });
|
||||
await found.adapter.deleteZone(zoneId);
|
||||
} catch (err) {
|
||||
return res.status(502).json({ error: "dns_provider_error", message: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
const cachedRecords = await db.select({ id: dnsRecordsCache.id }).from(dnsRecordsCache).where(and(eq(dnsRecordsCache.providerId, providerId), eq(dnsRecordsCache.zoneId, zoneId)));
|
||||
await db.delete(dnsRecordsCache).where(and(eq(dnsRecordsCache.providerId, providerId), eq(dnsRecordsCache.zoneId, zoneId)));
|
||||
await db.delete(dnsZonesCache).where(and(eq(dnsZonesCache.providerId, providerId), eq(dnsZonesCache.zoneId, zoneId)));
|
||||
await recordAudit({ actor: req.currentUser!, category: "dns", action: "delete_zone", targetType: "dns_zone", targetId: zoneId, detail: { providerId, name: parsed.data.name, cachedRecords: cachedRecords.length } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// ─── Records (cached) ────────────────────────────────────────────────────────
|
||||
|
||||
dnsRouter.get("/providers/:id/zones/:zoneId/records", asyncHandler(async (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user