-
Notifications
You must be signed in to change notification settings - Fork 4
/
hits.js
48 lines (39 loc) · 1.28 KB
/
hits.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
'use strict';
const AWS = require('aws-sdk');
const waf = new AWS.WAF();
const { MAX_WAF_ITEMS } = process.env;
const getRuleIds = (aclId) => new Promise((res, rej) => {
waf.getWebACL({ WebACLId: aclId }, (err, data) => {
if (err) rej(err);
else res(data.WebACL.Rules.map(r => r.RuleId));
});
});
const getRuleHits = (aclId, ruleId, { start, end }) => new Promise((res, rej) => {
const params = {
MaxItems: MAX_WAF_ITEMS,
RuleId: ruleId,
TimeWindow: { StartTime: start, EndTime: end },
WebAclId: aclId,
};
waf.getSampledRequests(params, (err, data) => {
if (err) rej(err);
else {
res(data.SampledRequests.reduce((acc, req) => {
return [ ...acc, Object.assign(req, { WebAclId: aclId, RuleId: ruleId }) ];
}, []));
}
});
});
const normaliseHeaders = (hit) => {
hit.Request.Headers = hit.Request.Headers.reduce((headers, { Name, Value }) => {
headers[Name.toLowerCase()] = Value;
return headers;
}, {});
return hit;
};
const flatten = (arrs) => Array.prototype.concat.apply([], arrs);
module.exports.getWebAclHits = (aclId, timeRange) =>
getRuleIds(aclId)
.then(ruleIds => Promise.all(ruleIds.map(ruleId => getRuleHits(aclId, ruleId, timeRange))))
.then(flatten)
.then(hits => hits.map(normaliseHeaders));