-
Notifications
You must be signed in to change notification settings - Fork 17
/
sbom.go
154 lines (126 loc) · 4.5 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
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 BOMScanner -case=underscore
type BOMScanner interface {
Scan(location libcnb.BOMLocation, scanDir string, formats ...libcnb.BOMFormat)
}
type SyftCLIBOMScanner struct {
Layer libcnb.Layer
Executor effect.Executor
Logger bard.Logger
}
// Scan will use syft CLI to scan the scanDir and write it's output to the location in the given formats
func (b SyftCLIBOMScanner) Scan(location libcnb.BOMLocation, scanDir string, formats ...libcnb.BOMFormat) 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 {
if err := b.runSyft(location, 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(location, 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 SyftCLIBOMScanner) ConvertCycloneDXXMLtoJSON(location libcnb.BOMLocation, backup bool) error {
inputPath := b.Layer.BOMPath(location, libcnb.CycloneDXJSON)
if backup {
if err := b.backupXMLFile(inputPath); err != nil {
return fmt.Errorf("unable to backup file\n%w", err)
}
}
bom, err := b.readXMLBOM(inputPath)
if err != nil {
return fmt.Errorf("unable to backup file\n%w", err)
}
if err := b.writeJSONBOM(inputPath, bom); err != nil {
return fmt.Errorf("unable to backup file\n%w", err)
}
return nil
}
func (b SyftCLIBOMScanner) writeJSONBOM(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 SyftCLIBOMScanner) readXMLBOM(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 SyftCLIBOMScanner) 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 SyftCLIBOMScanner) runSyft(location libcnb.BOMLocation, scanDir string, format libcnb.BOMFormat) error {
bomOutputPath := b.Layer.BOMPath(location, format)
writer, err := os.Create(bomOutputPath)
if err != nil {
return fmt.Errorf("unable to open output BOM file %s\n%w", bomOutputPath, err)
}
defer writer.Close()
err = b.Executor.Execute(effect.Execution{
Command: "syft",
Args: []string{"packges", "-o", BOMFormatToSyftOutputFormat(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
}
// BOMFormatToSyftOutputFormat converts a libcnb.BOMFormat to the syft matching syft output format string
func BOMFormatToSyftOutputFormat(format libcnb.BOMFormat) string {
var formatRaw string
switch format {
case libcnb.CycloneDXJSON:
formatRaw = "cyclonedx"
case libcnb.SPDXJSON:
formatRaw = "spdx-json"
case libcnb.SyftJSON:
formatRaw = "json"
}
return formatRaw
}