2020-11-30 09:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-10-13 10:28:24 +00:00
|
|
|
"github.com/fatih/color"
|
2020-11-30 09:37:17 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2022-10-07 09:05:35 +00:00
|
|
|
|
2023-10-04 08:42:47 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
2022-10-07 09:05:35 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewParsersCmd() *cobra.Command {
|
2023-10-12 12:53:17 +00:00
|
|
|
cmdParsers := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
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 {
|
2023-10-04 08:42:47 +00:00
|
|
|
if err := require.Hub(csConfig); err != nil {
|
2023-08-16 19:04:46 +00:00
|
|
|
return err
|
2021-03-24 17:16:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
cmdParsers.AddCommand(NewParsersInstallCmd())
|
|
|
|
cmdParsers.AddCommand(NewParsersRemoveCmd())
|
|
|
|
cmdParsers.AddCommand(NewParsersUpgradeCmd())
|
|
|
|
cmdParsers.AddCommand(NewParsersInspectCmd())
|
|
|
|
cmdParsers.AddCommand(NewParsersListCmd())
|
|
|
|
|
|
|
|
return cmdParsers
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runParsersInstall(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
downloadOnly, err := flags.GetBool("download-only")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
force, err := flags.GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreError, err := flags.GetBool("ignore")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
|
|
|
t := cwhub.GetItem(cwhub.PARSERS, name)
|
|
|
|
if t == nil {
|
|
|
|
nearestItem, score := GetDistance(cwhub.PARSERS, name)
|
|
|
|
Suggest(cwhub.PARSERS, name, nearestItem.Name, score, ignoreError)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2023-01-19 12:29:36 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
if err := cwhub.InstallItem(csConfig, name, cwhub.PARSERS, force, downloadOnly); err != nil {
|
|
|
|
if !ignoreError {
|
|
|
|
return fmt.Errorf("error while installing '%s': %w", name, err)
|
|
|
|
}
|
|
|
|
log.Errorf("Error while installing '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParsersInstallCmd() *cobra.Command {
|
|
|
|
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)
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runParsersInstall,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdParsersInstall.Flags()
|
|
|
|
flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
|
|
|
|
flags.Bool("force", false, "Force install: Overwrite tainted and outdated files")
|
|
|
|
flags.Bool("ignore", false, "Ignore errors when installing multiple parsers")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdParsersInstall
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runParsersRemove(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
purge, err := flags.GetBool("purge")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
force, err := flags.GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
all, err := flags.GetBool("all")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
|
|
|
err := cwhub.RemoveMany(csConfig, cwhub.PARSERS, "", all, purge, force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("specify at least one parser to remove or '--all'")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
|
|
|
err := cwhub.RemoveMany(csConfig, cwhub.PARSERS, name, all, purge, force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
func NewParsersRemoveCmd() *cobra.Command {
|
2023-08-16 19:04:46 +00:00
|
|
|
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`,
|
|
|
|
Example: `cscli parsers remove crowdsec/xxx crowdsec/xyz`,
|
2023-08-16 19:04:46 +00:00
|
|
|
Aliases: []string{"delete"},
|
2021-08-31 13:03:47 +00:00
|
|
|
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)
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runParsersRemove,
|
|
|
|
}
|
2022-03-16 16:26:31 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdParsersRemove.Flags()
|
|
|
|
flags.Bool("purge", false, "Delete source file too")
|
|
|
|
flags.Bool("force", false, "Force remove: Remove tainted and outdated files")
|
|
|
|
flags.Bool("all", false, "Delete all the parsers")
|
2022-03-16 16:26:31 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
return cmdParsersRemove
|
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runParsersUpgrade(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
force, err := flags.GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
all, err := flags.GetBool("all")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
if all {
|
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, "", force)
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("specify at least one parser to upgrade or '--all'")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
|
|
|
cwhub.UpgradeConfig(csConfig, cwhub.PARSERS, name, force)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-01-19 12:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewParsersUpgradeCmd() *cobra.Command {
|
2023-08-16 19:04:46 +00:00
|
|
|
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)
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runParsersUpgrade,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdParsersUpgrade.Flags()
|
|
|
|
flags.Bool("all", false, "Upgrade all the parsers")
|
|
|
|
flags.Bool("force", false, "Force upgrade : Overwrite tainted and outdated files")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdParsersUpgrade
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runParsersInspect(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2023-10-16 10:10:11 +00:00
|
|
|
url, err := flags.GetString("url")
|
2023-10-12 12:53:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-16 10:10:11 +00:00
|
|
|
if url != "" {
|
|
|
|
csConfig.Cscli.PrometheusUrl = url
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
InspectItem(args[0], cwhub.PARSERS)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
func NewParsersInspectCmd() *cobra.Command {
|
2023-10-12 12:53:17 +00:00
|
|
|
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`,
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
2023-10-12 12:53:17 +00:00
|
|
|
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)
|
|
|
|
},
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runParsersInspect,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdParsersInspect.Flags()
|
|
|
|
flags.StringP("url", "u", "", "Prometheus url")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdParsersInspect
|
|
|
|
}
|
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
func runParsersList(cmd *cobra.Command, args []string) error {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
all, err := flags.GetBool("all")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: will happily ignore missing parsers
|
|
|
|
ListItems(color.Output, []string{cwhub.PARSERS}, args, false, true, all)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
func NewParsersListCmd() *cobra.Command {
|
2023-10-12 12:53:17 +00:00
|
|
|
cmdParsersList := &cobra.Command{
|
2020-11-30 09:37:17 +00:00
|
|
|
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,
|
2023-10-12 12:53:17 +00:00
|
|
|
RunE: runParsersList,
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|
2023-08-16 19:04:46 +00:00
|
|
|
|
2023-10-12 12:53:17 +00:00
|
|
|
flags := cmdParsersList.Flags()
|
|
|
|
flags.BoolP("all", "a", false, "List disabled items as well")
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2023-01-19 12:29:36 +00:00
|
|
|
return cmdParsersList
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|