crowdsec_service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package csconfig
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/pkg/errors"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. /*Configurations needed for crowdsec to load parser/scenarios/... + acquisition*/
  10. type CrowdsecServiceCfg struct {
  11. AcquisitionFilePath string `yaml:"acquisition_path,omitempty"`
  12. AcquisitionDirPath string `yaml:"acquisition_dir,omitempty"`
  13. AcquisitionFiles []string `yaml:"-"`
  14. ParserRoutinesCount int `yaml:"parser_routines"`
  15. BucketsRoutinesCount int `yaml:"buckets_routines"`
  16. OutputRoutinesCount int `yaml:"output_routines"`
  17. SimulationConfig *SimulationConfig `yaml:"-"`
  18. LintOnly bool `yaml:"-"` //if set to true, exit after loading configs
  19. BucketStateFile string `yaml:"state_input_file,omitempty"` //if we need to unserialize buckets at start
  20. BucketStateDumpDir string `yaml:"state_output_dir,omitempty"` //if we need to unserialize buckets on shutdown
  21. BucketsGCEnabled bool `yaml:"-"` //we need to garbage collect buckets when in forensic mode
  22. HubDir string `yaml:"-"`
  23. DataDir string `yaml:"-"`
  24. ConfigDir string `yaml:"-"`
  25. HubIndexFile string `yaml:"-"`
  26. SimulationFilePath string `yaml:"-"`
  27. }
  28. func (c *Config) LoadCrowdsec() error {
  29. var err error
  30. // Configuration paths are dependency to load crowdsec configuration
  31. if err := c.LoadConfigurationPaths(); err != nil {
  32. return err
  33. }
  34. if c.Crowdsec == nil {
  35. log.Warningf("crowdsec agent is disabled")
  36. c.DisableAgent = true
  37. return nil
  38. }
  39. if c.Crowdsec.AcquisitionFilePath != "" {
  40. log.Debugf("non-empty acquisition file path %s", c.Crowdsec.AcquisitionFilePath)
  41. if _, err := os.Stat(c.Crowdsec.AcquisitionFilePath); err != nil {
  42. return errors.Wrapf(err, "while checking acquisition path %s", c.Crowdsec.AcquisitionFilePath)
  43. }
  44. c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, c.Crowdsec.AcquisitionFilePath)
  45. }
  46. if c.Crowdsec.AcquisitionDirPath != "" {
  47. c.Crowdsec.AcquisitionDirPath, err = filepath.Abs(c.Crowdsec.AcquisitionDirPath)
  48. if err != nil {
  49. return errors.Wrapf(err, "can't get absolute path of '%s'", c.Crowdsec.AcquisitionDirPath)
  50. }
  51. files, err := filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yaml")
  52. if err != nil {
  53. return errors.Wrap(err, "while globbing acquis_dir")
  54. }
  55. c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...)
  56. files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yml")
  57. if err != nil {
  58. return errors.Wrap(err, "while globbing acquis_dir")
  59. }
  60. c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...)
  61. }
  62. if c.Crowdsec.AcquisitionDirPath == "" && c.Crowdsec.AcquisitionFilePath == "" {
  63. log.Warningf("no acquisition_path nor acquisition_dir")
  64. }
  65. if err := c.LoadSimulation(); err != nil {
  66. return errors.Wrap(err, "load error (simulation)")
  67. }
  68. c.Crowdsec.ConfigDir = c.ConfigPaths.ConfigDir
  69. c.Crowdsec.DataDir = c.ConfigPaths.DataDir
  70. c.Crowdsec.HubDir = c.ConfigPaths.HubDir
  71. c.Crowdsec.HubIndexFile = c.ConfigPaths.HubIndexFile
  72. if c.Crowdsec.ParserRoutinesCount <= 0 {
  73. c.Crowdsec.ParserRoutinesCount = 1
  74. }
  75. if c.Crowdsec.BucketsRoutinesCount <= 0 {
  76. c.Crowdsec.BucketsRoutinesCount = 1
  77. }
  78. if c.Crowdsec.OutputRoutinesCount <= 0 {
  79. c.Crowdsec.OutputRoutinesCount = 1
  80. }
  81. var crowdsecCleanup = []*string{
  82. &c.Crowdsec.AcquisitionFilePath,
  83. }
  84. for _, k := range crowdsecCleanup {
  85. if *k == "" {
  86. continue
  87. }
  88. *k, err = filepath.Abs(*k)
  89. if err != nil {
  90. return errors.Wrapf(err, "failed to get absolute path of '%s'", *k)
  91. }
  92. }
  93. for i, file := range c.Crowdsec.AcquisitionFiles {
  94. f, err := filepath.Abs(file)
  95. if err != nil {
  96. return errors.Wrapf(err, "failed to get absolute path of '%s'", file)
  97. }
  98. c.Crowdsec.AcquisitionFiles[i] = f
  99. }
  100. if err := c.LoadAPIClient(); err != nil {
  101. return fmt.Errorf("loading api client: %s", err.Error())
  102. }
  103. if err := c.LoadHub(); err != nil {
  104. return errors.Wrap(err, "while loading hub")
  105. }
  106. return nil
  107. }