-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathcheck-tools.js
More file actions
60 lines (50 loc) · 1.78 KB
/
Copy pathcheck-tools.js
File metadata and controls
60 lines (50 loc) · 1.78 KB
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
#!/usr/bin/env node
const { execFileSync } = require('child_process');
if (process.argv.includes('--help')) {
process.stdout.write(`check-tools.js — Detects whether opengrep and trivy are installed.
Usage:
node check-tools.js
No flags required.
Exit codes:
0 Both tools available
1 At least one tool missing (see error field per tool)
Output (stdout, JSON):
{
"opengrep": { "available": true, "version": "1.50.0", "error": null },
"trivy": { "available": false, "version": null, "error": "command not found" }
}
`);
process.exit(0);
}
function probe(runVersion, parseVersion) {
try {
const out = runVersion();
return { available: true, version: parseVersion(out), error: null };
} catch (err) {
return { available: false, version: null, error: (err.stderr || err.message || '').toString().trim() };
}
}
// Keep executable names at the child_process call sites. Passing an executable
// into probe() would make future CLI/env-derived values indistinguishable from
// this fixed two-tool allowlist to the repository security validator.
const runOpenGrepVersion = () => execFileSync('opengrep', ['--version'], {
encoding: 'utf8',
timeout: 60000,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
});
const runTrivyVersion = () => execFileSync('trivy', ['--version'], {
encoding: 'utf8',
timeout: 60000,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
});
const result = {
opengrep: probe(runOpenGrepVersion, (out) => (out.match(/[\d.]+/) || [null])[0]),
trivy: probe(runTrivyVersion, (out) => {
const m = out.match(/Version:\s*([\d.]+)/i) || out.match(/[\d.]+/);
return m ? m[1] || m[0] : null;
}),
};
process.stdout.write(JSON.stringify(result, null, 2) + '\n');
process.exit(result.opengrep.available && result.trivy.available ? 0 : 1);