// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import { containerName, type GroupConfig } from './env'; import type { AuditLedger } from './ledger'; import { run } from './proc'; import type { Recorder } from './recorder'; import type { SecretRegistry } from './secrets'; export interface CliResult { code: number | null; stdout: string; stderr: string; } export const USAGE_TEXT = `usage: coredns-web-ui start the HTTP server coredns-web-ui help print this usage text coredns-web-ui version print the version and commit coredns-web-ui healthcheck query the local /health endpoint coredns-web-ui user list [all] list the active users, or every user with all coredns-web-ui user add create a user with role admin or user, reading the password from stdin coredns-web-ui user passwd set the password of a user, reading it from stdin coredns-web-ui user role set the role of a user to admin or user coredns-web-ui user delete mark a user as deleted and end their sessions coredns-web-ui check [zone] report the rows the plugin cannot serve correctly coredns-web-ui audit [n] print the latest n audit rows (default 20) `; export function parseTable(stdout: string): { header: string[]; rows: string[][] } { const lines = stdout.split('\n').filter((l) => l.trim() !== ''); if (lines.length === 0) return { header: [], rows: [] }; const headerLine = lines[0]; const starts: number[] = []; const re = /\S+/g; let m: RegExpExecArray | null; while ((m = re.exec(headerLine))) starts.push(m.index); const header = starts.map((s, i) => headerLine.slice(s, i + 1 < starts.length ? starts[i + 1] : undefined).trim()); const rows = lines.slice(1).map((line) => starts.map((s, i) => line.slice(s, i + 1 < starts.length ? starts[i + 1] : undefined).trim())); return { header, rows }; } export class Cli { constructor( readonly group: GroupConfig, private readonly recorder: Recorder, private readonly secrets: SecretRegistry, private readonly ledger?: AuditLedger, ) {} get container(): string { return containerName(this.group.service); } async run(args: string[], stdin?: string): Promise { const dockerArgs = ['exec', ...(stdin !== undefined ? ['-i'] : []), this.container, '/app', ...args]; const r = await run('docker', dockerArgs, { input: stdin, timeoutMs: 120_000 }); this.recorder.addCli({ container: this.container, args, stdinBytes: stdin === undefined ? 0 : Buffer.byteLength(stdin), exitCode: r.code, durationMs: r.durationMs, stdout: this.secrets.redact(r.stdout), stderr: this.secrets.redact(r.stderr), }); return { code: r.code, stdout: r.stdout, stderr: r.stderr }; } private async change(operation: string, target: string, args: string[], stdin?: string): Promise { const r = await this.run(args, stdin); if (r.code === 0) this.ledger?.cli(operation, target, `cli ${args.join(' ')}`); return r; } userAdd(username: string, role: string, password: string): Promise { this.secrets.register(password, `password of ${username}`); return this.change('user_create', username, ['user', 'add', username, role], `${password}\n`); } userPasswd(username: string, password: string): Promise { this.secrets.register(password, `password of ${username}`); return this.change('user_password', username, ['user', 'passwd', username], `${password}\n`); } userRole(username: string, role: string): Promise { return this.change('user_role', username, ['user', 'role', username, role]); } userDelete(username: string): Promise { return this.change('user_delete', username, ['user', 'delete', username]); } userList(all = false): Promise { return this.run(all ? ['user', 'list', 'all'] : ['user', 'list']); } check(zone?: string): Promise { return this.run(zone === undefined ? ['check'] : ['check', zone]); } audit(n?: number | string): Promise { return this.run(n === undefined ? ['audit'] : ['audit', String(n)]); } }