-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsyft.go
219 lines (190 loc) · 5.5 KB
/
syft.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
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
package syft
import (
"context"
"crypto"
"fmt"
"io"
"os"
"path/filepath"
"runtime/debug"
"strings"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/cataloging/filecataloging"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/format/cyclonedxjson"
"github.com/anchore/syft/syft/format/cyclonedxxml"
"github.com/anchore/syft/syft/format/github"
"github.com/anchore/syft/syft/format/spdxjson"
"github.com/anchore/syft/syft/format/spdxtagvalue"
"github.com/anchore/syft/syft/format/syftjson"
"github.com/anchore/syft/syft/format/table"
"github.com/anchore/syft/syft/format/text"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
"github.com/ckotzbauer/libk8soci/pkg/oci"
"github.com/ckotzbauer/sbom-operator/internal/kubernetes"
"github.com/sirupsen/logrus"
)
type Syft struct {
sbomFormat string
resolveVersion func() string
proxyRegistryMap map[string]string
appVersion string
}
func New(sbomFormat string, proxyRegistryMap map[string]string, appVersion string) *Syft {
return &Syft{
sbomFormat: sbomFormat,
resolveVersion: getSyftVersion,
proxyRegistryMap: proxyRegistryMap,
appVersion: appVersion,
}
}
func (s Syft) WithSyftVersion(version string) Syft {
s.resolveVersion = func() string { return version }
return s
}
func (s *Syft) ExecuteSyft(img *oci.RegistryImage) (string, error) {
logrus.Infof("Processing image %s", img.ImageID)
oriImage := img.Image
oriImageID := img.ImageID
err := kubernetes.ApplyProxyRegistry(img, true, s.proxyRegistryMap)
if err != nil {
return "", err
}
opts := &image.RegistryOptions{Credentials: oci.ConvertSecrets(*img, s.proxyRegistryMap)}
src, err := getSource(context.Background(), opts, img.ImageID)
// revert image info to the original value - we want to register with original names
img.Image = oriImage
img.ImageID = oriImageID
if err != nil {
logrus.WithError(fmt.Errorf("failed to construct source from input registry:%s: %w", img.ImageID, err)).Error("Source-Creation failed")
return "", err
}
defer func() {
if src != nil {
if err := src.Close(); err != nil {
logrus.WithError(err).Infof("unable to close source")
}
}
}()
cfg := syft.DefaultCreateSBOMConfig().
WithParallelism(5).
WithTool("sbom-operator", s.appVersion).
WithFilesConfig(
filecataloging.DefaultConfig().
WithSelection(file.FilesOwnedByPackageSelection).
WithHashers(
crypto.SHA1,
crypto.SHA256,
),
)
result, err := syft.CreateSBOM(context.Background(), src, cfg)
if err != nil {
logrus.WithError(err).Error("SBOM-Creation failed")
return "", err
}
// you can use other formats such as format.CycloneDxJSONOption or format.SPDXJSONOption ...
encoder, err := GetEncoder(s.sbomFormat)
if err != nil {
logrus.WithError(err).Error("Could not resolve encoder")
return "", err
}
b, err := format.Encode(*result, encoder)
if err != nil {
logrus.WithError(err).Error("Encoding of result failed")
return "", err
}
bom := string(b)
err = removeTempContents()
if err != nil {
logrus.WithError(err).Warn("Could not cleanup tmp directory")
}
return bom, nil
}
func getSource(ctx context.Context, registryOptions *image.RegistryOptions, userInput string) (source.Source, error) {
cfg := syft.DefaultGetSourceConfig().
WithSources("registry").
WithRegistryOptions(registryOptions)
var err error
src, err := syft.GetSource(ctx, userInput, cfg)
if err != nil {
return nil, fmt.Errorf("could not determine source: %w", err)
}
return src, nil
}
func GetEncoder(sbomFormat string) (sbom.FormatEncoder, error) {
switch sbomFormat {
case "json", "syftjson":
return syftjson.NewFormatEncoder(), nil
case "cyclonedx", "cyclone", "cyclonedxxml":
return cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.DefaultEncoderConfig())
case "cyclonedxjson":
return cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())
case "spdx", "spdxtv", "spdxtagvalue":
return spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.DefaultEncoderConfig())
case "spdxjson":
return spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig())
case "github", "githubjson":
return github.NewFormatEncoder(), nil
case "text":
return text.NewFormatEncoder(), nil
case "table":
return table.NewFormatEncoder(), nil
default:
return syftjson.NewFormatEncoder(), nil
}
}
func GetFileName(sbomFormat string) string {
switch sbomFormat {
case "json", "syftjson", "cyclonedxjson", "spdxjson", "github", "githubjson":
return "sbom.json"
case "cyclonedx", "cyclone", "cyclonedxxml":
return "sbom.xml"
case "spdx", "spdxtv", "spdxtagvalue":
return "sbom.spdx"
case "text":
return "sbom.txt"
case "table":
return "sbom.txt"
default:
return "sbom.json"
}
}
func getSyftVersion() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
logrus.Warnf("failed to read build info")
}
for _, dep := range bi.Deps {
if strings.EqualFold("github.com/anchore/syft", dep.Path) {
return dep.Version
}
}
return ""
}
func removeTempContents() error {
dir := "/tmp"
d, err := os.Open(dir)
if err != nil {
return err
}
defer closeOrLog(d)
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func closeOrLog(c io.Closer) {
if err := c.Close(); err != nil {
logrus.WithError(err).Warnf("Could not close file")
}
}