-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcyclonedx.go
More file actions
278 lines (241 loc) · 6.99 KB
/
cyclonedx.go
File metadata and controls
278 lines (241 loc) · 6.99 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package sbom
import (
"errors"
"io"
"strings"
"time"
cyclonedx "github.com/CycloneDX/cyclonedx-go"
"github.com/google/uuid"
"github.com/package-url/packageurl-go"
)
func NewCycloneDX(format string) *CycloneDX {
switch format {
case FormatCycloneDxXML:
return &CycloneDX{
Format: cyclonedx.BOMFileFormatXML,
}
default:
return &CycloneDX{
Format: cyclonedx.BOMFileFormatJSON,
}
}
}
var _ Decoder = &CycloneDX{}
type CycloneDX struct {
opts renderOpts
Format cyclonedx.BOMFileFormat
}
func (ccx *CycloneDX) convertToCycloneDx(bom *Sbom) (*cyclonedx.BOM, error) {
sbom := cyclonedx.NewBOM()
sbom.SerialNumber = uuid.New().URN()
sbom.Metadata = &cyclonedx.Metadata{
Timestamp: time.Now().Format(time.RFC3339),
Tools: &cyclonedx.ToolsChoice{
Components: &[]cyclonedx.Component{
{
Type: cyclonedx.ComponentTypeApplication,
Author: bom.Generator.Vendor,
Name: bom.Generator.Name,
Version: bom.Generator.Version,
},
},
},
Component: &cyclonedx.Component{
BOMRef: uuid.New().String(),
// TODO: understand the device type
// Type: cyclonedx.ComponentTypeContainer,
Type: cyclonedx.ComponentTypeDevice,
Name: bom.Asset.Name,
},
}
components := []cyclonedx.Component{}
// add os as component
cpe := ""
if len(bom.Asset.Platform.Cpes) > 0 {
cpe = bom.Asset.Platform.Cpes[0]
}
components = append(components, cyclonedx.Component{
BOMRef: uuid.New().String(),
Type: cyclonedx.ComponentTypeOS,
Name: bom.Asset.Platform.Name,
Version: bom.Asset.Platform.Version,
CPE: cpe,
})
// add os packages as components
for i := range bom.Packages {
pkg := bom.Packages[i]
cpe := ""
if len(pkg.Cpes) > 0 && ccx.opts.IncludeCPE {
cpe = pkg.Cpes[0]
}
fileLocations := []cyclonedx.EvidenceOccurrence{}
// pkg.Location is deprecated, use pkg.Evidences instead
if pkg.Location != "" {
fileLocations = append(fileLocations, cyclonedx.EvidenceOccurrence{
Location: pkg.Location,
})
}
if pkg.EvidenceList != nil && ccx.opts.IncludeEvidence {
for i := range pkg.EvidenceList {
e := pkg.EvidenceList[i]
if e.Type == EvidenceType_EVIDENCE_TYPE_FILE {
fileLocations = append(fileLocations, cyclonedx.EvidenceOccurrence{
Location: e.Value,
})
}
}
}
var evidence *cyclonedx.Evidence
if len(fileLocations) > 0 {
evidence = &cyclonedx.Evidence{
Occurrences: &fileLocations,
}
}
bomPkg := cyclonedx.Component{
BOMRef: uuid.New().String(), // temporary, we need to store the relationships next
Type: cyclonedx.ComponentTypeLibrary,
Name: pkg.Name,
Version: pkg.Version,
PackageURL: pkg.Purl,
CPE: cpe,
Evidence: evidence,
}
components = append(components, bomPkg)
}
sbom.Components = &components
return sbom, nil
}
func (s *CycloneDX) ApplyOptions(opts ...renderOption) {
for _, opt := range opts {
opt(&s.opts)
}
}
func (ccx *CycloneDX) Convert(bom *Sbom) (any, error) {
return ccx.convertToCycloneDx(bom)
}
func (ccx *CycloneDX) Render(w io.Writer, bom *Sbom) error {
sbom, err := ccx.convertToCycloneDx(bom)
if err != nil {
return err
}
enc := cyclonedx.NewBOMEncoder(w, ccx.Format)
enc.SetPretty(true)
return enc.Encode(sbom)
}
func (ccx *CycloneDX) Parse(r io.ReadSeeker) (*Sbom, error) {
doc := &cyclonedx.BOM{
Components: &[]cyclonedx.Component{},
}
err := cyclonedx.NewBOMDecoder(r, ccx.Format).Decode(doc)
if err != nil {
return nil, err
}
return ccx.convertCycloneDxToSbom(doc)
}
func (ccx *CycloneDX) convertCycloneDxToSbom(bom *cyclonedx.BOM) (*Sbom, error) {
if bom == nil {
return nil, nil
}
// check if the BOM is empty
if bom.Metadata == nil || bom.Metadata.Component == nil || bom.Components == nil {
return nil, errors.New("not a valid cyclone dx BOM")
}
rootComponent := bom.Metadata.Component
sbom := &Sbom{
Asset: &Asset{
Name: rootComponent.Name,
Platform: &Platform{
Version: rootComponent.Version,
Title: rootComponent.Description,
},
},
Packages: make([]*Package, 0),
}
switch rootComponent.Type {
case cyclonedx.ComponentTypeOS:
hostnameId := "//platformid.api.mondoo.app/hostname/" + rootComponent.Name
sbom.Asset.PlatformIds = append(sbom.Asset.PlatformIds, hostnameId)
case cyclonedx.ComponentTypeContainer:
// we need to figure out where to get the container ID from properly. For now, we use the BOMRef
bomRefId := "//platformid.api.mondoo.app/runtime/docker/images/" + rootComponent.BOMRef
sbom.Asset.PlatformIds = append(sbom.Asset.PlatformIds, bomRefId)
}
if bom.Metadata.Tools != nil {
if bom.Metadata.Tools.Components != nil {
// last one wins :-) - we only support one tool
for _, component := range *bom.Metadata.Tools.Components {
sbom.Generator = &Generator{
Name: component.Name,
Version: component.Version,
Vendor: component.Author,
}
}
}
// if we have no generator info, fallback to trying tools. these are deprecated
// but might still be present
if sbom.Generator == nil && bom.Metadata.Tools.Tools != nil {
for _, tool := range *bom.Metadata.Tools.Tools {
sbom.Generator = &Generator{
Name: tool.Name,
Version: tool.Version,
Vendor: tool.Vendor,
}
}
}
}
for _, component := range *bom.Components {
pkg := &Package{
Name: component.Name,
Version: component.Version,
Purl: component.PackageURL,
Description: component.Description,
}
// parse purl to gather package type
if component.PackageURL != "" {
url, err := packageurl.FromString(component.PackageURL)
if err == nil {
pkg.Type = url.Type
}
}
if component.CPE != "" {
pkg.Cpes = []string{component.CPE}
}
if component.Evidence != nil && component.Evidence.Occurrences != nil && ccx.opts.IncludeEvidence {
pkg.EvidenceList = make([]*Evidence, 0)
for _, e := range *component.Evidence.Occurrences {
pkg.EvidenceList = append(pkg.EvidenceList, &Evidence{
Type: EvidenceType_EVIDENCE_TYPE_FILE,
Value: e.Location,
})
}
}
switch component.Type {
case cyclonedx.ComponentTypeOS:
sbom.Asset.Platform.Name = component.Name
sbom.Asset.Platform.Version = component.Version
sbom.Asset.Platform.Title = component.Description
sbom.Asset.Platform.Family = familyMap[strings.ToLower(component.Name)]
if len(component.CPE) > 0 {
sbom.Asset.Platform.Cpes = []string{component.CPE}
}
sbom.Packages = append(sbom.Packages, pkg)
case cyclonedx.ComponentTypeLibrary:
sbom.Packages = append(sbom.Packages, pkg)
case cyclonedx.ComponentTypeApplication:
sbom.Packages = append(sbom.Packages, pkg)
}
}
return sbom, nil
}
var familyMap = map[string][]string{
"windows": {"windows", "os"},
"macos": {"darwin", "bsd", "unix", "os"},
"debian": {"linux", "unix", "os"},
"ubuntu": {"linux", "unix", "os"},
"centos": {"linux", "unix", "os"},
"alpine": {"linux", "unix", "os"},
"fedora": {"linux", "unix", "os"},
"rhel": {"linux", "unix", "os"},
}