Support a separate database for Web UI tables

This commit is contained in:
bryanpedini committed 2026-11-21 20:36:03 +01:00
1 parent a2f8e59d9b
commit f464dc0af9
38 files changed
+880 -302

No files matched your search

+43 -11
View File
@@ -5,7 +5,8 @@ import { test, expect } from '../harness/fixtures';
import { run } from '../harness/proc';
import { docker } from '../harness/docker';
import { parseDig } from '../harness/dig';
import { containerName, GROUPS, project, RUN_LABEL } from '../harness/env';
import { containerName, GROUP_NAMES, GROUPS, project, RUN_LABEL } from '../harness/env';
import { Db } from '../harness/mysql';
import { decodedApiPath, matchRoute, sanitizeActor } from '../harness/ledger';
import { parseTable, USAGE_TEXT } from '../harness/cli';
import { rawRecord, sqlZone, canonicalContent, digData, bumpSerial, utcDay, type RawRecordInput } from '../harness/records';
@@ -126,16 +127,47 @@ test.describe('harness self-checks', () => {
expect(world.recorder.cli.length).toBeGreaterThanOrEqual(6);
});
test('the application connects with the least-privilege grants of the plan', async ({ world }) => {
const grants = (await world.db.query<Record<string, string>>(`SHOW GRANTS FOR '${world.g.appUser}'@'%'`)).map((r) => Object.values(r)[0]);
const on = (table: string) => grants.find((g) => g.includes(`\`${world.g.database}\`.\`${world.g.prefix}${table}\``)) ?? '';
expect(on('records')).toMatch(/GRANT SELECT, INSERT, UPDATE, DELETE ON/);
expect(on('webui_users')).not.toMatch(/DELETE/);
expect(on('webui_audit')).not.toMatch(/UPDATE|DELETE/);
const dns = (await world.db.query<Record<string, string>>(`SHOW GRANTS FOR '${world.g.dnsUser}'@'%'`)).map((r) => Object.values(r)[0]);
expect(dns.filter((g) => g.includes(world.g.database))).toEqual([`GRANT SELECT ON \`${world.g.database}\`.\`${world.g.prefix}records\` TO \`${world.g.dnsUser}\`@\`%\``]);
for (const table of ['webui_schema', 'webui_users', 'webui_sessions', 'webui_zone_delegations', 'webui_audit']) {
expect(await world.db.tableExists(`${world.g.prefix}${table}`), `${table} created by the migrations`).toBe(true);
test('the application runs with grants on the records table only and full grants on its own database', async ({ world }) => {
const g = world.g;
expect(g.webuiDatabase).not.toBe(g.database);
expect(g.webuiGrants).toBe('database');
const grants = (await world.db.query<Record<string, string>>(`SHOW GRANTS FOR '${g.appUser}'@'%'`)).map((r) => Object.values(r)[0]);
expect(grants.filter((x) => x.includes(`\`${g.database}\``))).toEqual([
`GRANT SELECT, INSERT, UPDATE, DELETE ON \`${g.database}\`.\`${g.prefix}records\` TO \`${g.appUser}\`@\`%\``,
]);
expect(grants.filter((x) => x.includes(`\`${g.webuiDatabase}\``))).toEqual([`GRANT ALL PRIVILEGES ON \`${g.webuiDatabase}\`.* TO \`${g.appUser}\`@\`%\``]);
const dns = (await world.db.query<Record<string, string>>(`SHOW GRANTS FOR '${g.dnsUser}'@'%'`)).map((r) => Object.values(r)[0]);
expect(dns.filter((x) => x.includes(g.database))).toEqual([`GRANT SELECT ON \`${g.database}\`.\`${g.prefix}records\` TO \`${g.dnsUser}\`@\`%\``]);
expect(await world.db.tables(g.database)).toEqual([`${g.prefix}records`]);
const webuiTables = ['webui_audit', 'webui_locks', 'webui_schema', 'webui_sessions', 'webui_users', 'webui_zone_delegations'];
expect(await world.db.tables(g.webuiDatabase)).toEqual(webuiTables.map((t) => `${g.webuiPrefix}${t}`));
});
test('each group keeps the webui tables where its configuration puts them, with the least-privilege grants of the plan', async () => {
for (const name of GROUP_NAMES) {
const g = GROUPS[name];
const db = new Db(g);
try {
const tables = await db.tables(g.database);
const webui = ['webui_audit', 'webui_locks', 'webui_schema', 'webui_sessions', 'webui_users', 'webui_zone_delegations'].map((t) => `${g.webuiPrefix}${t}`);
if (g.webuiDatabase === g.database) {
expect(tables, name).toEqual([`${g.prefix}records`, ...webui].sort());
} else {
expect(tables, name).toEqual([`${g.prefix}records`]);
expect(await db.tables(g.webuiDatabase), name).toEqual(webui);
}
if (g.webuiGrants !== 'tables') continue;
const grants = (await db.query<Record<string, string>>(`SHOW GRANTS FOR '${g.appUser}'@'%'`)).map((r) => Object.values(r)[0]);
const on = (schema: string, table: string) => grants.find((x) => x.includes(`\`${schema}\`.\`${table}\``)) ?? '';
expect(on(g.database, `${g.prefix}records`), name).toMatch(/GRANT SELECT, INSERT, UPDATE, DELETE ON/);
expect(on(g.webuiDatabase, `${g.webuiPrefix}webui_users`), name).toMatch(/GRANT .*INSERT/);
expect(on(g.webuiDatabase, `${g.webuiPrefix}webui_users`), name).not.toMatch(/DELETE/);
expect(on(g.webuiDatabase, `${g.webuiPrefix}webui_audit`), name).toMatch(/GRANT .*INSERT/);
expect(on(g.webuiDatabase, `${g.webuiPrefix}webui_audit`), name).not.toMatch(/UPDATE|DELETE/);
expect(grants.some((x) => / ON `[^`]+`\.\* /.test(x)), name).toBe(false);
} finally {
await db.close();
}
}
});