-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheol.ts
220 lines (189 loc) · 7.24 KB
/
eol.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
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
import fs from 'node:fs';
import path from 'node:path';
import { Command, Flags, ux } from '@oclif/core';
import { batchSubmitPurls } from '../../api/nes/nes.client.ts';
import type { ScanResult } from '../../api/types/hd-cli.types.js';
import type { ComponentStatus, InsightsEolScanComponent } from '../../api/types/nes.types.ts';
import type { Sbom } from '../../service/eol/cdx.svc.ts';
import { getErrorMessage, isErrnoException } from '../../service/error.svc.ts';
import { extractPurls, parsePurlsFile } from '../../service/purls.svc.ts';
import { createStatusDisplay, createTableForStatus, groupComponentsByStatus } from '../../ui/eol.ui.ts';
import { INDICATORS, STATUS_COLORS } from '../../ui/shared.ui.ts';
import ScanSbom from './sbom.ts';
export default class ScanEol extends Command {
static override description = 'Scan a given sbom for EOL data';
static enableJsonFlag = true;
static override examples = [
'<%= config.bin %> <%= command.id %> --dir=./my-project',
'<%= config.bin %> <%= command.id %> --file=path/to/sbom.json',
'<%= config.bin %> <%= command.id %> --purls=path/to/purls.json',
'<%= config.bin %> <%= command.id %> -a --dir=./my-project',
];
static override flags = {
file: Flags.string({
char: 'f',
description: 'The file path of an existing cyclonedx sbom to scan for EOL',
}),
purls: Flags.string({
char: 'p',
description: 'The file path of a list of purls to scan for EOL',
}),
dir: Flags.string({
char: 'd',
description: 'The directory to scan in order to create a cyclonedx sbom',
}),
save: Flags.boolean({
char: 's',
default: false,
description: 'Save the generated report as eol.report.json in the scanned directory',
}),
all: Flags.boolean({
char: 'a',
description: 'Show all components (default is EOL and SUPPORTED only)',
default: false,
}),
table: Flags.boolean({
char: 't',
description: 'Display the results in a table',
default: false,
}),
};
public async run(): Promise<{ components: InsightsEolScanComponent[] }> {
const { flags } = await this.parse(ScanEol);
const scan = await this.getScan(flags, this.config);
ux.action.stop('\nScan completed');
const components = this.getFilteredComponents(scan, flags.all);
if (flags.save) {
await this.saveReport(components);
}
if (!this.jsonEnabled()) {
if (flags.table) {
this.log(`${scan.components.size} components scanned`);
this.displayResultsInTable(scan, flags.all);
} else {
this.displayResults(scan, flags.all);
}
}
return { components };
}
private async getScan(flags: Record<string, string>, config: Command['config']): Promise<ScanResult> {
if (flags.purls) {
ux.action.start(`Scanning purls from ${flags.purls}`);
const purls = this.getPurlsFromFile(flags.purls);
return batchSubmitPurls(purls);
}
const sbom = await ScanSbom.loadSbom(flags, config);
return this.scanSbom(sbom);
}
private getPurlsFromFile(filePath: string): string[] {
try {
const purlsFileString = fs.readFileSync(filePath, 'utf8');
return parsePurlsFile(purlsFileString);
} catch (error) {
this.error(`Failed to read purls file. ${getErrorMessage(error)}`);
}
}
private async scanSbom(sbom: Sbom): Promise<ScanResult> {
let scan: ScanResult;
let purls: string[];
try {
purls = await extractPurls(sbom);
} catch (error) {
this.error(`Failed to extract purls from sbom. ${getErrorMessage(error)}`);
}
try {
scan = await batchSubmitPurls(purls);
} catch (error) {
this.error(`Failed to submit scan to NES from sbom. ${getErrorMessage(error)}`);
}
if (scan.components.size === 0) {
this.warn('No components found in scan');
}
return scan;
}
private getFilteredComponents(scan: ScanResult, all: boolean) {
return Array.from(scan.components.values()).filter(
(component) => all || ['EOL', 'SUPPORTED'].includes(component.info.status),
);
}
private async saveReport(components: InsightsEolScanComponent[]): Promise<void> {
const { flags } = await this.parse(ScanEol);
const reportPath = path.join(flags.dir || process.cwd(), 'eol.report.json');
try {
fs.writeFileSync(reportPath, JSON.stringify({ components }, null, 2));
this.log('Report saved to eol.report.json');
} catch (error) {
if (!isErrnoException(error)) {
this.error(`Failed to save report: ${getErrorMessage(error)}`);
}
switch (error.code) {
case 'EACCES':
this.error('Permission denied. Unable to save report to eol.report.json');
break;
case 'ENOSPC':
this.error('No space left on device. Unable to save report to eol.report.json');
break;
default:
this.error(`Failed to save report: ${getErrorMessage(error)}`);
}
}
}
private displayResults(scan: ScanResult, all: boolean) {
const { UNKNOWN, OK, SUPPORTED, EOL } = createStatusDisplay(scan.components, all);
if (!UNKNOWN.length && !OK.length && !SUPPORTED.length && !EOL.length) {
this.displayNoComponentsMessage(all);
return;
}
this.log(ux.colorize('bold', 'Here are the results of the scan:'));
this.logLine();
// Display sections in order of increasing severity
for (const components of [UNKNOWN, OK, SUPPORTED, EOL]) {
this.displayStatusSection(components);
}
this.logLegend();
}
private displayResultsInTable(scan: ScanResult, all: boolean) {
const grouped = groupComponentsByStatus(scan.components);
const statuses: ComponentStatus[] = ['SUPPORTED', 'EOL'];
if (all) {
statuses.unshift('UNKNOWN', 'OK');
}
for (const status of statuses) {
const components = grouped[status];
if (components.length > 0) {
const table = createTableForStatus(grouped, status);
this.displayTable(table, components.length, status);
}
}
this.logLegend();
}
private displayTable(table: string, count: number, status: ComponentStatus): void {
this.log(ux.colorize(STATUS_COLORS[status], `${INDICATORS[status]} ${count} ${status} Component(s):`));
this.log(ux.colorize(STATUS_COLORS[status], table));
}
private displayNoComponentsMessage(all: boolean): void {
if (!all) {
this.log(ux.colorize('yellow', 'No End-of-Life or Supported components found in scan.'));
this.log(ux.colorize('yellow', 'Use --all flag to view all components.'));
} else {
this.log(ux.colorize('yellow', 'No components found in scan.'));
}
}
private logLine(): void {
this.log(ux.colorize('bold', '-'.repeat(50)));
}
private displayStatusSection(components: string[]): void {
if (components.length > 0) {
this.log(components.join('\n'));
this.logLine();
}
}
private logLegend(): void {
this.log(ux.colorize(STATUS_COLORS.UNKNOWN, `${INDICATORS.UNKNOWN} = No Known Issues`));
this.log(ux.colorize(STATUS_COLORS.OK, `${INDICATORS.OK} = OK`));
this.log(
ux.colorize(STATUS_COLORS.SUPPORTED, `${INDICATORS.SUPPORTED}= Supported: End-of-Life (EOL) is scheduled`),
);
this.log(ux.colorize(STATUS_COLORS.EOL, `${INDICATORS.EOL} = End of Life (EOL)`));
}
}