2022-01-13 15:46:16 +00:00
|
|
|
package csconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2024-03-04 13:22:53 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2023-06-01 14:31:56 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
2022-01-13 15:46:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SEND_CUSTOM_SCENARIOS = "custom"
|
|
|
|
SEND_TAINTED_SCENARIOS = "tainted"
|
|
|
|
SEND_MANUAL_SCENARIOS = "manual"
|
2023-01-31 13:47:44 +00:00
|
|
|
CONSOLE_MANAGEMENT = "console_management"
|
2023-01-04 15:50:02 +00:00
|
|
|
SEND_CONTEXT = "context"
|
2022-01-13 15:46:16 +00:00
|
|
|
)
|
|
|
|
|
2023-01-31 13:47:44 +00:00
|
|
|
var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_CONTEXT, CONSOLE_MANAGEMENT}
|
2024-01-19 14:49:00 +00:00
|
|
|
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",
|
|
|
|
}
|
2022-01-13 15:46:16 +00:00
|
|
|
|
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"`
|
2023-03-06 14:38:58 +00:00
|
|
|
ConsoleManagement *bool `yaml:"console_management"`
|
2023-01-04 15:50:02 +00:00
|
|
|
ShareContext *bool `yaml:"share_context"`
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 13:56:14 +00:00
|
|
|
func (c *ConsoleConfig) EnabledOptions() []string {
|
|
|
|
ret := []string{}
|
|
|
|
if c == nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ShareCustomScenarios != nil && *c.ShareCustomScenarios {
|
|
|
|
ret = append(ret, SEND_CUSTOM_SCENARIOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ShareTaintedScenarios != nil && *c.ShareTaintedScenarios {
|
|
|
|
ret = append(ret, SEND_TAINTED_SCENARIOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ShareManualDecisions != nil && *c.ShareManualDecisions {
|
|
|
|
ret = append(ret, SEND_MANUAL_SCENARIOS)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ConsoleManagement != nil && *c.ConsoleManagement {
|
|
|
|
ret = append(ret, CONSOLE_MANAGEMENT)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ShareContext != nil && *c.ShareContext {
|
|
|
|
ret = append(ret, SEND_CONTEXT)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-01-16 08:16:21 +00:00
|
|
|
func (c *ConsoleConfig) IsPAPIEnabled() bool {
|
|
|
|
if c == nil || c.ConsoleManagement == nil {
|
|
|
|
return false
|
|
|
|
}
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2024-01-16 08:16:21 +00:00
|
|
|
return *c.ConsoleManagement
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
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")
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
|
|
|
|
c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
|
|
|
|
c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
|
|
|
|
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
|
|
|
|
c.ConsoleConfig.ShareContext = ptr.Of(false)
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
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 {
|
2024-03-04 13:22:53 +00:00
|
|
|
return fmt.Errorf("reading console config file '%s': %w", c.ConsoleConfigPath, err)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
|
|
|
|
if err != nil {
|
2024-03-04 13:22:53 +00:00
|
|
|
return fmt.Errorf("unmarshaling console config file '%s': %w", c.ConsoleConfigPath, err)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.ConsoleConfig.ShareCustomScenarios == nil {
|
|
|
|
log.Debugf("no share_custom scenarios found, setting to true")
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
if c.ConsoleConfig.ShareTaintedScenarios == nil {
|
|
|
|
log.Debugf("no share_tainted scenarios found, setting to true")
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2024-03-04 13:22:53 +00:00
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
if c.ConsoleConfig.ShareManualDecisions == nil {
|
|
|
|
log.Debugf("no share_manual scenarios found, setting to false")
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
|
2022-01-13 15:46:16 +00:00
|
|
|
}
|
2023-01-04 15:50:02 +00:00
|
|
|
|
2023-12-08 13:55:45 +00:00
|
|
|
if c.ConsoleConfig.ConsoleManagement == nil {
|
2023-01-31 13:47:44 +00:00
|
|
|
log.Debugf("no console_management found, setting to false")
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
|
2023-01-31 13:47:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 15:50:02 +00:00
|
|
|
if c.ConsoleConfig.ShareContext == nil {
|
|
|
|
log.Debugf("no 'context' found, setting to false")
|
2023-06-01 14:31:56 +00:00
|
|
|
c.ConsoleConfig.ShareContext = ptr.Of(false)
|
2023-01-04 15:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 15:46:16 +00:00
|
|
|
log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|