crowdsec/cmd/crowdsec-cli/waap_rules.go

321 lines
8 KiB
Go
Raw Normal View History

2023-04-12 11:32:14 +00:00
package main
import (
"fmt"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2023-10-19 10:18:16 +00:00
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
2023-04-12 11:32:14 +00:00
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
)
2023-10-19 10:18:16 +00:00
func NewWaapRulesCmd() *cobra.Command {
cmdWaapRules := &cobra.Command{
Use: "waap-rules <action> [waap-rule]...",
Short: "Manage hub waap rules",
Example: `cscli waap-rules list -a
cscli waap-rules install crowdsecurity/crs
cscli waap-rules inspect crowdsecurity/crs
cscli waap-rules upgrade crowdsecurity/crs
cscli waap-rules remove crowdsecurity/crs
2023-04-12 11:32:14 +00:00
`,
Args: cobra.MinimumNArgs(1),
2023-10-18 15:17:57 +00:00
Aliases: []string{"waap-rule"},
2023-04-12 11:32:14 +00:00
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2023-10-19 12:19:37 +00:00
if _, err := require.Hub(csConfig); err != nil {
2023-10-19 10:18:16 +00:00
return err
2023-04-12 11:32:14 +00:00
}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if cmd.Name() == "inspect" || cmd.Name() == "list" {
return
}
log.Infof(ReloadMessage())
},
}
2023-10-19 10:18:16 +00:00
cmdWaapRules.AddCommand(NewCmdWaapRulesInstall())
cmdWaapRules.AddCommand(NewCmdWaapRulesRemove())
cmdWaapRules.AddCommand(NewCmdWaapRulesUpgrade())
cmdWaapRules.AddCommand(NewCmdWaapRulesInspect())
cmdWaapRules.AddCommand(NewCmdWaapRulesList())
2023-04-12 11:32:14 +00:00
2023-10-19 10:18:16 +00:00
return cmdWaapRules
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
func runWaapRulesInstall(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
}
2023-10-19 12:19:37 +00:00
hub, err := cwhub.GetHub()
if err != nil {
return err
}
2023-10-19 10:18:16 +00:00
for _, name := range args {
2023-10-19 12:19:37 +00:00
t := hub.GetItem(cwhub.WAAP_RULES, name)
2023-10-19 10:18:16 +00:00
if t == nil {
nearestItem, score := GetDistance(cwhub.WAAP_RULES, name)
Suggest(cwhub.WAAP_RULES, name, nearestItem.Name, score, ignoreError)
2023-04-12 11:32:14 +00:00
2023-10-19 10:18:16 +00:00
continue
}
2023-10-19 12:19:37 +00:00
if err := hub.InstallItem(name, cwhub.WAAP_RULES, force, downloadOnly); err != nil {
2023-10-19 10:18:16 +00:00
if !ignoreError {
return fmt.Errorf("error while installing '%s': %w", name, err)
}
log.Errorf("Error while installing '%s': %s", name, err)
}
}
return nil
}
func NewCmdWaapRulesInstall() *cobra.Command {
cmdWaapRulesInstall := &cobra.Command{
Use: "install <waap-rule>...",
Short: "Install given waap rule(s)",
Long: `Fetch and install one or more waap rules from the hub`,
Example: `cscli waap-rules install crowdsecurity/crs`,
2023-04-12 11:32:14 +00:00
Args: cobra.MinimumNArgs(1),
DisableAutoGenTag: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2023-10-18 15:11:43 +00:00
return compAllItems(cwhub.WAAP_RULES, args, toComplete)
2023-04-12 11:32:14 +00:00
},
2023-10-19 10:18:16 +00:00
RunE: runWaapRulesInstall,
}
flags := cmdWaapRulesInstall.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 waap rules")
return cmdWaapRulesInstall
}
func runWaapRulesRemove(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
}
2023-10-19 12:19:37 +00:00
hub, err := cwhub.GetHub()
if err != nil {
return err
}
2023-10-19 10:18:16 +00:00
if all {
2023-10-19 12:19:37 +00:00
err := hub.RemoveMany(cwhub.WAAP_RULES, "", all, purge, force)
2023-10-19 10:18:16 +00:00
if err != nil {
return err
}
return nil
}
if len(args) == 0 {
return fmt.Errorf("specify at least one waap rule to remove or '--all'")
}
for _, name := range args {
2023-10-19 12:19:37 +00:00
err := hub.RemoveMany(cwhub.WAAP_RULES, name, all, purge, force)
2023-10-19 10:18:16 +00:00
if err != nil {
return err
}
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
return nil
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
func NewCmdWaapRulesRemove() *cobra.Command {
cmdWaapRulesRemove := &cobra.Command{
Use: "remove <waap-rule>...",
Short: "Remove given waap rule(s)",
Long: `remove one or more waap rules`,
Example: `cscli waap-rules remove crowdsecurity/crs`,
2023-04-12 11:32:14 +00:00
Aliases: []string{"delete"},
DisableAutoGenTag: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2023-10-18 15:11:43 +00:00
return compInstalledItems(cwhub.WAAP_RULES, args, toComplete)
2023-04-12 11:32:14 +00:00
},
2023-10-19 10:18:16 +00:00
RunE: runWaapRulesRemove,
}
2023-04-12 11:32:14 +00:00
2023-10-19 10:18:16 +00:00
flags := cmdWaapRulesRemove.Flags()
flags.Bool("purge", false, "Delete source file too")
flags.Bool("force", false, "Force remove: remove tainted and outdated files")
flags.Bool("all", false, "Remove all the waap rules")
2023-04-12 11:32:14 +00:00
2023-10-19 10:18:16 +00:00
return cmdWaapRulesRemove
}
func runWaapRulesUpgrade(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
}
2023-10-19 12:19:37 +00:00
hub, err := cwhub.GetHub()
if err != nil {
return err
}
2023-10-19 10:18:16 +00:00
if all {
2023-10-19 12:19:37 +00:00
if err := hub.UpgradeConfig(cwhub.WAAP_RULES, "", force); err != nil {
2023-10-19 10:18:16 +00:00
return err
}
return nil
}
if len(args) == 0 {
return fmt.Errorf("specify at least one waap rule to upgrade or '--all'")
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
for _, name := range args {
2023-10-19 12:19:37 +00:00
if err := hub.UpgradeConfig(cwhub.WAAP_RULES, name, force); err != nil {
2023-10-19 10:18:16 +00:00
return err
}
}
return nil
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
func NewCmdWaapRulesUpgrade() *cobra.Command {
cmdWaapRulesUpgrade := &cobra.Command{
Use: "upgrade <waap-rule>...",
Short: "Upgrade given waap rule(s)",
Long: `Fetch and upgrade one or more waap rules from the hub`,
Example: `cscli waap-rules upgrade crowdsecurity/crs`,
2023-04-12 11:32:14 +00:00
DisableAutoGenTag: true,
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2023-10-18 15:11:43 +00:00
return compInstalledItems(cwhub.WAAP_RULES, args, toComplete)
2023-04-12 11:32:14 +00:00
},
2023-10-19 10:18:16 +00:00
RunE: runWaapRulesUpgrade,
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
flags := cmdWaapRulesUpgrade.Flags()
flags.BoolP("all", "a", false, "Upgrade all the waap rules")
flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
return cmdWaapRulesUpgrade
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
func runWaapRulesInspect(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
url, err := flags.GetString("url")
if err != nil {
return err
}
if url != "" {
csConfig.Cscli.PrometheusUrl = url
}
noMetrics, err := flags.GetBool("no-metrics")
if err != nil {
return err
}
for _, name := range args {
if err = InspectItem(name, cwhub.WAAP_RULES, noMetrics); err != nil {
return err
}
}
return nil
}
func NewCmdWaapRulesInspect() *cobra.Command {
cmdWaapRulesInspect := &cobra.Command{
Use: "inspect <waap-rule>",
Short: "Inspect a waap rule",
Long: `Inspect a waap rule`,
Example: `cscli waap-rules inspect crowdsecurity/crs`,
2023-04-12 11:32:14 +00:00
Args: cobra.MinimumNArgs(1),
2023-10-19 10:18:16 +00:00
DisableAutoGenTag: true,
2023-04-12 11:32:14 +00:00
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2023-10-18 15:11:43 +00:00
return compInstalledItems(cwhub.WAAP_RULES, args, toComplete)
2023-04-12 11:32:14 +00:00
},
2023-10-19 10:18:16 +00:00
RunE: runWaapRulesInspect,
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
flags := cmdWaapRulesInspect.Flags()
flags.StringP("url", "u", "", "Prometheus url")
flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
return cmdWaapRulesInspect
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
func runWaapRulesList(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
all, err := flags.GetBool("all")
if err != nil {
return err
}
if err = ListItems(color.Output, []string{cwhub.WAAP_RULES}, args, false, true, all); err != nil {
return err
}
return nil
}
func NewCmdWaapRulesList() *cobra.Command {
cmdWaapRulesList := &cobra.Command{
Use: "list [waap-rule]...",
Short: "List waap rules",
Long: `List of installed/available/specified waap rules`,
2023-10-18 15:17:57 +00:00
Example: `cscli waap-rules list
2023-10-19 10:18:16 +00:00
cscli waap-rules list -a
cscli waap-rules list crowdsecurity/crs`,
2023-04-12 11:32:14 +00:00
DisableAutoGenTag: true,
2023-10-19 10:18:16 +00:00
RunE: runWaapRulesList,
2023-04-12 11:32:14 +00:00
}
2023-10-19 10:18:16 +00:00
flags := cmdWaapRulesList.Flags()
flags.BoolP("all", "a", false, "List disabled items as well")
return cmdWaapRulesList
2023-04-12 11:32:14 +00:00
}