-
Notifications
You must be signed in to change notification settings - Fork 310
/
scan.go
184 lines (161 loc) · 6.15 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
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
package console
import (
_ "embed" // Embed kics CLI img and scan-flags
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/Checkmarx/kics/internal/console/flags"
consoleHelpers "github.com/Checkmarx/kics/internal/console/helpers"
"github.com/Checkmarx/kics/internal/constants"
sentryReport "github.com/Checkmarx/kics/internal/sentry"
"github.com/Checkmarx/kics/pkg/engine/source"
"github.com/Checkmarx/kics/pkg/scan"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
var (
//go:embed assets/kics-console
banner string
//go:embed assets/scan-flags.json
scanFlagsListContent string
)
const (
scanCommandStr = "scan"
initError = "initialization error - "
)
// NewScanCmd creates a new instance of the scan Command
func NewScanCmd() *cobra.Command {
return &cobra.Command{
Use: scanCommandStr,
Short: "Executes a scan analysis",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return preRun(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd)
},
}
}
func initScanCmd(scanCmd *cobra.Command) error {
if err := flags.InitJSONFlags(
scanCmd,
scanFlagsListContent,
false,
source.ListSupportedPlatforms(),
source.ListSupportedCloudProviders()); err != nil {
return err
}
if err := scanCmd.MarkFlagRequired(flags.PathFlag); err != nil {
sentryReport.ReportSentry(&sentryReport.Report{
Message: "Failed to add command required flags",
Err: err,
Location: "func initScanCmd()",
}, true)
log.Err(err).Msg("Failed to add command required flags")
}
return nil
}
func run(cmd *cobra.Command) error {
changedDefaultQueryPath := cmd.Flags().Lookup(flags.QueriesPath).Changed
changedDefaultLibrariesPath := cmd.Flags().Lookup(flags.LibrariesPath).Changed
if err := consoleHelpers.InitShouldIgnoreArg(flags.GetStrFlag(flags.IgnoreOnExitFlag)); err != nil {
return err
}
if err := consoleHelpers.InitShouldFailArg(flags.GetMultiStrFlag(flags.FailOnFlag)); err != nil {
return err
}
if flags.GetStrFlag(flags.OutputPathFlag) != "" {
updateReportFormats()
flags.SetStrFlag(flags.OutputNameFlag, filepath.Base(flags.GetStrFlag(flags.OutputNameFlag)))
if filepath.Ext(flags.GetStrFlag(flags.OutputPathFlag)) != "" {
flags.SetStrFlag(flags.OutputPathFlag, filepath.Join(flags.GetStrFlag(flags.OutputPathFlag), string(os.PathSeparator)))
}
if err := os.MkdirAll(flags.GetStrFlag(flags.OutputPathFlag), os.ModePerm); err != nil {
return err
}
}
if flags.GetStrFlag(flags.PayloadPathFlag) != "" && filepath.Dir(flags.GetStrFlag(flags.PayloadPathFlag)) != "." {
if err := os.MkdirAll(filepath.Dir(flags.GetStrFlag(flags.PayloadPathFlag)), os.ModePerm); err != nil {
return err
}
}
gracefulShutdown()
// save the scan parameters into the ScanParameters struct
scanParams := getScanParameters(changedDefaultQueryPath, changedDefaultLibrariesPath)
return executeScan(scanParams)
}
func updateReportFormats() {
for _, format := range flags.GetMultiStrFlag(flags.ReportFormatsFlag) {
if strings.EqualFold(format, "all") {
flags.SetMultiStrFlag(flags.ReportFormatsFlag, consoleHelpers.ListReportFormats())
break
}
}
}
func getScanParameters(changedDefaultQueryPath, changedDefaultLibrariesPath bool) *scan.Parameters {
scanParams := scan.Parameters{
CloudProvider: flags.GetMultiStrFlag(flags.CloudProviderFlag),
DisableCISDesc: flags.GetBoolFlag(flags.DisableCISDescFlag),
DisableFullDesc: flags.GetBoolFlag(flags.DisableFullDescFlag),
ExcludeCategories: flags.GetMultiStrFlag(flags.ExcludeCategoriesFlag),
ExcludePaths: flags.GetMultiStrFlag(flags.ExcludePathsFlag),
ExcludeQueries: flags.GetMultiStrFlag(flags.ExcludeQueriesFlag),
ExcludeResults: flags.GetMultiStrFlag(flags.ExcludeResultsFlag),
ExcludeSeverities: flags.GetMultiStrFlag(flags.ExcludeSeveritiesFlag),
IncludeQueries: flags.GetMultiStrFlag(flags.IncludeQueriesFlag),
InputData: flags.GetStrFlag(flags.InputDataFlag),
OutputName: flags.GetStrFlag(flags.OutputNameFlag),
OutputPath: flags.GetStrFlag(flags.OutputPathFlag),
Path: flags.GetMultiStrFlag(flags.PathFlag),
PayloadPath: flags.GetStrFlag(flags.PayloadPathFlag),
PreviewLines: flags.GetIntFlag(flags.PreviewLinesFlag),
QueriesPath: flags.GetMultiStrFlag(flags.QueriesPath),
LibrariesPath: flags.GetStrFlag(flags.LibrariesPath),
ReportFormats: flags.GetMultiStrFlag(flags.ReportFormatsFlag),
Platform: flags.GetMultiStrFlag(flags.TypeFlag),
QueryExecTimeout: flags.GetIntFlag(flags.QueryExecTimeoutFlag),
LineInfoPayload: flags.GetBoolFlag(flags.LineInfoPayloadFlag),
DisableSecrets: flags.GetBoolFlag(flags.DisableSecretsFlag),
SecretsRegexesPath: flags.GetStrFlag(flags.SecretsRegexesPathFlag),
ScanID: scanID,
ChangedDefaultLibrariesPath: changedDefaultLibrariesPath,
ChangedDefaultQueryPath: changedDefaultQueryPath,
BillOfMaterials: flags.GetBoolFlag(flags.BomFlag),
}
return &scanParams
}
func executeScan(scanParams *scan.Parameters) error {
log.Debug().Msg("console.scan()")
for _, warn := range warnings {
log.Warn().Msgf(warn)
}
console := newConsole()
console.preScan()
client, err := scan.NewClient(scanParams, console.ProBarBuilder, console.Printer)
if err != nil {
log.Err(err)
return err
}
err = client.PerformScan(ctx)
if err != nil {
log.Err(err)
return err
}
return nil
}
// gracefulShutdown catches signal interrupt and returns the appropriate exit code
func gracefulShutdown() {
c := make(chan os.Signal)
// This line should not be lint, since golangci-lint has an issue about it (https://github.com/golang/go/issues/45043)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) // nolint
showErrors := consoleHelpers.ShowError("errors")
interruptCode := constants.SignalInterruptCode
go func(showErrors bool, interruptCode int) {
<-c
if showErrors {
os.Exit(interruptCode)
}
}(showErrors, interruptCode)
}