-
Notifications
You must be signed in to change notification settings - Fork 17
/
sbom.go
183 lines (151 loc) · 5.7 KB
/
sbom.go
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
package sherpa
import (
"fmt"
"io"
"os"
"github.com/CycloneDX/cyclonedx-go"
"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/effect"
)
//go:generate mockery -name SBOMScanner -case=underscore
type SBOMScanner interface {
ScanLayer(layer libcnb.Layer, scanDir string, formats ...libcnb.SBOMFormat) error
ScanBuild(scanDir string, formats ...libcnb.SBOMFormat) error
ScanLaunch(scanDir string, formats ...libcnb.SBOMFormat) error
}
type SyftCLISBOMScanner struct {
Executor effect.Executor
Layers libcnb.Layers
Logger bard.Logger
}
func NewSyftCLISBOMScanner(layers libcnb.Layers, executor effect.Executor, logger bard.Logger) SyftCLISBOMScanner {
return SyftCLISBOMScanner{
Executor: executor,
Layers: layers,
Logger: logger,
}
}
// ScanLayer will use syft CLI to scan the scanDir and write it's output to the layer SBoM file in the given formats
func (b SyftCLISBOMScanner) ScanLayer(layer libcnb.Layer, scanDir string, formats ...libcnb.SBOMFormat) error {
return b.scan(func(fmt libcnb.SBOMFormat) string {
return layer.SBOMPath(fmt)
}, scanDir, formats...)
}
// ScanBuild will use syft CLI to scan the scanDir and write it's output to the build SBoM file in the given formats
func (b SyftCLISBOMScanner) ScanBuild(scanDir string, formats ...libcnb.SBOMFormat) error {
return b.scan(func(fmt libcnb.SBOMFormat) string {
return b.Layers.BuildSBOMPath(fmt)
}, scanDir, formats...)
}
// ScanLaunch will use syft CLI to scan the scanDir and write it's output to the launch SBoM file in the given formats
func (b SyftCLISBOMScanner) ScanLaunch(scanDir string, formats ...libcnb.SBOMFormat) error {
return b.scan(func(fmt libcnb.SBOMFormat) string {
return b.Layers.LaunchSBOMPath(fmt)
}, scanDir, formats...)
}
func (b SyftCLISBOMScanner) scan(sbomPathCreator func(libcnb.SBOMFormat) string, scanDir string, formats ...libcnb.SBOMFormat) error {
// syft doesn't presently support outputting multiple formats at once
// to workaround this we are running syft multiple times
// when syft supports multiple output formats or conversion between formats, this method should change
for _, format := range formats {
sbomLocation := sbomPathCreator(format)
if err := b.runSyft(sbomLocation, scanDir, format); err != nil {
return fmt.Errorf("unable to run syft\n%w", err)
}
if format == libcnb.CycloneDXJSON {
// syft doesn't presently support cyclonedx JSON output and we need to convert
// until https://github.com/anchore/syft/issues/631 is addressed
if err := b.ConvertCycloneDXXMLtoJSON(sbomLocation, false); err != nil {
return fmt.Errorf("unable convert XML to JSON\n%w", err)
}
}
}
return nil
}
// ConvertCycloneDXXMLtoJSON reads input CycloneDX XML, converts to JSON and overwrites the XML optionally keeping a backup copy of the xml
func (b SyftCLISBOMScanner) ConvertCycloneDXXMLtoJSON(inputPath string, backup bool) error {
if backup {
if err := b.backupXMLFile(inputPath); err != nil {
return fmt.Errorf("unable to backup file\n%w", err)
}
}
bom, err := b.readXMLSBOM(inputPath)
if err != nil {
return fmt.Errorf("unable to read XML file for conversion\n%w", err)
}
if err := b.writeJSONSBOM(inputPath, bom); err != nil {
return fmt.Errorf("unable to write converted JSON BOM file\n%w", err)
}
return nil
}
func (b SyftCLISBOMScanner) writeJSONSBOM(outputPath string, bom cyclonedx.BOM) error {
outputFile, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("unable to create BOM file %s\n%w", outputPath, err)
}
defer outputFile.Close()
decoder := cyclonedx.NewBOMEncoder(outputFile, cyclonedx.BOMFileFormatJSON)
if err = decoder.Encode(&bom); err != nil {
return fmt.Errorf("unable to decode BOM\n%w", err)
}
return nil
}
func (b SyftCLISBOMScanner) readXMLSBOM(inputPath string) (cyclonedx.BOM, error) {
inputFile, err := os.Open(inputPath)
if err != nil {
return cyclonedx.BOM{}, fmt.Errorf("unable to read file to convert %s\n%w", inputPath, err)
}
defer inputFile.Close()
var bom cyclonedx.BOM
decoder := cyclonedx.NewBOMDecoder(inputFile, cyclonedx.BOMFileFormatXML)
if err = decoder.Decode(&bom); err != nil {
return cyclonedx.BOM{}, fmt.Errorf("unable to decode BOM\n%w", err)
}
return bom, nil
}
func (b SyftCLISBOMScanner) backupXMLFile(inputPath string) error {
backupPath := fmt.Sprintf("%s.bak", inputPath)
outputFile, err := os.Create(backupPath)
if err != nil {
return fmt.Errorf("unable to create backup file %s\n%w", backupPath, err)
}
defer outputFile.Close()
inputFile, err := os.Open(inputPath)
if err != nil {
return fmt.Errorf("unable to read file for backup %s\n%w", inputPath, err)
}
defer inputFile.Close()
_, err = io.Copy(outputFile, inputFile)
return err
}
func (b SyftCLISBOMScanner) runSyft(sbomOutputPath string, scanDir string, format libcnb.SBOMFormat) error {
writer, err := os.Create(sbomOutputPath)
if err != nil {
return fmt.Errorf("unable to open output BOM file %s\n%w", sbomOutputPath, err)
}
defer writer.Close()
err = b.Executor.Execute(effect.Execution{
Command: "syft",
Args: []string{"packges", "-o", SBOMFormatToSyftOutputFormat(format), fmt.Sprintf("dir:%s", scanDir)},
Stdout: writer,
Stderr: b.Logger.TerminalErrorWriter(),
})
if err != nil {
return fmt.Errorf("unable to run syft on directory %s\n%w", scanDir, err)
}
return nil
}
// SBOMFormatToSyftOutputFormat converts a libcnb.SBOMFormat to the syft matching syft output format string
func SBOMFormatToSyftOutputFormat(format libcnb.SBOMFormat) string {
var formatRaw string
switch format {
case libcnb.CycloneDXJSON:
formatRaw = "cyclonedx"
case libcnb.SPDXJSON:
formatRaw = "spdx-json"
case libcnb.SyftJSON:
formatRaw = "json"
}
return formatRaw
}