2022-01-13 15:46:16 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SEND_CUSTOM_SCENARIOS = "custom"
|
|
|
|
SEND_TAINTED_SCENARIOS = "tainted"
|
|
|
|
SEND_MANUAL_SCENARIOS = "manual"
|
|
|
|
)
|
|
|
|
|
|
|
|
var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS}
|
|
|
|
|
2022-02-01 09:34:53 +00:00
|
|
|
var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml")
|
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
type ConsoleConfig struct {
|
|
|
|
ShareManualDecisions *bool `yaml:"share_manual_decisions"`
|
|
|
|
ShareTaintedScenarios *bool `yaml:"share_tainted"`
|
|
|
|
ShareCustomScenarios *bool `yaml:"share_custom"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LocalApiServerCfg) LoadConsoleConfig() error {
|
|
|
|
c.ConsoleConfig = &ConsoleConfig{}
|
|
|
|
if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
|
|
|
|
log.Debugf("no console configuration to load")
|
|
|
|
c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
|
|
|
|
c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
|
|
|
|
c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:55:03 +00:00
|
|
|
yamlFile, err := os.ReadFile(c.ConsoleConfigPath)
|
2022-01-13 15:46:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading console config file '%s': %s", c.ConsoleConfigPath, err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ConsoleConfig.ShareCustomScenarios == nil {
|
|
|
|
log.Debugf("no share_custom scenarios found, setting to true")
|
|
|
|
c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
|
|
|
|
}
|
|
|
|
if c.ConsoleConfig.ShareTaintedScenarios == nil {
|
|
|
|
log.Debugf("no share_tainted scenarios found, setting to true")
|
|
|
|
c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
|
|
|
|
}
|
|
|
|
if c.ConsoleConfig.ShareManualDecisions == nil {
|
|
|
|
log.Debugf("no share_manual scenarios found, setting to false")
|
|
|
|
c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
|
|
|
|
}
|
|
|
|
log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LocalApiServerCfg) DumpConsoleConfig() error {
|
|
|
|
var out []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if out, err = yaml.Marshal(c.ConsoleConfig); err != nil {
|
|
|
|
return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", c.ConsoleConfigPath)
|
|
|
|
}
|
|
|
|
if c.ConsoleConfigPath == "" {
|
2022-02-01 09:34:53 +00:00
|
|
|
c.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
|
|
|
log.Debugf("Empty console_path, defaulting to %s", c.ConsoleConfigPath)
|
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
|
|
|
|
return errors.Wrapf(err, "while dumping console config to %s", c.ConsoleConfigPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|