mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
fix(scripts): rotate the SQLite file TREK_DB_FILE names
The key rotation now opens the database the server runs on when TREK_DB_FILE is set, writes the new key to the key file the server reads, and refuses DB_PATH and TREK_DB_FILE naming different files.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
* appear in shell history, process arguments, or log output.
|
||||
*
|
||||
* It works on the database the server runs on. Without TREK_DB_URL that is the
|
||||
* SQLite file (DB_PATH, or data/travel.db), opened with better-sqlite3. With
|
||||
* SQLite file (DB_PATH, TREK_DB_FILE or data/travel.db), opened with better-sqlite3. With
|
||||
* TREK_DB_URL it is the Postgres database, reached through the server's own
|
||||
* engine (its pool, SSL settings and write lock): from src/ in a source
|
||||
* checkout, from the built dist/ in the image. Either way the rotation is one
|
||||
@@ -875,12 +875,22 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve DB path
|
||||
const dbPath = path.resolve(process.env.DB_PATH ?? path.join(__dirname, '../data/travel.db'));
|
||||
// The file the server opens: TREK_DB_FILE, else data/travel.db. DB_PATH names it for this script alone.
|
||||
const dataDir = path.join(__dirname, '../data');
|
||||
const scriptDbPath = process.env.DB_PATH?.trim();
|
||||
const serverDbFile = process.env.TREK_DB_FILE?.trim();
|
||||
if (scriptDbPath && serverDbFile && path.resolve(scriptDbPath) !== path.resolve(serverDbFile)) {
|
||||
rl.close();
|
||||
console.error(
|
||||
'DB_PATH and TREK_DB_FILE name different files, so it is not clear which database is meant; nothing was changed.',
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
const dbPath = path.resolve(scriptDbPath || serverDbFile || path.join(dataDir, 'travel.db'));
|
||||
|
||||
if (!fs.existsSync(dbPath)) {
|
||||
console.error(`Database not found at: ${dbPath}`);
|
||||
console.error('Set DB_PATH env var if your database is in a non-standard location.');
|
||||
console.error('Set TREK_DB_FILE (or DB_PATH) if your database is in a non-standard location.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -948,13 +958,13 @@ async function main() {
|
||||
const result = await rotateOnSqlite(db, oldKey, newKey);
|
||||
db.close();
|
||||
|
||||
// The key file next to the database.
|
||||
// The key file next to a DB_PATH database, else the one the server reads in its data directory.
|
||||
finish(
|
||||
result,
|
||||
backupPath,
|
||||
path.join(path.dirname(dbPath), '.encryption_key'),
|
||||
path.join(scriptDbPath ? path.dirname(dbPath) : dataDir, '.encryption_key'),
|
||||
newKey,
|
||||
'No key file next to the database. Store the new key wherever TREK reads it from (ENCRYPTION_KEY or data/.encryption_key).',
|
||||
'No key file found. Store the new key wherever TREK reads it from (ENCRYPTION_KEY or data/.encryption_key).',
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -496,3 +496,56 @@ describe('migrate-encryption.ts rotates the stores the app writes', () => {
|
||||
expect(fs.readFileSync(keyFile, 'utf8')).toBe('new-key');
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
describe('migrate-encryption.ts picks the SQLite file the server opens', () => {
|
||||
let tmpDir: string;
|
||||
let dbPath: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-migrate-encryption-file-'));
|
||||
dbPath = path.join(tmpDir, 'trek.db');
|
||||
const seed = new Database(dbPath);
|
||||
seed.exec(BASE_SCHEMA);
|
||||
seed.close();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
/** The script with `env` over the test's environment, minus both database variables; answers no at the prompt. */
|
||||
function rotate(env: Record<string, string>): { status: number; output: string } {
|
||||
const base = { ...process.env };
|
||||
delete base.DB_PATH;
|
||||
delete base.TREK_DB_FILE;
|
||||
delete base.TREK_DB_URL;
|
||||
try {
|
||||
const stdout = execFileSync(process.execPath, ['--import', 'tsx', 'scripts/migrate-encryption.ts'], {
|
||||
cwd: SERVER_ROOT,
|
||||
env: { ...base, ...env },
|
||||
input: ['old-key', 'new-key', 'no', ''].join(String.fromCharCode(10)),
|
||||
stdio: 'pipe',
|
||||
encoding: 'utf8',
|
||||
});
|
||||
return { status: 0, output: stdout };
|
||||
} catch (err) {
|
||||
const e = err as { status: number; stdout?: string; stderr?: string };
|
||||
return { status: e.status, output: `${e.stdout ?? ''}${e.stderr ?? ''}` };
|
||||
}
|
||||
}
|
||||
|
||||
it('ROTPAR-018: works on the file TREK_DB_FILE names when DB_PATH is not set', () => {
|
||||
const r = rotate({ TREK_DB_FILE: dbPath });
|
||||
expect(r.output).toContain(`Database: ${dbPath}`);
|
||||
expect(r.output).toContain('Aborted.');
|
||||
expect(r.status).toBe(0);
|
||||
}, 30000);
|
||||
|
||||
it('ROTPAR-019: refuses DB_PATH and TREK_DB_FILE naming different files before it asks for a key', () => {
|
||||
const r = rotate({ TREK_DB_FILE: dbPath, DB_PATH: path.join(tmpDir, 'other.db') });
|
||||
expect(r.status).toBe(1);
|
||||
expect(r.output).toContain('DB_PATH and TREK_DB_FILE name different files');
|
||||
expect(r.output).not.toContain('Old ENCRYPTION_KEY');
|
||||
expect(fs.readdirSync(tmpDir)).toEqual(['trek.db']);
|
||||
}, 30000);
|
||||
});
|
||||
|
||||
@@ -58,7 +58,7 @@ To find your current key: check the `ENCRYPTION_KEY` environment variable or rea
|
||||
|
||||
Use `scripts/migrate-encryption.ts` to re-encrypt the stored secrets listed below without downtime or manual re-entry, on SQLite and on Postgres alike.
|
||||
|
||||
The script works on the database TREK runs on. Without `TREK_DB_URL` that is the SQLite file (`DB_PATH`, default `data/travel.db`). On a [Postgres](Database-Postgres) install (`TREK_DB_URL` set) it is the Postgres database, reached with the server's own connection settings (SSL included); no `travel.db` is opened, created or backed up then, and the script refuses to run when `DB_PATH` or `TREK_DB_FILE` is set beside the URL, since it would not be clear which database is meant.
|
||||
The script works on the database TREK runs on. Without `TREK_DB_URL` that is the SQLite file the server opens (`TREK_DB_FILE`, default `data/travel.db`; `DB_PATH` names another file for the script alone, and the script refuses to run when it names a different file than `TREK_DB_FILE`). On a [Postgres](Database-Postgres) install (`TREK_DB_URL` set) it is the Postgres database, reached with the server's own connection settings (SSL included); no `travel.db` is opened, created or backed up then, and the script refuses to run when `DB_PATH` or `TREK_DB_FILE` is set beside the URL, since it would not be clear which database is meant.
|
||||
|
||||
**Docker:**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user