From 70627fda1b26341b3ffa97f6fec31487e3e1deee Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 31 Jan 2024 16:39:33 +0100 Subject: [PATCH 1/4] cscli hub: avoid global csConfig --- cmd/crowdsec-cli/hub.go | 21 +++++++++++++-------- cmd/crowdsec-cli/main.go | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 3a2913f0513..cff1243a973 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -10,13 +10,18 @@ import ( "gopkg.in/yaml.v3" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" + "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) -type cliHub struct{} +type cliHub struct{ + cfg func() *csconfig.Config +} -func NewCLIHub() *cliHub { - return &cliHub{} +func NewCLIHub(getconfig func() *csconfig.Config) *cliHub { + return &cliHub{ + cfg: getconfig, + } } func (cli cliHub) NewCommand() *cobra.Command { @@ -50,7 +55,7 @@ func (cli cliHub) list(cmd *cobra.Command, args []string) error { return err } - hub, err := require.Hub(csConfig, nil, log.StandardLogger()) + hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger()) if err != nil { return err } @@ -96,8 +101,8 @@ func (cli cliHub) NewListCmd() *cobra.Command { } func (cli cliHub) update(cmd *cobra.Command, args []string) error { - local := csConfig.Hub - remote := require.RemoteHub(csConfig) + local := cli.cfg().Hub + remote := require.RemoteHub(cli.cfg()) // don't use require.Hub because if there is no index file, it would fail hub, err := cwhub.NewHub(local, remote, true, log.StandardLogger()) @@ -135,7 +140,7 @@ func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error { return err } - hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), log.StandardLogger()) + hub, err := require.Hub(cli.cfg(), require.RemoteHub(cli.cfg()), log.StandardLogger()) if err != nil { return err } @@ -186,7 +191,7 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if } func (cli cliHub) types(cmd *cobra.Command, args []string) error { - switch csConfig.Cscli.Output { + switch cli.cfg().Cscli.Output { case "human": s, err := yaml.Marshal(cwhub.ItemTypes) if err != nil { diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index fda4cddc2bc..b65cfbfebc6 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -190,7 +190,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall cmd.AddCommand(NewCLIDoc().NewCommand(cmd)) cmd.AddCommand(NewCLIVersion().NewCommand()) cmd.AddCommand(NewConfigCmd()) - cmd.AddCommand(NewCLIHub().NewCommand()) + cmd.AddCommand(NewCLIHub(getconfig).NewCommand()) cmd.AddCommand(NewMetricsCmd()) cmd.AddCommand(NewCLIDashboard().NewCommand()) cmd.AddCommand(NewCLIDecisions().NewCommand()) From 8a9365cc96bdda6902906afc92f42cd7d0ed9aaa Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 31 Jan 2024 16:41:45 +0100 Subject: [PATCH 2/4] cscli hub: don't export constructors --- cmd/crowdsec-cli/hub.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index cff1243a973..69b14c83bb6 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -39,10 +39,10 @@ cscli hub upgrade`, DisableAutoGenTag: true, } - cmd.AddCommand(cli.NewListCmd()) - cmd.AddCommand(cli.NewUpdateCmd()) - cmd.AddCommand(cli.NewUpgradeCmd()) - cmd.AddCommand(cli.NewTypesCmd()) + cmd.AddCommand(cli.newListCmd()) + cmd.AddCommand(cli.newUpdateCmd()) + cmd.AddCommand(cli.newUpgradeCmd()) + cmd.AddCommand(cli.newTypesCmd()) return cmd } @@ -85,7 +85,7 @@ func (cli cliHub) list(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) NewListCmd() *cobra.Command { +func (cli cliHub) newListCmd() *cobra.Command { cmd := &cobra.Command{ Use: "list [-a]", Short: "List all installed configurations", @@ -117,7 +117,7 @@ func (cli cliHub) update(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) NewUpdateCmd() *cobra.Command { +func (cli cliHub) newUpdateCmd() *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Download the latest index (catalog of available configurations)", @@ -172,7 +172,7 @@ func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) NewUpgradeCmd() *cobra.Command { +func (cli cliHub) newUpgradeCmd() *cobra.Command { cmd := &cobra.Command{ Use: "upgrade", Short: "Upgrade all configurations to their latest version", @@ -215,7 +215,7 @@ func (cli cliHub) types(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) NewTypesCmd() *cobra.Command { +func (cli cliHub) newTypesCmd() *cobra.Command { cmd := &cobra.Command{ Use: "types", Short: "List supported item types", From a9f433bd07a2051a3bf29b58ab984d47ac02071c Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 31 Jan 2024 16:53:14 +0100 Subject: [PATCH 3/4] cscli hub: flags as closure variables --- cmd/crowdsec-cli/hub.go | 56 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 69b14c83bb6..929bff501ca 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -24,7 +24,7 @@ func NewCLIHub(getconfig func() *csconfig.Config) *cliHub { } } -func (cli cliHub) NewCommand() *cobra.Command { +func (cli *cliHub) NewCommand() *cobra.Command { cmd := &cobra.Command{ Use: "hub [action]", Short: "Manage hub index", @@ -47,14 +47,7 @@ cscli hub upgrade`, return cmd } -func (cli cliHub) list(cmd *cobra.Command, args []string) error { - flags := cmd.Flags() - - all, err := flags.GetBool("all") - if err != nil { - return err - } - +func (cli *cliHub) list(all bool) error { hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger()) if err != nil { return err @@ -85,22 +78,26 @@ func (cli cliHub) list(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) newListCmd() *cobra.Command { +func (cli *cliHub) newListCmd() *cobra.Command { + var all bool + cmd := &cobra.Command{ Use: "list [-a]", Short: "List all installed configurations", Args: cobra.ExactArgs(0), DisableAutoGenTag: true, - RunE: cli.list, + RunE: func(_ *cobra.Command, _ []string) error { + return cli.list(all) + }, } flags := cmd.Flags() - flags.BoolP("all", "a", false, "List disabled items as well") + flags.BoolVarP(&all, "all", "a", false, "List disabled items as well") return cmd } -func (cli cliHub) update(cmd *cobra.Command, args []string) error { +func (cli *cliHub) update() error { local := cli.cfg().Hub remote := require.RemoteHub(cli.cfg()) @@ -117,7 +114,7 @@ func (cli cliHub) update(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) newUpdateCmd() *cobra.Command { +func (cli *cliHub) newUpdateCmd() *cobra.Command { cmd := &cobra.Command{ Use: "update", Short: "Download the latest index (catalog of available configurations)", @@ -126,20 +123,15 @@ Fetches the .index.json file from the hub, containing the list of available conf `, Args: cobra.ExactArgs(0), DisableAutoGenTag: true, - RunE: cli.update, + RunE: func(_ *cobra.Command, _ []string) error { + return cli.update() + }, } return cmd } -func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error { - flags := cmd.Flags() - - force, err := flags.GetBool("force") - if err != nil { - return err - } - +func (cli *cliHub) upgrade(force bool) error { hub, err := require.Hub(cli.cfg(), require.RemoteHub(cli.cfg()), log.StandardLogger()) if err != nil { return err @@ -172,7 +164,9 @@ func (cli cliHub) upgrade(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) newUpgradeCmd() *cobra.Command { +func (cli *cliHub) newUpgradeCmd() *cobra.Command { + var force bool + cmd := &cobra.Command{ Use: "upgrade", Short: "Upgrade all configurations to their latest version", @@ -181,16 +175,18 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if `, Args: cobra.ExactArgs(0), DisableAutoGenTag: true, - RunE: cli.upgrade, + RunE: func(_ *cobra.Command, _ []string) error { + return cli.upgrade(force) + }, } flags := cmd.Flags() - flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files") + flags.BoolVar(&force, "force", false, "Force upgrade: overwrite tainted and outdated files") return cmd } -func (cli cliHub) types(cmd *cobra.Command, args []string) error { +func (cli *cliHub) types() error { switch cli.cfg().Cscli.Output { case "human": s, err := yaml.Marshal(cwhub.ItemTypes) @@ -215,7 +211,7 @@ func (cli cliHub) types(cmd *cobra.Command, args []string) error { return nil } -func (cli cliHub) newTypesCmd() *cobra.Command { +func (cli *cliHub) newTypesCmd() *cobra.Command { cmd := &cobra.Command{ Use: "types", Short: "List supported item types", @@ -224,7 +220,9 @@ List the types of supported hub items. `, Args: cobra.ExactArgs(0), DisableAutoGenTag: true, - RunE: cli.types, + RunE: func(_ *cobra.Command, _ []string) error { + return cli.types() + }, } return cmd From 42bcfc6ccf2633370ace769da6b47083d4508219 Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 31 Jan 2024 17:00:14 +0100 Subject: [PATCH 4/4] cscli: type configGetter --- cmd/crowdsec-cli/bouncers.go | 5 ++--- cmd/crowdsec-cli/hub.go | 5 ++--- cmd/crowdsec-cli/main.go | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/crowdsec-cli/bouncers.go b/cmd/crowdsec-cli/bouncers.go index 410827b3159..d2685901ebb 100644 --- a/cmd/crowdsec-cli/bouncers.go +++ b/cmd/crowdsec-cli/bouncers.go @@ -16,7 +16,6 @@ import ( "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1" - "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/types" ) @@ -38,10 +37,10 @@ func askYesNo(message string, defaultAnswer bool) (bool, error) { type cliBouncers struct { db *database.Client - cfg func() *csconfig.Config + cfg configGetter } -func NewCLIBouncers(getconfig func() *csconfig.Config) *cliBouncers { +func NewCLIBouncers(getconfig configGetter) *cliBouncers { return &cliBouncers{ cfg: getconfig, } diff --git a/cmd/crowdsec-cli/hub.go b/cmd/crowdsec-cli/hub.go index 929bff501ca..d3ce380bb6f 100644 --- a/cmd/crowdsec-cli/hub.go +++ b/cmd/crowdsec-cli/hub.go @@ -10,15 +10,14 @@ import ( "gopkg.in/yaml.v3" "github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require" - "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/cwhub" ) type cliHub struct{ - cfg func() *csconfig.Config + cfg configGetter } -func NewCLIHub(getconfig func() *csconfig.Config) *cliHub { +func NewCLIHub(getconfig configGetter) *cliHub { return &cliHub{ cfg: getconfig, } diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index b65cfbfebc6..654da1e8ecc 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -29,6 +29,8 @@ var mergedConfig string // flagBranch overrides the value in csConfig.Cscli.HubBranch var flagBranch = "" +type configGetter func() *csconfig.Config + func initConfig() { var err error