games_app/backend/src/services/encryptionRotationService.ts
2026-07-07 15:03:17 +02:00

161 lines
5.4 KiB
TypeScript

/**
* 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.
*/
import { query } from '../db/connection';
import { reencrypt, reencryptSensitive } from '../utils/crypto';
import { decryptPrivateKey, encryptPrivateKey } from './hdWallet';
export type RotationStats = {
users: number;
hotWallets: number;
securityAlerts: number;
adminNotifications: number;
ledgerAdjustments: number;
fundOperationErrors: number;
exchangeFundingNotes: number;
};
async function rotateUserSecrets(): Promise<number> {
const rows = await query<
{
id: string;
email_encrypted: string | null;
phone_encrypted: string | null;
totp_secret_encrypted: string | null;
security_reason_encrypted: string | null;
}[]
>(
`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 ? reencrypt(row.email_encrypted) : null;
const phone = row.phone_encrypted ? reencrypt(row.phone_encrypted) : null;
const totp = row.totp_secret_encrypted ? reencrypt(row.totp_secret_encrypted) : null;
const reason = reencryptSensitive(row.security_reason_encrypted);
await 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(): Promise<number> {
const rows = await query<{ id: string; encrypted_private_key: string }[]>(
`SELECT id, encrypted_private_key FROM hot_wallets`
);
for (const row of rows) {
const plain = decryptPrivateKey(row.encrypted_private_key);
const encrypted = encryptPrivateKey(plain);
await query(`UPDATE hot_wallets SET encrypted_private_key = $2 WHERE id = $1`, [
row.id,
encrypted,
]);
}
return rows.length;
}
async function rotateSecurityAlerts(): Promise<number> {
const rows = await query<
{ id: string; details_encrypted: string | null; resolution_note_encrypted: string | null }[]
>(`SELECT id, details_encrypted, resolution_note_encrypted FROM security_alerts`);
for (const row of rows) {
await query(
`UPDATE security_alerts
SET details_encrypted = COALESCE($2, details_encrypted),
resolution_note_encrypted = COALESCE($3, resolution_note_encrypted)
WHERE id = $1`,
[
row.id,
reencryptSensitive(row.details_encrypted),
reencryptSensitive(row.resolution_note_encrypted),
]
);
}
return rows.length;
}
async function rotateAdminNotifications(): Promise<number> {
const rows = await query<{ id: string; body_encrypted: string | null }[]>(
`SELECT id, body_encrypted FROM admin_notifications WHERE body_encrypted IS NOT NULL`
);
for (const row of rows) {
await query(`UPDATE admin_notifications SET body_encrypted = $2 WHERE id = $1`, [
row.id,
reencryptSensitive(row.body_encrypted),
]);
}
return rows.length;
}
async function rotateLedgerAdjustments(): Promise<number> {
const rows = await query<{ id: string; reason_encrypted: string }[]>(
`SELECT id, reason_encrypted FROM ledger_adjustments`
);
for (const row of rows) {
await query(`UPDATE ledger_adjustments SET reason_encrypted = $2 WHERE id = $1`, [
row.id,
reencryptSensitive(row.reason_encrypted) ?? row.reason_encrypted,
]);
}
return rows.length;
}
async function rotateFundOperationErrors(): Promise<number> {
const rows = await query<{ id: string; error_message_encrypted: string | null }[]>(
`SELECT id, error_message_encrypted FROM fund_operation_requests WHERE error_message_encrypted IS NOT NULL`
);
for (const row of rows) {
await query(`UPDATE fund_operation_requests SET error_message_encrypted = $2 WHERE id = $1`, [
row.id,
reencryptSensitive(row.error_message_encrypted),
]);
}
return rows.length;
}
async function rotateExchangeFundingNotes(): Promise<number> {
const rows = await query<{ id: string; notes_encrypted: string | null }[]>(
`SELECT id, notes_encrypted FROM exchange_funding WHERE notes_encrypted IS NOT NULL`
);
for (const row of rows) {
await query(`UPDATE exchange_funding SET notes_encrypted = $2 WHERE id = $1`, [
row.id,
reencryptSensitive(row.notes_encrypted),
]);
}
return rows.length;
}
/** Re-encrypts all application-managed secrets with ENCRYPTION_KEY. */
export async function rotateAllEncryptedData(): Promise<RotationStats> {
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(),
};
}