-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
97 lines (75 loc) · 2.4 KB
/
main.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
package main
import (
grype_models "github.com/anchore/grype/grype/presenter/models"
"github.com/Portshift/klar/config"
"github.com/Portshift/klar/run"
"github.com/Portshift/klar/types"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/Portshift/klar/forwarding"
vulutils "github.com/Portshift/klar/utils/vulnerability"
)
func exit(code int, conf *config.Config, scanResults *forwarding.ImageVulnerabilities) {
if err := forwarding.SendScanResults(conf.ResultServicePath, scanResults); err != nil {
log.Errorf("Failed to send scan results: %v", err)
}
os.Exit(code)
}
func getImageName() (string, error) {
if len(os.Args) < 2 {
return "", fmt.Errorf("image name must be provided")
}
return os.Args[1], nil
}
func main() {
initLogs()
result := &forwarding.ImageVulnerabilities{
Success: false,
ScanUUID: os.Getenv("SCAN_UUID"),
}
imageName, err := getImageName()
if err != nil {
log.Error(err)
os.Exit(2)
}
result.Image = imageName
conf, err := config.NewConfig(imageName)
if err != nil {
log.Errorf("Invalid options: %v", err)
os.Exit(2)
}
vulnerabilities, commands, err := run.ExecuteRemoteGrypeScan(imageName, conf)
if err != nil {
errMsg := fmt.Errorf("failed to execute scan: %w", err)
log.Error(errMsg)
result.ScanErr = types.ConvertError(errMsg)
exit(2, conf, result)
}
result.Vulnerabilities = filterVulnerabilities(conf.SeverityThreshold, vulnerabilities)
result.LayerCommands = commands
result.Success = true
log.Infof("Found %d vulnerabilities", len(vulnerabilities.Matches))
if err := forwarding.SendScanResults(conf.ResultServicePath, result); err != nil {
log.Errorf("Failed to send scan results: %v", err)
}
}
func initLogs() {
if os.Getenv(config.OptionKlarTrace) == "true" {
log.SetLevel(log.DebugLevel)
}
}
func filterVulnerabilities(severityThresholdStr string, vulnerabilities *grype_models.Document) *grype_models.Document {
var filtered []grype_models.Match
severityThreshold := vulutils.GetSeverityFromString(severityThresholdStr)
for _, vulnerability := range vulnerabilities.Matches {
if vulutils.GetSeverityFromString(vulnerability.Vulnerability.Severity) < severityThreshold {
log.Debugf("Vulnerability severity below threshold. vulnerability=%+v, threshold=%+v", vulnerability,
severityThresholdStr)
continue
}
filtered = append(filtered, vulnerability)
}
vulnerabilities.Matches = filtered
return vulnerabilities
}