115 lines
5.2 KiB
JavaScript
115 lines
5.2 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Re-encrypts database ciphertext with the current ENCRYPTION_KEY.
|
|
* Set ENCRYPTION_KEY_PREVIOUS to the old key before running so decrypt() can read legacy rows.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.rotateAllEncryptedData = rotateAllEncryptedData;
|
|
const connection_1 = require("../db/connection");
|
|
const crypto_1 = require("../utils/crypto");
|
|
const hdWallet_1 = require("./hdWallet");
|
|
async function rotateUserSecrets() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, email_encrypted, phone_encrypted, totp_secret_encrypted, security_reason_encrypted
|
|
FROM users
|
|
WHERE email_encrypted IS NOT NULL
|
|
OR phone_encrypted IS NOT NULL
|
|
OR totp_secret_encrypted IS NOT NULL
|
|
OR security_reason_encrypted IS NOT NULL`);
|
|
let updated = 0;
|
|
for (const row of rows) {
|
|
const email = row.email_encrypted ? (0, crypto_1.reencrypt)(row.email_encrypted) : null;
|
|
const phone = row.phone_encrypted ? (0, crypto_1.reencrypt)(row.phone_encrypted) : null;
|
|
const totp = row.totp_secret_encrypted ? (0, crypto_1.reencrypt)(row.totp_secret_encrypted) : null;
|
|
const reason = (0, crypto_1.reencryptSensitive)(row.security_reason_encrypted);
|
|
await (0, connection_1.query)(`UPDATE users
|
|
SET email_encrypted = COALESCE($2, email_encrypted),
|
|
phone_encrypted = COALESCE($3, phone_encrypted),
|
|
totp_secret_encrypted = COALESCE($4, totp_secret_encrypted),
|
|
security_reason_encrypted = COALESCE($5, security_reason_encrypted)
|
|
WHERE id = $1`, [row.id, email, phone, totp, reason]);
|
|
updated++;
|
|
}
|
|
return updated;
|
|
}
|
|
async function rotateHotWallets() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, encrypted_private_key FROM hot_wallets`);
|
|
for (const row of rows) {
|
|
const plain = (0, hdWallet_1.decryptPrivateKey)(row.encrypted_private_key);
|
|
const encrypted = (0, hdWallet_1.encryptPrivateKey)(plain);
|
|
await (0, connection_1.query)(`UPDATE hot_wallets SET encrypted_private_key = $2 WHERE id = $1`, [
|
|
row.id,
|
|
encrypted,
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
async function rotateSecurityAlerts() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, details_encrypted, resolution_note_encrypted FROM security_alerts`);
|
|
for (const row of rows) {
|
|
await (0, connection_1.query)(`UPDATE security_alerts
|
|
SET details_encrypted = COALESCE($2, details_encrypted),
|
|
resolution_note_encrypted = COALESCE($3, resolution_note_encrypted)
|
|
WHERE id = $1`, [
|
|
row.id,
|
|
(0, crypto_1.reencryptSensitive)(row.details_encrypted),
|
|
(0, crypto_1.reencryptSensitive)(row.resolution_note_encrypted),
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
async function rotateAdminNotifications() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, body_encrypted FROM admin_notifications WHERE body_encrypted IS NOT NULL`);
|
|
for (const row of rows) {
|
|
await (0, connection_1.query)(`UPDATE admin_notifications SET body_encrypted = $2 WHERE id = $1`, [
|
|
row.id,
|
|
(0, crypto_1.reencryptSensitive)(row.body_encrypted),
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
async function rotateLedgerAdjustments() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, reason_encrypted FROM ledger_adjustments`);
|
|
for (const row of rows) {
|
|
await (0, connection_1.query)(`UPDATE ledger_adjustments SET reason_encrypted = $2 WHERE id = $1`, [
|
|
row.id,
|
|
(0, crypto_1.reencryptSensitive)(row.reason_encrypted) ?? row.reason_encrypted,
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
async function rotateFundOperationErrors() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, error_message_encrypted FROM fund_operation_requests WHERE error_message_encrypted IS NOT NULL`);
|
|
for (const row of rows) {
|
|
await (0, connection_1.query)(`UPDATE fund_operation_requests SET error_message_encrypted = $2 WHERE id = $1`, [
|
|
row.id,
|
|
(0, crypto_1.reencryptSensitive)(row.error_message_encrypted),
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
async function rotateExchangeFundingNotes() {
|
|
const rows = await (0, connection_1.query)(`SELECT id, notes_encrypted FROM exchange_funding WHERE notes_encrypted IS NOT NULL`);
|
|
for (const row of rows) {
|
|
await (0, connection_1.query)(`UPDATE exchange_funding SET notes_encrypted = $2 WHERE id = $1`, [
|
|
row.id,
|
|
(0, crypto_1.reencryptSensitive)(row.notes_encrypted),
|
|
]);
|
|
}
|
|
return rows.length;
|
|
}
|
|
/** Re-encrypts all application-managed secrets with ENCRYPTION_KEY. */
|
|
async function rotateAllEncryptedData() {
|
|
if (!process.env.ENCRYPTION_KEY) {
|
|
throw new Error('ENCRYPTION_KEY is required');
|
|
}
|
|
return {
|
|
users: await rotateUserSecrets(),
|
|
hotWallets: await rotateHotWallets(),
|
|
securityAlerts: await rotateSecurityAlerts(),
|
|
adminNotifications: await rotateAdminNotifications(),
|
|
ledgerAdjustments: await rotateLedgerAdjustments(),
|
|
fundOperationErrors: await rotateFundOperationErrors(),
|
|
exchangeFundingNotes: await rotateExchangeFundingNotes(),
|
|
};
|
|
}
|
|
//# sourceMappingURL=encryptionRotationService.js.map
|