@@ -14,35 +14,42 @@ var args = require('minimist')(process.argv.slice(2));
14
14
var region = args . region ;
15
15
var input = args . input ;
16
16
17
+
18
+ let encrypt = function ( region , input , callback ) {
19
+ kmsCrypto . setRegion ( region ) ;
20
+
21
+ kmsCrypto . encrypt ( input , function ( err , encryptedCiphertext ) {
22
+ if ( err ) return callback ( err ) ;
23
+
24
+ kmsCrypto . decrypt ( encryptedCiphertext , function ( err , plaintext ) {
25
+ if ( err ) return callback ( err ) ;
26
+
27
+ if ( plaintext . toString ( ) === input ) {
28
+ console . log ( "Encryption completed and verified with AWS KMS" ) ;
29
+
30
+ callback ( {
31
+ inputValue : input ,
32
+ configurationEntryValue : kmsCrypto . toLambdaStringFormat ( encryptedCiphertext )
33
+ } ) ;
34
+ } else {
35
+ callback ( "Encryption completed but could not be validated. Result: " + plaintext . toString ( ) ) ;
36
+ }
37
+ } ) ;
38
+
39
+ } ) ;
40
+ }
41
+ exports . encrypt = encrypt ;
42
+
17
43
if ( ! region || ! input ) {
18
- console . log ( "You must provide a region for the KMS Service and an input value to Encrypt" ) ;
44
+ console . log ( "You must provide a region (--region) for the KMS Service and an input value (--input) to Encrypt" ) ;
19
45
process . exit ( ERROR ) ;
20
46
} else {
21
- kmsCrypto . setRegion ( region ) ;
22
-
23
- kmsCrypto . encrypt ( input , function ( err , encryptedCiphertext ) {
24
- if ( err ) {
25
- console . log ( err ) ;
26
- process . exit ( ERROR ) ;
27
- } else {
28
- kmsCrypto . decrypt ( encryptedCiphertext , function ( err , plaintext ) {
29
- if ( err ) {
30
- console . log ( err ) ;
31
- process . exit ( ERROR ) ;
32
- } else {
33
- if ( plaintext . toString ( ) === input ) {
34
- console . log ( "Encryption completed and verified with AWS KMS" ) ;
35
-
36
- console . log ( JSON . stringify ( {
37
- inputValue : input ,
38
- configurationEntryValue : kmsCrypto . toLambdaStringFormat ( encryptedCiphertext )
39
- } ) ) ;
40
- } else {
41
- console . log ( "Encryption completed but could not be verified" ) ;
42
- process . exit ( ERROR ) ;
43
- }
44
- }
45
- } ) ;
46
- }
47
+ encrypt ( region , input , function ( err , result ) {
48
+ if ( err ) {
49
+ console . log ( err ) ;
50
+ process . exit ( ERROR ) ;
51
+ } else {
52
+ console . log ( JSON . stringify ( result ) ) ;
53
+ }
47
54
} ) ;
48
55
}
0 commit comments