-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.js
executable file
·528 lines (514 loc) · 27.9 KB
/
entrypoint.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env node
'use strict';
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const simplify = require('simplify-sdk')
const provider = require('simplify-sdk/provider')
const utilities = require('simplify-sdk/utilities')
const asciichart = require('asciichart');
const GREEN = '\x1b[32m'
const RED = '\x1b[31m'
const YELLOW = '\x1b[33m'
const WHITE = '\x1b[0m'
const BLUE = '\x1b[34m'
const VIOLET = '\x1b[35m'
const RESET = '\x1b[0m'
const opName = `SecurityOps`
var argv = require('yargs')
.usage('simplify-security export|report|verify|patch|check|metric|snapshot| [options]')
.string('input')
.alias('i', 'input')
.describe('input', 'Input lambda functions.csv file or *.JSON security report file')
.default('input', 'functions.csv')
.string('searchFor')
.describe('searchFor', 'Search for a term in function name')
.string('searchInAlias')
.describe('searchInAlias', 'Search a function has an alias in its configuration')
.string('format')
.alias('f', 'format')
.describe('format', 'Input file format either CSV or JSON')
.string('output')
.alias('o', 'output')
.describe('output', 'Output snapshot folder')
.default('output', '.snapshot')
.string('baseline')
.alias('b', 'baseline')
.describe('baseline', 'baseline snapshot date YYYY-MM-DD')
.string('hours')
.describe('hours', 'How many hours since now eg: 12 - last 12 hours')
.alias('h', 'hours')
.string('periods')
.describe('periods', 'Time resolution periods eg: 5 10 30 60 N*60 in seconds')
.alias('t', 'periods')
.string('profile')
.describe('profile', 'AWS Profile configuration')
.alias('p', 'profile')
.default('profile', 'default')
.string('region')
.describe('region', 'AWS Specific Region')
.alias('r', 'region')
.default('region', 'eu-west-1')
.string('extended')
.alias('e', 'extended')
.describe('extended', 'Extended verification view')
.string('plot')
.describe('plot', 'Drawing chart series')
.demandOption(['i'])
.demandCommand(1)
.argv;
var configInputFile = argv.input || 'functions.csv'
var scanOutput = {}
var cmdOPS = (argv._[0] || 'verify').toUpperCase()
var fileFormat = (typeof argv.format === 'undefined' ? (cmdOPS == 'REPORT' ? 'JSON' : 'CSV') : 'CSV').toUpperCase()
var lineIndex = 0
var funcList = []
var files = []
var headers = []
var securityReports = {}
var securityServerity = { critical: 0, high: 0, medium: 0, low: 0, info: 0 }
function getSnapshotFromFile(snapshotPath) {
simplify.consoleWithMessage(opName, `${cmdOPS} Snapshot from ${snapshotPath}`)
if (fs.existsSync(snapshotPath)) {
return JSON.parse(fs.readFileSync(snapshotPath).toString())
} else {
return undefined
}
}
function takeSnapshotToFile(functionList, outputPath) {
const outputDir = path.dirname(outputPath)
if (!fs.existsSync(outputDir)) {
mkdirp.sync(outputDir);
}
fs.writeFileSync(outputPath, JSON.stringify(functionList.map(f => {
return {
FunctionName: f.functionInfo.FunctionName,
CodeSha256: f.functionInfo.CodeSha256,
LastModified: f.functionInfo.LastModified,
Version: f.functionInfo.Version,
RoleARN: f.functionInfo.Role,
RolePolicy: f.functionInfo.RolePolicy,
ResourcePolicy: f.functionInfo.ResourcePolicy,
Runtime: f.functionInfo.Runtime,
Handler: f.functionInfo.Handler,
Layers: f.Layers.map(layer => {
return {
CodeSha256: layer.Content.CodeSha256,
LayerVersionArn: layer.LayerVersionArn,
CreatedDate: layer.CreatedDate
}
}),
LogGroup: { LogGroupName: (f.LogGroup || {}).logGroupName }
}
}), null, 2), 'utf8');
simplify.consoleWithMessage(opName, `${cmdOPS} to ${outputPath} \x1b[32m (OK) \x1b[0m`)
}
function analyseOrPatch(args) {
const { functionInfo, logRetention, customKmsArn, secureFunction, secureLog } = args
return new Promise((resolve, reject) => {
const combinedKmsKeyArn = customKmsArn || functionInfo.KMSKeyArn
if (!functionInfo.KMSKeyArn) {
if (cmdOPS === 'PATCH') {
functionInfo.KMSKeyArn = combinedKmsKeyArn
let functionConfig = {
FunctionName: functionInfo.FunctionName
}
if (secureFunction /** enabled */ && functionInfo.KMSKeyArn) {
functionConfig.KMSKeyArn = functionInfo.KMSKeyArn
simplify.updateFunctionConfiguration({
adaptor: provider.getFunction(),
functionConfig: functionConfig
}).then(_ => {
simplify.enableOrDisableLogEncryption({
adaptor: provider.getKMS(),
logger: provider.getLogger(),
functionInfo: functionInfo,
retentionInDays: logRetention,
enableOrDisable: secureLog
}).then(function (data) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${(logRetention ? `Configured${secureLog ? ' KMS ' : ' '}LogGroup retention for ${logRetention} days!` : `LogGroup is not required to configure`)} \x1b[32m (OK) \x1b[0m`)
resolve(args)
}).catch(function (err) {
reject(`${err}`)
})
}).catch(function (err) {
reject(`${err}`)
})
} else if (secureFunction /** enabled */ && !functionInfo.KMSKeyArn) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : You must provide a KMS Custom KeyId! \x1b[31m (ERROR) \x1b[0m`)
reject(`Missing KMS KeyId for ${functionInfo.FunctionName}`)
} else {
simplify.enableOrDisableLogEncryption({
adaptor: provider.getKMS(),
logger: provider.getLogger(),
functionInfo: functionInfo,
retentionInDays: logRetention,
enableOrDisable: secureLog
}).then(function (_) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${(logRetention ? `Configured${secureLog ? ' KMS ' : ' '}LogGroup retention for ${logRetention} days!` : `LogGroup is not required to configure`)} \x1b[32m (OK) \x1b[0m`)
resolve(args)
}).catch(function (err) {
reject(`${err}`)
})
}
} else if (cmdOPS === 'CHECK') {
if (secureFunction) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${functionInfo.KMSKeyArn == customKmsArn ? (functionInfo.KMSKeyArn ? `Has already configure with KMS Custom KeyId \x1b[32m[GOOD]\x1b[0m` : `Provide KMS Custom KeyId to setup secure function! \x1b[33m (WARN) \x1b[0m`) : (customKmsArn ? `Has KMS Custom KeyId but not set! \x1b[33m (WARN) \x1b[0m` : `Missing KMS Custom KeyId \x1b[33m (WARN) \x1b[0m`)}`)
} else {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${functionInfo.KMSKeyArn == customKmsArn ? `Not require to use KMS Custom KeyId \x1b[32m[GOOD]\x1b[0m` : `Not matching KMS Custom KeyIds \x1b[33m (WARN) \x1b[0m`}`)
}
resolve(args)
} else {
resolve(args)
}
} else {
if (cmdOPS === 'PATCH') {
functionInfo.KMSKeyArn = combinedKmsKeyArn ? combinedKmsKeyArn : functionInfo.KMSKeyArn
/** record new SHA256 Code Here */
simplify.enableOrDisableLogEncryption({
adaptor: provider.getKMS(),
logger: provider.getLogger(),
functionInfo: functionInfo,
retentionInDays: logRetention,
enableOrDisable: secureLog
}).then(function (_) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${(logRetention ? `Configured${secureLog ? ' KMS ' : ' '}LogGroup retention for ${logRetention} days!` : `LogGroup is not required to configure`)} \x1b[32m (OK) \x1b[0m`)
if (secureFunction) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : Configured with KMS Custom KeyId \x1b[32m (OK) \x1b[0m`)
args.customKmsArn = functionInfo.KMSKeyArn
}
resolve(args)
}).catch(function (err) {
reject(`${err}`)
})
} else if (cmdOPS === 'CHECK') {
if (secureFunction) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${functionInfo.KMSKeyArn == customKmsArn ? (functionInfo.KMSKeyArn ? `Has already configure with KMS Custom KeyId \x1b[32m[GOOD]\x1b[0m` : `Provide KMS Custom KeyId to setup secure function! \x1b[33m (WARN) \x1b[0m`) : (customKmsArn ? `Has KMS Custom KeyId but not set! \x1b[33m (WARN) \x1b[0m` : `Has configured with KMS but Custom KeyId input is not provided. \x1b[33m (WARN) \x1b[0m`)}`)
} else {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionInfo.FunctionName} : ${functionInfo.KMSKeyArn == customKmsArn ? `Has already configure with Custom KMS KeyId \x1b[32m[GOOD]\x1b[0m` : `Not matching KMS Custom KeyIds \x1b[33m (WARN) \x1b[0m`}`)
}
resolve(args)
} else {
resolve(args)
}
}
})
}
const secOpsFunctions = function (files, callback) {
const currentLine = files[lineIndex++]
if (currentLine) {
const parts = currentLine.split(',')
if (parts.length >= 2) {
const functionName = parts[2]
const functionVersion = parts[3] || undefined
const logRetention = parts[4] || undefined
const accountId = parts[1]
if (accountId.indexOf('E') !== -1) {
simplify.consoleWithMessage(opName, `${cmdOPS} ${functionName} Invalid AccountId number format \x1b[31m (ERROR) \x1b[0m`)
}
const customKmsArn = parts[5] ? `arn:aws:kms:${parts[0]}:${accountId}:key/${parts[5]}` : null
const secureFunction = JSON.parse((parts[6] || 'false').toLowerCase())
const secureLog = JSON.parse((parts[7] || 'false').toLowerCase())
if (cmdOPS === 'METRIC') {
funcList.push({ functionInfo: { FunctionName: `${functionName}` } })
if (lineIndex >= files.length) {
callback && callback(funcList)
} else {
secOpsFunctions(files, callback)
}
} else {
simplify.getFunctionMetaInfos({
adaptor: provider.getFunction(),
logger: provider.getLogger(),
functionConfig: { FunctionName: functionName, Version: functionVersion },
silentIs: true
}).then(function (functionMeta) {
const functionInfo = functionMeta.Configuration
simplify.getFunctionRolePolicies({
adaptor: provider.getIAM(),
functionConfig: { FunctionName: functionName, Version: functionVersion, ...functionInfo }
}).then(function (roleData) {
functionInfo.RolePolicy = roleData
if (!scanOutput[functionInfo.FunctionName]) {
scanOutput[functionInfo.FunctionName] = {}
}
simplify.getFunctionPolicy({
adaptor: provider.getFunction(),
functionConfig: { FunctionName: functionName, Version: functionVersion, ...functionInfo }
}).then(function (roleBasePolicy) {
return Promise.resolve(JSON.parse(roleBasePolicy.Policy))
}).catch(err => {
return Promise.resolve()
}).then(result => {
functionInfo.ResourcePolicy = result
scanOutput[functionInfo.FunctionName] = functionInfo
analyseOrPatch({ functionInfo, logRetention, customKmsArn, secureFunction, secureLog }).then(res => {
funcList.push({ ...res, Layers: functionMeta.LayerInfos, LogGroup: functionMeta.LogGroup })
if (lineIndex >= files.length) {
callback && callback(funcList)
} else {
secOpsFunctions(files, callback)
}
}).catch(err => simplify.consoleWithErrors(opName, `${functionName} ${functionInfo.FunctionName}: ${err}`))
})
}).catch(err => simplify.consoleWithErrors(opName, `${functionName}: ${err}`))
}).catch(err => simplify.consoleWithErrors(opName, `${functionName}: ${err}`))
}
}
} else {
callback && callback(funcList)
}
}
function printMetricCharts(metrics, functionList, pIndex, mIndex) {
pIndex = pIndex < functionList.length ? pIndex : 0
const functionName = functionList[pIndex].functionInfo.FunctionName
const lastHours = parseInt(argv.hours || 3)
const periodMins = parseInt(argv.periods || 300) / 60
const totalValues = {}
const series = metrics.MetricDataResults.map(m => {
const functionId = m.Id.split('_')[1]
const labelValue = `${m.Label}`
if (functionId == pIndex) {
if (!m.Values.length) {
m.Values.push(0)
}
const totalPeriodValue = parseFloat(m.Values.reduce((count, x) => count + x, 0))
if (!totalValues[labelValue]) totalValues[labelValue] = 0
if (labelValue === 'Duration' || labelValue === 'Concurrency') {
totalValues[labelValue] = Math.max(totalValues[labelValue], ...m.Values)
totalValues[labelValue] = (labelValue === 'Duration' ? (totalValues[labelValue]).toFixed(2) : totalValues[labelValue])
} else {
totalValues[labelValue] += totalPeriodValue
}
return m.Values
}
return undefined
}).filter(m => m)
console.log(`\n * (${functionName}): In the last ${lastHours} hours at every ${periodMins} minutes \n`)
const pColors = [
asciichart.blue,
asciichart.red,
asciichart.green,
asciichart.yellow,
asciichart.default
]
console.log(asciichart.plot(series.splice(mIndex || 0, 1), {
colors: [pColors[mIndex || 0]],
height: 20
}))
const totalLabels = {
"Invocations": `1- Invocations ${BLUE}BLUE${RESET}`,
"Errors": `2- Errors ${RED}RED${RESET}`,
"Duration": `3- Max Duration ${GREEN}GREEN${RESET}`,
"Concurrency": `4- Max Concurrency ${YELLOW}YELLOW${RESET}`,
"Throttles": `5- Throttles ${WHITE}WHITE${RESET}`
}
console.log(`\n * ${Object.keys(totalValues).map(kv => `${totalLabels[kv]}: ${totalValues[kv]}`).join(' | ')} \n`)
}
function printMetricTable(metrics, functionList) {
const mData = {}
const totalValues = {}
const lastHours = parseInt(argv.hours || 3)
const periodMins = parseInt(argv.periods || 300) / 60
const table = new utilities.PrintTable()
metrics.MetricDataResults.map((m, idx) => {
const data = {}
const labelValue = `${m.Label}`
if (!m.Values.length) {
m.Values.push(0)
}
const totalPeriodValue = parseFloat(m.Values.reduce((count, x) => count + x, 0))
if (!totalValues[labelValue]) totalValues[labelValue] = 0
if (labelValue === 'Duration' || labelValue === 'Concurrency') {
totalValues[labelValue] = Math.max(totalValues[labelValue], ...m.Values)
totalValues[labelValue] = (labelValue === 'Duration' ? totalValues[labelValue].toFixed(2) : totalValues[labelValue])
} else {
totalValues[labelValue] += totalPeriodValue
}
const functionId = m.Id.split('_')[1]
const functionName = functionList[functionId].functionInfo.FunctionName
data[labelValue] = (labelValue === 'Duration' || labelValue === 'Concurrency' ? Math.max(...m.Values) : totalPeriodValue)
data[labelValue] = (labelValue === 'Duration' ? data[labelValue].toFixed(2) : data[labelValue])
mData[functionId] = { 'Index': (parseInt(functionId) + 1), 'Function': functionName.truncateLeft(50), ...mData[functionId], ...data }
})
let dataRows = Object.keys(mData).map(k => mData[k])
table.addRows(dataRows)
table.addRow({ 'Function': `In ${lastHours} hours at every ${periodMins} minutes`, ...totalValues }, { color: 'white_bold' })
table.printTable()
}
const processLambda = function () {
try {
var config = simplify.getInputConfig({
Region: argv.region || 'eu-west-1',
Profile: argv.profile || 'default',
Bucket: { Name: 'default' }
})
provider.setConfig(config).then(function () {
if (headers.startsWith('Region')) {
secOpsFunctions(files, function (functionList) {
if (cmdOPS === 'METRIC') {
let startDate = new Date()
const lastHours = parseInt(argv.hours || 3)
startDate.setHours(startDate.getHours() - (lastHours))
simplify.getFunctionMetricData({
adaptor: provider.getMetrics(),
functions: functionList.map(f => { return { FunctionName: f.functionInfo.FunctionName } }),
periods: parseInt(argv.periods || 300),
startDate: startDate,
endDate: new Date()
}).then(metrics => {
if (typeof argv.plot === 'undefined') {
printMetricTable(metrics, functionList)
} else {
const indexes = argv.plot.split(',')
const pIndex = parseInt(indexes[0] || 1) - 1
const mIndex = indexes.length > 0 ? parseInt(indexes[1]) - 1 : 0
printMetricCharts(metrics, functionList, pIndex < 0 ? 0 : pIndex, mIndex < 0 ? 0 : mIndex)
}
}).catch(err => simplify.consoleWithMessage(opName, `${err}`))
} else if (cmdOPS === 'VERIFY') {
let isSimpleView = true
if (typeof argv.extended !== 'undefined') {
isSimpleView = false
}
const snapshotList = getSnapshotFromFile(path.resolve(argv.output, `${argv.baseline || utilities.getDateToday()}.json`))
const outputTable = functionList.map((func, idx) => {
const snapshot = snapshotList ? snapshotList.find(f => f.FunctionName === func.functionInfo.FunctionName) : { Layers: [] }
var areLayersValid = snapshotList ? true : false
snapshot && snapshot.Layers.map(layer => {
const layerInfo = func.Layers.find(info => info.LayerVersionArn === layer.LayerVersionArn)
if (layerInfo && layerInfo.Content.CodeSha256 !== layer.CodeSha256) {
areLayersValid = false
}
})
func.LogGroup = func.LogGroup || {}
func.functionInfo = func.functionInfo || {}
const basicView = {
Index: idx + 1,
FunctionName: func.functionInfo.FunctionName.truncateLeft(50),
CodeSha256: `${func.functionInfo.CodeSha256.truncateLeft(5, '')} (${func.functionInfo.CodeSha256 === (snapshot || {}).CodeSha256 ? 'OK' : 'NOK'})`,
Layers: `${func.Layers.length} (${areLayersValid ? 'OK' : 'NOK'})`,
LogRetention: `${func.LogGroup.retentionInDays || '-'} / ${func.logRetention || '-'} (${func.LogGroup.retentionInDays == func.logRetention ? 'OK' : 'PATCH'})`,
EncryptionKey: (func.customKmsArn ? `KMS ${func.functionInfo.KMSKeyArn === func.customKmsArn ? '(OK)' : '(PATCH)'}` : `${func.functionInfo.KMSKeyArn ? 'KMS' : '-'} ${func.functionInfo.KMSKeyArn === func.customKmsArn ? '(OK)' : '(PATCH)'}`).truncateLeft(13),
SecureFunction: func.secureFunction ? (func.functionInfo.KMSKeyArn ? 'YES (OK)' : 'YES (PATCH)') : (func.functionInfo.KMSKeyArn ? 'NO (PATCH)' : 'NO (OK)'),
SecureLog: func.secureLog ? (func.LogGroup.kmsKeyId ? 'YES (OK)' : 'YES (PATCH)') : (func.LogGroup.kmsKeyId ? 'NO (PATCH)' : 'NO (OK)')
}
const extendedView = {
Index: idx + 1,
FunctionName: func.functionInfo.FunctionName.truncateLeft(50),
LastModified: utilities.formatTimeSinceAgo(new Date(func.functionInfo.LastModified)),
State: func.functionInfo.State,
CodeSize: `${utilities.formatBytesToKBMB(parseInt(func.functionInfo.CodeSize))}`,
MemorySize: `${utilities.formatBytesToKBMB(parseInt(func.functionInfo.MemorySize) * 1024 * 1024)}`,
Timeout: `${func.functionInfo.Timeout} s`,
Runtime: func.functionInfo.Runtime
}
return isSimpleView ? basicView : extendedView
})
utilities.printTableWithJSON(outputTable)
} else if (cmdOPS === 'SNAPSHOT') {
takeSnapshotToFile(functionList, path.resolve(argv.output, `${utilities.getDateToday()}.json`))
} else {
}
})
} else {
}
})
} catch (err) {
simplify.finishWithErrors(`${opName}-Function`, err)
}
}
const processReport = function () {
try {
if (cmdOPS === 'REPORT') {
const vulnerabilities = securityReports.vulnerabilities || []
if (vulnerabilities.length) {
utilities.printTableWithJSON(vulnerabilities.map((v, idx) => {
securityServerity.critical += v.severity == 'Critical' ? 1 : 0
securityServerity.high += v.severity == 'High' ? 1 : 0
securityServerity.medium += v.severity == 'Medium' ? 1 : 0
securityServerity.low += v.severity == 'Low' ? 1 : 0
securityServerity.info += v.severity == 'Unknown' ? 1 : 0
return {
index: idx + 1,
severity: v.severity == 'Unknown' ? 'Information' : (v.severity || ''),
category: v.category == 'secret_detection' ? 'secret' : (v.category || ''),
identifier: (v.identifiers || []).map(i => i.type == 'eslint_rule_id' ?
i.value : i.type == 'njsscan_rule_type' ?
`name/${i.name}` : i.type == 'gitleaks_rule_id' ? `secret-type/${i.value}` :
i.type !== 'cwe' ? `${i.type}/${i.value.truncateLeft(10)}` : undefined).filter(o => o),
location: v.location.file.truncateLeft(30),
lines: `${v.location.start_line}/${v.location.end_line}`
}
}))
if (securityServerity.critical || securityServerity.high) {
throw (`${RED}ERROR ${RESET}from ${RESET}${configInputFile}${RESET} - There are ${securityServerity.critical} ${VIOLET}(CRITICAL)${RESET} and ${securityServerity.high} ${YELLOW}(HIGH)${RESET} severity findings.${RESET}`)
}
} else {
simplify.consoleWithMessage(`${opName}-Report`, `There was no finding has been found yet.`)
}
}
} catch (err) {
simplify.finishWithErrors(`${opName}-Report`, err)
}
}
if (cmdOPS === 'EXPORT') {
var config = simplify.getInputConfig({
Region: argv.region || 'eu-west-1',
Profile: argv.profile || 'default',
Bucket: { Name: 'default' }
})
provider.setConfig(config).then(function () {
simplify.listFunctionsInfo({
adaptor: provider.getFunction(),
searchFor: argv.searchFor
}).then(functions => {
let csvOutput = ''
csvOutput = 'Region,Account,FunctionName,Version,LogRetention,KMS,SecureFunction,SecureLog\r\n'
const promiseResults = functions.map(f => {
const parts = f.FunctionArn.split(':')
if (!argv.searchInAlias || argv.searchInAlias == '') {
return Promise.resolve(`${parts[3]},${parts[4]},${parts[6]},$LATEST,90,${f.KMSKeyArn || ''},FALSE,FALSE`)
}
return new Promise((resolve) => {
provider.getFunction().listAliases({
FunctionName: f.FunctionName
}, (err, data) => {
if (!err && data.Aliases) {
const matchedAlias = data.Aliases.find(alias => alias.Name.toLowerCase().startsWith((argv.searchInAlias || "").toLowerCase()))
if (matchedAlias) {
resolve(`${parts[3]},${parts[4]},${parts[6]},${matchedAlias.FunctionVersion},90,${f.KMSKeyArn || ''},FALSE,FALSE`)
} else {
resolve(undefined)
}
} else {
resolve(undefined)
}
})
});
})
Promise.all(promiseResults).then(results => {
const filteredResult = results.filter(x => x)
csvOutput += filteredResult.join('\r\n')
if (fs.existsSync(path.resolve(__dirname, 'functions.csv'))) {
fs.unlinkSync(path.resolve(__dirname, 'functions.csv'))
}
fs.writeFileSync(path.resolve(__dirname, 'functions.csv'), csvOutput)
if (argv.searchInAlias) {
simplify.consoleWithMessage(opName, `Filtering in ${functions.length} result${functions.length > 1 ? 's' : ''} with "${argv.searchInAlias}" alias: ${filteredResult.length} result${filteredResult.length > 1 ? 's' : ''} matches`)
}
console.log('\r\n - A list of functions have been written to:', path.resolve(__dirname, 'functions.csv'))
})
}).catch(err => simplify.consoleWithErrors(opName, err))
})
} else if (fs.existsSync(path.resolve(configInputFile))) {
var fileContent = fs.readFileSync(path.resolve(configInputFile), 'utf-8')
if (fileFormat == 'CSV') {
files = fileContent.split(/\r?\n/)
headers = files[lineIndex++]
processLambda()
} else if (fileFormat == 'JSON') {
securityReports = JSON.parse(fileContent)
processReport()
}
}