| import 'dotenv/config'; |
| import { PrismaClient } from '@prisma/client'; |
| import { encrypt } from '../../../packages/shared-types/src/crypto'; |
|
|
| |
| const prisma = new PrismaClient(); |
|
|
| async function migrateSecrets() { |
| const ENCRYPTION_SECRET = process.env.PROD_ENCRYPTION_SECRET || process.env.ENCRYPTION_SECRET; |
| |
| if (!ENCRYPTION_SECRET || ENCRYPTION_SECRET.length < 32) { |
| console.error('❌ ERREUR: Une clé ENCRYPTION_SECRET valide de 32 caractères minimum est requise.'); |
| process.exit(1); |
| } |
|
|
| console.log('🔍 Début de la migration des secrets...'); |
| const orgs = await prisma.organization.findMany(); |
| let updatedCount = 0; |
|
|
| for (const org of orgs) { |
| const dataToUpdate: any = {}; |
| |
| if (org.systemUserToken && !org.systemUserToken.startsWith('enc:')) { |
| dataToUpdate.systemUserToken = encrypt(org.systemUserToken, ENCRYPTION_SECRET); |
| } |
| if (org.webhookSecret && !org.webhookSecret.startsWith('enc:')) { |
| dataToUpdate.webhookSecret = encrypt(org.webhookSecret, ENCRYPTION_SECRET); |
| } |
| if (org.openAiApiKey && !org.openAiApiKey.startsWith('enc:')) { |
| dataToUpdate.openAiApiKey = encrypt(org.openAiApiKey, ENCRYPTION_SECRET); |
| } |
| if (org.googleAiApiKey && !org.googleAiApiKey.startsWith('enc:')) { |
| dataToUpdate.googleAiApiKey = encrypt(org.googleAiApiKey, ENCRYPTION_SECRET); |
| } |
|
|
| if (Object.keys(dataToUpdate).length > 0) { |
| await prisma.organization.update({ |
| where: { id: org.id }, |
| data: dataToUpdate |
| }); |
| console.log(`✅ Organisation sécurisée : ${org.name} (${org.id})`); |
| updatedCount++; |
| } |
| } |
| |
| console.log(`\n🎉 Migration terminée. ${updatedCount} organisation(s) mise(s) à jour.`); |
| await prisma.$disconnect(); |
| } |
|
|
| migrateSecrets().catch(console.error); |
|
|