forked from CycloneDX/cdxgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.js
313 lines (304 loc) · 7.89 KB
/
display.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
import { existsSync, readFileSync } from "fs";
import { createStream, table } from "table";
// https://github.com/yangshun/tree-node-cli/blob/master/src/index.js
const SYMBOLS_ANSI = {
BRANCH: "├── ",
EMPTY: "",
INDENT: " ",
LAST_BRANCH: "└── ",
VERTICAL: "│ "
};
const MAX_TREE_DEPTH = 6;
export const printTable = (bomJson) => {
if (!bomJson || !bomJson.components) {
return;
}
if (
bomJson.metadata &&
bomJson.metadata.component &&
["operating-system", "platform"].includes(bomJson.metadata.component.type)
) {
return printOSTable(bomJson);
}
const config = {
columnDefault: {
width: 30
},
columnCount: 4,
columns: [
{ width: 25 },
{ width: 35 },
{ width: 25, alignment: "right" },
{ width: 15 }
]
};
const stream = createStream(config);
stream.write(["Group", "Name", "Version", "Scope"]);
for (const comp of bomJson.components) {
stream.write([
comp.group || "",
comp.name,
`\x1b[1;35m${comp.version}\x1b[0m`,
comp.scope || ""
]);
}
console.log();
console.log(
"BOM includes",
bomJson.components.length,
"components and",
bomJson.dependencies.length,
"dependencies"
);
};
const formatProps = (props) => {
const retList = [];
for (const p of props) {
retList.push(`\x1b[0;32m${p.name}\x1b[0m ${p.value}`);
}
return retList.join("\n");
};
export const printOSTable = (bomJson) => {
const config = {
columnDefault: {
width: 50
},
columnCount: 3,
columns: [{ width: 20 }, { width: 40 }, { width: 50 }]
};
const stream = createStream(config);
stream.write(["Type", "Title", "Properties"]);
for (const comp of bomJson.components) {
stream.write([
comp.type,
`\x1b[1;35m${comp.name.replace(/\+/g, " ").replace(/--/g, "::")}\x1b[0m`,
formatProps(comp.properties || [])
]);
}
console.log();
};
export const printServices = (bomJson) => {
const data = [["Name", "Endpoints", "Authenticated", "X Trust Boundary"]];
if (!bomJson || !bomJson.services) {
return;
}
for (const aservice of bomJson.services) {
data.push([
aservice.name || "",
aservice.endpoints ? aservice.endpoints.join("\n") : "",
aservice.authenticated ? "\x1b[1;35mYes\x1b[0m" : "",
aservice.xTrustBoundary ? "\x1b[1;35mYes\x1b[0m" : ""
]);
}
const config = {
header: {
alignment: "center",
content: "List of Services\nGenerated with \u2665 by cdxgen"
}
};
if (data.length > 1) {
console.log(table(data, config));
}
};
const locationComparator = (a, b) => {
if (a && b && a.includes("#") && b.includes("#")) {
const tmpA = a.split("#");
const tmpB = b.split("#");
if (tmpA.length === 2 && tmpB.length === 2) {
if (tmpA[0] == tmpB[0]) {
return tmpA[1] - tmpB[1];
}
}
}
return a.localeCompare(b);
};
export const printOccurrences = (bomJson) => {
const data = [["Group", "Name", "Version", "Occurrences"]];
if (!bomJson || !bomJson.components) {
return;
}
for (const comp of bomJson.components) {
if (!comp.evidence || !comp.evidence.occurrences) {
continue;
}
data.push([
comp.group || "",
comp.name,
comp.version,
comp.evidence.occurrences
.map((l) => l.location)
.sort(locationComparator)
.join("\n")
]);
}
const config = {
header: {
alignment: "center",
content: "Component Evidence\nGenerated with \u2665 by cdxgen"
}
};
if (data.length > 1) {
console.log(table(data, config));
}
};
export const printCallStack = (bomJson) => {
const data = [["Group", "Name", "Version", "Call Stack"]];
if (!bomJson || !bomJson.components) {
return;
}
for (const comp of bomJson.components) {
if (
!comp.evidence ||
!comp.evidence.callstack ||
!comp.evidence.callstack.frames
) {
continue;
}
const frames = Array.from(
new Set(
comp.evidence.callstack.frames.map(
(c) => `${c.fullFilename}${c.line ? "#" + c.line : ""}`
)
)
).sort(locationComparator);
let frameDisplay = [frames[0]];
if (frames.length > 1) {
for (let i = 1; i < frames.length - 1; i++) {
frameDisplay.push(`${SYMBOLS_ANSI.BRANCH} ${frames[i]}`);
}
frameDisplay.push(
`${SYMBOLS_ANSI.LAST_BRANCH} ${frames[frames.length - 1]}`
);
}
data.push([
comp.group || "",
comp.name,
comp.version,
frameDisplay.join("\n")
]);
}
const config = {
header: {
alignment: "center",
content: "Component Call Stack Evidence\nGenerated with \u2665 by cdxgen"
}
};
if (data.length > 1) {
console.log(table(data, config));
}
};
export const printDependencyTree = (bomJson) => {
const dependencies = bomJson.dependencies || [];
if (!dependencies.length) {
return;
}
const depMap = {};
for (const d of dependencies) {
if (d.dependsOn && d.dependsOn.length) {
depMap[d.ref] = d.dependsOn.sort();
}
}
const shownList = [];
const treeGraphics = [];
recursePrint(depMap, dependencies, 0, shownList, treeGraphics);
// table library is too slow for display large lists.
// Fixes #491
if (treeGraphics.length < 100) {
const config = {
header: {
alignment: "center",
content: "Dependency Tree\nGenerated with \u2665 by cdxgen"
}
};
console.log(table([[treeGraphics.join("\n")]], config));
} else {
console.log(treeGraphics.join("\n"));
}
};
const levelPrefix = (level, isLast) => {
if (level === 0) {
return SYMBOLS_ANSI.EMPTY;
}
let prefix = `${isLast ? SYMBOLS_ANSI.LAST_BRANCH : SYMBOLS_ANSI.BRANCH}`;
for (let i = 0; i < level - 1; i++) {
prefix = `${
isLast
? SYMBOLS_ANSI.LAST_BRANCH.replace(" ", "─")
: SYMBOLS_ANSI.VERTICAL
}${isLast ? "" : SYMBOLS_ANSI.INDENT}${prefix}`;
}
return prefix;
};
const isReallyRoot = (depMap, refStr) => {
for (const k of Object.keys(depMap)) {
const dependsOn = depMap[k] || [];
if (
dependsOn.includes(refStr) ||
dependsOn.includes(refStr.toLowerCase())
) {
return false;
}
}
return true;
};
const recursePrint = (depMap, subtree, level, shownList, treeGraphics) => {
const listToUse = Array.isArray(subtree) ? subtree : [subtree];
for (let i = 0; i < listToUse.length; i++) {
const l = listToUse[i];
const refStr = l.ref || l;
if (
(level === 0 &&
isReallyRoot(depMap, refStr) &&
!shownList.includes(refStr.toLowerCase())) ||
level > 0
) {
treeGraphics.push(
`${levelPrefix(level, i == listToUse.length - 1)}${refStr}`
);
shownList.push(refStr.toLowerCase());
if (l && depMap[refStr]) {
if (level < MAX_TREE_DEPTH) {
recursePrint(
depMap,
depMap[refStr],
level + 1,
shownList,
treeGraphics
);
}
}
}
}
};
export const printReachables = (sliceArtefacts) => {
const reachablesSlicesFile = sliceArtefacts.reachablesSlicesFile;
if (!existsSync(reachablesSlicesFile)) {
return;
}
const purlCounts = {};
const reachablesSlices = JSON.parse(
readFileSync(reachablesSlicesFile, "utf-8")
);
for (const areachable of reachablesSlices.reachables || []) {
const purls = areachable.purls || [];
for (const apurl of purls) {
purlCounts[apurl] = (purlCounts[apurl] || 0) + 1;
}
}
const sortedPurls = Object.fromEntries(
Object.entries(purlCounts).sort(([, a], [, b]) => b - a)
);
const data = [["Package URL", "Reachable Flows"]];
for (const apurl of Object.keys(sortedPurls)) {
data.push([apurl, "" + sortedPurls[apurl]]);
}
const config = {
header: {
alignment: "center",
content: "Reachable Components\nGenerated with \u2665 by cdxgen"
}
};
if (data.length > 1) {
console.log(table(data, config));
}
};