-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scan.go
62 lines (49 loc) · 1.58 KB
/
scan.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
package ospkg
import (
"context"
"fmt"
"sort"
"time"
"golang.org/x/xerrors"
ospkgDetector "github.com/aquasecurity/trivy/pkg/detector/ospkg"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/types"
)
type Scanner interface {
Scan(ctx context.Context, target types.ScanTarget, options types.ScanOptions) (types.Result, bool, error)
}
type scanner struct{}
func NewScanner() Scanner {
return &scanner{}
}
func (s *scanner) Scan(ctx context.Context, target types.ScanTarget, opts types.ScanOptions) (types.Result, bool, error) {
if !target.OS.Detected() {
log.Debug("Detected OS: unknown")
return types.Result{}, false, nil
}
log.Info("Detected OS", log.String("family",
string(target.OS.Family)), log.String("version", target.OS.Name))
if target.OS.Extended {
// TODO: move the logic to each detector
target.OS.Name += "-ESM"
}
result := types.Result{
Target: fmt.Sprintf("%s (%s %s)", target.Name, target.OS.Family, target.OS.Name),
Class: types.ClassOSPkg,
Type: target.OS.Family,
}
sort.Sort(target.Packages)
result.Packages = target.Packages
if !opts.Scanners.Enabled(types.VulnerabilityScanner) {
// Return packages only
return result, false, nil
}
vulns, eosl, err := ospkgDetector.Detect(ctx, "", target.OS.Family, target.OS.Name, target.Repository, time.Time{},
target.Packages)
if err != nil {
// Return a result for those who want to override the error handling.
return result, false, xerrors.Errorf("failed vulnerability detection of OS packages: %w", err)
}
result.Vulnerabilities = vulns
return result, eosl, nil
}