Files
TREK/server/scripts/migrate-encryption-stores.ts
T
Maurice 96f2427518 fix(db): rotate the encryption key on Postgres through the server's engine
The rotation ran on the SQLite file only and refused when TREK_DB_URL was
set. Its statements now go through a small store per engine: better-sqlite3
for the file, unchanged, and on Postgres the server's own MikroORM
connection, in one transaction that takes the engine's write lock first. The
Postgres path checks that it reaches a database holding TREK's tables and
that the data directory is writable before it asks for a key, writes a
pg_dump copy before it changes anything, refuses DB_PATH or TREK_DB_FILE
beside the URL, and reads the jsonb addon config the driver hands back
parsed. The SQLite transaction is now BEGIN IMMEDIATE, since better-sqlite3
takes no async function.
2026-11-17 23:03:55 +01:00

105 lines
4.1 KiB
TypeScript

/**
* The database side of the encryption key rotation (`migrate-encryption.ts`):
* the statements it runs, on the SQLite file through better-sqlite3 or on a
* Postgres database through the server's MikroORM connection, and where the
* Postgres path finds the server's code. Kept beside the command so that one
* stays within the size limit; the image copies both.
*/
import type { EntityManager } from '@mikro-orm/core';
import type Database from 'better-sqlite3';
import fs from 'fs';
import path from 'path';
/** The parts of the server the Postgres path uses, loaded at run time (`loadServer`). */
export interface ServerModules {
engine: typeof import('../src/db/engine');
appConfig: typeof import('../src/app-config');
pgTools: typeof import('../src/db/engine/postgres/pg-tools');
}
/**
* The statements the rotation needs, on either engine. Every statement is
* written once, with `?` placeholders both engines take; only the catalog
* lookups differ. The rotation runs inside one transaction the caller opens.
*/
export interface SecretStore {
all<T>(sql: string, params?: unknown[]): Promise<T[]>;
get<T>(sql: string, params?: unknown[]): Promise<T | undefined>;
run(sql: string, params?: unknown[]): Promise<void>;
tableExists(name: string): Promise<boolean>;
columnsOf(table: string): Promise<Set<string>>;
}
/** The SQLite file through better-sqlite3, whose calls are synchronous; the store only wraps them. */
export function sqliteStore(db: Database.Database): SecretStore {
return {
async all<T>(sql: string, params: unknown[] = []) {
return db.prepare(sql).all(...params) as T[];
},
async get<T>(sql: string, params: unknown[] = []) {
return db.prepare(sql).get(...params) as T | undefined;
},
async run(sql: string, params: unknown[] = []) {
db.prepare(sql).run(...params);
},
async tableExists(name: string) {
return !!db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name);
},
async columnsOf(table: string) {
const rows = db.prepare('SELECT name FROM pragma_table_info(?)').all(table) as { name: string }[];
return new Set(rows.map((r) => r.name));
},
};
}
/**
* A Postgres database through the server's MikroORM connection, inside the
* transaction `em` holds when it is a transactional fork. The catalog lookups
* read the current schema, the one TREK keeps its tables in.
*/
export function postgresStore(em: EntityManager): SecretStore {
const connection = em.getConnection();
const ctx = em.getTransactionContext();
const all = async <T>(sql: string, params: unknown[] = []): Promise<T[]> =>
(await connection.execute(sql, params, 'all', ctx)) as T[];
return {
all,
async get<T>(sql: string, params: unknown[] = []) {
const [row] = await all<T>(sql, params);
return row;
},
async run(sql: string, params: unknown[] = []) {
await connection.execute(sql, params, 'run', ctx);
},
async tableExists(name: string) {
const [row] = await all<{ present: boolean }>('SELECT to_regclass(?) IS NOT NULL AS present', [name]);
return row?.present === true;
},
async columnsOf(table: string) {
const rows = await all<{ name: string }>(
'SELECT column_name AS name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ?',
[table],
);
return new Set(rows.map((r) => r.name));
},
};
}
/**
* The server's engine, app-config and Postgres tools: from src/ under tsx in a
* source checkout, so it runs without a build, else from the built dist/ the
* image ships (it has no src/).
*/
export function loadServer(): ServerModules {
const source = path.join(__dirname, '..', 'src');
const root = fs.existsSync(path.join(source, 'db', 'engine', 'index.ts'))
? source
: path.join(__dirname, '..', 'dist');
return {
engine: require(path.join(root, 'db', 'engine')) as ServerModules['engine'],
appConfig: require(path.join(root, 'app-config')) as ServerModules['appConfig'],
pgTools: require(path.join(root, 'db', 'engine', 'postgres', 'pg-tools')) as ServerModules['pgTools'],
};
}