Add support for multiple encryption keys in data handling
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 1m1s
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 1m1s
This commit introduces a mechanism to handle multiple possible encryption keys for data decryption across various modules, including auth.js, members.js, newsletter.js, and encryption.js. It adds functions to retrieve potential old keys for migration purposes and updates the decryption logic to attempt decryption with these keys. Additionally, it includes warnings for users when old keys are used and provides guidance for re-encrypting data. This enhancement improves data migration capabilities and ensures backward compatibility with previously encrypted data.
This commit is contained in:
@@ -52,6 +52,30 @@ function getEncryptionKey() {
|
||||
return process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
|
||||
}
|
||||
|
||||
// Liste möglicher alter Verschlüsselungsschlüssel (für Migration)
|
||||
function getPossibleEncryptionKeys() {
|
||||
const currentKey = getEncryptionKey()
|
||||
const oldKeys = [
|
||||
'default-key-change-in-production',
|
||||
'local_development_encryption_key_change_in_production'
|
||||
]
|
||||
|
||||
// Aktueller Schlüssel zuerst, dann alte Schlüssel
|
||||
const keys = [currentKey]
|
||||
for (const oldKey of oldKeys) {
|
||||
if (oldKey !== currentKey) {
|
||||
keys.push(oldKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: Alter Schlüssel aus Environment-Variable
|
||||
if (process.env.OLD_ENCRYPTION_KEY && process.env.OLD_ENCRYPTION_KEY !== currentKey) {
|
||||
keys.push(process.env.OLD_ENCRYPTION_KEY)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Check if data is encrypted by trying to parse as JSON first
|
||||
function isEncrypted(data) {
|
||||
try {
|
||||
@@ -78,11 +102,47 @@ export async function readUsers() {
|
||||
let users = []
|
||||
|
||||
if (encrypted) {
|
||||
// Versuche mit verschiedenen Schlüsseln zu entschlüsseln (für Migration)
|
||||
const possibleKeys = getPossibleEncryptionKeys()
|
||||
const encryptionKey = getEncryptionKey()
|
||||
try {
|
||||
users = decryptObject(data, encryptionKey)
|
||||
} catch (decryptError) {
|
||||
console.error('Fehler beim Entschlüsseln der Benutzerdaten:', decryptError)
|
||||
|
||||
let lastError = null
|
||||
for (let i = 0; i < possibleKeys.length; i++) {
|
||||
const key = possibleKeys[i]
|
||||
try {
|
||||
users = decryptObject(data, key)
|
||||
|
||||
// Wenn mit altem Schlüssel entschlüsselt wurde, warnen und neu verschlüsseln
|
||||
if (i > 0 && key !== encryptionKey) {
|
||||
console.warn(`⚠️ Benutzerdaten wurden mit altem Schlüssel entschlüsselt. Automatische Neuverschlüsselung...`)
|
||||
try {
|
||||
await writeUsers(users)
|
||||
console.log('✅ Benutzerdaten erfolgreich mit neuem Schlüssel neu verschlüsselt')
|
||||
} catch (writeError) {
|
||||
console.error('❌ Fehler beim Neuverschlüsseln:', writeError.message)
|
||||
console.error(' Bitte führen Sie manuell aus: node scripts/re-encrypt-data.js')
|
||||
}
|
||||
}
|
||||
|
||||
break // Erfolgreich entschlüsselt
|
||||
} catch (decryptError) {
|
||||
lastError = decryptError
|
||||
// Versuche nächsten Schlüssel
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn alle Schlüssel fehlgeschlagen sind
|
||||
if (!users || users.length === 0) {
|
||||
console.error('Fehler beim Entschlüsseln der Benutzerdaten:')
|
||||
console.error(' Versuchte Schlüssel:', possibleKeys.length)
|
||||
console.error(' Letzter Fehler:', lastError?.message || 'Unbekannter Fehler')
|
||||
console.error('')
|
||||
console.error('💡 Lösung: Führen Sie das Re-Encrypt-Skript aus:')
|
||||
console.error(' node scripts/re-encrypt-data.js --old-key="<alter-schlüssel>"')
|
||||
console.error(' Oder setzen Sie OLD_ENCRYPTION_KEY als Environment-Variable')
|
||||
|
||||
// Fallback: try to read as plain JSON
|
||||
try {
|
||||
users = JSON.parse(data)
|
||||
console.warn('Entschlüsselung fehlgeschlagen, versuche als unverschlüsseltes Format zu lesen')
|
||||
|
||||
Reference in New Issue
Block a user