forked from google/osv-scalibr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.go
368 lines (320 loc) · 10.8 KB
/
extractor.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// 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 extractor provides the interface for inventory extraction plugins.
package extractor
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"slices"
"time"
"github.com/google/osv-scalibr/extractor/internal"
"github.com/google/osv-scalibr/log"
"github.com/google/osv-scalibr/plugin"
"github.com/google/osv-scalibr/purl"
"github.com/google/osv-scalibr/stats"
)
// InventoryExtractor is the interface extraction plugin, used to extract inventory data such as
// OS and language packages.
type InventoryExtractor interface {
plugin.Plugin
// FileRequired should return true if the file described by path and mode is
// relevant for the extractor.
// Note that the plugin doesn't traverse the filesystem itself but relies on the core
// library for that.
FileRequired(path string, mode fs.FileMode) bool
// Extract extracts inventory data relevant for the extractor from a given file.
Extract(ctx context.Context, input *ScanInput) ([]*Inventory, error)
// ToPURL converts an inventory created by this extractor into a PURL.
ToPURL(i *Inventory) (*purl.PackageURL, error)
// ToCPEs converts an inventory created by this extractor into CPEs, if supported.
ToCPEs(i *Inventory) ([]string, error)
}
// ScanInput describes one file to extract from.
type ScanInput struct {
Path string
// The root directory to start all extractions from.
ScanRoot string
Info fs.FileInfo
// A reader for accessing contents of the file.
// Note that the file is closed by the core library, not the plugin.
Reader io.Reader
}
// Config stores the config settings for an extraction run.
type Config struct {
Extractors []InventoryExtractor
ScanRoot string
FS fs.FS
// Directories that the file system walk should ignore, relative to the FS root.
// TODO(b/279413691): Also skip local paths, e.g. "Skip all .git dirs"
DirsToSkip []string
// 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
}
// LINT.IfChange
// Inventory is an instance of a software package or library found by the extractor.
type Inventory struct {
// A human-readable name representation of the package. Note that this field
// should only be used for things like logging as different packages can have
// multiple different types of names (e.g. .deb packages have a source name
// and a binary name), in which case we arbitrarily pick one of them to use here.
// In cases when the exact name type used is important (e.g. when matching
// against vuln feeds) you should use the specific name field from the Metadata.
Name string
// The version of this package.
Version string
// Paths or source of files related to the package.
Locations []string
// The name of the InventoryExtractor that found this software instance. Set by the core library.
Extractor string
// The additional data found in the package.
Metadata any
}
// LINT.ThenChange(/binary/proto/scan_result.proto)
// Run runs the specified extractors and returns their extraction results,
// as well as info about whether the plugin runs completed successfully.
func Run(ctx context.Context, config *Config) ([]*Inventory, []*plugin.Status, error) {
config.FS = os.DirFS(config.ScanRoot)
return RunFS(ctx, config)
}
// RunFS runs the specified extractors and returns their extraction results,
// as well as info about whether the plugin runs completed successfully.
// scanRoot is the location of fsys.
// This method is for testing, use Run() to avoid confusion with scanRoot vs fsys.
func RunFS(ctx context.Context, config *Config) ([]*Inventory, []*plugin.Status, error) {
if len(config.Extractors) == 0 {
return []*Inventory{}, []*plugin.Status{}, nil
}
start := time.Now()
wc := walkContext{
ctx: ctx,
stats: config.Stats,
extractors: config.Extractors,
fs: config.FS,
scanRoot: config.ScanRoot,
dirsToSkip: stringListToMap(config.DirsToSkip),
skipDirRegex: config.SkipDirRegex,
readSymlinks: config.ReadSymlinks,
maxInodes: config.MaxInodes,
inodesVisited: 0,
lastStatus: time.Now(),
inventory: []*Inventory{},
errors: make(map[string]error),
foundInv: make(map[string]bool),
mapInodes: make(map[string]int),
mapExtracts: make(map[string]int),
}
err := internal.WalkDirUnsorted(config.FS, ".", wc.handleFile)
log.Infof("End status: %d inodes visited, %d Extract calls, %s elapsed",
wc.inodesVisited, wc.extractCalls, time.Since(start))
printAnalyseInodes(&wc)
return wc.inventory, errToExtractorStatus(config.Extractors, wc.foundInv, wc.errors), err
}
type walkContext struct {
ctx context.Context
stats stats.Collector
extractors []InventoryExtractor
fs fs.FS
scanRoot string
dirsToSkip map[string]bool // Anything under these paths should be skipped.
skipDirRegex *regexp.Regexp
maxInodes int
inodesVisited int
// Inventories found.
inventory []*Inventory
// Extractor name to runtime errors.
errors map[string]error
// Whether an extractor found any inventory.
foundInv map[string]bool
// Whether to read symlinks.
readSymlinks bool
// Data for status printing.
lastStatus time.Time
lastInodes int
extractCalls int
lastExtracts int
// Data for analytics.
mapInodes map[string]int
mapExtracts map[string]int
}
func (wc *walkContext) handleFile(path string, d fs.DirEntry, fserr error) error {
wc.printStatus(path)
wc.inodesVisited++
if wc.maxInodes > 0 && wc.inodesVisited > wc.maxInodes {
return fmt.Errorf("maxInodes (%d) exceeded", wc.maxInodes)
}
wc.stats.AfterInodeVisited(path)
if wc.ctx.Err() != nil {
return wc.ctx.Err()
}
if fserr != nil {
if os.IsPermission(fserr) {
// Permission errors are expected when traversing the entire filesystem.
log.Debugf("fserr: %v", fserr)
} else {
log.Errorf("fserr: %v", fserr)
}
return nil
}
if d.Type().IsDir() {
if wc.shouldSkipDir(path) { // Skip everything inside this dir.
return fs.SkipDir
}
return nil
}
// Ignore non regular files except symlinks.
if !d.Type().IsRegular() {
// Ignore the file because symlink reading is disabled.
if !wc.readSymlinks {
return nil
}
// Ignore non-symlinks.
if (d.Type() & fs.ModeType) != fs.ModeSymlink {
return nil
}
}
s, err := fs.Stat(wc.fs, path)
if err != nil {
log.Warnf("os.Stat(%s): %v", path, err)
return nil
}
wc.mapInodes[internal.ParentDir(filepath.Dir(path), 3)]++
for _, ex := range wc.extractors {
wc.runExtractor(ex, path, s.Mode())
}
return nil
}
func (wc *walkContext) shouldSkipDir(path string) bool {
if _, ok := wc.dirsToSkip[path]; ok {
return true
}
if wc.skipDirRegex != nil {
return wc.skipDirRegex.MatchString(path)
}
return false
}
func (wc *walkContext) runExtractor(ex InventoryExtractor, path string, mode fs.FileMode) {
if !ex.FileRequired(path, mode) {
return
}
rc, err := wc.fs.Open(path)
if err != nil {
addErrToMap(wc.errors, ex.Name(), fmt.Errorf("Open(%s): %v", path, err))
return
}
defer rc.Close()
info, err := rc.Stat()
if err != nil {
addErrToMap(wc.errors, ex.Name(), fmt.Errorf("stat(%s): %v", path, err))
return
}
wc.mapExtracts[internal.ParentDir(filepath.Dir(path), 3)]++
wc.extractCalls++
start := time.Now()
results, err := ex.Extract(wc.ctx, &ScanInput{Path: path, ScanRoot: wc.scanRoot, Info: info, Reader: rc})
wc.stats.AfterExtractorRun(ex.Name(), time.Since(start), err)
if err != nil {
addErrToMap(wc.errors, ex.Name(), fmt.Errorf("%s: %w", path, err))
}
if len(results) > 0 {
wc.foundInv[ex.Name()] = true
for _, r := range results {
wc.inventory = append(wc.inventory, r)
}
}
}
func stringListToMap(paths []string) map[string]bool {
result := make(map[string]bool)
for _, p := range paths {
result[p] = true
}
return result
}
func addErrToMap(errors map[string]error, key string, err error) {
if prev, ok := errors[key]; !ok {
errors[key] = err
} else {
errors[key] = fmt.Errorf("%w\n%w", prev, err)
}
}
func errToExtractorStatus(extractors []InventoryExtractor, foundInv map[string]bool, errors map[string]error) []*plugin.Status {
result := make([]*plugin.Status, 0, len(extractors))
for _, ex := range extractors {
result = append(result, plugin.StatusFromErr(ex, foundInv[ex.Name()], errors[ex.Name()]))
}
return result
}
func (wc *walkContext) printStatus(path string) {
if time.Since(wc.lastStatus) < 2*time.Second {
return
}
log.Infof("Status: new inodes: %d, %.1f inodes/s, new extract calls: %d, path: %q\n",
wc.inodesVisited-wc.lastInodes,
float64(wc.inodesVisited-wc.lastInodes)/time.Since(wc.lastStatus).Seconds(),
wc.extractCalls-wc.lastExtracts, path)
wc.lastStatus = time.Now()
wc.lastInodes = wc.inodesVisited
wc.lastExtracts = wc.extractCalls
}
func printAnalyseInodes(wc *walkContext) {
printSizeInformation(wc)
transitiveInodes := internal.BuildTransitiveMaps(wc.mapInodes)
transitiveExtracts := internal.BuildTransitiveMaps(wc.mapExtracts)
dirs := mapToList(wc, transitiveInodes, transitiveExtracts)
slices.SortFunc(dirs, func(a, b pathCount) int { return b.inodes - a.inodes })
printTop10(dirs)
}
func printSizeInformation(wc *walkContext) {
b := 0
for p := range wc.mapInodes {
b += len(p) + 4
}
for p := range wc.mapExtracts {
b += len(p) + 4
}
log.Infof("Analytics data: %d dirs in mapInodes, %d dirs in mapExtracts, estimated bytes: %d",
len(wc.mapInodes), len(wc.mapExtracts), b)
}
type pathCount struct {
path string
inodes int
}
func mapToList(wc *walkContext, transitiveInodes, transitiveExtracts map[string]int) []pathCount {
dirs := make([]pathCount, 0, len(wc.mapInodes))
for p, inodes := range wc.mapInodes {
// If the directory contains any Extract calls, filter it out.
if wc.mapExtracts[p]+transitiveExtracts[p] > 0 {
continue
}
dirs = append(dirs, pathCount{p, inodes + transitiveInodes[p]})
}
return dirs
}
func printTop10(dirs []pathCount) {
out := ""
for _, d := range dirs[:min(len(dirs), 10)] {
out += fmt.Sprintf("%9d %s\n", d.inodes, d.path)
}
log.Infof("Top 10 directories by number of files without Extract calls:\n%s", out)
}