forked from alexjurkiewicz/ecr-scan-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
287 lines (263 loc) · 9.46 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
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
const core = require('@actions/core')
const AWS = require('aws-sdk')
const proxy = require('proxy-agent');
/**
* @typedef {{
* critical: number,
* high: number,
* medium: number,
* low: number,
* informational: number,
* undefined: number,
* total: number }} IgnoredCounts
*/
/**
* Get paginated AWS JS SDK results
* @param {*} fn
* @author https://advancedweb.hu/how-to-paginate-the-aws-js-sdk-using-async-generators/
*/
const getPaginatedResults = async (fn) => {
const EMPTY = Symbol('empty');
const res = [];
for await (const lf of (async function*() {
let NextMarker = EMPTY;
while (NextMarker || NextMarker === EMPTY) {
const { marker, results } = await fn(NextMarker !== EMPTY ? NextMarker : undefined);
yield* results;
NextMarker = marker;
}
})()) {
res.push(lf);
}
return res;
};
/**
* @param {AWS.ECR} ECR
* @param {string} repository
* @param {string} tag
* @returns {AWS.Request|AWS.AWSError|null} Results, Error or `null`.
*/
const getFindings = async (ECR, repository, tag) => {
return ECR.describeImageScanFindings({
imageId: {
imageTag: tag
},
repositoryName: repository
}).promise().catch(
(err) => {
if (err.code === 'ScanNotFoundException') { return null }
throw err
})
}
/**
* Method to collect all scan results.
* @param {AWS.ECR} ECR
* @param {string} repository
* @param {string} tag
* @returns {AWS.ECR.ImageScanFinding[]|AWS.AWSError|null} Results, Error or `null`.
*/
const getAllFindings = async (ECR, repository, tag) => {
return await getPaginatedResults(async (NextMarker) => {
const findings = await ECR.describeImageScanFindings({
imageId: {
imageTag: tag
},
maxResults: 1000, // Valid range: 1-1000, default: 100
repositoryName: repository,
nextToken: NextMarker
}).promise().catch(
(err) => {
if (err.code === 'ScanNotFoundException') { return null }
throw err
})
return {
marker: findings.nextToken,
results: findings.imageScanFindings.findings,
};
})
};
/**
* Tally findings by severity.
* @param {AWS.ECR.ImageScanFinding[]} ignoredFindings
* @returns {IgnoredCounts} counts
*/
const countIgnoredFindings = (ignoredFindings) =>
ignoredFindings.reduce(
(counts, finding) => {
const updatedCount = { ...counts }
const severity = finding.severity.toLowerCase()
updatedCount[severity]++
updatedCount.total++
return updatedCount
},
{ critical: 0, high: 0, medium: 0, low: 0, informational: 0, undefined: 0, total: 0 }
)
/**
* Returns display text for a severity level.
* @param {keyof IgnoredCounts} severity
* @param {IgnoredCounts} counts
* @returns {string}
*/
const getCount = (severity, counts) =>
counts[severity] ? `(${counts[severity]} ignored)` : ''
/**
* Build an array with CVE IDs to ignore in the counts.
* @param {string | string[]} list - Comma/space/newline-separated list or array of CVE IDs.
* @returns {string[]} Array of CVE IDs
*/
const parseIgnoreList = (list) => {
if (Array.isArray(list)) return list // when GitHub Actions allow arrays to be passed in.
if (!list) return []
const ignoreList =
list
.trim() // remove trailing newlines if any
.replace(/\n|\s/g, ',') // replace newlines or spaces with commas, if any
.split(',') // build the array
.map((i) => i.trim()) // ensure each item doesn't contain any white-space
.filter(Boolean) // remove empty items
return ignoreList
}
function configureGlobalProxy(proxyUrl) {
core.debug("Using proxy URL: " + proxyUrl);
AWS.config.update({
httpOptions: { agent: proxy(proxyUrl) }
});
}
function countFailingVulnerabilities(failThreshold, foundCounts, ignoredCounts) {
let count = foundCounts.critical - ignoredCounts.critical;
if (failThreshold === 'critical') {
return count;
}
count += foundCounts.high - ignoredCounts.high;
if (failThreshold === 'high') {
return count;
}
count += foundCounts.medium - ignoredCounts.medium;
if (failThreshold === 'medium') {
return count;
}
count += foundCounts.low - ignoredCounts.low;
if (failThreshold === 'low') {
return count;
}
return count + foundCounts.informational - ignoredCounts.informational;
}
const main = async () => {
core.debug('Entering main')
const repository = core.getInput('repository', { required: true })
const tag = core.getInput('tag', { required: true })
const failThreshold = core.getInput('fail_threshold') || 'high'
const ignoreList = parseIgnoreList(core.getInput('ignore_list'))
const proxyUrl = process.env.HTTPS_PROXY || process.env.https_proxy
if (proxyUrl !== undefined) {
configureGlobalProxy(proxyUrl)
}
if (
failThreshold !== 'critical' &&
failThreshold !== 'high' &&
failThreshold !== 'medium' &&
failThreshold !== 'low' &&
failThreshold !== 'informational'
) {
throw new Error('fail_threshold input value is invalid')
}
core.debug(`Repository:${repository}, Tag:${tag}, Ignore list:${ignoreList}`)
const ECR = new AWS.ECR()
core.debug('Checking for existing findings')
let status = null
let findings = await getFindings(ECR, repository, tag, !!ignoreList.length)
if (findings) {
status = findings.imageScanStatus.status
console.log(`A scan for this image was already requested, the scan's status is ${status}`)
if (status == 'FAILED') {
throw new Error(`Image scan failed: ${findings.imageScanStatus.description}`)
}
} else {
console.log('Requesting image scan')
await ECR.startImageScan({
imageId: {
imageTag: tag
},
repositoryName: repository
}).promise()
status = 'IN_PROGRESS'
}
let firstPoll = true
while (status === 'IN_PROGRESS') {
if (!firstPoll) {
await new Promise((resolve) => {
setTimeout(resolve, 5000)
})
}
console.log('Polling ECR for image scan findings...')
findings = await getFindings(ECR, repository, tag)
status = findings.imageScanStatus.status
core.debug(`Scan status: ${status}`)
firstPoll = false
}
// Sanity check
if (status !== 'COMPLETE') {
throw new Error(`Unhandled scan status "${status}". API response: ${JSON.stringify(findings)}`)
}
const findingsList = !!ignoreList.length ? await getAllFindings(ECR, repository, tag) : [] // only fetch all findings if we have an ignore list
const ignoredFindings = findingsList.filter(({ name }) => ignoreList.includes(name))
if (ignoreList.length !== ignoredFindings.length) {
const missedIgnores = ignoreList.filter(name => !ignoredFindings.map(({ name }) => name).includes(name))
console.log('The following CVEs were not found in the result set:')
missedIgnores.forEach(miss => console.log(` ${miss}`))
throw new Error(`Ignore list contains CVE IDs that were not returned in the findings result set. They may be invalid or no longer be current vulnerabilities.`)
}
const ignoredCounts = countIgnoredFindings(ignoredFindings)
const findingsDetails = findings.imageScanFindings.findings || []
const counts = findings.imageScanFindings.findingSeverityCounts
const critical = counts.CRITICAL || 0
const high = counts.HIGH || 0
const medium = counts.MEDIUM || 0
const low = counts.LOW || 0
const informational = counts.INFORMATIONAL || 0
const indeterminate = counts.UNDEFINED || 0
const ignored = ignoredFindings.length
const total = critical + high + medium + low + informational + indeterminate
core.setOutput('findingsDetails', JSON.stringify(findingsDetails))
core.setOutput('critical', critical.toString())
core.setOutput('high', high.toString())
core.setOutput('medium', medium.toString())
core.setOutput('low', low.toString())
core.setOutput('informational', informational.toString())
core.setOutput('undefined', indeterminate.toString())
core.setOutput('ignored', ignored.toString())
core.setOutput('total', total.toString())
core.startGroup('Findings')
findingsDetails.forEach((findingDetail, index) => {
let findingAttributes = []
findingDetail.attributes.forEach((attribute) => {
findingAttributes.push(`${attribute.key}=${attribute.value}`)
})
console.log(`${index + 1}. ${findingDetail.name} (${findingDetail.severity}) ${findingAttributes.join(" ")}`)
})
core.endGroup()
console.log('Vulnerabilities found:')
console.log(`${critical.toString().padStart(3, ' ')} Critical ${getCount('critical', ignoredCounts)}`)
console.log(`${high.toString().padStart(3, ' ')} High ${getCount('high', ignoredCounts)}`)
console.log(`${medium.toString().padStart(3, ' ')} Medium ${getCount('medium', ignoredCounts)}`)
console.log(`${low.toString().padStart(3, ' ')} Low ${getCount('low', ignoredCounts)}`)
console.log(`${informational.toString().padStart(3, ' ')} Informational ${getCount('informational', ignoredCounts)}`)
console.log(`${indeterminate.toString().padStart(3, ' ')} Undefined ${getCount('undefined', ignoredCounts)}`)
console.log('=================')
console.log(`${total.toString().padStart(3, ' ')} Total ${getCount('total', ignoredCounts)}`)
const numFailingVulns = countFailingVulnerabilities(
failThreshold,
{ informational, low, medium, high, critical },
ignoredCounts,
)
if (numFailingVulns > 0) {
throw new Error(`Detected ${numFailingVulns} vulnerabilities with severity >= ${failThreshold} (the currently configured fail_threshold).`)
}
}
;(async function () {
try {
await main()
} catch (error) {
core.setFailed(error.message)
}
})()