forked from google/osv-scalibr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalibr.go
330 lines (301 loc) · 10.4 KB
/
scalibr.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package scalibr provides an interface for running software inventory
// extraction and security finding detection on a machine.
package scalibr
import (
"cmp"
"context"
"errors"
"fmt"
"regexp"
"slices"
"sort"
"time"
"github.com/google/osv-scalibr/detector"
"github.com/google/osv-scalibr/extractor"
"github.com/google/osv-scalibr/extractor/filesystem"
"github.com/google/osv-scalibr/extractor/standalone"
scalibrfs "github.com/google/osv-scalibr/fs"
"github.com/google/osv-scalibr/inventoryindex"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/stats"
el "github.com/google/osv-scalibr/extractor/filesystem/list"
sl "github.com/google/osv-scalibr/extractor/standalone/list"
)
var (
errNoScanRoot = fmt.Errorf("no scan root specified")
errFilesWithSeveralRoots = fmt.Errorf("can't extract specific files with several scan roots")
)
// Scanner is the main entry point of the scanner.
type Scanner struct{}
// New creates a new scanner instance.
func New() *Scanner { return &Scanner{} }
// ScanConfig stores the config settings of a scan run such as the plugins to
// use and the dir to consider the root of the scanned system.
type ScanConfig struct {
FilesystemExtractors []filesystem.Extractor
StandaloneExtractors []standalone.Extractor
Detectors []detector.Detector
// Capabilities that the scanning environment satisfies, e.g. whether there's
// network access. Some plugins can only run if certain requirements are met.
Capabilities *plugin.Capabilities
// ScanRoots contain the list of root dir used by file walking during extraction.
// All extractors and detectors will assume files are relative to these dirs.
// Example use case: Scanning a container image or source code repo that is
// mounted to a local dir.
ScanRoots []*scalibrfs.ScanRoot
// Optional: Individual files to extract inventory from. If specified, the
// extractors will only look at these files during the filesystem traversal.
// Note that on real filesystems these are not relative to the ScanRoots and
// thus need to be in sub-directories of one of the ScanRoots.
FilesToExtract []string
// Optional: Directories that the file system walk should ignore.
// Note that on real filesystems these are not relative to the ScanRoots and
// thus need to be in sub-directories of one of the ScanRoots.
// TODO(b/279413691): Also skip local paths, e.g. "Skip all .git dirs"
DirsToSkip []string
// Optional: If the regex matches a directory, it will be skipped.
SkipDirRegex *regexp.Regexp
// Optional: stats allows to enter a metric hook. If left nil, no metrics will be recorded.
Stats stats.Collector
// Optional: Whether to read symlinks.
ReadSymlinks bool
// Optional: Limit for visited inodes. If 0, no limit is applied.
MaxInodes int
// Optional: By default, inventories stores a path relative to the scan root. If StoreAbsolutePath
// is set, the absolute path is stored instead.
StoreAbsolutePath bool
// Optional: If true, print a detailed analysis of the duration of each extractor.
PrintDurationAnalysis bool
}
// EnableRequiredExtractors adds those extractors to the config that are required by enabled
// detectors but have not been explicitly enabled.
func (cfg *ScanConfig) EnableRequiredExtractors() error {
enabledExtractors := map[string]struct{}{}
for _, e := range cfg.FilesystemExtractors {
enabledExtractors[e.Name()] = struct{}{}
}
for _, e := range cfg.StandaloneExtractors {
enabledExtractors[e.Name()] = struct{}{}
}
for _, d := range cfg.Detectors {
for _, e := range d.RequiredExtractors() {
if _, enabled := enabledExtractors[e]; enabled {
continue
}
ex, err := el.ExtractorFromName(e)
stex, sterr := sl.ExtractorFromName(e)
if err != nil && sterr != nil {
return fmt.Errorf("required extractor %q not present in list.go: %w, %w", e, err, sterr)
}
enabledExtractors[e] = struct{}{}
if err == nil {
cfg.FilesystemExtractors = append(cfg.FilesystemExtractors, ex)
}
if sterr == nil {
cfg.StandaloneExtractors = append(cfg.StandaloneExtractors, stex)
}
}
}
return nil
}
// ValidatePluginRequirements checks that the scanning environment's capabilities satisfy
// the requirements of all enabled plugin.
func (cfg *ScanConfig) ValidatePluginRequirements() error {
plugins := make([]plugin.Plugin, 0, len(cfg.FilesystemExtractors)+len(cfg.StandaloneExtractors)+len(cfg.Detectors))
for _, p := range cfg.FilesystemExtractors {
plugins = append(plugins, p)
}
for _, p := range cfg.StandaloneExtractors {
plugins = append(plugins, p)
}
for _, p := range cfg.Detectors {
plugins = append(plugins, p)
}
errs := []error{}
for _, p := range plugins {
if err := plugin.ValidateRequirements(p, cfg.Capabilities); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
// LINT.IfChange
// ScanResult stores the software inventory and security findings that a scan run found.
type ScanResult struct {
Version string
StartTime time.Time
EndTime time.Time
// Status of the overall scan.
Status *plugin.ScanStatus
// Status and versions of the inventory+vuln plugins that ran.
PluginStatus []*plugin.Status
Inventories []*extractor.Inventory
Findings []*detector.Finding
}
// LINT.ThenChange(/binary/proto/scan_result.proto)
// Scan executes the extraction and detection using the provided scan config.
func (Scanner) Scan(ctx context.Context, config *ScanConfig) (sr *ScanResult) {
if config.Stats == nil {
config.Stats = stats.NoopCollector{}
}
defer func() {
config.Stats.AfterScan(time.Since(sr.StartTime), sr.Status)
}()
sro := &newScanResultOptions{
StartTime: time.Now(),
Inventories: []*extractor.Inventory{},
Findings: []*detector.Finding{},
}
if err := config.EnableRequiredExtractors(); err != nil {
sro.Err = err
} else if err := config.ValidatePluginRequirements(); err != nil {
sro.Err = err
} else if len(config.ScanRoots) == 0 {
sro.Err = errNoScanRoot
} else if len(config.FilesToExtract) > 0 && len(config.ScanRoots) > 1 {
sro.Err = errFilesWithSeveralRoots
}
if sro.Err != nil {
sro.EndTime = time.Now()
return newScanResult(sro)
}
extractorConfig := &filesystem.Config{
Stats: config.Stats,
ReadSymlinks: config.ReadSymlinks,
Extractors: config.FilesystemExtractors,
FilesToExtract: config.FilesToExtract,
DirsToSkip: config.DirsToSkip,
SkipDirRegex: config.SkipDirRegex,
ScanRoots: config.ScanRoots,
MaxInodes: config.MaxInodes,
StoreAbsolutePath: config.StoreAbsolutePath,
PrintDurationAnalysis: config.PrintDurationAnalysis,
}
inventories, extractorStatus, err := filesystem.Run(ctx, extractorConfig)
if err != nil {
sro.Err = err
sro.EndTime = time.Now()
return newScanResult(sro)
}
sro.Inventories = inventories
sro.ExtractorStatus = extractorStatus
sysroot := config.ScanRoots[0]
standaloneCfg := &standalone.Config{
Extractors: config.StandaloneExtractors,
ScanRoot: &scalibrfs.ScanRoot{FS: sysroot.FS, Path: sysroot.Path},
}
standaloneInv, standaloneStatus, err := standalone.Run(ctx, standaloneCfg)
if err != nil {
sro.Err = err
sro.EndTime = time.Now()
return newScanResult(sro)
}
sro.Inventories = append(sro.Inventories, standaloneInv...)
sro.ExtractorStatus = append(sro.ExtractorStatus, standaloneStatus...)
ix, err := inventoryindex.New(sro.Inventories)
if err != nil {
sro.Err = err
sro.EndTime = time.Now()
return newScanResult(sro)
}
findings, detectorStatus, err := detector.Run(
ctx, config.Stats, config.Detectors, &scalibrfs.ScanRoot{FS: sysroot.FS, Path: sysroot.Path}, ix,
)
sro.Findings = findings
sro.DetectorStatus = detectorStatus
if err != nil {
sro.Err = err
}
sro.EndTime = time.Now()
return newScanResult(sro)
}
type newScanResultOptions struct {
StartTime time.Time
EndTime time.Time
ExtractorStatus []*plugin.Status
Inventories []*extractor.Inventory
DetectorStatus []*plugin.Status
Findings []*detector.Finding
Err error
}
func newScanResult(o *newScanResultOptions) *ScanResult {
status := &plugin.ScanStatus{}
if o.Err != nil {
status.Status = plugin.ScanStatusFailed
status.FailureReason = o.Err.Error()
} else {
status.Status = plugin.ScanStatusSucceeded
}
r := &ScanResult{
StartTime: o.StartTime,
EndTime: o.EndTime,
Status: status,
PluginStatus: append(o.ExtractorStatus, o.DetectorStatus...),
Inventories: o.Inventories,
Findings: o.Findings,
}
// Sort results for better diffing.
sortResults(r)
return r
}
func hasFailedPlugins(statuses []*plugin.Status) bool {
for _, s := range statuses {
if s.Status.Status != plugin.ScanStatusSucceeded {
return true
}
}
return false
}
// sortResults sorts the result to make the output deterministic and diffable.
func sortResults(results *ScanResult) {
for _, inventory := range results.Inventories {
sort.Strings(inventory.Locations)
}
slices.SortFunc(results.PluginStatus, cmpStatus)
slices.SortFunc(results.Inventories, CmpInventories)
slices.SortFunc(results.Findings, cmpFindings)
}
// CmpInventories is a comparison helper fun to be used for sorting Inventory structs.
func CmpInventories(a, b *extractor.Inventory) int {
res := cmp.Or(
cmp.Compare(a.Name, b.Name),
cmp.Compare(a.Version, b.Version),
cmp.Compare(a.Extractor.Name(), b.Extractor.Name()),
)
if res != 0 {
return res
}
aloc := fmt.Sprintf("%v", a.Locations)
bloc := fmt.Sprintf("%v", b.Locations)
return cmp.Compare(aloc, bloc)
}
func cmpStatus(a, b *plugin.Status) int {
return cmpString(a.Name, b.Name)
}
func cmpFindings(a, b *detector.Finding) int {
if a.Adv.ID.Reference != b.Adv.ID.Reference {
return cmpString(a.Adv.ID.Reference, b.Adv.ID.Reference)
}
return cmpString(a.Extra, b.Extra)
}
func cmpString(a, b string) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}