Support a separate database for Web UI tables
This commit is contained in:
1 parent
a2f8e59d9b
commit
f464dc0af9
38 files changed
+880
-302
No files matched your search
@@ -162,6 +162,7 @@ services:
|
||||
DB_USER: webui_harness
|
||||
DB_PASSWORD: webui-harness-e2e-pass
|
||||
DB_NAME: e2e_harness
|
||||
DB_WEBUI_NAME: e2e_harness_webui
|
||||
|
||||
app-core:
|
||||
<<: *app
|
||||
@@ -171,6 +172,7 @@ services:
|
||||
DB_USER: webui_core
|
||||
DB_PASSWORD: webui-core-e2e-pass
|
||||
DB_NAME: e2e_core
|
||||
DB_WEBUI_NAME: e2e_core_webui
|
||||
|
||||
app-session:
|
||||
<<: *app
|
||||
@@ -191,6 +193,7 @@ services:
|
||||
DB_USER: webui_power
|
||||
DB_PASSWORD: webui-power-e2e-pass
|
||||
DB_NAME: e2e_power
|
||||
DB_WEBUI_NAME: e2e_power_webui
|
||||
|
||||
app-stupid:
|
||||
<<: *app
|
||||
@@ -209,6 +212,8 @@ services:
|
||||
DB_USER: webui_malicious
|
||||
DB_PASSWORD: webui-malicious-e2e-pass
|
||||
DB_NAME: e2e_malicious
|
||||
DB_WEBUI_NAME: e2e_malicious_webui
|
||||
DB_WEBUI_TABLE_PREFIX: mal_
|
||||
DB_TABLE_PREFIX: e2e_
|
||||
LOG_LEVEL: TRACE
|
||||
TRUSTED_PROXIES: 192.0.2.0/24
|
||||
@@ -221,6 +226,7 @@ services:
|
||||
DB_USER: webui_fuzzer
|
||||
DB_PASSWORD: webui-fuzzer-e2e-pass
|
||||
DB_NAME: e2e_fuzzer
|
||||
DB_WEBUI_NAME: e2e_fuzzer_webui
|
||||
LOG_LEVEL: TRACE
|
||||
|
||||
app-operator:
|
||||
@@ -231,6 +237,7 @@ services:
|
||||
DB_USER: webui_operator
|
||||
DB_PASSWORD: webui-operator-e2e-pass
|
||||
DB_NAME: e2e_operator
|
||||
DB_WEBUI_NAME: e2e_operator_webui
|
||||
|
||||
app-a11y:
|
||||
<<: *app
|
||||
@@ -240,6 +247,7 @@ services:
|
||||
DB_USER: webui_a11y
|
||||
DB_PASSWORD: webui-a11y-e2e-pass
|
||||
DB_NAME: e2e_a11y
|
||||
DB_WEBUI_NAME: e2e_a11y_webui
|
||||
|
||||
runner:
|
||||
image: ${E2E_RUNNER_IMAGE}
|
||||
|
||||
+10
-4
@@ -24,6 +24,9 @@ export interface GroupConfig {
|
||||
service: string;
|
||||
database: string;
|
||||
prefix: string;
|
||||
webuiDatabase: string;
|
||||
webuiPrefix: string;
|
||||
webuiGrants: 'tables' | 'database';
|
||||
appUser: string;
|
||||
appPassword: string;
|
||||
dnsUser: string;
|
||||
@@ -42,6 +45,9 @@ function group(name: GroupName, dnsPort: number, extra: Partial<GroupConfig> = {
|
||||
service: `app-${name}`,
|
||||
database: `e2e_${name}`,
|
||||
prefix: 'coredns_',
|
||||
webuiDatabase: `e2e_${name}_webui`,
|
||||
webuiPrefix: 'coredns_',
|
||||
webuiGrants: 'tables',
|
||||
appUser: `webui_${name}`,
|
||||
appPassword: `webui-${name}-e2e-pass`,
|
||||
dnsUser: `dns_${name}`,
|
||||
@@ -57,12 +63,12 @@ function group(name: GroupName, dnsPort: number, extra: Partial<GroupConfig> = {
|
||||
}
|
||||
|
||||
export const GROUPS: Readonly<Record<GroupName, GroupConfig>> = {
|
||||
harness: group('harness', 5300),
|
||||
harness: group('harness', 5300, { webuiGrants: 'database' }),
|
||||
core: group('core', 5301),
|
||||
session: group('session', 5302, { idleTimeoutMs: 60_000, maxAgeMs: 180_000 }),
|
||||
session: group('session', 5302, { webuiDatabase: 'e2e_session', idleTimeoutMs: 60_000, maxAgeMs: 180_000 }),
|
||||
power: group('power', 5303),
|
||||
stupid: group('stupid', 5304),
|
||||
malicious: group('malicious', 5305, { prefix: 'e2e_', logLevel: 'TRACE', trustedProxies: '192.0.2.0/24' }),
|
||||
stupid: group('stupid', 5304, { webuiDatabase: 'e2e_stupid' }),
|
||||
malicious: group('malicious', 5305, { prefix: 'e2e_', webuiPrefix: 'mal_', logLevel: 'TRACE', trustedProxies: '192.0.2.0/24' }),
|
||||
fuzzer: group('fuzzer', 5306, { logLevel: 'TRACE' }),
|
||||
operator: group('operator', 5307),
|
||||
a11y: group('a11y', 5308),
|
||||
|
||||
+22
-12
@@ -63,6 +63,10 @@ export interface SessionRow {
|
||||
export type RawRecord = { zone: string; name: string; ttl: number | null; content: string | null; record_type: string };
|
||||
|
||||
const INIT_TEMPLATE = path.resolve(process.cwd(), 'stack', 'mysql-init.sql');
|
||||
const WEBUI_GRANT_TEMPLATES = {
|
||||
tables: path.resolve(process.cwd(), 'stack', 'mysql-webui-tables.sql'),
|
||||
database: path.resolve(process.cwd(), 'stack', 'mysql-webui-database.sql'),
|
||||
} as const;
|
||||
|
||||
function connectionOptions(database?: string): mysql.ConnectionOptions {
|
||||
return {
|
||||
@@ -85,11 +89,17 @@ function q(identifier: string): string {
|
||||
return '`' + identifier + '`';
|
||||
}
|
||||
|
||||
function qualified(schema: string, table: string): string {
|
||||
return `${q(schema)}.${q(table)}`;
|
||||
}
|
||||
|
||||
export function initSql(g: GroupConfig): string {
|
||||
const template = fs.readFileSync(INIT_TEMPLATE, 'utf8');
|
||||
const template = fs.readFileSync(INIT_TEMPLATE, 'utf8') + '\n' + fs.readFileSync(WEBUI_GRANT_TEMPLATES[g.webuiGrants], 'utf8');
|
||||
const values: Record<string, string> = {
|
||||
DATABASE: g.database,
|
||||
PREFIX: g.prefix,
|
||||
WEBUI_DATABASE: g.webuiDatabase,
|
||||
WEBUI_PREFIX: g.webuiPrefix,
|
||||
DNS_USER: g.dnsUser,
|
||||
DNS_PASSWORD: g.dnsPassword,
|
||||
APP_USER: g.appUser,
|
||||
@@ -101,7 +111,7 @@ export function initSql(g: GroupConfig): string {
|
||||
.join('\n')
|
||||
.replace(/\{\{([A-Z_]+)\}\}/g, (_m, k: string) => {
|
||||
const v = values[k];
|
||||
if (v === undefined || !/^[A-Za-z0-9_-]+$/.test(v)) throw new HarnessError('infrastructure', `bad template value ${k}`);
|
||||
if (v === undefined || !/^[A-Za-z0-9_-]*$/.test(v) || (v === '' && !k.endsWith('PREFIX'))) throw new HarnessError('infrastructure', `bad template value ${k}`);
|
||||
return v;
|
||||
});
|
||||
}
|
||||
@@ -140,14 +150,14 @@ export class Db {
|
||||
|
||||
constructor(readonly group: GroupConfig) {
|
||||
this.pool = mysql.createPool({ ...connectionOptions(group.database), connectionLimit: 4 });
|
||||
const p = group.prefix;
|
||||
const ui = (table: string) => qualified(group.webuiDatabase, `${group.webuiPrefix}webui_${table}`);
|
||||
this.t = {
|
||||
records: q(`${p}records`),
|
||||
users: q(`${p}webui_users`),
|
||||
sessions: q(`${p}webui_sessions`),
|
||||
delegations: q(`${p}webui_zone_delegations`),
|
||||
audit: q(`${p}webui_audit`),
|
||||
schema: q(`${p}webui_schema`),
|
||||
records: qualified(group.database, `${group.prefix}records`),
|
||||
users: ui('users'),
|
||||
sessions: ui('sessions'),
|
||||
delegations: ui('zone_delegations'),
|
||||
audit: ui('audit'),
|
||||
schema: ui('schema'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -289,9 +299,9 @@ export class Db {
|
||||
return Number(rows[0].n);
|
||||
}
|
||||
|
||||
async tableExists(name: string): Promise<boolean> {
|
||||
const rows = await this.query<{ n: number }>('SELECT COUNT(*) AS n FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?', [name]);
|
||||
return Number(rows[0].n) === 1;
|
||||
async tables(schema: string): Promise<string[]> {
|
||||
const rows = await this.query<{ name: string }>('SELECT TABLE_NAME AS name FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME', [schema]);
|
||||
return rows.map((r) => r.name);
|
||||
}
|
||||
|
||||
async snapshot(): Promise<string> {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
CREATE DATABASE `{{DATABASE}}` DEFAULT CHARSET = utf8mb4;
|
||||
|
||||
CREATE DATABASE IF NOT EXISTS `{{WEBUI_DATABASE}}` DEFAULT CHARSET = utf8mb4;
|
||||
|
||||
CREATE TABLE `{{DATABASE}}`.`{{PREFIX}}records` (
|
||||
`id` INT NOT NULL AUTO_INCREMENT,
|
||||
`zone` VARCHAR(255) NOT NULL,
|
||||
@@ -18,9 +20,3 @@ GRANT SELECT ON `{{DATABASE}}`.`{{PREFIX}}records` TO '{{DNS_USER}}'@'%';
|
||||
|
||||
CREATE USER '{{APP_USER}}'@'%' IDENTIFIED BY '{{APP_PASSWORD}}';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON `{{DATABASE}}`.`{{PREFIX}}records` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE ON `{{DATABASE}}`.`{{PREFIX}}webui_locks` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `{{DATABASE}}`.`{{PREFIX}}webui_schema` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `{{DATABASE}}`.`{{PREFIX}}webui_users` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `{{DATABASE}}`.`{{PREFIX}}webui_sessions` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `{{DATABASE}}`.`{{PREFIX}}webui_zone_delegations` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, CREATE, ALTER, INDEX, REFERENCES ON `{{DATABASE}}`.`{{PREFIX}}webui_audit` TO '{{APP_USER}}'@'%';
|
||||
@@ -0,0 +1,4 @@
|
||||
-- SPDX-License-Identifier: GPL-2.0-or-later
|
||||
-- Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
GRANT ALL PRIVILEGES ON `{{WEBUI_DATABASE}}`.* TO '{{APP_USER}}'@'%';
|
||||
@@ -0,0 +1,9 @@
|
||||
-- SPDX-License-Identifier: GPL-2.0-or-later
|
||||
-- Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_locks` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_schema` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, CREATE, ALTER, INDEX, REFERENCES ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_users` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_sessions` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, DELETE, CREATE, ALTER, INDEX, REFERENCES ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_zone_delegations` TO '{{APP_USER}}'@'%';
|
||||
GRANT SELECT, INSERT, CREATE, ALTER, INDEX, REFERENCES ON `{{WEBUI_DATABASE}}`.`{{WEBUI_PREFIX}}webui_audit` TO '{{APP_USER}}'@'%';
|
||||
+43
-11
@@ -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();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user