-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (159 loc) · 5.12 KB
/
main.go
File metadata and controls
195 lines (159 loc) · 5.12 KB
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
package cmd
import (
"context"
"fmt"
"github.com/checkmarx/2ms/v4/engine"
"github.com/checkmarx/2ms/v4/lib/config"
"github.com/checkmarx/2ms/v4/plugins"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var Version = "0.0.0"
const (
stdoutFormatRegexpPattern = `^(ya?ml|json|sarif|human)$`
reportFormatRegexpPattern = `^(ya?ml|json|sarif)$`
configFileFlag = "config"
logLevelFlagName = "log-level"
reportPathFlagName = "report-path"
stdoutFormatFlagName = "stdout-format"
customRegexRuleFlagName = "regex"
ruleFlagName = "rule"
ignoreRuleFlagName = "ignore-rule"
ignoreFlagName = "ignore-result"
allowedValuesFlagName = "allowed-values"
specialRulesFlagName = "add-special-rule"
ignoreOnExitFlagName = "ignore-on-exit"
maxTargetMegabytesFlagName = "max-target-megabytes"
validate = "validate"
)
var (
logLevelVar string
reportPathVar []string
stdoutFormatVar string
customRegexRuleVar []string
ignoreOnExitVar = ignoreOnExitNone
engineConfigVar engine.EngineConfig
validateVar bool
logLevelUserDefined bool
)
const envPrefix = "2MS"
var configFilePath string
var vConfig = viper.New()
var allPlugins = []plugins.IPlugin{
plugins.NewConfluencePlugin(),
&plugins.DiscordPlugin{},
&plugins.FileSystemPlugin{},
&plugins.SlackPlugin{},
&plugins.PaligoPlugin{},
plugins.NewGitPlugin(),
}
func Execute() (int, error) {
vConfig.SetEnvPrefix(envPrefix)
vConfig.AutomaticEnv()
rootCmd := &cobra.Command{
Use: "2ms",
Short: "2ms Secrets Detection",
Long: "2ms Secrets Detection: A tool to detect secrets in public websites and communication services.",
Version: Version,
}
setupFlags(rootCmd)
rootCmd.AddCommand(engine.GetRulesCommand(&engineConfigVar))
// Override detector worker pool size from environment if set
if detectorWorkerPoolSize := vConfig.GetInt("TWOMS_DETECTOR_WORKERPOOL_SIZE"); detectorWorkerPoolSize != 0 {
engineConfigVar.DetectorWorkerPoolSize = detectorWorkerPoolSize
log.Info().Msgf("TWOMS_DETECTOR_WORKERPOOL_SIZE is set to %d", detectorWorkerPoolSize)
}
group := "Scan Commands"
rootCmd.AddGroup(&cobra.Group{Title: group, ID: group})
channels := plugins.NewChannels()
var engineInstance engine.IEngine
// Process flags and initialize engine with complete configuration
cobra.OnInitialize(func() {
if err := processFlags(rootCmd); err != nil {
cobra.CheckErr(err)
}
if len(engineConfigVar.CustomRegexPatterns) > 0 {
log.Info().Msgf("Custom regex patterns configured: %v", engineConfigVar.CustomRegexPatterns)
}
if len(engineConfigVar.IgnoreList) > 0 {
log.Info().Msgf("Ignore rules configured: %v", engineConfigVar.IgnoreList)
}
var err error
engineInstance, err = engine.Init(&engineConfigVar, engine.WithPluginChannels(channels))
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to initialize engine: %w", err))
}
})
// Set up plugins
for _, plugin := range allPlugins {
subCommand, err := plugin.DefineCommand(channels.GetItemsCh(), channels.GetErrorsCh())
if err != nil {
return 0, fmt.Errorf("error while defining command for plugin %s: %s", plugin.GetName(), err.Error())
}
subCommand.GroupID = group
pluginPreRun := subCommand.PreRunE
// Capture plugin name for closure
pluginName := plugin.GetName()
subCommand.PreRunE = func(cmd *cobra.Command, args []string) error {
// run plugin's own PreRunE (if any)
if pluginPreRun != nil {
if err := pluginPreRun(cmd, args); err != nil {
return err
}
}
// run engine-level PreRunE
return preRun(pluginName, engineInstance, cmd, args)
}
subCommand.PostRunE = func(cmd *cobra.Command, args []string) error {
return postRun(engineInstance)
}
rootCmd.AddCommand(subCommand)
}
listenForErrors(channels.GetErrorsCh())
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
return 0, err
}
if engineInstance != nil {
return engineInstance.GetReport().GetTotalSecretsFound(), nil
}
return 0, nil
}
func preRun(pluginName string, engineInstance engine.IEngine, _ *cobra.Command, _ []string) error {
if engineInstance == nil {
return fmt.Errorf("engine instance not initialized")
}
if err := validateFormat(stdoutFormatVar, reportPathVar); err != nil {
return err
}
engineInstance.Scan(pluginName)
return nil
}
func postRun(engineInstance engine.IEngine) error {
if engineInstance == nil {
return fmt.Errorf("engine instance not initialized")
}
engineInstance.Wait()
cfg := config.LoadConfig("2ms", Version)
report := engineInstance.GetReport()
if report.GetTotalItemsScanned() > 0 {
if zerolog.GlobalLevel() != zerolog.Disabled {
if err := report.ShowReport(stdoutFormatVar, cfg); err != nil {
return err
}
}
if len(reportPathVar) > 0 {
err := report.WriteFile(reportPathVar, cfg)
if err != nil {
return fmt.Errorf("failed to create report file with error: %s", err)
}
}
} else {
log.Info().Msg("Scan completed with empty content")
}
if err := engineInstance.Shutdown(); err != nil {
return err
}
return nil
}