-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhandler.js
82 lines (67 loc) · 1.81 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// A simple lambda function to revoke IAM roles of groups periodically
// By @dvdtoth
'use strict';
const aws = require('aws-sdk');
const iam = new aws.IAM();
// Delete an access key by id
function deleteAccessKey(id, username) {
let params = {
AccessKeyId: id,
UserName: username
};
return iam.deleteAccessKey(params).promise()
.catch(err => {
// Key might have been deleted already if user is in multiple groups
if (err.code == "NoSuchEntity") {
Promise.resolve();
}
else {
throw err;
}
})
}
// Delete access keys of username
function deleteByUser(username) {
let params = {
UserName: username
};
return iam.listAccessKeys(params).promise()
.then(data => {
return Promise.all(data.AccessKeyMetadata.map(metadata => {
console.log('Deleting access key ' + metadata.AccessKeyId + ' for user ' + username);
return deleteAccessKey(metadata.AccessKeyId, username);
}))
})
}
// Delete all keys for users in group
function deleteKeysInGroup(group) {
let params = {
GroupName: group,
};
return iam.getGroup(params).promise()
.then(group_data => {
let group_users = group_data.Users;
return Promise.all(group_data.Users.map(user => {
return deleteByUser(user.UserName);
}))
})
}
// AWS Lambda handler
module.exports.revoke = (event, context, callback) => {
// clear whitespaces, split by comma
let groups = process.env.GROUPS.replace(/\s/g, '').split(',');
// Delete access keys asynchronously from groups
Promise.all(groups.map(deleteKeysInGroup))
.then(data => {
callback(null, {
message: 'Keys successfully deleted',
event
});
})
.catch(err => {
callback(err, {
message: 'Failed to delete keys',
event
});
})
}