-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.ts
116 lines (95 loc) · 3.39 KB
/
index.ts
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
import * as core from '@actions/core';
import fs from 'fs';
import { generateSARIFReport } from './src/sarif';
import { cliScannerName, cliScannerResult, cliScannerURL, executeScan, numericPriorityForSeverity, pullScanner, ScanExecutionResult, ScanMode } from './src/scanner';
import { ActionInputs, defaultSecureEndpoint } from './src/action';
import { generateSummary } from './src/summary';
import { Report } from './src/report';
export class ExecutionError extends Error {
constructor(stdout: string, stderr: string) {
super("execution error\n\nstdout: " + stdout + "\n\nstderr: " + stderr);
}
}
function writeReport(reportData: string) {
fs.writeFileSync("./report.json", reportData);
core.setOutput("scanReport", "./report.json");
}
export async function run() {
try {
let opts = ActionInputs.parseActionInputs();
opts.printOptions();
let scanFlags = opts.composeFlags();
let scanResult: ScanExecutionResult;
// Download CLI Scanner from 'cliScannerURL'
let retCode = await pullScanner(opts.cliScannerURL);
if (retCode == 0) {
// Execute Scanner
scanResult = await executeScan(scanFlags);
retCode = scanResult.ReturnCode;
if (retCode == 0 || retCode == 1) {
// Transform Scan Results to other formats such as SARIF
if (opts.mode == ScanMode.vm) {
await processScanResult(scanResult, opts);
}
} else {
core.error("Terminating scan. Scanner couldn't be executed.")
}
} else {
core.error("Terminating scan. Scanner couldn't be pulled.")
}
if (opts.stopOnFailedPolicyEval && retCode == 1) {
core.setFailed(`Stopping because Policy Evaluation was FAILED.`);
} else if (opts.standalone && retCode == 0) {
core.info("Policy Evaluation was OMITTED.");
} else if (retCode == 0) {
core.info("Policy Evaluation was PASSED.");
} else if (opts.stopOnProcessingError && retCode > 1) {
core.setFailed(`Stopping because the scanner terminated with an error.`);
} // else: Don't stop regardless the outcome.
} catch (error) {
if (core.getInput('stop-on-processing-error') == 'true') {
core.setFailed("Unexpected error");
}
core.error(error as string);
}
}
function filterResult(report: Report, severity: string) {
let filter_num: number = numericPriorityForSeverity(severity) ?? 5;
report.result.packages.forEach(pkg => {
if (pkg.vulns) pkg.vulns = pkg.vulns.filter((vuln) => numericPriorityForSeverity(vuln.severity.value) ?? 5 <= filter_num);
});
return report;
}
export async function processScanResult(result: ScanExecutionResult, opts: ActionInputs) {
writeReport(result.Output);
let report: Report;
try {
report = JSON.parse(result.Output);
} catch (error) {
core.error("Error parsing analysis JSON report: " + error + ". Output was: " + result.Output);
throw new ExecutionError(result.Output, result.Error);
}
if (report) {
if (opts.severityAtLeast) {
report = filterResult(report, opts.severityAtLeast);
}
generateSARIFReport(report, opts.groupByPackage);
if (!opts.skipSummary) {
core.info("Generating Summary...")
await generateSummary(opts, report);
} else {
core.info("Skipping Summary...")
}
}
}
export {
cliScannerURL,
defaultSecureEndpoint,
pullScanner,
cliScannerName,
executeScan,
cliScannerResult,
};
if (require.main === module) {
run();
}