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:
@@ -23,6 +23,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
|
||||
}
|
||||
|
||||
// Prüft ob Daten verschlüsselt sind
|
||||
function isEncrypted(data) {
|
||||
try {
|
||||
@@ -45,20 +69,54 @@ export async function readSubscribers() {
|
||||
const encrypted = isEncrypted(data)
|
||||
|
||||
if (encrypted) {
|
||||
// Versuche mit verschiedenen Schlüsseln zu entschlüsseln (für Migration)
|
||||
const possibleKeys = getPossibleEncryptionKeys()
|
||||
const encryptionKey = getEncryptionKey()
|
||||
try {
|
||||
return decryptObject(data, encryptionKey)
|
||||
} catch (decryptError) {
|
||||
console.error('Fehler beim Entschlüsseln der Newsletter-Abonnenten:', decryptError)
|
||||
|
||||
let lastError = null
|
||||
for (let i = 0; i < possibleKeys.length; i++) {
|
||||
const key = possibleKeys[i]
|
||||
try {
|
||||
const plainData = JSON.parse(data)
|
||||
console.warn('Entschlüsselung fehlgeschlagen, versuche als unverschlüsseltes Format zu lesen')
|
||||
return plainData
|
||||
} catch (_parseError) {
|
||||
console.error('Konnte Newsletter-Abonnenten weder entschlüsseln noch als JSON lesen')
|
||||
return []
|
||||
const decrypted = decryptObject(data, key)
|
||||
|
||||
// Wenn mit altem Schlüssel entschlüsselt wurde, warnen und neu verschlüsseln
|
||||
if (i > 0 && key !== encryptionKey) {
|
||||
console.warn(`⚠️ Newsletter-Abonnenten wurden mit altem Schlüssel entschlüsselt. Automatische Neuverschlüsselung...`)
|
||||
try {
|
||||
await writeSubscribers(decrypted)
|
||||
console.log('✅ Newsletter-Abonnenten 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')
|
||||
}
|
||||
}
|
||||
|
||||
return decrypted
|
||||
} catch (decryptError) {
|
||||
lastError = decryptError
|
||||
// Versuche nächsten Schlüssel
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Alle Schlüssel fehlgeschlagen
|
||||
console.error('Fehler beim Entschlüsseln der Newsletter-Abonnenten:')
|
||||
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 {
|
||||
const plainData = JSON.parse(data)
|
||||
console.warn('Entschlüsselung fehlgeschlagen, versuche als unverschlüsseltes Format zu lesen')
|
||||
return plainData
|
||||
} catch (_parseError) {
|
||||
console.error('Konnte Newsletter-Abonnenten weder entschlüsseln noch als JSON lesen')
|
||||
return []
|
||||
}
|
||||
} else {
|
||||
// Plain JSON - migriere zu verschlüsselter Speicherung
|
||||
const subscribers = JSON.parse(data)
|
||||
|
||||
Reference in New Issue
Block a user