fix(scripts): rotate the wanderer token with the encryption key

The key rotation now re-encrypts wanderer_connections.token, so a wanderer connection keeps working after the key changes instead of calling out without its token.
This commit is contained in:
Maurice
2026-11-23 14:59:21 +01:00
parent 9c454a04e1
commit 60885627b1
2 changed files with 47 additions and 0 deletions
+15
View File
@@ -562,6 +562,21 @@ async function rotateSecrets(store: SecretStore, oldKey: string, newKey: string)
}
}
// --- wanderer_connections: token ---
// One row per user, like dawarich_connections. Left out of a rotation, the
// connection still shows as connected but every call goes out without a token.
if (await store.tableExists('wanderer_connections')) {
const connections = await store.all<{ user_id: number; token: string }>(
'SELECT user_id, token FROM wanderer_connections WHERE token IS NOT NULL',
);
for (const row of connections) {
const newVal = migrateApiKeyValue(row.token, `wanderer_connections[${row.user_id}].token`);
if (newVal !== null) {
await store.run('UPDATE wanderer_connections SET token = ? WHERE user_id = ?', [newVal, row.user_id]);
}
}
}
// --- trek_photos: passphrase ---
const photos = await store.all<{ id: number; passphrase: string }>(
'SELECT id, passphrase FROM trek_photos WHERE passphrase IS NOT NULL',
@@ -431,6 +431,38 @@ describe('migrate-encryption.ts rotates the stores the app writes', () => {
expect(output).toContain('All secrets successfully re-encrypted.');
}, 30000);
// wanderer.service.ts stores the token with maybe_encrypt_api_key; missed by a
// rotation, the connection still reads as connected but calls go out without it.
it('ROTPAR-017: re-encrypts the wanderer token', () => {
const wanderer = fs.readFileSync(path.join(SERVER_ROOT, 'src', 'nest', 'wanderer', 'wanderer.service.ts'), 'utf8');
expect(wanderer).toContain('maybe_encrypt_api_key(typed)');
const seed = new Database(dbPath);
seed.exec(BASE_SCHEMA);
seed.exec('CREATE TABLE wanderer_connections (user_id INTEGER PRIMARY KEY, url TEXT NOT NULL, token TEXT)');
seed
.prepare('INSERT INTO wanderer_connections (user_id, url, token) VALUES (1, ?, ?)')
.run('https://wanderer.example', encryptWith('old-key', 'wanderer-token'));
seed.prepare("INSERT INTO wanderer_connections (user_id, url, token) VALUES (2, 'https://w2.example', NULL)").run();
seed.close();
execFileSync(process.execPath, ['--import', 'tsx', 'scripts/migrate-encryption.ts'], {
cwd: SERVER_ROOT,
env: { ...process.env, DB_PATH: dbPath },
input: ['old-key', 'new-key', 'yes', ''].join(String.fromCharCode(10)),
stdio: 'pipe',
encoding: 'utf8',
});
const after = new Database(dbPath, { readonly: true });
const rows = after.prepare('SELECT user_id, token FROM wanderer_connections ORDER BY user_id').all() as Array<{
user_id: number;
token: string | null;
}>;
after.close();
expect(decryptWith('new-key', rows[0].token!)).toBe('wanderer-token');
expect(rows[1].token).toBeNull();
}, 30000);
it('ROTPAR-013: re-encrypts the AI Parsing addon key and moves the key file to the new key', () => {
const seed = new Database(dbPath);
seed.exec(BASE_SCHEMA);