-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbom.go
More file actions
232 lines (204 loc) · 5.67 KB
/
sbom.go
File metadata and controls
232 lines (204 loc) · 5.67 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
package inspect
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
// SBOMDocument represents a CycloneDX 1.5 Software Bill of Materials.
type SBOMDocument struct {
BOMFormat string `json:"bomFormat"`
SpecVersion string `json:"specVersion"`
SerialNumber string `json:"serialNumber,omitempty"`
Version int `json:"version"`
Metadata SBOMMetadata `json:"metadata"`
Components []SBOMComponent `json:"components"`
}
// SBOMMetadata contains metadata about the SBOM.
type SBOMMetadata struct {
Timestamp string `json:"timestamp"`
Tools []SBOMTool `json:"tools,omitempty"`
Component *SBOMComponent `json:"component,omitempty"`
}
// SBOMTool describes the tool that generated the SBOM.
type SBOMTool struct {
Vendor string `json:"vendor"`
Name string `json:"name"`
Version string `json:"version"`
}
// SBOMComponent represents a software component/dependency.
type SBOMComponent struct {
Type string `json:"type"`
Name string `json:"name"`
Version string `json:"version"`
PURL string `json:"purl,omitempty"`
Scope string `json:"scope,omitempty"`
}
// GenerateSBOM produces a CycloneDX 1.5 SBOM from project dependency files.
func GenerateSBOM(projectDir string, version string) (*SBOMDocument, error) {
if version == "" {
version = "dev"
}
doc := &SBOMDocument{
BOMFormat: "CycloneDX",
SpecVersion: "1.5",
Version: 1,
Metadata: SBOMMetadata{
Timestamp: time.Now().UTC().Format(time.RFC3339),
Tools: []SBOMTool{{
Vendor: "GrayCodeAI",
Name: "inspect",
Version: version,
}},
},
}
// Scan for dependency files
goModPath := filepath.Join(projectDir, "go.mod")
if _, err := os.Stat(goModPath); err == nil {
comps := scanGoModForSBOM(goModPath)
doc.Components = append(doc.Components, comps...)
}
pkgJSONPath := filepath.Join(projectDir, "package.json")
if _, err := os.Stat(pkgJSONPath); err == nil {
comps := scanPackageJSONForSBOM(pkgJSONPath)
doc.Components = append(doc.Components, comps...)
}
reqPath := filepath.Join(projectDir, "requirements.txt")
if _, err := os.Stat(reqPath); err == nil {
comps := scanRequirementsForSBOM(reqPath)
doc.Components = append(doc.Components, comps...)
}
return doc, nil
}
// GenerateSBOMJSON produces a JSON string of the SBOM.
func GenerateSBOMJSON(projectDir string, version string) (string, error) {
doc, err := GenerateSBOM(projectDir, version)
if err != nil {
return "", err
}
data, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return "", fmt.Errorf("inspect: SBOM marshal failed: %w", err)
}
return string(data), nil
}
func scanGoModForSBOM(path string) []SBOMComponent {
var components []SBOMComponent
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
inRequire := false
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "require (") || strings.HasPrefix(line, "require(") {
inRequire = true
continue
}
if line == ")" {
inRequire = false
continue
}
if strings.HasPrefix(line, "require ") && !strings.Contains(line, "(") {
line = strings.TrimPrefix(line, "require ")
if comp := goModLineToComponent(line); comp != nil {
components = append(components, *comp)
}
continue
}
if inRequire && line != "" && !strings.HasPrefix(line, "//") {
if comp := goModLineToComponent(line); comp != nil {
components = append(components, *comp)
}
}
}
return components
}
func goModLineToComponent(line string) *SBOMComponent {
if idx := strings.Index(line, "//"); idx >= 0 {
line = line[:idx]
}
line = strings.TrimSpace(line)
parts := strings.Fields(line)
if len(parts) < 2 {
return nil
}
pkg := parts[0]
version := strings.TrimPrefix(parts[1], "v")
return &SBOMComponent{
Type: "library",
Name: pkg,
Version: version,
PURL: fmt.Sprintf("pkg:golang/%s@%s", pkg, version),
Scope: "required",
}
}
func scanPackageJSONForSBOM(path string) []SBOMComponent {
var components []SBOMComponent
data, err := os.ReadFile(path)
if err != nil {
return nil
}
var pkg struct {
Name string `json:"name"`
Version string `json:"version"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
}
if err := json.Unmarshal(data, &pkg); err != nil {
return nil
}
for name, version := range pkg.Dependencies {
cleaned := cleanNPMVersion(version)
components = append(components, SBOMComponent{
Type: "library",
Name: name,
Version: cleaned,
PURL: fmt.Sprintf("pkg:npm/%s@%s", name, cleaned),
Scope: "required",
})
}
for name, version := range pkg.DevDependencies {
cleaned := cleanNPMVersion(version)
components = append(components, SBOMComponent{
Type: "library",
Name: name,
Version: cleaned,
PURL: fmt.Sprintf("pkg:npm/%s@%s", name, cleaned),
Scope: "optional",
})
}
return components
}
func scanRequirementsForSBOM(path string) []SBOMComponent {
var components []SBOMComponent
file, err := os.Open(path)
if err != nil {
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, "-") {
continue
}
name, version := parseRequirementLine(line)
if name == "" {
continue
}
components = append(components, SBOMComponent{
Type: "library",
Name: name,
Version: version,
PURL: fmt.Sprintf("pkg:pypi/%s@%s", name, version),
Scope: "required",
})
}
return components
}