console.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package csconfig
  2. import (
  3. "fmt"
  4. "os"
  5. log "github.com/sirupsen/logrus"
  6. "gopkg.in/yaml.v2"
  7. "github.com/crowdsecurity/go-cs-lib/ptr"
  8. )
  9. const (
  10. SEND_CUSTOM_SCENARIOS = "custom"
  11. SEND_TAINTED_SCENARIOS = "tainted"
  12. SEND_MANUAL_SCENARIOS = "manual"
  13. CONSOLE_MANAGEMENT = "console_management"
  14. SEND_CONTEXT = "context"
  15. )
  16. var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_CONTEXT, CONSOLE_MANAGEMENT}
  17. var CONSOLE_CONFIGS_HELP = map[string]string{
  18. SEND_CUSTOM_SCENARIOS: "Forward alerts from custom scenarios to the console",
  19. SEND_MANUAL_SCENARIOS: "Forward manual decisions to the console",
  20. SEND_TAINTED_SCENARIOS: "Forward alerts from tainted scenarios to the console",
  21. SEND_CONTEXT: "Forward context with alerts to the console",
  22. CONSOLE_MANAGEMENT: "Receive decisions from console",
  23. }
  24. var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml")
  25. type ConsoleConfig struct {
  26. ShareManualDecisions *bool `yaml:"share_manual_decisions"`
  27. ShareTaintedScenarios *bool `yaml:"share_tainted"`
  28. ShareCustomScenarios *bool `yaml:"share_custom"`
  29. ConsoleManagement *bool `yaml:"console_management"`
  30. ShareContext *bool `yaml:"share_context"`
  31. }
  32. func (c *ConsoleConfig) IsPAPIEnabled() bool {
  33. if c == nil || c.ConsoleManagement == nil {
  34. return false
  35. }
  36. return *c.ConsoleManagement
  37. }
  38. func (c *LocalApiServerCfg) LoadConsoleConfig() error {
  39. c.ConsoleConfig = &ConsoleConfig{}
  40. if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
  41. log.Debugf("no console configuration to load")
  42. c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
  43. c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
  44. c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
  45. c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
  46. c.ConsoleConfig.ShareContext = ptr.Of(false)
  47. return nil
  48. }
  49. yamlFile, err := os.ReadFile(c.ConsoleConfigPath)
  50. if err != nil {
  51. return fmt.Errorf("reading console config file '%s': %s", c.ConsoleConfigPath, err)
  52. }
  53. err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
  54. if err != nil {
  55. return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err)
  56. }
  57. if c.ConsoleConfig.ShareCustomScenarios == nil {
  58. log.Debugf("no share_custom scenarios found, setting to true")
  59. c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
  60. }
  61. if c.ConsoleConfig.ShareTaintedScenarios == nil {
  62. log.Debugf("no share_tainted scenarios found, setting to true")
  63. c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
  64. }
  65. if c.ConsoleConfig.ShareManualDecisions == nil {
  66. log.Debugf("no share_manual scenarios found, setting to false")
  67. c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
  68. }
  69. if c.ConsoleConfig.ConsoleManagement == nil {
  70. log.Debugf("no console_management found, setting to false")
  71. c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
  72. }
  73. if c.ConsoleConfig.ShareContext == nil {
  74. log.Debugf("no 'context' found, setting to false")
  75. c.ConsoleConfig.ShareContext = ptr.Of(false)
  76. }
  77. log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
  78. return nil
  79. }