mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
test(db): the key rotation on Postgres, and its refusals before a key is asked
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* PGROT-001..004: the encryption key rotation (`scripts/migrate-encryption.ts`)
|
||||
* on the lane's Postgres database, through the same rotation the command runs:
|
||||
* every kind of store is re-encrypted under the new key in one transaction
|
||||
* that takes the engine's write lock, the `jsonb` addon config included; a
|
||||
* second run finds everything on the new key and changes nothing; a value
|
||||
* neither key opens is reported while the rest commits; and a statement that
|
||||
* fails rolls the whole rotation back.
|
||||
*
|
||||
* The command around it (prompts, the pg_dump copy, the key file) is covered
|
||||
* in the SQLite lane (`tests/unit/db/maintenance-scripts-postgres.test.ts`):
|
||||
* the copy needs a pg_dump at least as new as the server, which the lane's
|
||||
* runner does not have for every major it tests.
|
||||
*/
|
||||
import { rotateOnPostgres } from '../../scripts/migrate-encryption';
|
||||
import { buildOrmConfig, resolveEngine, type DatabaseEngine } from '../../src/db/engine';
|
||||
import { Users } from '../../src/db/entities/Users.entity';
|
||||
import { updateRows } from '../helpers/factories/rows';
|
||||
import { readAppSetting, readUserSetting, setAppSetting, setUserSetting } from '../helpers/factories/settings';
|
||||
import { makeUser } from '../helpers/factories/users';
|
||||
import { MikroORM, type EntityManager } from '@mikro-orm/core';
|
||||
|
||||
import crypto from 'node:crypto';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
const OLD_KEY = 'pg-rotation-old-key';
|
||||
const NEW_KEY = 'pg-rotation-new-key';
|
||||
const ENCRYPTED_PREFIX = 'enc:v1:';
|
||||
|
||||
const apiKeyFor = (key: string) => crypto.createHash('sha256').update(`${key}:api_keys:v1`).digest();
|
||||
const mfaKeyFor = (key: string) => crypto.createHash('sha256').update(`${key}:mfa:v1`).digest();
|
||||
|
||||
function seal(derived: Buffer, plain: string): Buffer {
|
||||
const iv = crypto.randomBytes(12);
|
||||
const cipher = crypto.createCipheriv('aes-256-gcm', derived, iv);
|
||||
const enc = Buffer.concat([cipher.update(plain, 'utf8'), cipher.final()]);
|
||||
return Buffer.concat([iv, cipher.getAuthTag(), enc]);
|
||||
}
|
||||
|
||||
function open(derived: Buffer, sealed: Buffer): string | null {
|
||||
try {
|
||||
const decipher = crypto.createDecipheriv('aes-256-gcm', derived, sealed.subarray(0, 12));
|
||||
decipher.setAuthTag(sealed.subarray(12, 28));
|
||||
return Buffer.concat([decipher.update(sealed.subarray(28)), decipher.final()]).toString('utf8');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const encryptApi = (key: string, plain: string) =>
|
||||
`${ENCRYPTED_PREFIX}${seal(apiKeyFor(key), plain).toString('base64')}`;
|
||||
const decryptApi = (key: string, value: string) =>
|
||||
value.startsWith(ENCRYPTED_PREFIX)
|
||||
? open(apiKeyFor(key), Buffer.from(value.slice(ENCRYPTED_PREFIX.length), 'base64'))
|
||||
: null;
|
||||
const encryptMfa = (key: string, plain: string) => seal(mfaKeyFor(key), plain).toString('base64');
|
||||
const decryptMfa = (key: string, value: string) => open(mfaKeyFor(key), Buffer.from(value, 'base64'));
|
||||
|
||||
let orm: MikroORM;
|
||||
let engine: DatabaseEngine;
|
||||
|
||||
/** Plain SQL on the lane's database, outside any transaction. */
|
||||
function sql<T>(text: string, params: unknown[] = []): Promise<T[]> {
|
||||
return orm.em.getConnection().execute(text, params, 'all') as Promise<T[]>;
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
engine = resolveEngine();
|
||||
orm = await MikroORM.init({ ...buildOrmConfig(engine), pool: { min: 0, max: 2 } });
|
||||
}, 60_000);
|
||||
|
||||
afterAll(async () => {
|
||||
await orm?.close(true);
|
||||
});
|
||||
|
||||
describe('migrate-encryption.ts on Postgres', () => {
|
||||
let userId: number;
|
||||
|
||||
it('PGROT-001: re-encrypts every kind of store under the new key, under the write lock', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
userId = user.id;
|
||||
await updateRows(
|
||||
orm,
|
||||
Users,
|
||||
{ id: userId },
|
||||
{
|
||||
maps_api_key: encryptApi(OLD_KEY, 'maps-secret'),
|
||||
mfa_secret: encryptMfa(OLD_KEY, 'JBSWY3DPEHPK3PXP'),
|
||||
},
|
||||
);
|
||||
await setAppSetting(orm, 'smtp_pass', encryptApi(OLD_KEY, 'smtp-secret'));
|
||||
await setAppSetting(
|
||||
orm,
|
||||
'storage.backends',
|
||||
JSON.stringify([{ name: 'bucket', type: 's3', options: { secretAccessKey: encryptApi(OLD_KEY, 's3-secret') } }]),
|
||||
);
|
||||
await setUserSetting(orm, userId, 'llm_api_key', encryptApi(OLD_KEY, 'llm-user-secret'));
|
||||
await sql('INSERT INTO addons (id, name, config) VALUES (?, ?, ?)', [
|
||||
'pgrot-addon',
|
||||
'Rotation addon',
|
||||
JSON.stringify({ apiKey: encryptApi(OLD_KEY, 'addon-secret'), model: 'kept' }),
|
||||
]);
|
||||
await sql('INSERT INTO plugins (id, name, config) VALUES (?, ?, ?)', [
|
||||
'pgrot-plugin',
|
||||
'Rotation plugin',
|
||||
JSON.stringify({ token: encryptApi(OLD_KEY, 'plugin-secret'), plain: 'kept' }),
|
||||
]);
|
||||
await sql('INSERT INTO plugin_settings_fields (plugin_id, field_key, secret, scope) VALUES (?, ?, 1, ?)', [
|
||||
'pgrot-plugin',
|
||||
'token',
|
||||
'instance',
|
||||
]);
|
||||
|
||||
const taken: EntityManager[] = [];
|
||||
const locking: Pick<DatabaseEngine, 'serializeWrites'> = {
|
||||
async serializeWrites(em) {
|
||||
taken.push(em);
|
||||
await engine.serializeWrites(em);
|
||||
// The advisory lock is held by the rotation's own transaction.
|
||||
const held = (await em
|
||||
.getConnection()
|
||||
.execute(
|
||||
"SELECT count(*)::int AS n FROM pg_locks WHERE locktype = 'advisory' AND pid = pg_backend_pid()",
|
||||
[],
|
||||
'all',
|
||||
em.getTransactionContext(),
|
||||
)) as { n: number }[];
|
||||
expect(held[0].n).toBe(1);
|
||||
},
|
||||
};
|
||||
|
||||
const result = await rotateOnPostgres(orm, locking, OLD_KEY, NEW_KEY);
|
||||
|
||||
expect(taken).toHaveLength(1);
|
||||
expect(result.errors).toEqual([]);
|
||||
expect(result.migrated).toBe(7);
|
||||
|
||||
const [userRow] = await sql<{ maps_api_key: string; mfa_secret: string }>(
|
||||
'SELECT maps_api_key, mfa_secret FROM users WHERE id = ?',
|
||||
[userId],
|
||||
);
|
||||
expect(decryptApi(NEW_KEY, userRow.maps_api_key)).toBe('maps-secret');
|
||||
expect(decryptMfa(NEW_KEY, userRow.mfa_secret)).toBe('JBSWY3DPEHPK3PXP');
|
||||
expect(decryptApi(NEW_KEY, (await readAppSetting(orm, 'smtp_pass')) as string)).toBe('smtp-secret');
|
||||
const backends = JSON.parse((await readAppSetting(orm, 'storage.backends')) as string) as {
|
||||
options: { secretAccessKey: string };
|
||||
}[];
|
||||
expect(decryptApi(NEW_KEY, backends[0].options.secretAccessKey)).toBe('s3-secret');
|
||||
expect(decryptApi(NEW_KEY, (await readUserSetting(orm, userId, 'llm_api_key')) as string)).toBe('llm-user-secret');
|
||||
|
||||
// jsonb on Postgres: read back parsed, written back as JSON, the other fields kept.
|
||||
const [addon] = await sql<{ config: { apiKey: string; model: string } }>('SELECT config FROM addons WHERE id = ?', [
|
||||
'pgrot-addon',
|
||||
]);
|
||||
expect(addon.config.model).toBe('kept');
|
||||
expect(decryptApi(NEW_KEY, addon.config.apiKey)).toBe('addon-secret');
|
||||
|
||||
const [plugin] = await sql<{ config: string }>('SELECT config FROM plugins WHERE id = ?', ['pgrot-plugin']);
|
||||
const pluginConfig = JSON.parse(plugin.config) as { token: string; plain: string };
|
||||
expect(pluginConfig.plain).toBe('kept');
|
||||
expect(decryptApi(NEW_KEY, pluginConfig.token)).toBe('plugin-secret');
|
||||
});
|
||||
|
||||
it('PGROT-002: a second run finds every value on the new key and changes nothing', async () => {
|
||||
const before = await sql<{ value: string }>("SELECT value FROM app_settings WHERE key = 'smtp_pass'");
|
||||
const result = await rotateOnPostgres(orm, engine, OLD_KEY, NEW_KEY);
|
||||
expect(result.migrated).toBe(0);
|
||||
expect(result.alreadyMigrated).toBe(7);
|
||||
expect(result.errors).toEqual([]);
|
||||
const after = await sql<{ value: string }>("SELECT value FROM app_settings WHERE key = 'smtp_pass'");
|
||||
expect(after).toEqual(before);
|
||||
});
|
||||
|
||||
it('PGROT-003: reports a value neither key opens and commits the rest', async () => {
|
||||
await setAppSetting(orm, 'admin_ntfy_token', encryptApi('a-third-key', 'unknown'));
|
||||
await setUserSetting(orm, userId, 'ntfy_token', encryptApi(NEW_KEY, 'next'));
|
||||
await setUserSetting(orm, userId, 'webhook_url', encryptApi(OLD_KEY, 'https://hook.example'));
|
||||
|
||||
const result = await rotateOnPostgres(orm, engine, OLD_KEY, NEW_KEY);
|
||||
|
||||
expect(result.errors).toEqual(['app_settings.admin_ntfy_token: decryption failed with both keys']);
|
||||
expect(result.migrated).toBe(1);
|
||||
expect(decryptApi(NEW_KEY, (await readUserSetting(orm, userId, 'webhook_url')) as string)).toBe(
|
||||
'https://hook.example',
|
||||
);
|
||||
});
|
||||
|
||||
it('PGROT-004: a statement that fails rolls the whole rotation back', async () => {
|
||||
await setAppSetting(orm, 'smtp_pass', encryptApi(OLD_KEY, 'smtp-again'));
|
||||
await setAppSetting(orm, 'admin_ntfy_token', encryptApi(OLD_KEY, 'ntfy-again'));
|
||||
const before = await sql<{ key: string; value: string }>(
|
||||
"SELECT key, value FROM app_settings WHERE key IN ('smtp_pass', 'admin_ntfy_token') ORDER BY key",
|
||||
);
|
||||
|
||||
// Renamed inside the rotation's own transaction, users.id makes the users
|
||||
// block fail after the app_settings rows were already updated there.
|
||||
const broken: Pick<DatabaseEngine, 'serializeWrites'> = {
|
||||
async serializeWrites(em) {
|
||||
await engine.serializeWrites(em);
|
||||
await em
|
||||
.getConnection()
|
||||
.execute('ALTER TABLE users RENAME COLUMN id TO id_renamed_for_test', [], 'run', em.getTransactionContext());
|
||||
},
|
||||
};
|
||||
|
||||
await expect(rotateOnPostgres(orm, broken, OLD_KEY, NEW_KEY)).rejects.toThrow();
|
||||
|
||||
const after = await sql<{ key: string; value: string }>(
|
||||
"SELECT key, value FROM app_settings WHERE key IN ('smtp_pass', 'admin_ntfy_token') ORDER BY key",
|
||||
);
|
||||
expect(after).toEqual(before);
|
||||
const columns = await sql<{ column_name: string }>(
|
||||
"SELECT column_name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = 'users' AND column_name = 'id'",
|
||||
);
|
||||
expect(columns).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -1,10 +1,11 @@
|
||||
/**
|
||||
* reset-admin.js and migrate-encryption.ts on an install whose database is
|
||||
* Postgres (TREK_DB_URL): neither opens or creates a SQLite file there. The
|
||||
* key rotation refuses outright, since it cannot re-encrypt the secrets in
|
||||
* Postgres yet; the reset refuses a TREK_DB_FILE beside the URL, as the server
|
||||
* does. The reset itself on Postgres goes through the built server's engine,
|
||||
* which these SQLite-lane tests do not have.
|
||||
* Postgres (TREK_DB_URL): neither opens or creates a SQLite file there, and
|
||||
* both refuse a SQLite path beside the URL. The key rotation checks that it
|
||||
* reaches the database before it asks for a key. The rotation itself on
|
||||
* Postgres is tests/postgres/migrate-encryption.test.ts; the reset on Postgres
|
||||
* goes through the built server's engine, which these SQLite-lane tests do
|
||||
* not have.
|
||||
*/
|
||||
import { execFileSync } from 'child_process';
|
||||
import fs from 'fs';
|
||||
@@ -13,7 +14,7 @@ import path from 'path';
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
const SERVER_ROOT = path.resolve(__dirname, '../../..');
|
||||
/** Never reached: both scripts stop before they connect. */
|
||||
/** Nothing listens there: the scripts stop before they connect, or fail to. */
|
||||
const PG_URL = 'postgres://trek:secret@127.0.0.1:1/trek?sslmode=disable';
|
||||
|
||||
let tmpDir: string;
|
||||
@@ -26,24 +27,28 @@ afterEach(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function run(args: string[], env: Record<string, string>, input = ''): { status: number; stderr: string } {
|
||||
function run(
|
||||
args: string[],
|
||||
env: Record<string, string>,
|
||||
input = '',
|
||||
): { status: number; stdout: string; stderr: string } {
|
||||
try {
|
||||
execFileSync(process.execPath, args, {
|
||||
const stdout = execFileSync(process.execPath, args, {
|
||||
cwd: SERVER_ROOT,
|
||||
env: { ...process.env, ...env },
|
||||
input,
|
||||
stdio: 'pipe',
|
||||
encoding: 'utf8',
|
||||
});
|
||||
return { status: 0, stderr: '' };
|
||||
return { status: 0, stdout, stderr: '' };
|
||||
} catch (err) {
|
||||
const e = err as { status: number; stderr: string };
|
||||
return { status: e.status, stderr: e.stderr };
|
||||
const e = err as { status: number; stdout: string; stderr: string };
|
||||
return { status: e.status, stdout: e.stdout, stderr: e.stderr };
|
||||
}
|
||||
}
|
||||
|
||||
describe('maintenance scripts with TREK_DB_URL', () => {
|
||||
it('PGSCRIPTS-001: migrate-encryption.ts refuses before it asks for a key, and touches no file', () => {
|
||||
it('PGSCRIPTS-001: migrate-encryption.ts refuses DB_PATH beside TREK_DB_URL before it asks for a key, and touches no file', () => {
|
||||
const dbPath = path.join(tmpDir, 'travel.db');
|
||||
const r = run(
|
||||
['--import', 'tsx', 'scripts/migrate-encryption.ts'],
|
||||
@@ -51,11 +56,24 @@ describe('maintenance scripts with TREK_DB_URL', () => {
|
||||
'old-key\nnew-key\nyes\n',
|
||||
);
|
||||
expect(r.status).toBe(1);
|
||||
expect(r.stderr).toContain('TREK_DB_URL is set');
|
||||
expect(r.stderr).toContain('Nothing was changed');
|
||||
expect(r.stdout).not.toContain('Old ENCRYPTION_KEY');
|
||||
expect(r.stderr).toContain('TREK_DB_URL and DB_PATH are both set');
|
||||
expect(r.stderr).toContain('nothing was changed');
|
||||
expect(fs.readdirSync(tmpDir)).toEqual([]);
|
||||
}, 60000);
|
||||
|
||||
it('PGSCRIPTS-003: migrate-encryption.ts stops before it asks for a key when the database cannot be reached', () => {
|
||||
const r = run(
|
||||
['--import', 'tsx', 'scripts/migrate-encryption.ts'],
|
||||
{ TREK_DB_URL: PG_URL, DB_PATH: '', TREK_DB_FILE: '' },
|
||||
'old-key\nnew-key\nyes\n',
|
||||
);
|
||||
expect(r.status).toBe(1);
|
||||
expect(r.stdout).not.toContain('Old ENCRYPTION_KEY');
|
||||
expect(r.stderr).toMatch(/Could not reach the database TREK_DB_URL names|Cannot write to/);
|
||||
expect(r.stdout).not.toContain('Backup created');
|
||||
}, 60000);
|
||||
|
||||
it('PGSCRIPTS-002: reset-admin.js refuses TREK_DB_FILE beside TREK_DB_URL and creates no SQLite file', () => {
|
||||
const dbPath = path.join(tmpDir, 'travel.db');
|
||||
const r = run(['reset-admin.js'], { TREK_DB_URL: PG_URL, TREK_DB_FILE: dbPath, RESET_ADMIN_PASSWORD: 'x' });
|
||||
|
||||
Reference in New Issue
Block a user