diff --git a/cmd/crowdsec-cli/console.go b/cmd/crowdsec-cli/console.go index 843bba690..c2ee8b383 100644 --- a/cmd/crowdsec-cli/console.go +++ b/cmd/crowdsec-cli/console.go @@ -39,6 +39,10 @@ func NewConsoleCmd() *cobra.Command { } log.Fatal("Local API is disabled, please run this command on the local API machine") } + if err := csConfig.LoadCrowdsec(); err != nil { + log.Fatalf("Unable to load CrowdSec Agent: %s", err) + } + if csConfig.DisableAPI { log.Fatal("Local API is disabled, please run this command on the local API machine") } @@ -280,7 +284,7 @@ Disable given information push to the central API.`, } csConfig.API.Server.ConsoleConfig.LabelsToSend[key] = data } - if err := csConfig.API.Server.DumpLabelConfigFile(); err != nil { + if err := csConfig.Crowdsec.DumpLabelConfigFile(); err != nil { log.Fatalf(err.Error()) } }, diff --git a/pkg/csconfig/console.go b/pkg/csconfig/console.go index d0306a82f..a68796847 100644 --- a/pkg/csconfig/console.go +++ b/pkg/csconfig/console.go @@ -21,7 +21,6 @@ const ( var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_LABEL} var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml") -var DefaultLabelsConfigFilePath = DefaultConfigPath("console", "labels.yaml") type ConsoleConfig struct { ShareManualDecisions *bool `yaml:"share_manual_decisions"` @@ -52,15 +51,6 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error { return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err) } - yamlFile, err = ioutil.ReadFile(DefaultLabelsConfigFilePath) - if err != nil { - return fmt.Errorf("reading console config file '%s': %s", DefaultLabelsConfigFilePath, err) - } - err = yaml.Unmarshal(yamlFile, c.ConsoleConfig.LabelsToSend) - if err != nil { - return fmt.Errorf("unmarshaling labels console config file '%s': %s", DefaultLabelsConfigFilePath, err) - } - if c.ConsoleConfig.ShareCustomScenarios == nil { log.Debugf("no share_custom scenarios found, setting to true") c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true) @@ -103,18 +93,3 @@ func (c *LocalApiServerCfg) DumpConsoleConfig() error { return nil } - -func (c *LocalApiServerCfg) DumpLabelConfigFile() error { - var out []byte - var err error - - if out, err = yaml.Marshal(c.ConsoleConfig.LabelsToSend); err != nil { - return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", DefaultLabelsConfigFilePath) - } - - if err := os.WriteFile(DefaultLabelsConfigFilePath, out, 0600); err != nil { - return errors.Wrapf(err, "while dumping console config to %s", DefaultLabelsConfigFilePath) - } - - return nil -} diff --git a/pkg/csconfig/crowdsec_service.go b/pkg/csconfig/crowdsec_service.go index b7a7c7b8e..f93e06d84 100644 --- a/pkg/csconfig/crowdsec_service.go +++ b/pkg/csconfig/crowdsec_service.go @@ -2,18 +2,20 @@ package csconfig import ( "fmt" + "io/ioutil" "os" "path/filepath" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v2" ) /*Configurations needed for crowdsec to load parser/scenarios/... + acquisition*/ type CrowdsecServiceCfg struct { - AcquisitionFilePath string `yaml:"acquisition_path,omitempty"` - AcquisitionDirPath string `yaml:"acquisition_dir,omitempty"` - + AcquisitionFilePath string `yaml:"acquisition_path,omitempty"` + AcquisitionDirPath string `yaml:"acquisition_dir,omitempty"` + ConsoleLabelsPath string `yaml:"console_labels_path"` AcquisitionFiles []string `yaml:"-"` ParserRoutinesCount int `yaml:"parser_routines"` BucketsRoutinesCount int `yaml:"buckets_routines"` @@ -24,13 +26,16 @@ type CrowdsecServiceCfg struct { BucketStateDumpDir string `yaml:"state_output_dir,omitempty"` //if we need to unserialize buckets on shutdown BucketsGCEnabled bool `yaml:"-"` //we need to garbage collect buckets when in forensic mode - HubDir string `yaml:"-"` - DataDir string `yaml:"-"` - ConfigDir string `yaml:"-"` - HubIndexFile string `yaml:"-"` - SimulationFilePath string `yaml:"-"` + HubDir string `yaml:"-"` + DataDir string `yaml:"-"` + ConfigDir string `yaml:"-"` + HubIndexFile string `yaml:"-"` + SimulationFilePath string `yaml:"-"` + LabelsToSend map[string][]string `yaml:"-"` } +var DefaultLabelsConfigFilePath = DefaultConfigPath("console", "labels.yaml") + func (c *Config) LoadCrowdsec() error { var err error // Configuration paths are dependency to load crowdsec configuration @@ -89,6 +94,18 @@ func (c *Config) LoadCrowdsec() error { if c.Crowdsec.OutputRoutinesCount <= 0 { c.Crowdsec.OutputRoutinesCount = 1 } + if c.Crowdsec.ConsoleLabelsPath == "" { + c.Crowdsec.ConsoleLabelsPath = DefaultLabelsConfigFilePath + } + yamlFile, err := ioutil.ReadFile(c.Crowdsec.ConsoleLabelsPath) + if err != nil { + return fmt.Errorf("reading console label file '%s': %s", c.Crowdsec.ConsoleLabelsPath, err) + } + c.Crowdsec.LabelsToSend = make(map[string][]string, 0) + err = yaml.Unmarshal(yamlFile, c.Crowdsec.LabelsToSend) + if err != nil { + return fmt.Errorf("unmarshaling labels console config file '%s': %s", DefaultLabelsConfigFilePath, err) + } var crowdsecCleanup = []*string{ &c.Crowdsec.AcquisitionFilePath, @@ -118,3 +135,18 @@ func (c *Config) LoadCrowdsec() error { } return nil } + +func (c *CrowdsecServiceCfg) DumpLabelConfigFile() error { + var out []byte + var err error + + if out, err = yaml.Marshal(c.LabelsToSend); err != nil { + return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", DefaultLabelsConfigFilePath) + } + + if err := os.WriteFile(DefaultLabelsConfigFilePath, out, 0600); err != nil { + return errors.Wrapf(err, "while dumping console config to %s", DefaultLabelsConfigFilePath) + } + + return nil +}