mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
fix(backup): refuse a Postgres restore the role or tools cannot run before anything is copied
The admin restore now answers a privileged role, old client tools or an unreachable server with a 400 naming the reason, before it keeps a pre-restore dump. RESTORE_FROM_BACKUP checks the same before it unpacks the archive, and removes the unpacked copy when the load fails.
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
answersSchema,
|
||||
answersHistoryRows,
|
||||
answersTables,
|
||||
failsWith,
|
||||
installFakeTool,
|
||||
listsMigrations,
|
||||
touches,
|
||||
@@ -142,15 +143,21 @@ describe('restoreOnFirstBoot', () => {
|
||||
* known migration as the dump's history, or writes a restricted script.
|
||||
* Their arguments are pinned in pg-tools.test.ts.
|
||||
*/
|
||||
const postgresPlan = (archivePath: string, tables: string[] = [], historyRows = 0) => {
|
||||
const postgresPlan = (
|
||||
archivePath: string,
|
||||
tables: string[] = [],
|
||||
historyRows = 0,
|
||||
server: Parameters<typeof answersSchema>[2] = {},
|
||||
load = touches(path.join(root, 'loaded')),
|
||||
) => {
|
||||
const bin = path.join(root, 'bin');
|
||||
fs.mkdirSync(bin, { recursive: true });
|
||||
installFakeTool(bin, 'psql', {
|
||||
snippets: [
|
||||
answersSchema(),
|
||||
answersSchema('public', 'public', server),
|
||||
answersTables(tables),
|
||||
answersHistoryRows(historyRows),
|
||||
touches(path.join(root, 'loaded')),
|
||||
load,
|
||||
],
|
||||
});
|
||||
const migration = [...knownMigrationNames()][0];
|
||||
@@ -200,6 +207,32 @@ describe('restoreOnFirstBoot', () => {
|
||||
expect(loaded()).toBe(false);
|
||||
});
|
||||
|
||||
const scratch = () => fs.readdirSync(dataDir).filter((f) => f.startsWith('restore-boot-'));
|
||||
|
||||
it('BOOT-RESTORE-004g: a restore the database refuses leaves neither the unpacked archive nor the reclaim marker', async () => {
|
||||
const plan = postgresPlan(
|
||||
await archive({ 'travel.pgdump': 'dump', 'uploads/a.jpg': 'jpeg' }),
|
||||
[],
|
||||
0,
|
||||
{},
|
||||
failsWith('psql: error: permission denied for schema public'),
|
||||
);
|
||||
await expect(restoreOnFirstBoot(plan)).rejects.toThrow('psql failed: psql: error: permission denied');
|
||||
expect(scratch()).toEqual([]);
|
||||
expect(fs.existsSync(reclaimResetFile)).toBe(false);
|
||||
});
|
||||
|
||||
it('BOOT-RESTORE-004h: a role that could reach the database host is refused before the archive is unpacked', async () => {
|
||||
const plan = postgresPlan(await archive({ 'travel.pgdump': 'dump' }), [], 0, { privileged: true });
|
||||
await expect(restoreOnFirstBoot(plan)).rejects.toThrow(
|
||||
/^RESTORE_FROM_BACKUP: TREK_DB_URL connects as a superuser/,
|
||||
);
|
||||
await expect(restoreOnFirstBoot(plan)).rejects.toBeInstanceOf(BootRestoreError);
|
||||
expect(loaded()).toBe(false);
|
||||
expect(scratch()).toEqual([]);
|
||||
expect(fs.existsSync(reclaimResetFile)).toBe(false);
|
||||
});
|
||||
|
||||
it('BOOT-RESTORE-004d: a SQLite backup is refused before anything is loaded, leaving no scratch', async () => {
|
||||
const plan = postgresPlan(await goodArchive());
|
||||
await expect(restoreOnFirstBoot(plan)).rejects.toThrow(
|
||||
|
||||
@@ -136,6 +136,26 @@ describe('PostgresDatabaseBackup', () => {
|
||||
else expect(refused.message).toMatch(message);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
id: 'PGBACKUP-002f',
|
||||
server: { privileged: true },
|
||||
message: /^TREK_DB_URL connects as a superuser or a member of a superuser role/,
|
||||
},
|
||||
{
|
||||
id: 'PGBACKUP-002g',
|
||||
server: { versionNum: '180001' },
|
||||
message: /^pg_restore is version 17, older than the PostgreSQL 18 server/,
|
||||
},
|
||||
])('$id: verify refuses what the restore itself would refuse, before a copy is kept', async ({ server, message }) => {
|
||||
writeFileSync(path.join(extract, 'travel.pgdump'), 'dump');
|
||||
installFakeTool(bin, 'pg_restore', { snippets: [listsMigrations(KNOWN.slice(0, 3))] });
|
||||
installFakeTool(bin, 'psql', { snippets: [answersSchema('public', 'public', server)] });
|
||||
const refused = await refusal(backup.verify(extract));
|
||||
expect(refused.status).toBe(400);
|
||||
expect(refused.message).toMatch(message);
|
||||
});
|
||||
|
||||
it('PGBACKUP-003: keeps a dump of the database a restore replaces, or says it could not', async () => {
|
||||
vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
||||
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
|
||||
Reference in New Issue
Block a user