-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.js
61 lines (51 loc) · 1.72 KB
/
handler.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
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const metafig = require('@cimpresscloud/metafig'),
Iam = require('./lib/aws'),
Aws = require('aws-sdk'),
felix = require('./lib/felix');
function publishReports (reports, topicArn) {
const sns = new Aws.SNS();
let status = 'Success';
if (reports.some(r => r.status === 'error')) {
status = 'Errors';
}
return new Promise((resolve, reject) => {
const params = {
Message: JSON.stringify({
default: `
Felix attempted to rotate ${reports.length} keys at ${new Date()}!
For each successfully-rotated user listed below, new keys have been created
and updated on the configured services. Old keys have been deactivated.
Failed rotations listed below are in an unknown state and may require manual
intervention. If you discover a problem, manually re-enable the key in question.
Deactivated keys will be deleted in the next Felix run.
Verbose report below:
${JSON.stringify(reports, null, 3)}`.trim()
}),
MessageStructure: 'json',
Subject: `[${status}] Felix rotated ${reports.length} keys at ${new Date()}!`,
TopicArn: topicArn
};
sns.publish(params, function (err, data) {
if (err) reject(err, err.stack);
else resolve(data);
});
});
}
module.exports.rotate = (event, context, callback) => {
let config = {}, iam = {};
metafig(require('./config.json'))
.then(conf => {
config = conf;
iam = new Iam();
return iam.getUsers(config.plugins.aws.userPath);
})
.then((users) => {
return Promise.all(users.map(u => { return felix.rotateKeyForUser(u, config); }));
})
.then(reports => {
return publishReports(reports, config.plugins.aws.snsTopic);
})
.then(console.log)
.catch(console.error);
};