Skip to content

Commit c58d2e4

Browse files
committed
fix(root): generate encryption envs in setup-env-files script
1 parent 817de1a commit c58d2e4

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

scripts/setup-env-files.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1+
const crypto = require('crypto');
12
const fs = require('fs');
23
const path = require('path');
34

4-
const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env') => {
5+
const ENCRYPTION_KEY_PLACEHOLDER = '<ENCRYPTION_KEY_MUST_BE_32_LONG>';
6+
7+
const generateEncryptionKey = () => crypto.randomBytes(16).toString('hex');
8+
9+
const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env', sharedEncryptionKey) => {
510
console.log(`Pre-populating .env files from .example.env for [${apps.join(',')}]`);
611
for (const folder of apps) {
7-
const exists = fs.existsSync(path.resolve(`${folderBasePath}/${folder}/${envFilePath}`));
12+
const destPath = path.resolve(`${folderBasePath}/${folder}/${envFilePath}`);
13+
const exists = fs.existsSync(destPath);
814
if (!exists) {
915
console.log(`Populating ${folderBasePath}/${folder} with .env file`);
10-
fs.copyFileSync(
11-
path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`),
12-
path.resolve(`${folderBasePath}/${folder}/${envFilePath}`)
13-
);
16+
const sourcePath = path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`);
17+
let content = fs.readFileSync(sourcePath, 'utf8');
18+
if (content.includes(ENCRYPTION_KEY_PLACEHOLDER) && sharedEncryptionKey) {
19+
content = content.replace(ENCRYPTION_KEY_PLACEHOLDER, sharedEncryptionKey);
20+
console.log(` Generated STORE_ENCRYPTION_KEY for ${folder}`);
21+
}
22+
fs.writeFileSync(destPath, content);
1423
}
1524
}
1625
};
1726

1827
(async () => {
1928
const appsBasePath = `${__dirname}/../apps`;
29+
const sharedEncryptionKey = generateEncryptionKey();
2030
console.log('----------------------------------------');
21-
prePopulateEnv(['api', 'ws', 'worker'], appsBasePath);
22-
prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env');
31+
prePopulateEnv(['api', 'ws', 'worker'], appsBasePath, 'src/.example.env', 'src/.env', sharedEncryptionKey);
32+
prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env', sharedEncryptionKey);
2333
console.log('Finished populating .env files');
2434
console.log('----------------------------------------');
2535
})();

0 commit comments

Comments
 (0)