-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathsbom.go
85 lines (70 loc) · 1.96 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
package sbom
import (
"context"
"os"
"path"
"strings"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/sbom"
)
func init() {
analyzer.RegisterAnalyzer(&sbomAnalyzer{})
}
const version = 1
var requiredSuffixes = []string{
".spdx",
".spdx.json",
".cdx",
".cdx.json",
}
type sbomAnalyzer struct{}
func (a sbomAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
// Format auto-detection
format, err := sbom.DetectFormat(input.Content)
if err != nil {
return nil, xerrors.Errorf("failed to detect SBOM format: %w", err)
}
bom, err := sbom.Decode(input.Content, format)
if err != nil {
return nil, xerrors.Errorf("SBOM decode error: %w", err)
}
// For Bitnami images
if strings.HasPrefix(input.FilePath, "opt/bitnami/") {
dir, file := path.Split(input.FilePath)
bin := strings.TrimPrefix(file, ".spdx-")
bin = strings.TrimSuffix(bin, ".spdx")
binPath := path.Join(input.FilePath, "../bin", bin)
for i, app := range bom.Applications {
// Replace the SBOM path with the binary path
bom.Applications[i].FilePath = binPath
for j, pkg := range app.Libraries {
if pkg.FilePath == "" {
continue
}
// Set the absolute path since SBOM in Bitnami images contain a relative path
// e.g. modules/apm/elastic-apm-agent-1.36.0.jar
// => opt/bitnami/elasticsearch/modules/apm/elastic-apm-agent-1.36.0.jar
bom.Applications[i].Libraries[j].FilePath = path.Join(dir, pkg.FilePath)
}
}
}
return &analyzer.AnalysisResult{
PackageInfos: bom.Packages,
Applications: bom.Applications,
}, nil
}
func (a sbomAnalyzer) Required(filePath string, _ os.FileInfo) bool {
for _, suffix := range requiredSuffixes {
if strings.HasSuffix(filePath, suffix) {
return true
}
}
return false
}
func (a sbomAnalyzer) Type() analyzer.Type {
return analyzer.TypeSBOM
}
func (a sbomAnalyzer) Version() int {
return version
}