add generate key script
This commit is contained in:
parent
6ac670a25b
commit
8a4efca141
|
@ -1,2 +1,4 @@
|
|||
APP_PORT=8000
|
||||
APP_DEBUG=true
|
||||
APP_DEBUG=true
|
||||
# a 256-bit base64 encryption key
|
||||
APP_KEY=TKe8lE2IdkgGBUrB4nxdq7mGMf8PK29xqOnGa3vU0PBmNXADJrVA5LKd8pg6g/YO5aFG/ESzUleo/9Hve3SAe4rvwLBejD/SKOmDR4gbaMv4PuiNi8S2sYL30aVyi1OeaSTyYsfjteumkFxFVwrsxhDCX94xvNEuTEfS4repfLo=
|
|
@ -2,18 +2,20 @@ const crypto = require('crypto');
|
|||
const path = require('path');
|
||||
const args = require('args-parser')(process.argv);
|
||||
const fs = require('fs');
|
||||
const base64 = require('js-base64');
|
||||
|
||||
let key = crypto.randomBytes(256).toString('base64');
|
||||
let dotenv = path.resolve('.env');
|
||||
|
||||
if (args['help']) {
|
||||
console.log(path.basename(__filename) + ' [--key-only] [--env|-e] [--set-key] [--dry-run]');
|
||||
console.log(path.basename(__filename) + ' [--key-only] [--env|-e] [--set-key] [--dry-run] [--stdout|-s]');
|
||||
console.log('\n' +
|
||||
' --help: Display this help\n' +
|
||||
' --key-only: Generate key only and put it in stdout\n' +
|
||||
' --env -e: Specify an env file\n' +
|
||||
' --set-key: Specify your key\n' +
|
||||
' --dry-run: Don\'t write anything, just do the thing'
|
||||
' --dry-run: Don\'t write anything, just do the thing\n' +
|
||||
' --stdout -s: Don\'t write to file, write to stdout instead'
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -41,6 +43,10 @@ for (let i = 0; i != lines.length; i++) {
|
|||
|
||||
const newfile = lines.join('\n');
|
||||
|
||||
const w_stdout = args['stdout'] || args['s'];
|
||||
|
||||
if (!args['dry-run']) {
|
||||
fs.writeFileSync(dotenv, newfile);
|
||||
if (w_stdout) console.log(newfile);
|
||||
else
|
||||
fs.writeFileSync(dotenv, newfile);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "scripts",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "generate_key.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"args-parser": "^1.3.0"
|
||||
}
|
||||
}
|
11
startup.js
11
startup.js
|
@ -5,6 +5,8 @@ if (process.env.APP_DEBUG) {
|
|||
}
|
||||
|
||||
const fs = require('fs');
|
||||
const {Base64} = require('js-base64');
|
||||
const crc32 = require('crc-32');
|
||||
|
||||
const hrt = () => {
|
||||
let hr = process.hrtime();
|
||||
|
@ -22,6 +24,15 @@ async function startup() {
|
|||
path: dotpath
|
||||
});
|
||||
|
||||
if (!process.env.APP_KEY) {
|
||||
throw new Error('APP_KEY is not set.')
|
||||
}
|
||||
process.env.APP_KEY = Base64.decode(process.env.APP_KEY);
|
||||
if (process.env.APP_KEY.length !== 256) {
|
||||
throw new Error('APP_KEY has to be a 256-byte base64 string.');
|
||||
}
|
||||
console.log('Using a key with CRC32: ' + crc32.bstr(process.env.APP_KEY.toString(16)));
|
||||
|
||||
await require('./helpers').ViewLoader.preload();
|
||||
console.log('Views compiled in ' + (hrt() - t1) + ' ms');
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
const test = require('unit.js');
|
||||
const execSync = require('child_process').execSync;
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
describe('TestS generate key script', () => {
|
||||
|
||||
it('Check if key is generated properly', () => {
|
||||
const stdout = execSync('node ./scripts/generate_key.js --key-only').toString('utf-8');
|
||||
const key = Buffer.from(stdout, 'base64');
|
||||
test.number(key.length).is(256);
|
||||
});
|
||||
it('Check if file is edited properly', () => {
|
||||
const stdout = execSync('node ./scripts/generate_key.js -s').toString('utf-8');
|
||||
const file = fs.readFileSync(path.resolve('.env'));
|
||||
test.string(stdout).isNot(file);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue