Files
wekan/tests/continuousBackupRestartRestore.test.cjs
T
Lauri Ojansivu 130af564c9 Docker image: distroless, with a Node.js entrypoint and no shell.
Artifact Hub's Trivy report of v13.30 listed 218 vulnerabilities in
ghcr.io/wekan/wekan, 214 of them in Debian packages WeKan never runs
(util-linux, login, tar, coreutils, systemd libraries, sqlite) with no
Debian fix to update to. The image is now gcr.io/distroless/cc-debian13
plus libatomic1, node, WeKan, FerretDB and the MongoDB tools; that base
scans at 55 findings, 2 HIGH. Distroless publishes no linux/386, so for
386 the same Debian 13 packages are assembled into scratch, each with
its dpkg record; the release workflow reads both bases from the
Dockerfile and the image keeps all seven platforms. The wekan user
stays 999:999.

With no shell, releases/ferretdb/wekan-entrypoint.mjs replaces
wekan-entrypoint.sh, with the same decisions, exported and tested. It
runs sqlite-recovery.mjs with its own node: the shell entrypoint ran
/build/node, which the image never had, so every start with the bundled
FerretDB stopped with status 127 since v11.52. It also makes cpu-exec's
qemu-user decision itself. The UCS migration launcher and tools, the
UCS hook's volume preparation (ucs-release.py), the MongoDB tool
launchers (symlinks to mongo-tools) and the Nextcloud capability check
run node instead of sh.

Thanks to xet7 !
2026-11-25 00:32:06 +02:00

83 lines
5.3 KiB
JavaScript

'use strict';
// A continuous-backup SQLite restore applied on the next restart (maintainer
// decision of 2026-10-03): server/lib/continuousBackup/restore.js stages a
// checked wekan.sqlite in <db>/continuous-restore and writes RESTORE_REQUESTED
// "continuous"; each startup script, before FerretDB opens its files, keeps
// the live database in continuous-restore/replaced and copies the staged one
// in. This runs each script's REAL restore block in a sandbox directory.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const { stageSqliteRestore } = require('../server/lib/continuousBackup/restore');
const root = path.join(__dirname, '..');
const SCRIPTS = [
// The Docker image is distroless: its entrypoint is Node.js, and its own
// requestedRestore() is what runs.
['releases/ferretdb/wekan-entrypoint.mjs', 'FERRETDB_SQLITE_DIR'],
['releases/ferretdb/start-wekan.sh', 'FERRETDB_SQLITE_DIR'],
['snap-src/bin/ferretdb-control', 'SQLITE_DIR'],
];
const sandbox = () => fs.mkdtempSync(path.join(process.env.TMPDIR || path.join(root, '.tools', 'tmp'), 'restart-restore-'));
// The script's own lines, from reading the request to the end of the restore.
function restoreBlock(rel) {
const lines = fs.readFileSync(path.join(root, rel), 'utf8').split('\n');
const start = lines.findIndex(line => /_restore_mode="\$\{WEKAN_FORCE_RESTORE:-\}"/.test(line));
const end = lines.findIndex((line, i) => i > start && /#6492 safety/.test(line));
assert.ok(start > 0 && end > start, `${rel}: restore block not found`);
return lines.slice(start, end).join('\n');
}
async function run(rel, variable, dir, env = {}) {
if (rel.endsWith('.mjs')) {
const { requestedRestore } = await import(require('node:url').pathToFileURL(path.join(root, rel)).href);
const quiet = () => {};
return requestedRestore(dir, env, quiet, quiet);
}
return execFileSync('sh', ['-c', `${restoreBlock(rel)}\n`], { env: { PATH: process.env.PATH, [variable]: dir, ...env },
encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
}
const events = dir => fs.readFileSync(path.join(dir, 'recovery-events.jsonl'), 'utf8').trim().split('\n').map(line => JSON.parse(line));
for (const [rel, variable] of SCRIPTS) {
test(`${rel}: a staged continuous restore replaces the live database, keeping it first`, async t => {
const dir = sandbox(); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
fs.writeFileSync(path.join(dir, 'wekan.sqlite'), 'live database');
fs.writeFileSync(path.join(dir, 'wekan.sqlite-wal'), 'live wal');
const built = path.join(dir, 'built.sqlite'); fs.writeFileSync(built, 'restored database');
await stageSqliteRestore({ file: built, database: 'wekan', sqliteDir: dir });
assert.equal(fs.readFileSync(path.join(dir, 'RESTORE_REQUESTED'), 'utf8'), 'continuous\n');
await run(rel, variable, dir);
assert.equal(fs.readFileSync(path.join(dir, 'wekan.sqlite'), 'utf8'), 'restored database');
assert.equal(fs.existsSync(path.join(dir, 'wekan.sqlite-wal')), false, 'a stale WAL never meets the restored file');
assert.equal(fs.readFileSync(path.join(dir, 'continuous-restore', 'replaced', 'wekan.sqlite'), 'utf8'), 'live database');
assert.equal(fs.readFileSync(path.join(dir, 'continuous-restore', 'replaced', 'wekan.sqlite-wal'), 'utf8'), 'live wal');
assert.equal(fs.existsSync(path.join(dir, 'RESTORE_REQUESTED')), false);
assert.equal(events(dir).at(-1).type, 'restore-continuous');
});
test(`${rel}: NEGATIVE - a continuous request with nothing staged changes nothing and stays`, async t => {
const dir = sandbox(); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
fs.writeFileSync(path.join(dir, 'wekan.sqlite'), 'live database');
fs.writeFileSync(path.join(dir, 'RESTORE_REQUESTED'), 'continuous\n');
try { await run(rel, variable, dir); } catch (error) { /* the scripts may report on stderr */ }
assert.equal(fs.readFileSync(path.join(dir, 'wekan.sqlite'), 'utf8'), 'live database');
assert.equal(fs.existsSync(path.join(dir, 'RESTORE_REQUESTED')), true, 'the request is kept for a retry');
assert.equal(events(dir).at(-1).type, 'manual-required');
});
}
test('NEGATIVE staging: only wekan, an unknown directory, or a pending request are refused', async t => {
const dir = sandbox(); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const built = path.join(dir, 'built.sqlite'); fs.writeFileSync(built, 'x');
await assert.rejects(stageSqliteRestore({ file: built, database: 'local', sqliteDir: dir }), /Only the wekan database/);
await assert.rejects(stageSqliteRestore({ file: built, database: 'wekan', sqliteDir: null }), /unknown/);
await assert.rejects(stageSqliteRestore({ file: built, database: 'wekan', sqliteDir: 'relative' }), /unknown/);
fs.writeFileSync(path.join(dir, 'RESTORE_REQUESTED'), 'backup\n');
await assert.rejects(stageSqliteRestore({ file: built, database: 'wekan', sqliteDir: dir }), /already requested/);
assert.equal(fs.readFileSync(path.join(dir, 'RESTORE_REQUESTED'), 'utf8'), 'backup\n', 'another request is never overwritten');
assert.equal(fs.existsSync(path.join(dir, 'continuous-restore')), false);
});