-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
255 lines (229 loc) · 8.7 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
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
package main
import (
_ "embed"
"flag"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/deepfence/vessel"
"github.com/gin-gonic/gin"
"github.com/deepfence/package-scanner/sbom"
"github.com/deepfence/package-scanner/scanner/router"
"github.com/deepfence/package-scanner/tools"
"github.com/deepfence/package-scanner/utils"
containerdRuntime "github.com/deepfence/vessel/containerd"
crioRuntime "github.com/deepfence/vessel/crio"
dockerRuntime "github.com/deepfence/vessel/docker"
podmanRuntime "github.com/deepfence/vessel/podman"
vc "github.com/deepfence/vessel/utils"
log "github.com/sirupsen/logrus"
)
const (
PluginName = "PackageScanner"
)
var (
supportedRuntime = []string{vc.DOCKER, vc.CONTAINERD, vc.CRIO, vc.PODMAN}
modes = []string{utils.ModeLocal, utils.ModeGRPCServer, utils.ModeHTTPServer, utils.ModeScannerOnly}
severities = []string{utils.CRITICAL, utils.HIGH, utils.MEDIUM, utils.LOW}
)
var (
//go:embed grype.yaml
grypeYaml []byte
)
var (
mode = flag.String("mode", utils.ModeLocal, strings.Join(modes, "/"))
socketPath = flag.String("socket-path", "", "Socket path for grpc server")
port = flag.String("port", "", "Port for grpc server")
output = flag.String("output", utils.TableOutput, "Output format: json or table")
quiet = flag.Bool("quiet", false, "Don't display any output in stdout")
consoleURL = flag.String("console-url", "", "Deepfence Management Console URL")
consolePort = flag.Int("console-port", 443, "Deepfence Management Console Port")
vulnerabilityScan = flag.Bool("vulnerability-scan", false, "Publish SBOM to Deepfence Management Console and run Vulnerability Scan")
deepfenceKey = flag.String("deepfence-key", "", "Deepfence key for auth")
source = flag.String("source", "", "Image name (nginx:latest) or directory (dir:/)")
scanType = flag.String("scan-type", "base,java,python,ruby,php,javascript,rust,rust-binary,golang,golang-binary,dotnet", "base,java,python,ruby,php,javascript,rust,rust-binary,golang,golang-binary,dotnet")
scanID = flag.String("scan-id", "", "(Optional) Scan id")
failOnCount = flag.Int("fail-on-count", -1, "Exit with status 1 if number of vulnerabilities found is >= this value (Default: -1)")
failOnCriticalCount = flag.Int("fail-on-critical-count", -1, "Exit with status 1 if number of critical vulnerabilities found is >= this value (Default: -1)")
failOnHighCount = flag.Int("fail-on-high-count", -1, "Exit with status 1 if number of high vulnerabilities found is >= this value (Default: -1)")
failOnMediumCount = flag.Int("fail-on-medium-count", -1, "Exit with status 1 if number of medium vulnerabilities found is >= this value (Default: -1)")
failOnLowCount = flag.Int("fail-on-low-count", -1, "Exit with status 1 if number of low vulnerabilities found is >= this value (Default: -1)")
failOnSeverityCount = flag.String("fail-on-count-severity", "", "Exit with status 1 if number of vulnerabilities of given severity found is >= fail-on-count")
failOnScore = flag.Float64("fail-on-score", -1, "Exit with status 1 if cumulative CVE score is >= this value (Default: -1)")
maskCveIds = flag.String("mask-cve-ids", "", "Comma separated cve id's to mask. Example: \"CVE-2019-9168,CVE-2019-9169\"")
cRuntime = flag.String("container-runtime", "auto", "container runtime to be used can be one of "+strings.Join(supportedRuntime, "/"))
severity = flag.String("severity", "", "Filter Vulnerabilities by severity, can be one or comma separated values of "+strings.Join(severities, "/"))
systemBin = flag.Bool("system-bin", false, "use system tools")
debug = flag.Bool("debug", false, "show debug logs")
keepSbom = flag.Bool("keep-sbom", false, "keep generated sbom file")
)
func main() {
// setup logger
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
ForceColors: true,
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return "", " " + path.Base(f.File) + ":" + strconv.Itoa(f.Line)
},
})
flag.Parse()
if *debug {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
cacheDir, dirErr := os.UserCacheDir()
if dirErr != nil {
log.Fatal(dirErr)
}
if err := os.MkdirAll(cacheDir, 0755); err != nil {
log.Fatal(err)
}
tmpPathPattern := "package-scanner-*"
// clean up old cache dir files if present
if dirs, err := filepath.Glob(path.Join(cacheDir, tmpPathPattern)); err != nil {
log.Error(err)
} else {
for _, dir := range dirs {
if err := os.RemoveAll(dir); err != nil {
log.Error(err)
}
}
}
tmpPath, tmpErr := os.MkdirTemp(cacheDir, tmpPathPattern)
if tmpErr != nil {
log.Fatal(tmpErr)
}
// remove tmpPath on exit
defer func() {
log.Infof("remove tools cache %s", tmpPath)
if err := os.RemoveAll(tmpPath); err != nil {
log.Fatal(err)
}
}()
syftBinPath := path.Join(tmpPath, "syft")
grypeBinPath := path.Join(tmpPath, "grype")
grypeConfigPath := path.Join(tmpPath, "grype.yaml")
log.Infof("tools cache dir: %s", tmpPath)
log.Infof("tools paths: %s %s %s", syftBinPath, grypeBinPath, grypeConfigPath)
// extract embedded binaries
if err := os.WriteFile(syftBinPath, tools.SyftBin, 0755); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(grypeBinPath, tools.GrypeBin, 0755); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(grypeConfigPath, grypeYaml, 0755); err != nil {
log.Fatal(err)
}
// make sure logs come to stdout in other modes except local
// local logs go to stderr to keep stdout clean for redirecting to file
if *mode != utils.ModeLocal {
log.SetOutput(os.Stdout)
}
var (
containerRuntime string
endpoint string
err error
)
// no need to determine runtime if local directory
if !strings.HasPrefix(*source, "dir:") {
if *cRuntime != "auto" {
if !utils.Contains(supportedRuntime, *cRuntime) {
log.Panicf("unsupported runtime has to be one of %s", strings.Join(supportedRuntime, "/"))
}
containerRuntime = *cRuntime
switch *cRuntime {
case vc.DOCKER:
endpoint = vc.DOCKER_SOCKET_URI
case vc.CONTAINERD:
endpoint = vc.CONTAINERD_SOCKET_URI
case vc.CRIO:
endpoint = vc.CRIO_SOCKET_URI
case vc.PODMAN:
endpoint = vc.PODMAN_SOCKET_URI
}
} else {
containerRuntime, endpoint, err = vessel.AutoDetectRuntime()
if err != nil {
log.Warnf("error detecting container runtime: %v", err)
}
}
}
config := utils.Config{
Mode: *mode,
SocketPath: *socketPath,
Port: *port,
Output: *output,
Quiet: *quiet,
ConsoleURL: *consoleURL,
ConsolePort: strconv.Itoa(*consolePort),
DeepfenceKey: *deepfenceKey,
Source: *source,
ScanType: *scanType,
VulnerabilityScan: *vulnerabilityScan,
ScanID: *scanID,
FailOnScore: *failOnScore,
FailOnCount: *failOnCount,
FailOnCriticalCount: *failOnCriticalCount,
FailOnHighCount: *failOnHighCount,
FailOnMediumCount: *failOnMediumCount,
FailOnLowCount: *failOnLowCount,
FailOnSeverityCount: *failOnSeverityCount,
MaskCveIds: *maskCveIds,
ContainerRuntimeName: containerRuntime,
SyftBinPath: "/usr/local/bin/syft",
GrypeBinPath: "/usr/local/bin/grype",
GrypeConfigPath: grypeConfigPath,
KeepSbom: *keepSbom,
}
if !*systemBin {
config.SyftBinPath = syftBinPath
config.GrypeBinPath = grypeBinPath
}
if !strings.HasPrefix(*source, "dir:") {
switch containerRuntime {
case vc.DOCKER:
config.ContainerRuntime = dockerRuntime.New(endpoint)
case vc.CONTAINERD:
config.ContainerRuntime = containerdRuntime.New(endpoint)
case vc.CRIO:
config.ContainerRuntime = crioRuntime.New(endpoint)
case vc.PODMAN:
config.ContainerRuntime = podmanRuntime.New(endpoint)
default:
// don't fail if runtime is not detected
log.Warnf("unsupported container runtime %s", containerRuntime)
}
}
switch *mode {
case utils.ModeLocal:
RunOnce(config)
case utils.ModeGRPCServer:
err := sbom.RunGrpcServer(PluginName, config)
if err != nil {
log.Panicf("error running grpc server: %v", err)
}
case utils.ModeHTTPServer:
err := sbom.RunHTTPServer(config)
if err != nil {
log.Panicf("error running http server: %v", err)
}
case utils.ModeScannerOnly:
r := router.New()
r.Use(gin.Logger())
if *port == "" {
*port = "8001"
}
log.Infof("listen on port: %s", *port)
log.Panic(r.Run(":" + *port))
default:
log.Panicf("unsupported mode %s", *mode)
}
}