forked from CycloneDX/cdxgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgen.js
178 lines (174 loc) · 5 KB
/
postgen.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
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { dirNameStr } from "./utils.js";
/**
* Filter and enhance BOM post generation.
*
* @param {Object} bomNSData BOM with namespaces object
* @param {Object} options CLI options
*
* @returns {Object} Modified bomNSData
*/
export const postProcess = (bomNSData, options) => {
let jsonPayload = bomNSData.bomJson;
if (
typeof bomNSData.bomJson === "string" ||
bomNSData.bomJson instanceof String
) {
jsonPayload = JSON.parse(bomNSData.bomJson);
}
bomNSData.bomJson = filterBom(jsonPayload, options);
bomNSData.bomJson = applyStandards(bomNSData.bomJson, options);
return bomNSData;
};
/**
* Apply definitions.standards based on options
*
* @param {Object} bomJson BOM JSON Object
* @param {Object} options CLI options
*
* @returns {Object} Filtered BOM JSON
*/
export const applyStandards = (bomJson, options) => {
if (options.standard && Array.isArray(options.standard)) {
for (const astandard of options.standard) {
const templateFile = join(
dirNameStr,
"data",
"templates",
`${astandard}.cdx.json`,
);
if (existsSync(templateFile)) {
const templateData = JSON.parse(readFileSync(templateFile, "utf-8"));
if (templateData?.metadata?.licenses) {
if (!bomJson.metadata.licenses) {
bomJson.metadata.licenses = [];
}
bomJson.metadata.licenses = bomJson.metadata.licenses.concat(
templateData.metadata.licenses,
);
}
if (templateData?.definitions?.standards) {
if (!bomJson.definitions) {
bomJson.definitions = { standards: [] };
}
bomJson.definitions.standards = bomJson.definitions.standards.concat(
templateData.definitions.standards,
);
}
}
}
}
return bomJson;
};
/**
* Filter BOM based on options
*
* @param {Object} bomJson BOM JSON Object
* @param {Object} options CLI options
*
* @returns {Object} Filtered BOM JSON
*/
export const filterBom = (bomJson, options) => {
const newPkgMap = {};
let filtered = false;
for (const comp of bomJson.components) {
if (
options.requiredOnly &&
comp.scope &&
["optional", "excluded"].includes(comp.scope)
) {
filtered = true;
} else if (options.only?.length) {
if (!Array.isArray(options.only)) {
options.only = [options.only];
}
let purlfiltered = false;
for (const filterstr of options.only) {
if (
filterstr.length &&
!comp.purl.toLowerCase().includes(filterstr.toLowerCase())
) {
filtered = true;
purlfiltered = true;
}
}
if (!purlfiltered) {
newPkgMap[comp["bom-ref"]] = comp;
}
} else if (options.filter?.length) {
if (!Array.isArray(options.filter)) {
options.filter = [options.filter];
}
let purlfiltered = false;
for (const filterstr of options.filter) {
// Check the purl
if (
filterstr.length &&
comp.purl.toLowerCase().includes(filterstr.toLowerCase())
) {
filtered = true;
purlfiltered = true;
continue;
}
// Look for any properties value matching the string
const properties = comp.properties || [];
for (const aprop of properties) {
if (
filterstr.length &&
aprop &&
aprop.value &&
aprop.value.toLowerCase().includes(filterstr.toLowerCase())
) {
filtered = true;
purlfiltered = true;
}
}
}
if (!purlfiltered) {
newPkgMap[comp["bom-ref"]] = comp;
}
} else {
newPkgMap[comp["bom-ref"]] = comp;
}
}
if (filtered) {
const newcomponents = [];
const newdependencies = [];
for (const aref of Object.keys(newPkgMap).sort()) {
newcomponents.push(newPkgMap[aref]);
}
for (const adep of bomJson.dependencies) {
if (newPkgMap[adep.ref]) {
const newdepson = (adep.dependsOn || []).filter((d) => newPkgMap[d]);
const obj = {
ref: adep.ref,
dependsOn: newdepson,
};
// Filter provides array if needed
if (adep.provides?.length) {
obj.provides = adep.provides.filter((d) => newPkgMap[d]);
}
newdependencies.push(obj);
}
}
bomJson.components = newcomponents;
bomJson.dependencies = newdependencies;
// We set the compositions.aggregate to incomplete by default
if (
options.specVersion >= 1.5 &&
options.autoCompositions &&
bomJson.metadata &&
bomJson.metadata.component
) {
if (!bomJson.compositions) {
bomJson.compositions = [];
}
bomJson.compositions.push({
"bom-ref": bomJson.metadata.component["bom-ref"],
aggregate: options.only ? "incomplete_first_party_only" : "incomplete",
});
}
}
return bomJson;
};