-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage-monitor.js
296 lines (282 loc) · 9.64 KB
/
usage-monitor.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import {buildMultiAccountLambdaHandler, publishNotification, buildApiForAccount} from './utils.js'
import {USAGE_MONITOR_EVENT_AGE_DAYS, ATHENA_WORKGROUP_NAME} from './runtime-envs.js'
import {
initialiseAthena_CloudFront,
queryAthena_CloudFront,
initialiseAthena_S3AccessLogs,
queryAthena_S3AccessLogs
} from './usage-utils-athena.js'
import {fetchLogEntries} from './usage-utils-logs.js'
import {USAGE_CHILD_ROLE_NAME} from './constants.js'
import {getIpInfo} from './ip-info.js'
import {
OUTPUT_PREFIX,
USAGE_TYPE_LOG_GROUP,
USAGE_TYPE_CLOUDFRONT,
USAGE_TYPE_S3_ACCESS_LOGS
} from '@tstibbs/cloud-core-utils/src/stacks/usage-tracking.js'
function fieldsArrayToMap(fields) {
return fields.reduce((all, entry) => {
all[entry.field] = entry.value
return all
}, {})
}
function timeToAthenaFormattedDate(timeInSeconds) {
return millisToFormattedDate(timeInSeconds * 1000)
}
function millisToFormattedDate(timeInMillis) {
let date = new Date(timeInMillis)
let month = `${date.getUTCMonth() + 1}`.padStart(2, '0')
let day = `${date.getUTCDate()}`.padStart(2, '0')
return `${date.getUTCFullYear()}-${month}-${day}`
}
async function queryCloudfront(apis, accountId, dates, stackName, resource) {
const {name: stackResourceName, source: bucketName} = resource
const {athena} = apis
let startDate = timeToAthenaFormattedDate(dates.startTime)
let endDate = timeToAthenaFormattedDate(dates.endTime)
let tableName = `cloudfrontlogs_${stackName}_${stackResourceName}`
//ensure the table exists
await initialiseAthena_CloudFront(athena, tableName, bucketName, stackName, stackResourceName, ATHENA_WORKGROUP_NAME)
console.log('table created')
let results = await queryAthena_CloudFront(athena, tableName, startDate, endDate, ATHENA_WORKGROUP_NAME)
let parsedResults = results.ResultSet.Rows.slice(1).map(({Data}) => {
let result = mapsToArray(Data)
let [count, status, date, sourceIp, method, geoBlocked, uriRoot] = result
let event = `${method} ${status}`
let values = {
accountId,
stackName,
stackResourceName,
date,
count,
event,
sourceIp,
geoBlocked
}
if (resource.splitReportingByUrlRoot) {
values.uriRoot = uriRoot
}
return values
})
return parsedResults
}
async function queryS3AccessLogs(apis, accountId, dates, stackName, resource) {
const {name: stackResourceName, source: bucketName} = resource
const {athena} = apis
let startDate = timeToAthenaFormattedDate(dates.startTime)
let endDate = timeToAthenaFormattedDate(dates.endTime)
let tableName = `s3accesslogs_${stackName}_${stackResourceName}`
//ensure the table exists
await initialiseAthena_S3AccessLogs(
athena,
tableName,
bucketName,
stackName,
stackResourceName,
ATHENA_WORKGROUP_NAME
)
console.log('table created')
let results = await queryAthena_S3AccessLogs(athena, tableName, startDate, endDate, ATHENA_WORKGROUP_NAME)
let parsedResults = results.ResultSet.Rows.slice(1).map(({Data}) => {
let result = mapsToArray(Data)
let [count, status, date, sourceIp, method] = result
let event = `${method} ${status}`
return {
accountId,
stackName,
stackResourceName,
date,
count,
event,
sourceIp,
geoBlocked: false
}
})
return parsedResults
}
function mapsToArray(arrayOfMaps) {
return arrayOfMaps.map(map => Object.values(map).join(';'))
}
async function queryLogGroup(apis, accountId, dates, stackName, resource) {
const {name: stackResourceName, source: logGroup} = resource
const {cloudWatchLogs} = apis
//supports any HTTP, WebSocket or REST API that has been configured with the log group settings from aws/utils/src/stacks/usage-tracking.js
let results = await fetchLogEntries(cloudWatchLogs, dates, logGroup)
return results.map(fieldsArray => {
let result = fieldsArrayToMap(fieldsArray)
let {date, count, sourceIp, method, path} = result
date = millisToFormattedDate(parseInt(date))
let event = `${method} ${path}`
return {
accountId,
stackName,
stackResourceName,
date,
count,
event,
sourceIp
}
})
}
async function processResources(accountId, now, stacks, apis) {
now.setUTCHours(0)
now.setUTCMinutes(0)
now.setUTCSeconds(0)
now.setUTCMilliseconds(0)
let endTime = Math.round(now.getTime() / 1000) //the most recent occurance of UTC midnight
let startTime = endTime - USAGE_MONITOR_EVENT_AGE_DAYS * 24 * 60 * 60
let dates = {startTime, endTime}
//
let allResults = []
let errors = []
for (let stack of stacks) {
let stackName = stack.name
for (let resource of stack.resources) {
let results = []
switch (resource.type) {
case USAGE_TYPE_CLOUDFRONT:
results = await queryCloudfront(apis, accountId, dates, stackName, resource)
break
case USAGE_TYPE_S3_ACCESS_LOGS:
results = await queryS3AccessLogs(apis, accountId, dates, stackName, resource)
break
case USAGE_TYPE_LOG_GROUP:
results = await queryLogGroup(apis, accountId, dates, stackName, resource)
break
default:
errors.push(resource)
}
allResults = allResults.concat(results)
}
}
return {
results: allResults,
errors
}
}
function formatResults(resultsFormatter, allResults, ipInfo, errors) {
//prepare report
let formattedResults = []
//add IP information
let formattedIps = Object.entries(ipInfo)
.map(([ip, {description, risk}]) => `${ip}: ${description} (${risk} risk)`)
.sort()
if (formattedIps.length > 0) {
formattedResults = formattedResults.concat(formattedIps, '') //empty string to add a newline
}
formattedResults.push(resultsFormatter(allResults, ipInfo))
//deal with error cases
if (errors.length > 0) {
formattedResults.push('') //empty string to add a newline
formattedResults.push('errors: ' + JSON.stringify(errors, null, 2))
}
if (allResults.length == 0) {
formattedResults.push('No usage found.')
}
//send report
let resultsText = `Usage info for the past ${USAGE_MONITOR_EVENT_AGE_DAYS} days:\n\n${formattedResults.join('\n')}`
return resultsText
}
function formatResultsForLog(allResults, ipInfo) {
let formattedResults = allResults
.map(result => {
let blockString = ''
if (result.geoBlocked) {
blockString = `, ${result.geoBlocked}`
}
return `${result.accountId}/${result.stackName}/${result.stackResourceName} ${result.date} (${result.count}): ${result.event}, ${result.sourceIp} (${result.risk} ip risk${blockString})`
})
.join('\n')
return formattedResults
}
function formatResultsForEmail(allResults, ipInfo) {
const createTitle = result => {
let prefix = `${result.accountId} / ${result.stackName} / ${result.stackResourceName}`
if (result.uriRoot != null) {
prefix = `${prefix} / ${result.uriRoot}`
}
return prefix
}
const uniques = (arr, extractor) => [...new Set(arr.map(extractor))].sort()
let stackDisplays = uniques(allResults, createTitle)
let stackSummaries = stackDisplays
.map(stackDisplay => {
let stackResults = allResults.filter(result => stackDisplay == createTitle(result))
let ipsToCount = stackResults.reduce((ipsToCount, stackResult) => {
let {count, sourceIp, geoBlocked} = stackResult
let ipDescriptor = sourceIp
if (sourceIp in ipInfo) {
let {shortDescription, risk} = ipInfo[sourceIp]
risk = `${risk} risk`
if (geoBlocked) {
risk = `${risk} (${geoBlocked})`
}
let descriptionRegex = /^([^\(]+\()AS\d+ (.+\))$/
let regexMatch = shortDescription.match(descriptionRegex)
if (regexMatch != null) {
shortDescription = `${regexMatch[1]}${regexMatch[2]}`
}
ipDescriptor = `${risk}: ${shortDescription}`
}
if (!(ipDescriptor in ipsToCount)) {
ipsToCount[ipDescriptor] = 0
}
ipsToCount[ipDescriptor] += parseInt(count)
return ipsToCount
}, {})
let summaries = Object.entries(ipsToCount)
.map(([ip, count]) => `${ip}: ${count}`)
.sort()
.join('\n')
let summary = `${stackDisplay}\n${summaries}`
return summary
})
.join('\n\n')
return stackSummaries
}
async function checkOneAccount(accountId) {
const cloudformation = await buildApiForAccount(accountId, USAGE_CHILD_ROLE_NAME, 'CloudFormation')
const athena = await buildApiForAccount(accountId, USAGE_CHILD_ROLE_NAME, 'Athena')
const cloudWatchLogs = await buildApiForAccount(accountId, USAGE_CHILD_ROLE_NAME, 'CloudWatchLogs')
const apis = {
athena,
cloudWatchLogs
}
//get outputs from stacks
let listResult = await cloudformation.describeStacks().promise()
let stacks = listResult.Stacks.map(stack => {
let resources = stack.Outputs.filter(output => output.OutputKey.startsWith(OUTPUT_PREFIX)).map(output =>
JSON.parse(output.OutputValue)
)
return {
name: stack.StackName,
resources
}
}).filter(stack => stack.resources.length > 0)
console.log(JSON.stringify(stacks, null, 2))
const now = new Date()
return await processResources(accountId, now, stacks, apis)
}
async function summariseAccounts(invocationId, allAcountsData) {
let allResults = allAcountsData.map(output => output.results).flat()
let allErrors = allAcountsData.map(output => output.errors).flat()
//add IP information
let ips = allResults.map(result => result.sourceIp)
let ipInfo = await getIpInfo(ips)
allResults.forEach(result => {
let ip = result.sourceIp
if (ip in ipInfo) {
result.risk = ipInfo[ip].risk
} else {
result.risk = 'unknown'
}
})
//simpler report for email, more detail for the log
let logReport = formatResults(formatResultsForLog, allResults, ipInfo, allErrors)
let emailReport = formatResults(formatResultsForEmail, allResults, ipInfo, allErrors)
console.log(`publishing sns alert:\n\`\`\`\n${emailReport}\n\`\`\`\n\n`)
console.log(`detailed report:\n\`\`\`\n${logReport}\n\`\`\`\n\n`)
await publishNotification(emailReport, 'AWS usage info', invocationId)
}
export const handler = buildMultiAccountLambdaHandler(checkOneAccount, summariseAccounts)