forked from anchore/scan-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (220 loc) · 6.83 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
const cache = require("@actions/tool-cache");
const core = require("@actions/core");
const { exec } = require("@actions/exec");
const fs = require("fs");
const stream = require("stream");
const { GRYPE_VERSION } = require("./GrypeVersion");
const grypeBinary = "grype";
const grypeVersion = core.getInput("grype-version") || GRYPE_VERSION;
async function downloadGrype(version) {
let url = `https://raw.githubusercontent.com/anchore/grype/main/install.sh`;
core.debug(`Installing ${version}`);
// TODO: when grype starts supporting unreleased versions, support it here
// Download the installer, and run
const installPath = await cache.downloadTool(url);
// Make sure the tool's executable bit is set
await exec(`chmod +x ${installPath}`);
let cmd = `${installPath} -b ${installPath}_grype ${version}`;
await exec(cmd);
let grypePath = `${installPath}_grype/grype`;
// Cache the downloaded file
return cache.cacheFile(grypePath, `grype`, `grype`, version);
}
async function installGrype(version) {
let grypePath = cache.find(grypeBinary, version);
if (!grypePath) {
// Not found, install it
grypePath = await downloadGrype(version);
}
// Add tool to path for this and future actions to use
core.addPath(grypePath);
return `${grypePath}/${grypeBinary}`;
}
// 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() {
var image = core.getInput("image");
var path = core.getInput("path");
var 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
path = ".";
}
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 acsReportEnable = core.getInput("acs-report-enable") || "true";
const severityCutoff = core.getInput("severity-cutoff") || "medium";
const out = await runScan({
source,
failBuild,
acsReportEnable,
severityCutoff,
});
Object.keys(out).map((key) => {
core.setOutput(key, out[key]);
});
} catch (error) {
core.setFailed(error.message);
}
}
async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
const out = {};
const 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"];
let cmdArgs = [];
if (core.isDebug()) {
cmdArgs.push(`-vv`);
}
failBuild = failBuild.toLowerCase() === "true";
acsReportEnable = acsReportEnable.toLowerCase() === "true";
if (acsReportEnable) {
cmdArgs.push("-o", "sarif");
} else {
cmdArgs.push("-o", "json");
}
if (
!SEVERITY_LIST.some(
(item) =>
typeof severityCutoff.toLowerCase() === "string" &&
item === severityCutoff.toLowerCase()
)
) {
throw new Error(
`Invalid severity-cutoff value is set to ${severityCutoff} - please ensure you are choosing either negligible, low, medium, high, or critical`
);
}
core.debug(`Installing grype version ${grypeVersion}`);
await installGrype(grypeVersion);
core.debug("Source: " + source);
core.debug("Fail Build: " + failBuild);
core.debug("Severity Cutoff: " + severityCutoff);
core.debug("ACS Enable: " + acsReportEnable);
core.debug("Creating options for GRYPE analyzer");
// Run the grype analyzer
let cmdOutput = "";
let cmd = `${grypeBinary}`;
if (severityCutoff !== "") {
cmdArgs.push("--fail-on");
cmdArgs.push(severityCutoff.toLowerCase());
}
cmdArgs.push(source);
// 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} output...`, async () => {
core.info(`Executing: ${cmd} ` + cmdArgs.join(" "));
return exec(cmd, cmdArgs, {
env,
ignoreReturnCode: true,
outStream,
listeners: {
stdout(buffer) {
cmdOutput += buffer.toString();
},
stderr(buffer) {
core.info(buffer.toString());
},
debug(message) {
core.debug(message);
},
},
});
});
if (core.isDebug()) {
core.debug("Grype output:");
core.debug(cmdOutput);
}
if (acsReportEnable) {
const SARIF_FILE = "./results.sarif";
fs.writeFileSync(SARIF_FILE, cmdOutput);
out.sarif = SARIF_FILE;
}
if (failBuild === true && exitCode > 0) {
core.setFailed(
`Failed minimum severity level. Found vulnerabilities with level ${severityCutoff} or higher`
);
}
// If there is a non-zero exit status code there are a couple of potential reporting paths
if (failBuild === false && exitCode > 0) {
// There was a non-zero exit status but it wasn't because of failing severity, this must be
// a grype problem
if (!severityCutoff) {
core.warning("grype had a non-zero exit status when running");
} 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((path) => {
core.info(`Downloaded Grype to: ${path}`);
core.setOutput("cmd", path);
});
break;
}
default: {
run().then();
}
}
}