|
| 1 | +/** |
| 2 | + * Use this command to automatically migrate the auth secret: curl -sSL https://dokploy.com/security/0.29.3.sh | bash |
| 3 | + * Migration script: re-encrypt 2FA secrets after rotating BETTER_AUTH_SECRET. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * OLD_SECRET=<old_secret> NEW_SECRET=<new_secret> npx tsx apps/dokploy/scripts/migrate-auth-secret.ts |
| 7 | + * |
| 8 | + * Both OLD_SECRET and NEW_SECRET are required. |
| 9 | + * Run this BEFORE restarting Dokploy with the new secret. |
| 10 | + */ |
| 11 | +import { db } from "@dokploy/server/db"; |
| 12 | +import { twoFactor } from "@dokploy/server/db/schema"; |
| 13 | +import { symmetricDecrypt, symmetricEncrypt } from "better-auth/crypto"; |
| 14 | +import { eq } from "drizzle-orm"; |
| 15 | + |
| 16 | +const OLD_SECRET = process.env.OLD_SECRET as string; |
| 17 | +const NEW_SECRET = process.env.NEW_SECRET as string; |
| 18 | + |
| 19 | +if (!OLD_SECRET || !NEW_SECRET) { |
| 20 | + console.error( |
| 21 | + "❌ OLD_SECRET and NEW_SECRET environment variables are required.", |
| 22 | + ); |
| 23 | + console.error( |
| 24 | + " Usage: OLD_SECRET=<old> NEW_SECRET=<new> npx tsx apps/dokploy/scripts/migrate-auth-secret.ts", |
| 25 | + ); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +if (OLD_SECRET === NEW_SECRET) { |
| 30 | + console.error("❌ OLD_SECRET and NEW_SECRET must be different."); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +async function reEncrypt( |
| 35 | + value: string, |
| 36 | + oldSecret: string, |
| 37 | + newSecret: string, |
| 38 | +): Promise<string> { |
| 39 | + const plaintext = await symmetricDecrypt({ key: oldSecret, data: value }); |
| 40 | + return symmetricEncrypt({ key: newSecret, data: plaintext }); |
| 41 | +} |
| 42 | + |
| 43 | +async function main() { |
| 44 | + console.log("🔍 Fetching 2FA records..."); |
| 45 | + const records = await db.select().from(twoFactor); |
| 46 | + |
| 47 | + if (records.length === 0) { |
| 48 | + console.log("✅ No 2FA records found, nothing to migrate."); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + console.log(`📦 Found ${records.length} 2FA record(s) to migrate.`); |
| 53 | + |
| 54 | + let migrated = 0; |
| 55 | + let failed = 0; |
| 56 | + |
| 57 | + await db.transaction(async (tx) => { |
| 58 | + for (const record of records) { |
| 59 | + try { |
| 60 | + const [newSecret, newBackupCodes] = await Promise.all([ |
| 61 | + reEncrypt(record.secret, OLD_SECRET, NEW_SECRET), |
| 62 | + reEncrypt(record.backupCodes, OLD_SECRET, NEW_SECRET), |
| 63 | + ]); |
| 64 | + |
| 65 | + await tx |
| 66 | + .update(twoFactor) |
| 67 | + .set({ secret: newSecret, backupCodes: newBackupCodes }) |
| 68 | + .where(eq(twoFactor.id, record.id)); |
| 69 | + |
| 70 | + migrated++; |
| 71 | + } catch (err) { |
| 72 | + console.error( |
| 73 | + `❌ Failed to migrate record ${record.id} (userId: ${record.userId}):`, |
| 74 | + err, |
| 75 | + ); |
| 76 | + failed++; |
| 77 | + throw err; // rollback the whole transaction |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + console.log(`✅ Migrated ${migrated} record(s) successfully.`); |
| 83 | + |
| 84 | + if (failed > 0) { |
| 85 | + console.error( |
| 86 | + `❌ ${failed} record(s) failed — transaction was rolled back.`, |
| 87 | + ); |
| 88 | + process.exit(1); |
| 89 | + } else { |
| 90 | + process.exit(0); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +main().catch((err) => { |
| 95 | + console.error("❌ Migration failed:", err); |
| 96 | + process.exit(1); |
| 97 | +}); |
0 commit comments