From 5618ba9f46841a80cf92256d4a14bbf673173266 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:42:47 +0200 Subject: [PATCH] cscli: refactor hub commands (#2500) --- cmd/crowdsec-cli/collections.go | 15 ++------- cmd/crowdsec-cli/config_backup.go | 9 ++---- cmd/crowdsec-cli/config_restore.go | 9 ++---- cmd/crowdsec-cli/console.go | 7 +--- cmd/crowdsec-cli/hub.go | 50 ++++++++++++++--------------- cmd/crowdsec-cli/lapi.go | 8 ++--- cmd/crowdsec-cli/parsers.go | 15 ++------- cmd/crowdsec-cli/postoverflows.go | 15 ++------- cmd/crowdsec-cli/require/require.go | 20 ++++++++++++ cmd/crowdsec-cli/scenarios.go | 15 ++------- cmd/crowdsec-cli/simulation.go | 7 ++-- cmd/crowdsec-cli/support.go | 22 ++----------- 12 files changed, 64 insertions(+), 128 deletions(-) diff --git a/cmd/crowdsec-cli/collections.go b/cmd/crowdsec-cli/collections.go index 2affe1faa..6806d39a7 100644 --- a/cmd/crowdsec-cli/collections.go +++ b/cmd/crowdsec-cli/collections.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -20,21 +21,9 @@ func NewCollectionsCmd() *cobra.Command { Aliases: []string{"collection"}, DisableAutoGenTag: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if csConfig.Hub == nil { - return fmt.Errorf("you must configure cli before interacting with hub") - } - - if err := cwhub.SetHubBranch(); err != nil { - return fmt.Errorf("while setting hub branch: %w", err) - } - - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get hub index: %w", err) - } return nil }, diff --git a/cmd/crowdsec-cli/config_backup.go b/cmd/crowdsec-cli/config_backup.go index 74fcc8deb..a2ac41502 100644 --- a/cmd/crowdsec-cli/config_backup.go +++ b/cmd/crowdsec-cli/config_backup.go @@ -8,7 +8,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/crowdsecurity/crowdsec/pkg/cwhub" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" ) /* @@ -130,15 +130,10 @@ func backupConfigToDirectory(dirPath string) error { } func runConfigBackup(cmd *cobra.Command, args []string) error { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get Hub index: %w", err) - } - if err := backupConfigToDirectory(args[0]); err != nil { return fmt.Errorf("failed to backup config: %w", err) } diff --git a/cmd/crowdsec-cli/config_restore.go b/cmd/crowdsec-cli/config_restore.go index 90160db98..fc7206235 100644 --- a/cmd/crowdsec-cli/config_restore.go +++ b/cmd/crowdsec-cli/config_restore.go @@ -11,8 +11,8 @@ import ( "github.com/spf13/cobra" "gopkg.in/yaml.v2" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/csconfig" - "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) type OldAPICfg struct { @@ -183,15 +183,10 @@ func runConfigRestore(cmd *cobra.Command, args []string) error { return err } - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get Hub index: %w", err) - } - if err := restoreConfigFromDirectory(args[0], oldBackup); err != nil { return fmt.Errorf("failed to restore config from %s: %w", args[0], err) } diff --git a/cmd/crowdsec-cli/console.go b/cmd/crowdsec-cli/console.go index 356266495..5f22d8d4b 100644 --- a/cmd/crowdsec-cli/console.go +++ b/cmd/crowdsec-cli/console.go @@ -71,15 +71,10 @@ After running this command your will need to validate the enrollment in the weba return fmt.Errorf("could not parse CAPI URL: %s", err) } - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to load hub index: %s", err) - } - scenarios, err := cwhub.GetInstalledScenariosAsString() if err != nil { return fmt.Errorf("failed to get installed scenarios: %s", err) diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 4fec8fc8d..b85785633 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -8,6 +8,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -50,16 +51,12 @@ func NewHubListCmd() *cobra.Command { Short: "List installed configs", Args: cobra.ExactArgs(0), DisableAutoGenTag: true, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { + if err := require.Hub(csConfig); err != nil { + return err + } - if err := csConfig.LoadHub(); err != nil { - log.Fatal(err) - } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - log.Fatalf("Failed to get Hub index : %v", err) - } - //use LocalSync to get warnings about tainted / outdated items + // use LocalSync to get warnings about tainted / outdated items _, warn := cwhub.LocalSync(csConfig.Hub) for _, v := range warn { log.Info(v) @@ -68,6 +65,8 @@ func NewHubListCmd() *cobra.Command { ListItems(color.Output, []string{ cwhub.COLLECTIONS, cwhub.PARSERS, cwhub.SCENARIOS, cwhub.PARSERS_OVFLW, }, args, true, false, all) + + return nil }, } cmdHubList.PersistentFlags().BoolVarP(&all, "all", "a", false, "List disabled items as well") @@ -94,19 +93,18 @@ Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.inde } return nil }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if err := csConfig.LoadHub(); err != nil { - log.Fatal(err) + return err } if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil { - if errors.Is(err, cwhub.ErrIndexNotFound) { - log.Warnf("Could not find index file for branch '%s', using 'master'", cwhub.HubBranch) - cwhub.HubBranch = "master" - if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil { - log.Fatalf("Failed to get Hub index after retry : %v", err) - } - } else { - log.Fatalf("Failed to get Hub index : %v", err) + if !errors.Is(err, cwhub.ErrIndexNotFound) { + return fmt.Errorf("failed to get Hub index : %w", err) + } + log.Warnf("Could not find index file for branch '%s', using 'master'", cwhub.HubBranch) + cwhub.HubBranch = "master" + if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil { + return fmt.Errorf("failed to get Hub index after retry: %w", err) } } //use LocalSync to get warnings about tainted / outdated items @@ -114,6 +112,8 @@ Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.inde for _, v := range warn { log.Info(v) } + + return nil }, } @@ -139,13 +139,9 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if } return nil }, - Run: func(cmd *cobra.Command, args []string) { - if err := csConfig.LoadHub(); err != nil { - log.Fatal(err) - } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - log.Fatalf("Failed to get Hub index : %v", err) + RunE: func(cmd *cobra.Command, args []string) error { + if err := require.Hub(csConfig); err != nil { + return err } log.Infof("Upgrading collections") @@ -156,6 +152,8 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if cwhub.UpgradeConfig(csConfig, cwhub.SCENARIOS, "", forceAction) log.Infof("Upgrading postoverflows") cwhub.UpgradeConfig(csConfig, cwhub.PARSERS_OVFLW, "", forceAction) + + return nil }, } cmdHubUpgrade.PersistentFlags().BoolVar(&forceAction, "force", false, "Force upgrade : Overwrite tainted and outdated files") diff --git a/cmd/crowdsec-cli/lapi.go b/cmd/crowdsec-cli/lapi.go index fc2e1e97d..8ad67b70b 100644 --- a/cmd/crowdsec-cli/lapi.go +++ b/cmd/crowdsec-cli/lapi.go @@ -16,6 +16,7 @@ import ( "github.com/crowdsecurity/go-cs-lib/version" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/alertcontext" "github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/csconfig" @@ -36,14 +37,11 @@ func runLapiStatus(cmd *cobra.Command, args []string) error { if err != nil { log.Fatalf("parsing api url ('%s'): %s", apiurl, err) } - if err := csConfig.LoadHub(); err != nil { + + if err := require.Hub(csConfig); err != nil { log.Fatal(err) } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - log.Fatalf("Failed to load hub index : %s", err) - } scenarios, err := cwhub.GetInstalledScenariosAsString() if err != nil { log.Fatalf("failed to get scenarios : %s", err) diff --git a/cmd/crowdsec-cli/parsers.go b/cmd/crowdsec-cli/parsers.go index 552dbf33d..d97b070db 100644 --- a/cmd/crowdsec-cli/parsers.go +++ b/cmd/crowdsec-cli/parsers.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -24,21 +25,9 @@ cscli parsers remove crowdsecurity/sshd-logs Aliases: []string{"parser"}, DisableAutoGenTag: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if csConfig.Hub == nil { - return fmt.Errorf("you must configure cli before interacting with hub") - } - - if err := cwhub.SetHubBranch(); err != nil { - return fmt.Errorf("while setting hub branch: %w", err) - } - - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get hub index: %w", err) - } return nil }, diff --git a/cmd/crowdsec-cli/postoverflows.go b/cmd/crowdsec-cli/postoverflows.go index df950cddc..f4db0a79e 100644 --- a/cmd/crowdsec-cli/postoverflows.go +++ b/cmd/crowdsec-cli/postoverflows.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -23,21 +24,9 @@ func NewPostOverflowsCmd() *cobra.Command { Aliases: []string{"postoverflow"}, DisableAutoGenTag: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if csConfig.Hub == nil { - return fmt.Errorf("you must configure cli before interacting with hub") - } - - if err := cwhub.SetHubBranch(); err != nil { - return fmt.Errorf("while setting hub branch: %w", err) - } - - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get hub index: %w", err) - } return nil }, diff --git a/cmd/crowdsec-cli/require/require.go b/cmd/crowdsec-cli/require/require.go index 379dec396..039cc6862 100644 --- a/cmd/crowdsec-cli/require/require.go +++ b/cmd/crowdsec-cli/require/require.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/crowdsecurity/crowdsec/pkg/csconfig" + "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) func LAPI(c *csconfig.Config) error { @@ -63,3 +64,22 @@ func Notifications(c *csconfig.Config) error { return nil } +func Hub (c *csconfig.Config) error { + if err := c.LoadHub(); err != nil { + return err + } + + if c.Hub == nil { + return fmt.Errorf("you must configure cli before interacting with hub") + } + + if err := cwhub.SetHubBranch(); err != nil { + return fmt.Errorf("while setting hub branch: %w", err) + } + + if err := cwhub.GetHubIdx(c.Hub); err != nil { + return fmt.Errorf("failed to read Hub index: '%w'. Run 'sudo cscli hub update' to download the index again", err) + } + + return nil +} diff --git a/cmd/crowdsec-cli/scenarios.go b/cmd/crowdsec-cli/scenarios.go index 3f2ac945e..01e0b02dc 100644 --- a/cmd/crowdsec-cli/scenarios.go +++ b/cmd/crowdsec-cli/scenarios.go @@ -7,6 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -24,21 +25,9 @@ cscli scenarios remove crowdsecurity/ssh-bf Aliases: []string{"scenario"}, DisableAutoGenTag: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { return err } - if csConfig.Hub == nil { - return fmt.Errorf("you must configure cli before interacting with hub") - } - - if err := cwhub.SetHubBranch(); err != nil { - return fmt.Errorf("while setting hub branch: %w", err) - } - - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - return fmt.Errorf("failed to get hub index: %w", err) - } return nil }, diff --git a/cmd/crowdsec-cli/simulation.go b/cmd/crowdsec-cli/simulation.go index db499e380..b9ffb734e 100644 --- a/cmd/crowdsec-cli/simulation.go +++ b/cmd/crowdsec-cli/simulation.go @@ -9,6 +9,7 @@ import ( "golang.org/x/exp/slices" "gopkg.in/yaml.v2" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) @@ -144,13 +145,9 @@ func NewSimulationEnableCmd() *cobra.Command { Example: `cscli simulation enable`, DisableAutoGenTag: true, Run: func(cmd *cobra.Command, args []string) { - if err := csConfig.LoadHub(); err != nil { + if err := require.Hub(csConfig); err != nil { log.Fatal(err) } - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - log.Info("Run 'sudo cscli hub update' to get the hub index") - log.Fatalf("Failed to get Hub index : %v", err) - } if len(args) > 0 { for _, scenario := range args { diff --git a/cmd/crowdsec-cli/support.go b/cmd/crowdsec-cli/support.go index cc5775d9a..a68cb6191 100644 --- a/cmd/crowdsec-cli/support.go +++ b/cmd/crowdsec-cli/support.go @@ -20,6 +20,7 @@ import ( "github.com/crowdsecurity/go-cs-lib/version" + "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" "github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/cwhub" "github.com/crowdsecurity/crowdsec/pkg/cwversion" @@ -131,24 +132,6 @@ func collectOSInfo() ([]byte, error) { return w.Bytes(), nil } -func initHub() error { - if err := csConfig.LoadHub(); err != nil { - return fmt.Errorf("cannot load hub: %s", err) - } - if csConfig.Hub == nil { - return fmt.Errorf("hub not configured") - } - - if err := cwhub.SetHubBranch(); err != nil { - return fmt.Errorf("cannot set hub branch: %s", err) - } - - if err := cwhub.GetHubIdx(csConfig.Hub); err != nil { - return fmt.Errorf("no hub index found: %s", err) - } - return nil -} - func collectHubItems(itemType string) []byte { out := bytes.NewBuffer(nil) log.Infof("Collecting %s list", itemType) @@ -312,8 +295,7 @@ cscli support dump -f /tmp/crowdsec-support.zip skipAgent = true } - err = initHub() - if err != nil { + if err := require.Hub(csConfig); err != nil { log.Warn("Could not init hub, running on LAPI ? Hub related information will not be collected") skipHub = true infos[SUPPORT_PARSERS_PATH] = []byte(err.Error())