102 lines
4.3 KiB
JavaScript
102 lines
4.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Daily reminder when ENCRYPTION_KEY has not been rotated within the configured window.
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.checkEncryptionKeyRotationDue = checkEncryptionKeyRotationDue;
|
|
exports.recordEncryptionKeyRotatedAt = recordEncryptionKeyRotatedAt;
|
|
exports.scheduleEncryptionKeyRotationReminder = scheduleEncryptionKeyRotationReminder;
|
|
const connection_1 = require("../db/connection");
|
|
const adminNotificationRepository = __importStar(require("../repositories/adminNotificationRepository"));
|
|
const DEFAULT_REMIND_DAYS = 90;
|
|
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
const SETTING_KEY = 'encryption_key_rotated_at';
|
|
function remindDays() {
|
|
const raw = process.env.ENCRYPTION_KEY_ROTATION_REMIND_DAYS;
|
|
if (!raw?.trim())
|
|
return DEFAULT_REMIND_DAYS;
|
|
const n = parseInt(raw, 10);
|
|
return Number.isFinite(n) && n > 0 ? n : DEFAULT_REMIND_DAYS;
|
|
}
|
|
async function readLastRotatedAt() {
|
|
const rows = await (0, connection_1.query)(`SELECT value FROM system_settings WHERE key = $1 LIMIT 1`, [SETTING_KEY]);
|
|
const raw = rows[0]?.value?.trim();
|
|
if (!raw)
|
|
return null;
|
|
const d = new Date(raw);
|
|
return Number.isNaN(d.getTime()) ? null : d;
|
|
}
|
|
async function checkEncryptionKeyRotationDue() {
|
|
const maxAgeDays = remindDays();
|
|
const lastRotated = await readLastRotatedAt();
|
|
const reference = lastRotated ?? new Date(0);
|
|
const ageMs = Date.now() - reference.getTime();
|
|
const ageDays = ageMs / (24 * 60 * 60 * 1000);
|
|
if (ageDays < maxAgeDays)
|
|
return;
|
|
const message = lastRotated
|
|
? `ENCRYPTION_KEY was last rotated on ${lastRotated.toISOString()} (${Math.floor(ageDays)} days ago).`
|
|
: 'ENCRYPTION_KEY rotation date is not recorded. Schedule a rotation per deploy/ENCRYPTION_KEY_ROTATION.md.';
|
|
console.warn(`[encryption-rotation] Reminder: ${message}`);
|
|
try {
|
|
await adminNotificationRepository.createNotification({
|
|
severity: 'medium',
|
|
title: 'Encryption key rotation due',
|
|
body: message,
|
|
});
|
|
}
|
|
catch (e) {
|
|
console.error('[encryption-rotation] Failed to create admin notification:', e);
|
|
}
|
|
}
|
|
/** Records successful rotation timestamp (called by rotateEncryptionKey script). */
|
|
async function recordEncryptionKeyRotatedAt(at = new Date()) {
|
|
await (0, connection_1.query)(`INSERT INTO system_settings (key, value, updated_at) VALUES ($1, $2, NOW())
|
|
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`, [SETTING_KEY, at.toISOString()]);
|
|
}
|
|
function scheduleEncryptionKeyRotationReminder() {
|
|
const run = async () => {
|
|
try {
|
|
await checkEncryptionKeyRotationDue();
|
|
}
|
|
catch (e) {
|
|
console.error('[encryption-rotation] Reminder check failed:', e);
|
|
}
|
|
setTimeout(run, CHECK_INTERVAL_MS);
|
|
};
|
|
setTimeout(run, 120000);
|
|
}
|
|
//# sourceMappingURL=encryptionRotationReminderService.js.map
|