Files
bryanpedini a67183b646
Test / test (push) Successful in 28m4s
Build and publish image / image (push) Successful in 28m43s
feat: manage TLSA records
2026-11-22 11:31:59 +01:00

194 lines
6.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
import { expect } from '@playwright/test';
export const TYPES = ['A', 'AAAA', 'CNAME', 'NS', 'MX', 'TXT', 'SRV', 'CAA', 'TLSA', 'SOA'] as const;
export type RecordType = (typeof TYPES)[number];
export const EDITABLE_TYPES = TYPES.filter((t) => t !== 'SOA');
export const CANONICAL_KEYS: Readonly<Record<RecordType, readonly string[]>> = {
A: ['ip'],
AAAA: ['ip'],
CNAME: ['host'],
NS: ['host'],
MX: ['host', 'preference'],
TXT: ['text'],
SRV: ['priority', 'weight', 'port', 'target'],
CAA: ['flag', 'tag', 'value'],
TLSA: ['usage', 'selector', 'matching_type', 'certificate'],
SOA: ['ns', 'MBox', 'refresh', 'retry', 'expire', 'minttl', 'serial'],
};
export type RecordData = Record<string, string | number>;
export interface ExpectedRecord {
zone: string;
name: string;
type: RecordType;
ttl: number;
data: RecordData;
}
export function canonicalObject(type: RecordType, data: RecordData): RecordData {
const out: RecordData = {};
for (const key of CANONICAL_KEYS[type]) {
if (data[key] === undefined) throw new Error(`missing ${key} for ${type}`);
out[key] = data[key];
}
return out;
}
export function canonicalContent(type: RecordType, data: RecordData): string {
return JSON.stringify(canonicalObject(type, data));
}
function unescapeGoJson(raw: string): string {
return raw.replace(/\\u003c/g, '<').replace(/\\u003e/g, '>').replace(/\\u0026/g, '&').replace(/\\u2028/g, '
').replace(/\\u2029/g, '
');
}
export function expectCanonicalContent(raw: string | null, type: RecordType, data: RecordData, where = 'content'): void {
expect(raw, `${where} is not NULL`).not.toBeNull();
const expected = canonicalContent(type, data);
let parsed: unknown;
try {
parsed = JSON.parse(raw as string);
} catch {
throw new Error(`${where} is not JSON: ${raw}`);
}
expect(JSON.stringify(parsed), `${where} has the canonical keys, order and values`).toBe(expected);
expect(unescapeGoJson(raw as string), `${where} is compact canonical JSON`).toBe(expected);
}
export function effectiveTtl(ttl: number): number {
return ttl === 0 ? 360 : ttl;
}
export function uiTtl(ttl: number | null): string {
if (ttl === null) return 'missing';
return ttl === 0 ? 'default (360 s)' : String(ttl);
}
function dnsQuote(s: string): string {
return '"' + s.replace(/"/g, '\\"') + '"';
}
export function digData(type: RecordType, d: RecordData): string {
switch (type) {
case 'A':
case 'AAAA':
return String(d.ip);
case 'CNAME':
case 'NS':
return String(d.host);
case 'MX':
return `${d.preference} ${d.host}`;
case 'TXT': {
const text = String(d.text);
const parts: string[] = [];
for (let i = 0; i < text.length; i += 255) parts.push(text.slice(i, i + 255));
return parts.map(dnsQuote).join(' ');
}
case 'SRV':
return `${d.priority} ${d.weight} ${d.port} ${d.target}`;
case 'CAA':
return `${d.flag} ${d.tag} ${dnsQuote(String(d.value))}`;
case 'TLSA':
return `${d.usage} ${d.selector} ${d.matching_type} ${d.certificate}`;
case 'SOA':
return `${d.ns} ${d.MBox} ${d.serial} ${d.refresh} ${d.retry} ${d.expire} ${d.minttl}`;
}
}
export function uiValue(type: RecordType, d: RecordData): string {
switch (type) {
case 'A':
case 'AAAA':
return String(d.ip);
case 'CNAME':
case 'NS':
return String(d.host);
case 'MX':
return `${d.preference} ${d.host}`;
case 'TXT':
return `"${d.text}"`;
case 'SRV':
return `${d.priority} ${d.weight} ${d.port} ${d.target}`;
case 'CAA':
return `${d.flag} ${d.tag} "${d.value}"`;
case 'TLSA':
return `${d.usage} ${d.selector} ${d.matching_type} ${d.certificate}`;
case 'SOA':
return [d.ns, d.MBox, d.serial ?? 'dynamic', d.refresh, d.retry, d.expire, d.minttl].join(' ');
}
}
export function zoneFileValue(type: RecordType, d: RecordData): string {
return digData(type, d);
}
export function utcDay(now = new Date()): number {
return Number(`${now.getUTCFullYear()}${String(now.getUTCMonth() + 1).padStart(2, '0')}${String(now.getUTCDate()).padStart(2, '0')}`) * 100;
}
export function bumpSerial(prev: number, now = new Date()): number {
let next = (prev + 1) >>> 0;
if (next === 0) next = 1;
return Math.max(next, utcDay(now));
}
export function newZoneSerial(now = new Date()): number {
return utcDay(now);
}
export function acceptableBumps(prev: number, before: Date, after: Date): number[] {
return [...new Set([bumpSerial(prev, before), bumpSerial(prev, after)])];
}
export function expectSerialBump(prev: number, actual: number, before: Date, after: Date, what = 'SOA serial'): void {
expect(acceptableBumps(prev, before, after), `${what} bumped once from ${prev}`).toContain(actual);
}
export function fqdnOf(name: string, zone: string): string {
return name === '' ? zone : `${name}.${zone}`;
}
export function normalizeZone(zone: string): string {
const z = zone.toLowerCase();
return z.endsWith('.') ? z : `${z}.`;
}
export interface RawRecordInput {
zone: string;
name: string;
type: RecordType;
ttl: number;
data: RecordData;
}
export function rawRecord(r: RawRecordInput): { zone: string; name: string; ttl: number; content: string; record_type: string } {
return { zone: r.zone, name: r.name, ttl: r.ttl, content: canonicalContent(r.type, r.data), record_type: r.type };
}
export function soaData(zone: string, serial: number, timers: Partial<Record<'refresh' | 'retry' | 'expire' | 'minttl', number>> = {}): RecordData {
return {
ns: `ns1.${zone}`,
MBox: `hostmaster.${zone}`,
refresh: timers.refresh ?? 3600,
retry: timers.retry ?? 600,
expire: timers.expire ?? 604800,
minttl: timers.minttl ?? 300,
serial,
};
}
export function sqlZone(zone: string, serial = newZoneSerial(), ttl = 3600): RawRecordInput[] {
return [
{ zone, name: '', type: 'SOA', ttl, data: soaData(zone, serial) },
{ zone, name: '', type: 'NS', ttl, data: { host: `ns1.${zone}` } },
{ zone, name: '', type: 'NS', ttl, data: { host: `ns2.${zone}` } },
{ zone, name: 'ns1', type: 'A', ttl, data: { ip: '192.0.2.1' } },
{ zone, name: 'ns2', type: 'A', ttl, data: { ip: '192.0.2.2' } },
];
}