-
Notifications
You must be signed in to change notification settings - Fork 77
/
index.js
473 lines (418 loc) · 13.1 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
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
const tools = require("@actions/tool-cache");
const core = require("@actions/core");
const cache = require("@actions/cache");
const exec = require("@actions/exec");
const fs = require("fs");
const os = require("os");
const path = require("path");
const process = require("process");
const stream = require("stream");
const { GRYPE_VERSION } = require("./GrypeVersion");
const grypeVersion = core.getInput("grype-version") || GRYPE_VERSION;
const grypeExecutableName = isWindows() ? "grype.exe" : "grype";
async function downloadGrypeWindowsWorkaround(version) {
const versionNoV = version.replace(/^v/, "");
// example URL: https://github.com/anchore/grype/releases/download/v0.79.2/grype_0.79.2_windows_amd64.zip
const url = `https://github.com/anchore/grype/releases/download/${version}/grype_${versionNoV}_windows_amd64.zip`;
core.info(`Downloading grype from ${url}`);
const zipPath = await tools.downloadTool(url);
core.debug(`Zip saved to ${zipPath}`);
const toolDir = await tools.extractZip(zipPath);
core.debug(`Zip extracted to ${toolDir}`);
const binaryPath = path.join(toolDir, grypeExecutableName);
core.debug(`Grype path is ${binaryPath}`);
return binaryPath;
}
function isWindows() {
return process.platform === "win32";
}
/* download grype and return a path to the executable */
async function downloadGrype(version) {
if (isWindows()) {
return await downloadGrypeWindowsWorkaround(version);
}
const installScriptUrl = `https://raw.githubusercontent.com/anchore/grype/main/install.sh`;
core.info(`Downloading grype ${version} via ${installScriptUrl}`);
// TODO: when grype starts supporting unreleased versions, support it here
// Download the installer, and run
const installScriptPath = await tools.downloadTool(installScriptUrl);
const installToDir = fs.mkdtempSync(
path.join(os.tmpdir(), "grype-download-"),
);
const { stdout, exitCode } = await runCommand("sh", [
installScriptPath,
"-d",
"-b",
installToDir,
version,
]);
if (exitCode !== 0) {
core.error("Error installing grype:");
core.error(stdout);
throw new Error("error installing grype");
}
return path.join(installToDir, isWindows() ? "grype.exe" : "grype");
}
async function installGrype(version) {
core.info(`Installing grype ${version}`);
let grypePath = tools.find(grypeExecutableName, version);
if (!grypePath) {
// Not found, install it
grypePath = await downloadGrype(version);
// Cache the downloaded file, get path to directory
grypePath = await tools.cacheFile(
grypePath,
grypeExecutableName,
grypeExecutableName,
version,
);
}
return path.join(grypePath, grypeExecutableName);
}
// Determines if multiple arguments are defined
function multipleDefined(...args) {
let defined = false;
for (const a of args) {
if (defined && a) {
return true;
}
if (a) {
defined = true;
}
}
return false;
}
function sourceInput() {
const image = core.getInput("image");
const path = core.getInput("path");
const sbom = core.getInput("sbom");
if (multipleDefined(image, path, sbom)) {
throw new Error(
"The following options are mutually exclusive: image, path, sbom",
);
}
if (image) {
return image;
}
if (sbom) {
return "sbom:" + sbom;
}
if (!path) {
// Default to the CWD
return "dir:.";
}
return "dir:" + path;
}
async function run() {
try {
core.debug(new Date().toTimeString());
// Grype accepts several input options, initially this action is supporting both `image` and `path`, so
// a check must happen to ensure one is selected at least, and then return it
const source = sourceInput();
const failBuild = core.getInput("fail-build") || "true";
const outputFormat = core.getInput("output-format") || "sarif";
const severityCutoff = core.getInput("severity-cutoff") || "medium";
const onlyFixed = core.getInput("only-fixed") || "false";
const addCpesIfNone = core.getInput("add-cpes-if-none") || "false";
const byCve = core.getInput("by-cve") || "false";
const vex = core.getInput("vex") || "";
const cacheDb = core.getInput("cache-db") || "false";
const outputFile = core.getInput("output-file") || "";
const out = await runScan({
source,
failBuild,
severityCutoff,
onlyFixed,
outputFile,
outputFormat,
addCpesIfNone,
byCve,
vex,
cacheDb,
});
Object.keys(out).map((key) => {
core.setOutput(key, out[key]);
});
} catch (error) {
core.setFailed(error.message);
}
}
async function getDbDir(grypeCommand) {
const { stdout } = await runCommand(
grypeCommand,
["config", "--load"],
process.env,
);
for (let line of stdout.split("\n")) {
line = line.trim();
if (line.startsWith("cache-dir:")) {
line = line.replace("cache-dir:", "");
line = line.replace("'~", os.homedir());
line = line.replaceAll("'", "");
line = line.trim();
return line;
}
}
throw new Error("unable to get grype db cache directory");
}
async function getDbBuildTime(grypeCommand) {
const { stdout, exitCode } = await runCommand(
grypeCommand,
["db", "status", "-vv"],
process.env,
);
if (exitCode !== 0) {
return;
}
for (let line of stdout.split("\n")) {
line = line.trim();
if (line.startsWith("Built:")) {
line = line.replace("Built:", "");
// 2024-07-25 01:30:47 +0000 UTC
return new Date(line.trim());
}
}
}
async function updateDb(grypeCommand) {
const { stdout, exitCode } = await runCommand(
grypeCommand,
["db", "update", "-vv"],
process.env,
);
if (exitCode !== 0) {
throw new Error("unable to update db: " + stdout);
}
}
// attempts to get an up-to-date database and from cache or update it,
// throws an exception if unable to get a database or use the cache
async function updateDbWithCache(grypeCommand) {
if (!cache.isFeatureAvailable()) {
throw new Error("cache not available");
}
const cacheDir = await getDbDir(grypeCommand);
// we want the cache to be shared by as many compatible branches as possible, so do not use a
// unique key across matrix builds. even when there is a timing conflict, there is a database
// available as expected
// see: https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
const cacheKey = `grype-db-${grypeVersion}`;
await cache.restoreCache([cacheDir], cacheKey, [], {}, true);
const cachedDbBuildTime = await getDbBuildTime(grypeCommand);
if (cachedDbBuildTime) {
core.info(
`Restored grype db from cache with db build time ${cachedDbBuildTime}`,
);
}
// updateDb will throw an exception on error and potentially skip downloading
// a database if no update exists
await updateDb(grypeCommand);
// if the database was not updated, don't re-cache it
const currentDbBuildTime = await getDbBuildTime(grypeCommand);
if (`${cachedDbBuildTime}` === `${currentDbBuildTime}`) {
core.debug(
`Skipping caching grype db, with build time ${cachedDbBuildTime}`,
);
return;
}
core.debug(`Caching grype db with key ${cacheKey}`);
// this needs to be able to be found by restoreCache, above
await cache.saveCache([cacheDir], cacheKey, {}, true);
}
async function runCommand(cmd, cmdArgs, env) {
let stdout = "";
// This /dev/null writable stream is required so the entire Grype output
// is not written to the GitHub action log. the listener below
// will actually capture the output
const outStream = new stream.Writable({
write(buffer, encoding, next) {
next();
},
});
const exitCode = await core.group(`${cmd} ${cmdArgs.join(" ")}`, () => {
return exec.exec(cmd, cmdArgs, {
env,
ignoreReturnCode: true,
outStream,
listeners: {
stdout(buffer) {
stdout += buffer.toString();
},
stderr(buffer) {
core.info(buffer.toString());
},
debug(message) {
core.debug(message);
},
},
});
});
core.debug(stdout);
return { stdout, exitCode };
}
async function runScan({
source,
failBuild,
severityCutoff,
onlyFixed,
outputFile,
outputFormat,
addCpesIfNone,
byCve,
vex,
cacheDb,
}) {
const out = {};
const env = {
...process.env,
GRYPE_CHECK_FOR_APP_UPDATE: "false",
};
const registryUser = core.getInput("registry-username");
const registryPass = core.getInput("registry-password");
if (registryUser || registryPass) {
env.GRYPE_REGISTRY_AUTH_USERNAME = registryUser;
env.GRYPE_REGISTRY_AUTH_PASSWORD = registryPass;
if (!registryUser || !registryPass) {
core.warning(
"WARNING: registry-username and registry-password must be specified together",
);
}
}
const SEVERITY_LIST = ["negligible", "low", "medium", "high", "critical"];
const FORMAT_LIST = [
"sarif",
"json",
"table",
"cyclonedx-xml",
"cyclonedx-json",
];
let cmdArgs = [];
if (core.isDebug()) {
cmdArgs.push(`-vv`);
}
failBuild = failBuild.toLowerCase() === "true";
onlyFixed = onlyFixed.toLowerCase() === "true";
addCpesIfNone = addCpesIfNone.toLowerCase() === "true";
byCve = byCve.toLowerCase() === "true";
cacheDb = cache.isFeatureAvailable() && cacheDb.toLowerCase() === "true";
cmdArgs.push("-o", outputFormat);
// always output to a file, this is read later to print table output
if (!outputFile) {
outputFile = path.join(
fs.mkdtempSync(path.join(os.tmpdir(), "grype-")),
"output",
);
}
cmdArgs.push("--file", outputFile);
if (
!SEVERITY_LIST.some(
(item) =>
typeof severityCutoff.toLowerCase() === "string" &&
item === severityCutoff.toLowerCase(),
)
) {
throw new Error(
`Invalid severity-cutoff value is set to ${severityCutoff} - must be one of ${SEVERITY_LIST.join(", ")}`,
);
}
if (
!FORMAT_LIST.some(
(item) =>
typeof outputFormat.toLowerCase() === "string" &&
item === outputFormat.toLowerCase(),
)
) {
throw new Error(
`Invalid output-format value is set to ${outputFormat} - must be one of: ${FORMAT_LIST.join(", ")}`,
);
}
core.debug(`Installing grype version ${grypeVersion}`);
const grypeCommand = await installGrype(grypeVersion);
if (cacheDb) {
await updateDbWithCache(grypeCommand);
// since the db was updated and cached separately, skip when running grype
env.GRYPE_DB_AUTO_UPDATE = "false";
}
core.debug("Source: " + source);
core.debug("Fail Build: " + failBuild);
core.debug("Severity Cutoff: " + severityCutoff);
core.debug("Only Fixed: " + onlyFixed);
core.debug("Add Missing CPEs: " + addCpesIfNone);
core.debug("Orient by CVE: " + byCve);
core.debug("Output Format: " + outputFormat);
core.debug("Creating options for GRYPE analyzer");
// Run the grype analyzer
if (severityCutoff !== "") {
cmdArgs.push("--fail-on");
cmdArgs.push(severityCutoff.toLowerCase());
}
if (onlyFixed === true) {
cmdArgs.push("--only-fixed");
}
if (addCpesIfNone === true) {
cmdArgs.push("--add-cpes-if-none");
}
if (byCve === true) {
cmdArgs.push("--by-cve");
}
if (vex) {
cmdArgs.push("--vex");
cmdArgs.push(vex);
}
cmdArgs.push(source);
const { exitCode } = await runCommand(grypeCommand, cmdArgs, env);
out[outputFormat] = outputFile;
if (outputFormat === "table") {
try {
const report = fs.readFileSync(outputFile);
core.info(report.toString());
} catch (e) {
core.warning(`error writing table output contents: ${e}`);
}
}
// If there is a non-zero exit status code there are a couple of potential reporting paths
if (exitCode > 0) {
if (!severityCutoff) {
// There was a non-zero exit status but it wasn't because of failing severity, this must be
// a grype problem
core.warning("grype had a non-zero exit status when running");
} else if (failBuild === true) {
core.setFailed(
`Failed minimum severity level. Found vulnerabilities with level '${severityCutoff}' or higher`,
);
} else {
// There is a non-zero exit status code with severity cut off, although there is still a chance this is grype
// that is broken, it will most probably be a failed severity. Using warning here will make it bubble up in the
// Actions UI
core.warning(
`Failed minimum severity level. Found vulnerabilities with level '${severityCutoff}' or higher`,
);
}
}
return out;
}
module.exports = {
run,
runScan,
installGrype,
};
if (require.main === module) {
const entrypoint = core.getInput("run");
switch (entrypoint) {
case "download-grype": {
installGrype(grypeVersion).then(async (path) => {
core.info(`Downloaded Grype to: ${path}`);
core.setOutput("cmd", path);
// optionally restore, update and cache the db
if (
cache.isFeatureAvailable() &&
(core.getInput("cache-db") || "").toLowerCase() === "true"
) {
await updateDbWithCache(path);
}
});
break;
}
default: {
run().then();
}
}
}