Implement checks for existing encryption with the new key in re-encryption scripts. Add functionality to skip re-encryption if data is already encrypted, enhancing efficiency and preventing redundant operations.
This commit is contained in:
@@ -82,10 +82,25 @@ function isEncrypted(data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prüft ob Daten bereits mit dem neuen Schlüssel verschlüsselt sind
|
||||||
|
async function isEncryptedWithNewKey(encryptedData) {
|
||||||
|
try {
|
||||||
|
await decryptObject(encryptedData, NEW_KEY)
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Versucht mit verschiedenen Schlüsseln zu entschlüsseln
|
// Versucht mit verschiedenen Schlüsseln zu entschlüsseln
|
||||||
async function decryptWithFallback(encryptedData, keys) {
|
async function decryptWithFallback(encryptedData, keys) {
|
||||||
const errors = []
|
const errors = []
|
||||||
|
|
||||||
|
// Prüfe zuerst, ob die Daten bereits mit dem neuen Schlüssel verschlüsselt sind
|
||||||
|
if (await isEncryptedWithNewKey(encryptedData)) {
|
||||||
|
throw new Error('ALREADY_ENCRYPTED_WITH_NEW_KEY')
|
||||||
|
}
|
||||||
|
|
||||||
// encryptedData sollte bereits ein String sein (entweder direkt verschlüsselt oder aus einem JSON-Objekt extrahiert)
|
// encryptedData sollte bereits ein String sein (entweder direkt verschlüsselt oder aus einem JSON-Objekt extrahiert)
|
||||||
// Versuche mit jedem Schlüssel zu entschlüsseln
|
// Versuche mit jedem Schlüssel zu entschlüsseln
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
@@ -115,6 +130,12 @@ async function reencryptUsers(backupDir, oldKeys) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prüfe ob bereits mit neuem Schlüssel verschlüsselt
|
||||||
|
if (await isEncryptedWithNewKey(data)) {
|
||||||
|
console.log('ℹ️ users.json ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
console.log('🔄 Entschlüssele users.json...')
|
console.log('🔄 Entschlüssele users.json...')
|
||||||
const decrypted = await decryptWithFallback(data, oldKeys)
|
const decrypted = await decryptWithFallback(data, oldKeys)
|
||||||
|
|
||||||
@@ -129,6 +150,10 @@ async function reencryptUsers(backupDir, oldKeys) {
|
|||||||
console.log('ℹ️ users.json existiert nicht, überspringe...')
|
console.log('ℹ️ users.json existiert nicht, überspringe...')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (error.message === 'ALREADY_ENCRYPTED_WITH_NEW_KEY') {
|
||||||
|
console.log('ℹ️ users.json ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...')
|
||||||
|
return
|
||||||
|
}
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,6 +172,12 @@ async function reencryptMembers(backupDir, oldKeys) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prüfe ob bereits mit neuem Schlüssel verschlüsselt
|
||||||
|
if (await isEncryptedWithNewKey(data)) {
|
||||||
|
console.log('ℹ️ members.json ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
console.log('🔄 Entschlüssele members.json...')
|
console.log('🔄 Entschlüssele members.json...')
|
||||||
const decrypted = await decryptWithFallback(data, oldKeys)
|
const decrypted = await decryptWithFallback(data, oldKeys)
|
||||||
|
|
||||||
@@ -161,6 +192,10 @@ async function reencryptMembers(backupDir, oldKeys) {
|
|||||||
console.log('ℹ️ members.json existiert nicht, überspringe...')
|
console.log('ℹ️ members.json existiert nicht, überspringe...')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (error.message === 'ALREADY_ENCRYPTED_WITH_NEW_KEY') {
|
||||||
|
console.log('ℹ️ members.json ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...')
|
||||||
|
return
|
||||||
|
}
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,6 +232,11 @@ async function reencryptMembershipApplications(backupDir, oldKeys) {
|
|||||||
|
|
||||||
// Prüfe ob encryptedData Feld vorhanden ist
|
// Prüfe ob encryptedData Feld vorhanden ist
|
||||||
if (parsed.encryptedData) {
|
if (parsed.encryptedData) {
|
||||||
|
// Prüfe ob bereits mit neuem Schlüssel verschlüsselt
|
||||||
|
if (await isEncryptedWithNewKey(parsed.encryptedData)) {
|
||||||
|
console.log(`ℹ️ ${file} ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...`)
|
||||||
|
skipped++
|
||||||
|
} else {
|
||||||
console.log(`🔄 Entschlüssele ${file}...`)
|
console.log(`🔄 Entschlüssele ${file}...`)
|
||||||
// Nur das encryptedData Feld entschlüsseln
|
// Nur das encryptedData Feld entschlüsseln
|
||||||
const decrypted = await decryptWithFallback(parsed.encryptedData, oldKeys)
|
const decrypted = await decryptWithFallback(parsed.encryptedData, oldKeys)
|
||||||
@@ -208,8 +248,14 @@ async function reencryptMembershipApplications(backupDir, oldKeys) {
|
|||||||
await fs.writeFile(filePath, JSON.stringify(parsed, null, 2), 'utf-8')
|
await fs.writeFile(filePath, JSON.stringify(parsed, null, 2), 'utf-8')
|
||||||
console.log(`✅ ${file} erfolgreich neu verschlüsselt`)
|
console.log(`✅ ${file} erfolgreich neu verschlüsselt`)
|
||||||
processed++
|
processed++
|
||||||
|
}
|
||||||
} else if (file.endsWith('.data')) {
|
} else if (file.endsWith('.data')) {
|
||||||
// .data Dateien sind direkt verschlüsselt
|
// .data Dateien sind direkt verschlüsselt
|
||||||
|
// Prüfe ob bereits mit neuem Schlüssel verschlüsselt
|
||||||
|
if (await isEncryptedWithNewKey(content)) {
|
||||||
|
console.log(`ℹ️ ${file} ist bereits mit dem neuen Schlüssel verschlüsselt, überspringe...`)
|
||||||
|
skipped++
|
||||||
|
} else {
|
||||||
console.log(`🔄 Entschlüssele ${file}...`)
|
console.log(`🔄 Entschlüssele ${file}...`)
|
||||||
const decrypted = await decryptWithFallback(content, oldKeys)
|
const decrypted = await decryptWithFallback(content, oldKeys)
|
||||||
|
|
||||||
@@ -219,6 +265,7 @@ async function reencryptMembershipApplications(backupDir, oldKeys) {
|
|||||||
await fs.writeFile(filePath, reencrypted, 'utf-8')
|
await fs.writeFile(filePath, reencrypted, 'utf-8')
|
||||||
console.log(`✅ ${file} erfolgreich neu verschlüsselt`)
|
console.log(`✅ ${file} erfolgreich neu verschlüsselt`)
|
||||||
processed++
|
processed++
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(`ℹ️ ${file} enthält keine verschlüsselten Daten, überspringe...`)
|
console.log(`ℹ️ ${file} enthält keine verschlüsselten Daten, überspringe...`)
|
||||||
skipped++
|
skipped++
|
||||||
|
|||||||
Reference in New Issue
Block a user