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"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
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/cwhub"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
)
|
|
|
|
|
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
|
2020-05-15 09:39:16 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
var downloadOnly bool
|
2020-12-02 17:47:17 +00:00
|
|
|
var forceAction bool
|
|
|
|
var purge bool
|
|
|
|
var all bool
|
2020-11-30 09:37:17 +00:00
|
|
|
var restoreOldBackup bool
|
|
|
|
|
|
|
|
var prometheusURL string
|
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)
|
|
|
|
}
|
2021-02-25 10:20:36 +00:00
|
|
|
logFormatter := &log.TextFormatter{TimestampFormat: "02-01-2006 03:04:05 PM", FullTimestamp: true}
|
|
|
|
log.SetFormatter(logFormatter)
|
2021-03-24 17:16:17 +00:00
|
|
|
csConfig, err = csconfig.NewConfig(ConfigFilePath, false, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Debugf("Using %s as configuration file", ConfigFilePath)
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadCSCLI(); err != nil {
|
2020-05-22 16:22:43 +00:00
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
|
2021-02-07 14:50:27 +00:00
|
|
|
if csConfig.Cscli == nil {
|
|
|
|
log.Fatalf("missing 'cscli' configuration in '%s', exiting", ConfigFilePath)
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
if cwhub.HubBranch == "" && csConfig.Cscli.HubBranch != "" {
|
|
|
|
cwhub.HubBranch = csConfig.Cscli.HubBranch
|
|
|
|
}
|
|
|
|
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-05-22 16:31:46 +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
|
|
|
|
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-05-18 14:13:33 +00:00
|
|
|
"config", "completion", "version", "console", "notifications",
|
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)
|
|
|
|
base := strings.TrimSuffix(name, path.Ext(name))
|
|
|
|
return fmt.Sprintf(header, base, strings.Replace(base, "_", " ", -1))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
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,
|
2020-11-30 09:37:17 +00:00
|
|
|
/*TBD examples*/
|
|
|
|
}
|
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,
|
2020-05-15 09:39:16 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-08-31 13:03:47 +00:00
|
|
|
if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", prepender, linkHandler); err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
log.Fatalf("Failed to generate cobra doc: %s", err.Error())
|
2020-05-20 15:50:56 +00:00
|
|
|
}
|
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",
|
|
|
|
Short: "Display version and exit.",
|
|
|
|
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")
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", "", "Output format : human, json, raw.")
|
2020-05-15 09:39:16 +00:00
|
|
|
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.")
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.PersistentFlags().BoolVar(&trace_lvl, "trace", false, "Set logging to trace.")
|
2020-09-01 12:32:45 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&cwhub.HubBranch, "branch", "", "Override hub branch on github")
|
2020-05-27 16:09:23 +00:00
|
|
|
if err := rootCmd.PersistentFlags().MarkHidden("branch"); err != nil {
|
|
|
|
log.Fatalf("failed to make branch hidden : %s", err)
|
|
|
|
}
|
2022-03-10 13:57:14 +00:00
|
|
|
|
2022-04-13 13:44:23 +00:00
|
|
|
if len(os.Args) > 1 && os.Args[1] != "completion" && os.Args[1] != "version" && os.Args[1] != "help" {
|
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())
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.AddCommand(NewHubCmd())
|
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())
|
|
|
|
// rootCmd.AddCommand(NewInspectCmd())
|
2020-07-16 13:59:09 +00:00
|
|
|
rootCmd.AddCommand(NewSimulationCmds())
|
2020-11-30 09:37:17 +00:00
|
|
|
rootCmd.AddCommand(NewBouncersCmd())
|
|
|
|
rootCmd.AddCommand(NewMachinesCmd())
|
|
|
|
rootCmd.AddCommand(NewParsersCmd())
|
|
|
|
rootCmd.AddCommand(NewScenariosCmd())
|
|
|
|
rootCmd.AddCommand(NewCollectionsCmd())
|
|
|
|
rootCmd.AddCommand(NewPostOverflowsCmd())
|
|
|
|
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())
|
2021-10-04 15:14:52 +00:00
|
|
|
rootCmd.AddCommand(NewExplainCmd())
|
|
|
|
rootCmd.AddCommand(NewHubTestCmd())
|
2022-05-18 14:13:33 +00:00
|
|
|
rootCmd.AddCommand(NewNotificationsCmd())
|
2020-05-20 08:49:17 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2022-05-19 08:48:08 +00:00
|
|
|
log.Fatal(err)
|
2020-05-20 08:49:17 +00:00
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|