console.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package csconfig
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/crowdsecurity/crowdsec/pkg/fflag"
  6. "github.com/crowdsecurity/crowdsec/pkg/types"
  7. "github.com/pkg/errors"
  8. log "github.com/sirupsen/logrus"
  9. "gopkg.in/yaml.v2"
  10. )
  11. const (
  12. SEND_CUSTOM_SCENARIOS = "custom"
  13. SEND_TAINTED_SCENARIOS = "tainted"
  14. SEND_MANUAL_SCENARIOS = "manual"
  15. CONSOLE_MANAGEMENT = "console_management"
  16. SEND_CONTEXT = "context"
  17. )
  18. var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS, SEND_CONTEXT, CONSOLE_MANAGEMENT}
  19. var DefaultConsoleConfigFilePath = DefaultConfigPath("console.yaml")
  20. type ConsoleConfig struct {
  21. ShareManualDecisions *bool `yaml:"share_manual_decisions"`
  22. ShareTaintedScenarios *bool `yaml:"share_tainted"`
  23. ShareCustomScenarios *bool `yaml:"share_custom"`
  24. ConsoleManagement *bool `yaml:"console_management"`
  25. ShareContext *bool `yaml:"share_context"`
  26. }
  27. func (c *LocalApiServerCfg) LoadConsoleConfig() error {
  28. c.ConsoleConfig = &ConsoleConfig{}
  29. if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
  30. log.Debugf("no console configuration to load")
  31. c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
  32. c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
  33. c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
  34. c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
  35. c.ConsoleConfig.ShareContext = types.BoolPtr(false)
  36. return nil
  37. }
  38. yamlFile, err := os.ReadFile(c.ConsoleConfigPath)
  39. if err != nil {
  40. return fmt.Errorf("reading console config file '%s': %s", c.ConsoleConfigPath, err)
  41. }
  42. err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
  43. if err != nil {
  44. return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err)
  45. }
  46. if c.ConsoleConfig.ShareCustomScenarios == nil {
  47. log.Debugf("no share_custom scenarios found, setting to true")
  48. c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
  49. }
  50. if c.ConsoleConfig.ShareTaintedScenarios == nil {
  51. log.Debugf("no share_tainted scenarios found, setting to true")
  52. c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
  53. }
  54. if c.ConsoleConfig.ShareManualDecisions == nil {
  55. log.Debugf("no share_manual scenarios found, setting to false")
  56. c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
  57. }
  58. if !fflag.PapiClient.IsEnabled() {
  59. c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
  60. } else if c.ConsoleConfig.ConsoleManagement == nil {
  61. log.Debugf("no console_management found, setting to false")
  62. c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
  63. }
  64. if c.ConsoleConfig.ShareContext == nil {
  65. log.Debugf("no 'context' found, setting to false")
  66. c.ConsoleConfig.ShareContext = types.BoolPtr(false)
  67. }
  68. log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
  69. return nil
  70. }
  71. func (c *LocalApiServerCfg) DumpConsoleConfig() error {
  72. var out []byte
  73. var err error
  74. if out, err = yaml.Marshal(c.ConsoleConfig); err != nil {
  75. return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", c.ConsoleConfigPath)
  76. }
  77. if c.ConsoleConfigPath == "" {
  78. c.ConsoleConfigPath = DefaultConsoleConfigFilePath
  79. log.Debugf("Empty console_path, defaulting to %s", c.ConsoleConfigPath)
  80. }
  81. if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
  82. return errors.Wrapf(err, "while dumping console config to %s", c.ConsoleConfigPath)
  83. }
  84. return nil
  85. }