@@ -3,6 +3,7 @@ package cmd
3
3
import (
4
4
"fmt"
5
5
"hash/fnv"
6
+ "slices"
6
7
"time"
7
8
8
9
"github.com/CycloneDX/cyclonedx-go"
@@ -12,8 +13,10 @@ import (
12
13
)
13
14
14
15
const (
15
- CdxPrefix = "cdx:"
16
- KSOCPrefix = "ksoc:kbom:"
16
+ CdxPrefix = "cdx:"
17
+ KSOCPrefix = "ksoc:kbom:"
18
+ K8sComponentType = "k8s:component:type"
19
+ K8sComponentName = "k8s:component:name"
17
20
18
21
ClusterType = "cluster"
19
22
NodeType = "node"
@@ -33,24 +36,13 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
33
36
Version : kbom .GeneratedBy .Version ,
34
37
},
35
38
},
36
- Component : & cyclonedx.Component {
37
- BOMRef : id (kbom .GeneratedBy ),
38
- Type : cyclonedx .ComponentTypeApplication ,
39
- Name : kbom .GeneratedBy .Name ,
40
- Hashes : & []cyclonedx.Hash {
41
- {
42
- Algorithm : cyclonedx .HashAlgoSHA256 ,
43
- Value : kbom .GeneratedBy .Commit ,
44
- },
45
- },
46
- Version : kbom .GeneratedBy .Version ,
47
- },
48
39
}
49
40
50
41
components := []cyclonedx.Component {}
42
+ dependencies := []cyclonedx.Dependency {}
51
43
clusterProperties := []cyclonedx.Property {
52
44
{
53
- Name : CdxPrefix + "k8s:component:type" ,
45
+ Name : CdxPrefix + K8sComponentType ,
54
46
Value : ClusterType ,
55
47
},
56
48
{
@@ -85,28 +77,29 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
85
77
}
86
78
87
79
clusterComponent := cyclonedx.Component {
88
- BOMRef : id ( kbom .Cluster ),
80
+ BOMRef : kbom .Cluster . BOMRef ( ),
89
81
Type : cyclonedx .ComponentTypePlatform ,
90
- Name : "cluster" ,
82
+ Name : kbom . Cluster . BOMName () ,
91
83
Version : kbom .Cluster .K8sVersion ,
92
84
Properties : & clusterProperties ,
93
85
}
86
+ cdxBOM .Metadata .Component = & clusterComponent
94
87
95
- components = append (components , clusterComponent )
96
-
88
+ clusterDependencies := make (map [string ]string )
97
89
for i := range kbom .Cluster .Nodes {
98
90
n := kbom .Cluster .Nodes [i ]
91
+ bomRef := id (n )
99
92
components = append (components , cyclonedx.Component {
100
- BOMRef : id ( n ) ,
93
+ BOMRef : bomRef ,
101
94
Type : cyclonedx .ComponentTypePlatform ,
102
95
Name : n .Name ,
103
96
Properties : & []cyclonedx.Property {
104
97
{
105
- Name : CdxPrefix + "k8s:component:type" ,
98
+ Name : CdxPrefix + K8sComponentType ,
106
99
Value : NodeType ,
107
100
},
108
101
{
109
- Name : CdxPrefix + "k8s:component:name" ,
102
+ Name : CdxPrefix + K8sComponentName ,
110
103
Value : n .Name ,
111
104
},
112
105
{
@@ -187,22 +180,24 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
187
180
},
188
181
},
189
182
})
183
+ clusterDependencies [bomRef ] = bomRef
190
184
}
191
185
192
186
for _ , img := range kbom .Cluster .Components .Images {
187
+ bomRef := img .PkgID ()
193
188
container := cyclonedx.Component {
194
- BOMRef : img . PkgID () ,
189
+ BOMRef : bomRef ,
195
190
Type : cyclonedx .ComponentTypeContainer ,
196
191
Name : img .Name ,
197
192
Version : img .Digest ,
198
- PackageURL : img . PkgID () ,
193
+ PackageURL : bomRef ,
199
194
Properties : & []cyclonedx.Property {
200
195
{
201
- Name : CdxPrefix + "k8s:component:type" ,
196
+ Name : CdxPrefix + K8sComponentType ,
202
197
Value : ContainerType ,
203
198
},
204
199
{
205
- Name : CdxPrefix + "k8s:component:name" ,
200
+ Name : CdxPrefix + K8sComponentName ,
206
201
Value : img .Name ,
207
202
},
208
203
{
@@ -225,17 +220,21 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
225
220
}
226
221
227
222
components = append (components , container )
223
+
224
+ if img .ControlPlane {
225
+ clusterDependencies [bomRef ] = bomRef
226
+ }
228
227
}
229
228
230
229
for _ , resList := range kbom .Cluster .Components .Resources {
231
230
for _ , res := range resList .Resources {
232
231
properties := []cyclonedx.Property {
233
232
{
234
- Name : CdxPrefix + "k8s:component:type" ,
233
+ Name : CdxPrefix + K8sComponentType ,
235
234
Value : resList .Kind ,
236
235
},
237
236
{
238
- Name : CdxPrefix + "k8s:component:name" ,
237
+ Name : CdxPrefix + K8sComponentName ,
239
238
Value : res .Name ,
240
239
},
241
240
{
@@ -263,9 +262,21 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
263
262
}
264
263
}
265
264
266
- cdxBOM .Components = & components
265
+ clusterDependenciesArr := make ([]string , 0 )
266
+ for _ , dep := range clusterDependencies {
267
+ clusterDependenciesArr = append (clusterDependenciesArr , dep )
268
+ }
269
+ slices .Sort (clusterDependenciesArr )
267
270
268
- // TODO: add relationships and dependencies
271
+ dependencies = append (dependencies ,
272
+ cyclonedx.Dependency {
273
+ Ref : clusterComponent .BOMRef ,
274
+ Dependencies : & clusterDependenciesArr ,
275
+ },
276
+ )
277
+
278
+ cdxBOM .Components = & components
279
+ cdxBOM .Dependencies = & dependencies
269
280
270
281
return cdxBOM
271
282
}
0 commit comments