Created
September 12, 2018 19:54
-
-
Save lucasklaassen/739181e621dcb187a1d0e08c82e8d9c6 to your computer and use it in GitHub Desktop.
Encrypt/Decrypt Angular 6 Secrets Committed To Source Control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You must create a folder in your root directory named environments/ | |
// Add the following lines to your .gitignore | |
// /src/environments/environment.ts | |
// /src/environments/*.development.ts | |
// /src/environments/*.production.ts | |
// /src/environments/*.staging.ts | |
// Under src/environments add your environment.ts files like you normally do. | |
// Make sure to run `npm install dotenv --save` and create a .env file | |
// Add the following to your .env file: ENVIRONMENT_ENCRYPTION_PASSWORD=yourpasswordhere | |
// in package.json add the following scripts | |
// "encrypt:secrets": "node ./scripts/encryption.js encrypt", | |
// "decrypt:secrets": "node ./scripts/encryption.js decrypt", | |
// "start": "node ./scripts/encryption.js decrypt && ng serve", | |
// "build": "node ./scripts/encryption.js decrypt && ng build --prod" | |
// To encrypt your secrets before committing to source control run `npm run encrypt:secrets` | |
// Running npm start or npm build will decrypt your secrets from your environments/ folder into src/environments | |
require('dotenv').config(); | |
const args = process.argv.slice(2); | |
const { exec } = require('child_process'); | |
const crypto = (type, input, output) => { | |
exec(`openssl aes-256-cbc ${type} -in ${input} -out ${output} -k ${process.env.ENVIRONMENT_ENCRYPTION_PASSWORD}`, (err) => { | |
if (err) { | |
console.log('Error while running encryption:', err); | |
return; | |
} | |
}); | |
} | |
switch (args[0]) { | |
case 'decrypt': | |
crypto('-d', 'environments/environment.ts.encrypted', 'src/environments/environment.ts'); | |
crypto('-d', 'environments/environment.development.ts.encrypted', 'src/environments/environment.development.ts'); | |
crypto('-d', 'environments/environment.staging.ts.encrypted', 'src/environments/environment.staging.ts'); | |
crypto('-d', 'environments/environment.production.ts.encrypted', 'src/environments/environment.production.ts'); | |
break; | |
case 'encrypt': | |
crypto('-e', 'src/environments/environment.ts', 'environments/environment.ts.encrypted'); | |
crypto('-e', 'src/environments/environment.development.ts', 'environments/environment.development.ts.encrypted'); | |
crypto('-e', 'src/environments/environment.staging.ts', 'environments/environment.staging.ts.encrypted'); | |
crypto('-e', 'src/environments/environment.production.ts', 'environments/environment.production.ts.encrypted'); | |
break; | |
default: | |
console.log('select either decrypt, encrypt or decryptFor'); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment