@@ -58,30 +58,31 @@ export default class CycloneDxSbom {
58
58
rootComponent
59
59
components
60
60
dependencies
61
+
61
62
constructor ( ) {
62
63
this . dependencies = new Array ( )
63
64
this . components = new Array ( )
64
65
65
66
66
67
}
68
+
67
69
/**
68
70
* @param {PackageURL } root - add main/root component for sbom
69
71
* @return {CycloneDxSbom } the CycloneDxSbom Sbom Object
70
72
*/
71
- addRoot ( root ) {
73
+ addRoot ( root ) {
72
74
73
75
this . rootComponent =
74
- getComponent ( root , "application" )
76
+ getComponent ( root , "application" )
75
77
this . components . push ( this . rootComponent )
76
78
return this
77
79
}
78
80
79
81
80
-
81
82
/**
82
83
* @return {{{"bom-ref": string, name, purl: string, type, version}} } root component of sbom.
83
84
*/
84
- getRoot ( ) {
85
+ getRoot ( ) {
85
86
return this . rootComponent
86
87
}
87
88
@@ -90,52 +91,52 @@ export default class CycloneDxSbom {
90
91
* @param {PackageURL } targetRef current dependency to add to Dependencies list of component sourceRef
91
92
* @return Sbom
92
93
*/
93
- addDependency ( sourceRef , targetRef ) {
94
+ addDependency ( sourceRef , targetRef ) {
94
95
let componentIndex = this . getComponentIndex ( sourceRef ) ;
95
- if ( componentIndex < 0 )
96
- {
97
- this . components . push ( getComponent ( sourceRef , "library" ) )
96
+ if ( componentIndex < 0 ) {
97
+ this . components . push ( getComponent ( sourceRef , "library" ) )
98
98
}
99
99
let dependencyIndex = this . getDependencyIndex ( sourceRef . purl )
100
- if ( dependencyIndex < 0 )
101
- {
100
+ if ( dependencyIndex < 0 ) {
102
101
this . dependencies . push ( createDependency ( sourceRef . purl ) )
103
102
dependencyIndex = this . getDependencyIndex ( sourceRef . purl )
104
103
}
105
104
106
105
//Only if the dependency doesn't exists on the dependency list of dependency, then add it to this list.
107
- if ( this . dependencies [ dependencyIndex ] . dependsOn . findIndex ( dep => dep === targetRef . toString ( ) ) === - 1 )
108
- {
106
+ if ( this . dependencies [ dependencyIndex ] . dependsOn . findIndex ( dep => dep === targetRef . toString ( ) ) === - 1 ) {
109
107
this . dependencies [ dependencyIndex ] . dependsOn . push ( targetRef . toString ( ) )
110
108
}
111
- if ( this . getDependencyIndex ( targetRef . toString ( ) ) < 0 )
112
- {
109
+ if ( this . getDependencyIndex ( targetRef . toString ( ) ) < 0 ) {
113
110
this . dependencies . push ( createDependency ( targetRef . toString ( ) ) )
114
111
}
115
- let newComponent = getComponent ( targetRef , "library" ) ;
112
+ let newComponent = getComponent ( targetRef , "library" ) ;
116
113
// Only if component doesn't exists in component list, add it to the list.
117
- if ( this . getComponentIndex ( newComponent ) < 0 )
118
- {
114
+ if ( this . getComponentIndex ( newComponent ) < 0 ) {
119
115
this . components . push ( newComponent )
120
116
}
121
117
return this
122
118
}
119
+
123
120
/**
124
121
* @return String CycloneDx Sbom json object in a string format
125
122
*/
126
- getAsJsonString ( ) {
123
+ getAsJsonString ( ) {
127
124
this . sbomObject = {
128
- "bomFormat" : "CycloneDX" ,
129
- "specVersion" : "1.4" ,
130
- "version" : 1 ,
131
- "metadata" : {
132
- "timestamp" : new Date ( ) ,
133
- "component" : this . rootComponent
125
+ "bomFormat" : "CycloneDX" ,
126
+ "specVersion" : "1.4" ,
127
+ "version" : 1 ,
128
+ "metadata" : {
129
+ "timestamp" : new Date ( ) ,
130
+ "component" : this . rootComponent
134
131
} ,
135
- "components" : this . components ,
136
- "dependencies" : this . dependencies
132
+ "components" : this . components ,
133
+ "dependencies" : this . dependencies
134
+ }
135
+ if ( this . rootComponent === undefined )
136
+ {
137
+ delete this . sbomObject . metadata . component
137
138
}
138
- if ( process . env [ "EXHORT_DEBUG" ] === "true" ) {
139
+ if ( process . env [ "EXHORT_DEBUG" ] === "true" ) {
139
140
console . log ( "SBOM Generated for manifest, to be sent to exhort service:" + EOL + JSON . stringify ( this . sbomObject , null , 4 ) )
140
141
}
141
142
return JSON . stringify ( this . sbomObject )
@@ -146,7 +147,7 @@ export default class CycloneDxSbom {
146
147
* @param {String } dependency - purl string of the component.
147
148
* @return {int } - the index of the dependency in dependencies Array, returns -1 if not found.
148
149
*/
149
- getDependencyIndex ( dependency ) {
150
+ getDependencyIndex ( dependency ) {
150
151
return this . dependencies . findIndex ( dep => dep . ref === dependency )
151
152
}
152
153
@@ -156,7 +157,7 @@ export default class CycloneDxSbom {
156
157
* @return {int } index of the found component entry, if not found returns -1.
157
158
* @private
158
159
*/
159
- getComponentIndex ( theComponent ) {
160
+ getComponentIndex ( theComponent ) {
160
161
161
162
return this . components . findIndex ( component => component . purl === theComponent . purl )
162
163
}
@@ -165,33 +166,30 @@ export default class CycloneDxSbom {
165
166
* @param purl {PackageURL}
166
167
* @return component
167
168
*/
168
- purlToComponent ( purl )
169
- {
170
- return getComponent ( purl , "library" )
169
+ purlToComponent ( purl ) {
170
+ return getComponent ( purl , "library" )
171
171
}
172
+
172
173
/**
173
174
* This method gets an array of dependencies to be ignored, and remove all of them from CycloneDx Sbom
174
175
* @param {Array } dependencies to be removed from sbom
175
176
* @return {CycloneDxSbom } without ignored dependencies
176
177
*/
177
- filterIgnoredDeps ( deps ) {
178
+ filterIgnoredDeps ( deps ) {
178
179
deps . forEach ( dep => {
179
- let index = this . components . findIndex ( component => component . name === dep ) ;
180
- if ( index >= 0 )
181
- {
182
- this . components . splice ( index , 1 )
180
+ let index = this . components . findIndex ( component => component . name === dep ) ;
181
+ if ( index >= 0 ) {
182
+ this . components . splice ( index , 1 )
183
183
}
184
184
index = this . dependencies . findIndex ( dependency => dependency . ref . includes ( dep ) ) ;
185
- if ( index >= 0 )
186
- {
187
- this . dependencies . splice ( index , 1 )
185
+ if ( index >= 0 ) {
186
+ this . dependencies . splice ( index , 1 )
188
187
}
189
188
190
189
this . dependencies . forEach ( dependency => {
191
190
let indexDependsOn = dependency . dependsOn . findIndex ( theDep => theDep . includes ( dep ) ) ;
192
- if ( indexDependsOn > - 1 )
193
- {
194
- dependency . dependsOn . splice ( indexDependsOn , 1 )
191
+ if ( indexDependsOn > - 1 ) {
192
+ dependency . dependsOn . splice ( indexDependsOn , 1 )
195
193
}
196
194
} )
197
195
} )
@@ -203,24 +201,21 @@ export default class CycloneDxSbom {
203
201
* @param {Array } dependencies to be removed from sbom
204
202
* @return {CycloneDxSbom } without ignored dependencies
205
203
*/
206
- filterIgnoredDepsIncludingVersion ( deps ) {
204
+ filterIgnoredDepsIncludingVersion ( deps ) {
207
205
deps . forEach ( dep => {
208
- let index = this . components . findIndex ( component => component . purl === dep ) ;
209
- if ( index >= 0 )
210
- {
211
- this . components . splice ( index , 1 )
206
+ let index = this . components . findIndex ( component => component . purl === dep ) ;
207
+ if ( index >= 0 ) {
208
+ this . components . splice ( index , 1 )
212
209
}
213
210
index = this . dependencies . findIndex ( dependency => dependency . ref === dep ) ;
214
- if ( index >= 0 )
215
- {
216
- this . dependencies . splice ( index , 1 )
211
+ if ( index >= 0 ) {
212
+ this . dependencies . splice ( index , 1 )
217
213
}
218
214
219
215
this . dependencies . forEach ( dependency => {
220
- let indexDependsOn = dependency . dependsOn . findIndex ( theDep => theDep === dep ) ;
221
- if ( indexDependsOn > - 1 )
222
- {
223
- dependency . dependsOn . splice ( indexDependsOn , 1 )
216
+ let indexDependsOn = dependency . dependsOn . findIndex ( theDep => theDep === dep ) ;
217
+ if ( indexDependsOn > - 1 ) {
218
+ dependency . dependsOn . splice ( indexDependsOn , 1 )
224
219
}
225
220
} )
226
221
} )
@@ -233,24 +228,28 @@ export default class CycloneDxSbom {
233
228
*
234
229
* @return {boolean }
235
230
*/
236
- checkIfPackageInsideDependsOnList ( component , name )
237
- {
231
+ checkIfPackageInsideDependsOnList ( component , name ) {
238
232
239
233
let dependencyIndex = this . getDependencyIndex ( component . purl )
240
- if ( dependencyIndex < 0 )
241
- {
234
+ if ( dependencyIndex < 0 ) {
242
235
return false
243
236
}
244
237
245
238
//Only if the dependency doesn't exists on the dependency list of dependency, then add it to this list.
246
- if ( this . dependencies [ dependencyIndex ] . dependsOn . findIndex ( dep => dep . includes ( name ) ) >= 0 )
247
- {
239
+ if ( this . dependencies [ dependencyIndex ] . dependsOn . findIndex ( dep => dep . includes ( name ) ) >= 0 ) {
248
240
return true ;
249
- }
250
- else {
241
+ } else {
251
242
return false
252
243
}
253
244
}
254
245
255
-
246
+ /** Removes the root component from the sbom
247
+ */
248
+ removeRootComponent ( ) {
249
+ let compIndex = this . getComponentIndex ( this . rootComponent )
250
+ let depIndex = this . getDependencyIndex ( this . rootComponent . purl )
251
+ this . components . splice ( compIndex , 1 )
252
+ this . dependencies . splice ( depIndex , 1 )
253
+ this . rootComponent = undefined
254
+ }
256
255
}
0 commit comments