2020-05-15 09:39:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-31 13:03:47 +00:00
|
|
|
"fmt"
|
2022-03-10 13:57:14 +00:00
|
|
|
"os"
|
2021-08-31 13:03:47 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
"slices"
|
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/fatih/color"
|
|
|
|
cc "github.com/ivanpirog/coloredcobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
|
2020-05-22 16:22:43 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2020-05-15 09:39:16 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
2022-12-20 15:11:51 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
2020-05-15 09:39:16 +00:00
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
var trace_lvl, dbg_lvl, nfo_lvl, wrn_lvl, err_lvl bool
|
|
|
|
|
|
|
|
var ConfigFilePath string
|
2021-03-24 17:16:17 +00:00
|
|
|
var csConfig *csconfig.Config
|
2020-11-30 09:37:17 +00:00
|
|
|
var dbClient *database.Client
|
|
|
|
|
|
|
|
var OutputFormat string
|
2022-10-07 09:05:35 +00:00
|
|
|
var OutputColor string
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2023-05-11 19:01:13 +00:00
|
|
|
var mergedConfig string
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// flagBranch overrides the value in csConfig.Cscli.HubBranch
|
|
|
|
var flagBranch = ""
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func initConfig() {
|
2021-03-24 17:16:17 +00:00
|
|
|
var err error
|
2020-11-30 09:37:17 +00:00
|
|
|
if trace_lvl {
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
} else if dbg_lvl {
|
2020-05-15 09:39:16 +00:00
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else if nfo_lvl {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
} else if wrn_lvl {
|
|
|
|
log.SetLevel(log.WarnLevel)
|
|
|
|
} else if err_lvl {
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
|
|
|
}
|
2022-07-13 10:00:26 +00:00
|
|
|
|
2023-05-31 10:39:22 +00:00
|
|
|
if !slices.Contains(NoNeedConfig, os.Args[1]) {
|
2023-09-12 09:04:56 +00:00
|
|
|
log.Debugf("Using %s as configuration file", ConfigFilePath)
|
2023-05-11 19:01:13 +00:00
|
|
|
csConfig, mergedConfig, err = csconfig.NewConfig(ConfigFilePath, false, false, true)
|
2022-07-13 10:00:26 +00:00
|
|
|
if err != nil {
|
2022-11-29 08:16:07 +00:00
|
|
|
log.Fatal(err)
|
2022-07-13 10:00:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
csConfig = csconfig.NewDefaultConfig()
|
2020-05-22 16:22:43 +00:00
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2023-01-13 12:42:42 +00:00
|
|
|
// recap of the enabled feature flags, because logging
|
|
|
|
// was not enabled when we set them from envvars
|
|
|
|
if fflist := csconfig.ListFeatureFlags(); fflist != "" {
|
|
|
|
log.Debugf("Enabled feature flags: %s", fflist)
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
if flagBranch != "" {
|
|
|
|
csConfig.Cscli.HubBranch = flagBranch
|
2021-02-07 14:50:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
if OutputFormat != "" {
|
|
|
|
csConfig.Cscli.Output = OutputFormat
|
|
|
|
if OutputFormat != "json" && OutputFormat != "raw" && OutputFormat != "human" {
|
|
|
|
log.Fatalf("output format %s unknown", OutputFormat)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
if csConfig.Cscli.Output == "" {
|
|
|
|
csConfig.Cscli.Output = "human"
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if csConfig.Cscli.Output == "json" {
|
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
2021-02-25 10:20:36 +00:00
|
|
|
log.SetLevel(log.ErrorLevel)
|
2020-11-30 09:37:17 +00:00
|
|
|
} else if csConfig.Cscli.Output == "raw" {
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
2020-06-01 10:56:32 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2022-10-07 09:05:35 +00:00
|
|
|
if OutputColor != "" {
|
|
|
|
csConfig.Cscli.Color = OutputColor
|
|
|
|
if OutputColor != "yes" && OutputColor != "no" && OutputColor != "auto" {
|
|
|
|
log.Fatalf("output color %s unknown", OutputColor)
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 18:07:58 +00:00
|
|
|
var validArgs = []string{
|
2021-06-28 15:34:19 +00:00
|
|
|
"scenarios", "parsers", "collections", "capi", "lapi", "postoverflows", "machines",
|
|
|
|
"metrics", "bouncers", "alerts", "decisions", "simulation", "hub", "dashboard",
|
2022-08-18 09:54:01 +00:00
|
|
|
"config", "completion", "version", "console", "notifications", "support",
|
2021-03-24 18:07:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 13:03:47 +00:00
|
|
|
func prepender(filename string) string {
|
|
|
|
const header = `---
|
|
|
|
id: %s
|
|
|
|
title: %s
|
|
|
|
---
|
|
|
|
`
|
|
|
|
name := filepath.Base(filename)
|
2023-07-26 08:29:58 +00:00
|
|
|
base := strings.TrimSuffix(name, filepath.Ext(name))
|
2022-11-07 09:36:50 +00:00
|
|
|
return fmt.Sprintf(header, base, strings.ReplaceAll(base, "_", " "))
|
2021-08-31 13:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func linkHandler(name string) string {
|
2021-09-03 10:56:17 +00:00
|
|
|
return fmt.Sprintf("/cscli/%s", name)
|
2021-08-31 13:03:47 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 10:00:26 +00:00
|
|
|
var (
|
|
|
|
NoNeedConfig = []string{
|
|
|
|
"help",
|
|
|
|
"completion",
|
|
|
|
"version",
|
|
|
|
"hubtest",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func main() {
|
2022-12-20 15:11:51 +00:00
|
|
|
// set the formatter asap and worry about level later
|
2023-11-24 13:59:28 +00:00
|
|
|
logFormatter := &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true}
|
2022-12-20 15:11:51 +00:00
|
|
|
log.SetFormatter(logFormatter)
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
if err := fflag.RegisterAllFeatures(); err != nil {
|
|
|
|
log.Fatalf("failed to register features: %s", err)
|
2022-12-21 16:19:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 12:42:42 +00:00
|
|
|
if err := csconfig.LoadFeatureFlagsEnv(log.StandardLogger()); err != nil {
|
|
|
|
log.Fatalf("failed to set feature flags from env: %s", err)
|
2022-12-21 16:19:20 +00:00
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "cscli",
|
|
|
|
Short: "cscli allows you to manage crowdsec",
|
|
|
|
Long: `cscli is the main command to interact with your crowdsec service, scenarios & db.
|
|
|
|
It is meant to allow you to manage bans, parsers/scenarios/etc, api and generally manage you crowdsec setup.`,
|
2021-09-03 10:56:17 +00:00
|
|
|
ValidArgs: validArgs,
|
|
|
|
DisableAutoGenTag: true,
|
2022-05-19 11:42:44 +00:00
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
/*TBD examples*/
|
|
|
|
}
|
2022-10-13 10:28:24 +00:00
|
|
|
|
|
|
|
cc.Init(&cc.Config{
|
2023-03-03 12:46:28 +00:00
|
|
|
RootCmd: rootCmd,
|
|
|
|
Headings: cc.Yellow,
|
|
|
|
Commands: cc.Green + cc.Bold,
|
2022-10-13 10:28:24 +00:00
|
|
|
CmdShortDescr: cc.Cyan,
|
2023-03-03 12:46:28 +00:00
|
|
|
Example: cc.Italic,
|
|
|
|
ExecName: cc.Bold,
|
|
|
|
Aliases: cc.Bold + cc.Italic,
|
2022-10-13 10:28:24 +00:00
|
|
|
FlagsDataType: cc.White,
|
2023-03-03 12:46:28 +00:00
|
|
|
Flags: cc.Green,
|
|
|
|
FlagsDescr: cc.Cyan,
|
2022-10-13 10:28:24 +00:00
|
|
|
})
|
|
|
|
rootCmd.SetOut(color.Output)
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
var cmdDocGen = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "doc",
|
|
|
|
Short: "Generate the documentation in `./doc/`. Directory must exist.",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
Hidden: true,
|
|
|
|
DisableAutoGenTag: true,
|
2023-02-20 14:05:42 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-08-31 13:03:47 +00:00
|
|
|
if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", prepender, linkHandler); err != nil {
|
2023-02-20 14:05:42 +00:00
|
|
|
return fmt.Errorf("Failed to generate cobra doc: %s", err)
|
2020-05-20 15:50:56 +00:00
|
|
|
}
|
2023-02-20 14:05:42 +00:00
|
|
|
return nil
|
2020-05-15 09:39:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(cmdDocGen)
|
|
|
|
/*usage*/
|
|
|
|
var cmdVersion = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "version",
|
2023-08-22 12:09:53 +00:00
|
|
|
Short: "Display version",
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
DisableAutoGenTag: true,
|
2020-05-15 09:39:16 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cwversion.Show()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rootCmd.AddCommand(cmdVersion)
|
|
|
|
|
2022-02-01 09:34:53 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&ConfigFilePath, "config", "c", csconfig.DefaultConfigPath("config.yaml"), "path to crowdsec config file")
|
2023-07-21 21:23:24 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", "", "Output format: human, json, raw")
|
|
|
|
rootCmd.PersistentFlags().StringVarP(&OutputColor, "color", "", "auto", "Output color: yes, no, auto")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&wrn_lvl, "warning", false, "Set logging to warning")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&err_lvl, "error", false, "Set logging to error")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&trace_lvl, "trace", false, "Set logging to trace")
|
2020-09-01 12:32:45 +00:00
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&flagBranch, "branch", "", "Override hub branch on github")
|
2020-05-27 16:09:23 +00:00
|
|
|
if err := rootCmd.PersistentFlags().MarkHidden("branch"); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatalf("failed to hide flag: %s", err)
|
2020-05-27 16:09:23 +00:00
|
|
|
}
|
2022-03-10 13:57:14 +00:00
|
|
|
|
2023-04-03 08:11:56 +00:00
|
|
|
// Look for "-c /path/to/config.yaml"
|
|
|
|
// This duplicates the logic in cobra, but we need to do it before
|
|
|
|
// because feature flags can change which subcommands are available.
|
|
|
|
for i, arg := range os.Args {
|
|
|
|
if arg == "-c" || arg == "--config" {
|
|
|
|
if len(os.Args) > i+1 {
|
|
|
|
ConfigFilePath = os.Args[i+1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := csconfig.LoadFeatureFlagsFile(ConfigFilePath, log.StandardLogger()); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-13 10:00:26 +00:00
|
|
|
if len(os.Args) > 1 {
|
2022-03-10 13:57:14 +00:00
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
/*don't sort flags so we can enforce order*/
|
|
|
|
rootCmd.Flags().SortFlags = false
|
|
|
|
rootCmd.PersistentFlags().SortFlags = false
|
|
|
|
|
|
|
|
rootCmd.AddCommand(NewConfigCmd())
|
2023-12-07 13:36:35 +00:00
|
|
|
rootCmd.AddCommand(NewCLIHub().NewCommand())
|
2020-05-15 09:39:16 +00:00
|
|
|
rootCmd.AddCommand(NewMetricsCmd())
|
|
|
|
rootCmd.AddCommand(NewDashboardCmd())
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.AddCommand(NewDecisionsCmd())
|
|
|
|
rootCmd.AddCommand(NewAlertsCmd())
|
2020-07-16 13:59:09 +00:00
|
|
|
rootCmd.AddCommand(NewSimulationCmds())
|
2023-12-07 13:36:35 +00:00
|
|
|
rootCmd.AddCommand(NewCLIBouncers().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewCLIMachines().NewCommand())
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.AddCommand(NewCapiCmd())
|
|
|
|
rootCmd.AddCommand(NewLapiCmd())
|
2021-03-24 18:07:58 +00:00
|
|
|
rootCmd.AddCommand(NewCompletionCmd())
|
2021-06-28 15:34:19 +00:00
|
|
|
rootCmd.AddCommand(NewConsoleCmd())
|
2023-12-07 13:36:35 +00:00
|
|
|
rootCmd.AddCommand(NewCLIExplain().NewCommand())
|
2021-10-04 15:14:52 +00:00
|
|
|
rootCmd.AddCommand(NewHubTestCmd())
|
2023-12-07 13:36:35 +00:00
|
|
|
rootCmd.AddCommand(NewCLINotifications().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewCLISupport().NewCommand())
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(NewCollectionCLI().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewParserCLI().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewScenarioCLI().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewPostOverflowCLI().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewAppsecConfigCLI().NewCommand())
|
|
|
|
rootCmd.AddCommand(NewAppsecRuleCLI().NewCommand())
|
2022-05-31 08:01:30 +00:00
|
|
|
|
2023-02-06 06:33:04 +00:00
|
|
|
if fflag.CscliSetup.IsEnabled() {
|
|
|
|
rootCmd.AddCommand(NewSetupCmd())
|
|
|
|
}
|
|
|
|
|
2023-03-03 12:46:28 +00:00
|
|
|
if fflag.PapiClient.IsEnabled() {
|
2023-12-07 13:36:35 +00:00
|
|
|
rootCmd.AddCommand(NewCLIPapi().NewCommand())
|
2023-03-03 12:46:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 08:49:17 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2023-04-03 08:11:56 +00:00
|
|
|
log.Fatal(err)
|
2020-05-20 08:49:17 +00:00
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|