Skip to content

Commit 6928e36

Browse files
committed
Reorganising codebase to make encryptValue.js programmatically accessible
1 parent 86ac84d commit 6928e36

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

encryptValue.js

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,42 @@ var args = require('minimist')(process.argv.slice(2));
1414
var region = args.region;
1515
var input = args.input;
1616

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+
1743
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");
1945
process.exit(ERROR);
2046
} 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+
}
4754
});
4855
}

0 commit comments

Comments
 (0)