From 202dcb182bdc69250c3d8b2925f2b18301ce0011 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Feb 2026 16:01:18 +0000 Subject: [PATCH 1/2] Initial plan From b474a226c11d8b5c14d2c3ec2a2a2091d737677e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 5 Feb 2026 16:08:25 +0000 Subject: [PATCH 2/2] Remove CONSOLE_CONFIGS, CONSOLE_CONFIGS_HELP, and DefaultConsoleConfigFilePath variables Replaced global variables with inline definitions to avoid global state: - Replaced CONSOLE_CONFIGS array with inline slice definitions where needed - Replaced CONSOLE_CONFIGS_HELP map with inline map or direct string literals - Replaced DefaultConsoleConfigFilePath with inline DefaultConfigPath("console.yaml") calls - Updated console_table.go to use a structured approach with embedded descriptions - Fixed line length linting issues by extracting allOptions variable in enable/disable commands Co-authored-by: mmetc <92726601+mmetc@users.noreply.github.com> --- cmd/crowdsec-cli/cliconsole/console.go | 55 ++++++++++++++++---- cmd/crowdsec-cli/cliconsole/console_table.go | 41 ++++++--------- pkg/csconfig/api.go | 2 +- pkg/csconfig/console.go | 11 ---- 4 files changed, 62 insertions(+), 47 deletions(-) diff --git a/cmd/crowdsec-cli/cliconsole/console.go b/cmd/crowdsec-cli/cliconsole/console.go index fcee388eeca..b5161600a19 100644 --- a/cmd/crowdsec-cli/cliconsole/console.go +++ b/cmd/crowdsec-cli/cliconsole/console.go @@ -104,8 +104,16 @@ func (cli *cliConsole) enroll(ctx context.Context, key string, name string, over return err } + helpTexts := map[string]string{ + csconfig.SEND_CUSTOM_SCENARIOS: "Forward alerts from custom scenarios to the console", + csconfig.SEND_MANUAL_SCENARIOS: "Forward manual decisions to the console", + csconfig.SEND_TAINTED_SCENARIOS: "Forward alerts from tainted scenarios to the console", + csconfig.SEND_CONTEXT: "Forward context with alerts to the console", + csconfig.CONSOLE_MANAGEMENT: "Receive decisions from console", + } + for _, opt := range opts { - log.Infof("Enabled %s : %s", opt, csconfig.CONSOLE_CONFIGS_HELP[opt]) + log.Infof("Enabled %s : %s", opt, helpTexts[opt]) } log.Info("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.") @@ -115,18 +123,20 @@ func (cli *cliConsole) enroll(ctx context.Context, key string, name string, over } func optionFilterEnable(opts []string, enableOpts []string) ([]string, error) { + validOptions := []string{csconfig.SEND_CUSTOM_SCENARIOS, csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS, csconfig.SEND_CONTEXT, csconfig.CONSOLE_MANAGEMENT} + if len(enableOpts) == 0 { return opts, nil } for _, opt := range enableOpts { if opt == "all" { - opts = append(opts, csconfig.CONSOLE_CONFIGS...) + opts = append(opts, validOptions...) // keep validating the rest of the option names continue } - if !slices.Contains(csconfig.CONSOLE_CONFIGS, opt) { + if !slices.Contains(validOptions, opt) { return nil, fmt.Errorf("option %s doesn't exist", opt) } @@ -139,6 +149,8 @@ func optionFilterEnable(opts []string, enableOpts []string) ([]string, error) { } func optionFilterDisable(opts []string, disableOpts []string) ([]string, error) { + validOptions := []string{csconfig.SEND_CUSTOM_SCENARIOS, csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS, csconfig.SEND_CONTEXT, csconfig.CONSOLE_MANAGEMENT} + if len(disableOpts) == 0 { return opts, nil } @@ -150,7 +162,7 @@ func optionFilterDisable(opts []string, disableOpts []string) ([]string, error) continue } - if !slices.Contains(csconfig.CONSOLE_CONFIGS, opt) { + if !slices.Contains(validOptions, opt) { return nil, fmt.Errorf("option %s doesn't exist", opt) } @@ -192,7 +204,14 @@ cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-E cscli console enroll --enable console_management YOUR-ENROLL-KEY cscli console enroll --disable context YOUR-ENROLL-KEY -valid options are : %s,all (see 'cscli console status' for details)`, strings.Join(csconfig.CONSOLE_CONFIGS, ",")), +valid options are : %s,all (see 'cscli console status' for details)`, strings.Join( + []string{ + csconfig.SEND_CUSTOM_SCENARIOS, + csconfig.SEND_MANUAL_SCENARIOS, + csconfig.SEND_TAINTED_SCENARIOS, + csconfig.SEND_CONTEXT, + csconfig.CONSOLE_MANAGEMENT, + }, ",")), Args: args.ExactArgs(1), DisableAutoGenTag: true, RunE: func(cmd *cobra.Command, args []string) error { @@ -225,17 +244,25 @@ valid options are : %s,all (see 'cscli console status' for details)`, strings.Jo func (cli *cliConsole) newEnableCmd() *cobra.Command { var enableAll bool + allOptions := []string{ + csconfig.SEND_CUSTOM_SCENARIOS, + csconfig.SEND_MANUAL_SCENARIOS, + csconfig.SEND_TAINTED_SCENARIOS, + csconfig.SEND_CONTEXT, + csconfig.CONSOLE_MANAGEMENT, + } + cmd := &cobra.Command{ Use: "enable [option]...", Short: "Enable a console option", Example: "sudo cscli console enable tainted", Long: ` Enable given information push to the central API. Allows to empower the console`, - ValidArgs: csconfig.CONSOLE_CONFIGS, + ValidArgs: allOptions, DisableAutoGenTag: true, RunE: func(_ *cobra.Command, args []string) error { if enableAll { - if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, true); err != nil { + if err := cli.setConsoleOpts(allOptions, true); err != nil { return err } @@ -267,17 +294,25 @@ Enable given information push to the central API. Allows to empower the console` func (cli *cliConsole) newDisableCmd() *cobra.Command { var disableAll bool + allOptions := []string{ + csconfig.SEND_CUSTOM_SCENARIOS, + csconfig.SEND_MANUAL_SCENARIOS, + csconfig.SEND_TAINTED_SCENARIOS, + csconfig.SEND_CONTEXT, + csconfig.CONSOLE_MANAGEMENT, + } + cmd := &cobra.Command{ Use: "disable [option]", Short: "Disable a console option", Example: "sudo cscli console disable tainted", Long: ` Disable given information push to the central API.`, - ValidArgs: csconfig.CONSOLE_CONFIGS, + ValidArgs: allOptions, DisableAutoGenTag: true, RunE: func(_ *cobra.Command, args []string) error { if disableAll { - if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, false); err != nil { + if err := cli.setConsoleOpts(allOptions, false); err != nil { return err } @@ -376,7 +411,7 @@ func (cli *cliConsole) dumpConfig() error { } if serverCfg.ConsoleConfigPath == "" { - serverCfg.ConsoleConfigPath = csconfig.DefaultConsoleConfigFilePath + serverCfg.ConsoleConfigPath = csconfig.DefaultConfigPath("console.yaml") log.Debugf("Empty console_path, defaulting to %s", serverCfg.ConsoleConfigPath) } diff --git a/cmd/crowdsec-cli/cliconsole/console_table.go b/cmd/crowdsec-cli/cliconsole/console_table.go index f957e1ec81e..ddc6fb3c432 100644 --- a/cmd/crowdsec-cli/cliconsole/console_table.go +++ b/cmd/crowdsec-cli/cliconsole/console_table.go @@ -17,33 +17,24 @@ func cmdConsoleStatusTable(out io.Writer, wantColor string, consoleCfg csconfig. t.SetHeaders("Option Name", "Activated", "Description") t.SetHeaderAlignment(text.AlignLeft, text.AlignLeft, text.AlignLeft) - for _, option := range csconfig.CONSOLE_CONFIGS { - activated := emoji.CrossMark + consoleOptions := []struct { + name string + enabled bool + description string + }{ + {csconfig.SEND_CUSTOM_SCENARIOS, *consoleCfg.ShareCustomScenarios, "Forward alerts from custom scenarios to the console"}, + {csconfig.SEND_MANUAL_SCENARIOS, *consoleCfg.ShareManualDecisions, "Forward manual decisions to the console"}, + {csconfig.SEND_TAINTED_SCENARIOS, *consoleCfg.ShareTaintedScenarios, "Forward alerts from tainted scenarios to the console"}, + {csconfig.SEND_CONTEXT, *consoleCfg.ShareContext, "Forward context with alerts to the console"}, + {csconfig.CONSOLE_MANAGEMENT, *consoleCfg.ConsoleManagement, "Receive decisions from console"}, + } - switch option { - case csconfig.SEND_CUSTOM_SCENARIOS: - if *consoleCfg.ShareCustomScenarios { - activated = emoji.CheckMarkButton - } - case csconfig.SEND_MANUAL_SCENARIOS: - if *consoleCfg.ShareManualDecisions { - activated = emoji.CheckMarkButton - } - case csconfig.SEND_TAINTED_SCENARIOS: - if *consoleCfg.ShareTaintedScenarios { - activated = emoji.CheckMarkButton - } - case csconfig.SEND_CONTEXT: - if *consoleCfg.ShareContext { - activated = emoji.CheckMarkButton - } - case csconfig.CONSOLE_MANAGEMENT: - if *consoleCfg.ConsoleManagement { - activated = emoji.CheckMarkButton - } + for _, option := range consoleOptions { + activated := emoji.CrossMark + if option.enabled { + activated = emoji.CheckMarkButton } - - t.AddRow(option, activated, csconfig.CONSOLE_CONFIGS_HELP[option]) + t.AddRow(option.name, activated, option.description) } t.Render() diff --git a/pkg/csconfig/api.go b/pkg/csconfig/api.go index 3c5f2365d0a..7e3cdcd2da3 100644 --- a/pkg/csconfig/api.go +++ b/pkg/csconfig/api.go @@ -424,7 +424,7 @@ func (c *Config) LoadAPIServer(inCli bool, skipOnlineCreds bool) error { } if c.API.Server.ConsoleConfigPath == "" { - c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath + c.API.Server.ConsoleConfigPath = DefaultConfigPath("console.yaml") } if err := c.API.Server.LoadConsoleConfig(); err != nil { diff --git a/pkg/csconfig/console.go b/pkg/csconfig/console.go index 21ecbf3d736..248e461cc69 100644 --- a/pkg/csconfig/console.go +++ b/pkg/csconfig/console.go @@ -18,17 +18,6 @@ const ( SEND_CONTEXT = "context" ) -var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_CONTEXT, CONSOLE_MANAGEMENT} -var CONSOLE_CONFIGS_HELP = map[string]string{ - SEND_CUSTOM_SCENARIOS: "Forward alerts from custom scenarios to the console", - SEND_MANUAL_SCENARIOS: "Forward manual decisions to the console", - SEND_TAINTED_SCENARIOS: "Forward alerts from tainted scenarios to the console", - SEND_CONTEXT: "Forward context with alerts to the console", - CONSOLE_MANAGEMENT: "Receive decisions from console", -} - -var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml") - type ConsoleConfig struct { ShareManualDecisions *bool `yaml:"share_manual_decisions"` ShareTaintedScenarios *bool `yaml:"share_tainted"`