UNPKG

1.35 kBJavaScriptView Raw
1var AWS = require( "aws-sdk" );
2
3const utils = require( "./utils" );
4
5function decrypt ( varName, externalDecrypt ) {
6
7 return new Promise( ( resolve, reject ) => {
8
9 function callDecryptMethod ( params, cb, externalEncrypt ) {
10 if ( externalDecrypt ) {
11 externalDecrypt( params, cb);
12 } else {
13 let kms = new AWS.KMS();
14 kms.decrypt( params, cb );
15 }
16 }
17
18 const params = {
19 "CiphertextBlob": new Buffer( utils.getProjectConfig().environmentVariables[ varName ], "base64" )
20 };
21
22 callDecryptMethod( params, ( err, data ) => {
23 if (err) {
24 reject( err );
25 } else {
26 resolve( data.Plaintext.toString() );
27 }
28 })
29
30 });
31
32}
33
34function commandLineArgsAreValid ( varName ) {
35
36 return ( varName !== null ) && ( utils.getProjectConfig().environmentVariables[ varName ] !== undefined );
37
38}
39
40function init ( varName ) {
41
42 utils.authenticate().then( () => {
43
44 if ( commandLineArgsAreValid( varName ) ) {
45
46 const projectConfig = utils.getProjectConfig();
47 AWS.config.region = projectConfig.region;
48 decrypt( varName )
49 .then( console.log )
50 .catch( console.log );
51
52 } else {
53
54 console.log( "ERROR: no variable defined. Please use the `--name` option to state a var that matches an environment variable in the colly.json file to decrypt" );
55
56 }
57
58 });
59
60}
61
62module.exports = {
63 "decrypt": decrypt,
64 "init": init
65}
\No newline at end of file