-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (103 loc) · 3.65 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var aws = require("aws-sdk");
/**
* Shim this legacy code to work on Node 10 while doing as little as possible to change it.
*/
exports.handler = async function (event, context) {
return new Promise(((resolve, reject) => {
legacyHandler(
event,
{
...context,
done: resolve
}
);
}));
};
/**
* A Simple Lambda to look up the User ID of a Role
**/
function legacyHandler(event, context) {
console.log("REQUEST RECEIVED:\n" + JSON.stringify(event));
// For Delete requests, immediately send a SUCCESS response.
if (event.RequestType == "Delete") {
sendResponse(event, context, "SUCCESS", event.PhysicalResourceId);
return;
}
var roleNames = (event.ResourceProperties.RoleNames || "")
.split(",")
.concat(event.ResourceProperties.RoleName)
.map(function (r) { return r ? r.trim() : r; })
.filter(function (r) { return r; });
var iam = new aws.IAM();
var hasErrored = false;
var results = [];
roleNames.forEach(function (roleName, ix) {
iam.getRole({
RoleName: roleName
}, function(err, getRoleResult) {
if (hasErrored) {
return;
}
if (err) {
hasErrored = true;
var responseData = {Error: "Failed to retrieve the Role with Role Name: " + roleName}
console.log(responseData.Error + ":\n", err);
sendResponse(event, context, "FAILED", context.logStreamName);
} else {
results[ix] = getRoleResult.Role.RoleId;
// Check if we've got all results.
for (var i=0; i < roleNames.length; i++) {
if (!results[i]) {
// We don't.
return;
}
}
if (event.ResourceProperties.AsGrant !== false && event.ResourceProperties.AsGrant !== "false") {
results = results.map(function (r) { return r + ":*" });
}
sendResponse(event, context, "SUCCESS", results.join(","));
}
});
});
};
// Send response to the pre-signed S3 URL
function sendResponse(event, context, responseStatus, physicalResourceId) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: "See the details in CloudWatch Log Stream: " + context.logStreamName,
PhysicalResourceId: physicalResourceId,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: {}
});
console.log("RESPONSE BODY:\n", responseBody);
var https = require("https");
var url = require("url");
var parsedUrl = url.parse(event.ResponseURL);
var options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: "PUT",
headers: {
"content-type": "",
"content-length": responseBody.length
}
};
console.log("SENDING RESPONSE...\n");
var request = https.request(options, function(response) {
console.log("STATUS: " + response.statusCode);
console.log("HEADERS: " + JSON.stringify(response.headers));
// Tell AWS Lambda that the function execution is done
context.done();
});
request.on("error", function(error) {
console.log("sendResponse Error:" + error);
// Tell AWS Lambda that the function execution is done
context.done();
});
// write data to request body
request.write(responseBody);
request.end();
}