-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (42 loc) · 1.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
const AWS = require('aws-sdk');
const qs = require('querystring');
const utils = require('./utils.js');
const kmsEncryptedToken = process.env.kmsEncryptedToken;
let token;
function processEvent(event, callback) {
const params = qs.parse(event.body);
const requestToken = params.token;
if (requestToken !== token) {
console.error(`Request token (${requestToken}) does not match expected`);
return callback('Invalid request token');
}
utils.handleRequest(params, callback);
}
exports.handler = (event, context, callback) => {
//console.log('Event', event);
const done = (err, res) => callback(null, {
statusCode: err ? '400' : '200',
body: err ? (err.message || err) : JSON.stringify(res),
headers: {
'Content-Type': 'application/json',
},
});
if (token) {
// Container reuse, simply process the event with the key in memory
processEvent(event, done);
} else if (kmsEncryptedToken && kmsEncryptedToken !== '<kmsEncryptedToken>') {
const cipherText = { CiphertextBlob: new Buffer(kmsEncryptedToken, 'base64') };
const kms = new AWS.KMS();
kms.decrypt(cipherText, (err, data) => {
if (err) {
console.log('Decrypt error:', err);
return done(err);
}
token = data.Plaintext.toString('ascii');
processEvent(event, done);
});
} else {
done('Token has not been set.');
}
};