2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewParsersCmd() *cobra.Command {
|
|
|
|
var cmdParsers = &cobra.Command{
|
|
|
|
Use: "parsers [action] [config]",
|
|
|
|
Short: "Install/Remove/Upgrade/Inspect parser(s) from hub",
|
|
|
|
Example: `cscli parsers install crowdsecurity/sshd-logs
|
|
|
|
cscli parsers inspect crowdsecurity/sshd-logs
|
|
|
|
cscli parsers upgrade crowdsecurity/sshd-logs
|
|
|
|
cscli parsers list
|
|
|
|
cscli parsers remove crowdsecurity/sshd-logs
|
|
|
|
`,
|
2021-08-31 13:03:47 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2022-04-20 13:44:48 +00:00
|
|
|
Aliases: []string{"parser"},
|
2021-08-31 13:03:47 +00:00
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2021-03-24 17:16:17 +00:00
|
|
|
if err := csConfig.LoadHub(); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Fatal(err)
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
|
|
|
if csConfig.Hub == nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
return fmt.Errorf("you must configure cli before interacting with hub")
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:46:48 +00:00
|
|
|
if err := cwhub.SetHubBranch(); err != nil {
|
2020-11-30 09:37:17 +00:00
|
|
|
return fmt.Errorf("error while setting hub branch: %s", err)
|
|
|
|
}
|
2021-03-24 17:16:17 +00:00
|
|
|
|
|
|
|
if err := cwhub.GetHubIdx(csConfig.Hub); err != nil {
|
2022-06-06 13:24:48 +00:00
|
|
|
log.Info("Run 'sudo cscli hub update' to get the hub index")
|
2021-03-24 17:16:17 +00:00
|
|
|
log.Fatalf("Failed to get Hub index : %v", err)
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
if cmd.Name() == "inspect" || cmd.Name() == "list" {
|
|
|
|
return
|
|
|
|
}
|
2021-03-26 16:42:56 +00:00
|
|
|
log.Infof(ReloadMessage())
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-16 16:27:30 +00:00
|
|
|
var ignoreError bool
|
2020-11-30 09:37:17 +00:00
|
|
|
var cmdParsersInstall = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "install [config]",
|
|
|
|
Short: "Install given parser(s)",
|
|
|
|
Long: `Fetch and install given parser(s) from hub`,
|
|
|
|
Example: `cscli parsers install crowdsec/xxx crowdsec/xyz`,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
2022-04-20 13:44:48 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return compAllItems(cwhub.PARSERS, args, toComplete)
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
for _, name := range args {
|
2022-04-19 10:07:35 +00:00
|
|
|
if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS, forceAction, downloadOnly); err != nil {
|
2022-03-16 16:27:30 +00:00
|
|
|
if ignoreError {
|
|
|
|
log.Errorf("Error while installing '%s': %s", name, err)
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Error while installing '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
|
2020-12-02 17:47:17 +00:00
|
|
|
cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
|
2022-03-16 16:27:30 +00:00
|
|
|
cmdParsersInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple parsers")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdParsers.AddCommand(cmdParsersInstall)
|
|
|
|
|
|
|
|
var cmdParsersRemove = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "remove [config]",
|
|
|
|
Short: "Remove given parser(s)",
|
|
|
|
Long: `Remove given parse(s) from hub`,
|
2022-04-20 13:44:48 +00:00
|
|
|
Aliases: []string{"delete"},
|
2021-08-31 13:03:47 +00:00
|
|
|
Example: `cscli parsers remove crowdsec/xxx crowdsec/xyz`,
|
|
|
|
DisableAutoGenTag: true,
|
2022-04-20 13:44:48 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return compInstalledItems(cwhub.PARSERS, args, toComplete)
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-12-02 17:47:17 +00:00
|
|
|
if all {
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.RemoveMany(csConfig, cwhub.PARSERS, "", all, purge, forceAction)
|
2022-03-16 16:26:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
log.Fatalf("Specify at least one parser to remove or '--all' flag.")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.RemoveMany(csConfig, cwhub.PARSERS, name, all, purge, forceAction)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-12-02 17:47:17 +00:00
|
|
|
cmdParsersRemove.PersistentFlags().BoolVar(&purge, "purge", false, "Delete source file too")
|
|
|
|
cmdParsersRemove.PersistentFlags().BoolVar(&forceAction, "force", false, "Force remove : Remove tainted and outdated files")
|
|
|
|
cmdParsersRemove.PersistentFlags().BoolVar(&all, "all", false, "Delete all the parsers")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdParsers.AddCommand(cmdParsersRemove)
|
|
|
|
|
|
|
|
var cmdParsersUpgrade = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "upgrade [config]",
|
|
|
|
Short: "Upgrade given parser(s)",
|
|
|
|
Long: `Fetch and upgrade given parser(s) from hub`,
|
|
|
|
Example: `cscli parsers upgrade crowdsec/xxx crowdsec/xyz`,
|
|
|
|
DisableAutoGenTag: true,
|
2022-04-20 13:44:48 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return compInstalledItems(cwhub.PARSERS, args, toComplete)
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-12-02 17:47:17 +00:00
|
|
|
if all {
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", forceAction)
|
2020-11-30 09:37:17 +00:00
|
|
|
} else {
|
2021-03-29 08:33:23 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
log.Fatalf("no target parser to upgrade")
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
for _, name := range args {
|
2022-04-19 10:07:35 +00:00
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, name, forceAction)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-12-02 17:47:17 +00:00
|
|
|
cmdParsersUpgrade.PersistentFlags().BoolVar(&all, "all", false, "Upgrade all the parsers")
|
|
|
|
cmdParsersUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdParsers.AddCommand(cmdParsersUpgrade)
|
|
|
|
|
|
|
|
var cmdParsersInspect = &cobra.Command{
|
2021-08-31 13:03:47 +00:00
|
|
|
Use: "inspect [name]",
|
|
|
|
Short: "Inspect given parser",
|
|
|
|
Long: `Inspect given parser`,
|
|
|
|
Example: `cscli parsers inspect crowdsec/xxx`,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
2022-04-20 13:44:48 +00:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return compInstalledItems(cwhub.PARSERS, args, toComplete)
|
|
|
|
},
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
InspectItem(args[0], cwhub.PARSERS)
|
|
|
|
},
|
|
|
|
}
|
2020-12-07 16:35:30 +00:00
|
|
|
cmdParsersInspect.PersistentFlags().StringVarP(&prometheusURL, "url", "u", "", "Prometheus url")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdParsers.AddCommand(cmdParsersInspect)
|
|
|
|
|
|
|
|
var cmdParsersList = &cobra.Command{
|
|
|
|
Use: "list [name]",
|
|
|
|
Short: "List all parsers or given one",
|
|
|
|
Long: `List all parsers or given one`,
|
|
|
|
Example: `cscli parsers list
|
|
|
|
cscli parser list crowdsecurity/xxx`,
|
2021-08-31 13:03:47 +00:00
|
|
|
DisableAutoGenTag: true,
|
2020-11-30 09:37:17 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-04-19 10:07:35 +00:00
|
|
|
ListItems([]string{cwhub.PARSERS}, args, false, true, all)
|
2020-11-30 09:37:17 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-03 16:09:07 +00:00
|
|
|
cmdParsersList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
2020-11-30 09:37:17 +00:00
|
|
|
cmdParsers.AddCommand(cmdParsersList)
|
|
|
|
|
|
|
|
return cmdParsers
|
|
|
|
}
|