github.com/crowdsecurity/crowdsec@v1.6.1/pkg/csconfig/crowdsec_service.go (about) 1 package csconfig 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 log "github.com/sirupsen/logrus" 9 "gopkg.in/yaml.v3" 10 11 "github.com/crowdsecurity/go-cs-lib/ptr" 12 ) 13 14 // CrowdsecServiceCfg contains the location of parsers/scenarios/... and acquisition files 15 type CrowdsecServiceCfg struct { 16 Enable *bool `yaml:"enable"` 17 AcquisitionFilePath string `yaml:"acquisition_path,omitempty"` 18 AcquisitionDirPath string `yaml:"acquisition_dir,omitempty"` 19 ConsoleContextPath string `yaml:"console_context_path"` 20 ConsoleContextValueLength int `yaml:"console_context_value_length"` 21 AcquisitionFiles []string `yaml:"-"` 22 ParserRoutinesCount int `yaml:"parser_routines"` 23 BucketsRoutinesCount int `yaml:"buckets_routines"` 24 OutputRoutinesCount int `yaml:"output_routines"` 25 SimulationConfig *SimulationConfig `yaml:"-"` 26 BucketStateFile string `yaml:"state_input_file,omitempty"` // if we need to unserialize buckets at start 27 BucketStateDumpDir string `yaml:"state_output_dir,omitempty"` // if we need to unserialize buckets on shutdown 28 BucketsGCEnabled bool `yaml:"-"` // we need to garbage collect buckets when in forensic mode 29 30 SimulationFilePath string `yaml:"-"` 31 ContextToSend map[string][]string `yaml:"-"` 32 } 33 34 func (c *Config) LoadCrowdsec() error { 35 var err error 36 37 if c.Crowdsec == nil { 38 log.Warning("crowdsec agent is disabled") 39 c.DisableAgent = true 40 return nil 41 } 42 43 if c.Crowdsec.Enable == nil { 44 // if the option is not present, it is enabled by default 45 c.Crowdsec.Enable = ptr.Of(true) 46 } 47 48 if !*c.Crowdsec.Enable { 49 log.Warning("crowdsec agent is disabled") 50 c.DisableAgent = true 51 return nil 52 } 53 54 if c.Crowdsec.AcquisitionFiles == nil { 55 c.Crowdsec.AcquisitionFiles = []string{} 56 } 57 58 if c.Crowdsec.AcquisitionFilePath != "" { 59 log.Debugf("non-empty acquisition_path %s", c.Crowdsec.AcquisitionFilePath) 60 if _, err = os.Stat(c.Crowdsec.AcquisitionFilePath); err != nil { 61 return fmt.Errorf("while checking acquisition_path: %w", err) 62 } 63 c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, c.Crowdsec.AcquisitionFilePath) 64 } 65 66 if c.Crowdsec.AcquisitionDirPath != "" { 67 c.Crowdsec.AcquisitionDirPath, err = filepath.Abs(c.Crowdsec.AcquisitionDirPath) 68 if err != nil { 69 return fmt.Errorf("can't get absolute path of '%s': %w", c.Crowdsec.AcquisitionDirPath, err) 70 } 71 72 var files []string 73 74 files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yaml") 75 if err != nil { 76 return fmt.Errorf("while globbing acquis_dir: %w", err) 77 } 78 c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...) 79 80 files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yml") 81 if err != nil { 82 return fmt.Errorf("while globbing acquis_dir: %w", err) 83 } 84 c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...) 85 } 86 87 if c.Crowdsec.AcquisitionDirPath == "" && c.Crowdsec.AcquisitionFilePath == "" { 88 log.Warning("no acquisition_path or acquisition_dir specified") 89 } 90 91 if len(c.Crowdsec.AcquisitionFiles) == 0 { 92 log.Warning("no acquisition file found") 93 } 94 95 if err = c.LoadSimulation(); err != nil { 96 return fmt.Errorf("load error (simulation): %w", err) 97 } 98 99 if c.Crowdsec.ParserRoutinesCount <= 0 { 100 c.Crowdsec.ParserRoutinesCount = 1 101 } 102 103 if c.Crowdsec.BucketsRoutinesCount <= 0 { 104 c.Crowdsec.BucketsRoutinesCount = 1 105 } 106 107 if c.Crowdsec.OutputRoutinesCount <= 0 { 108 c.Crowdsec.OutputRoutinesCount = 1 109 } 110 111 crowdsecCleanup := []*string{ 112 &c.Crowdsec.AcquisitionFilePath, 113 &c.Crowdsec.ConsoleContextPath, 114 } 115 116 for _, k := range crowdsecCleanup { 117 if *k == "" { 118 continue 119 } 120 *k, err = filepath.Abs(*k) 121 if err != nil { 122 return fmt.Errorf("failed to get absolute path of '%s': %w", *k, err) 123 } 124 } 125 126 // Convert relative paths to absolute paths 127 for i, file := range c.Crowdsec.AcquisitionFiles { 128 f, err := filepath.Abs(file) 129 if err != nil { 130 return fmt.Errorf("failed to get absolute path of '%s': %w", file, err) 131 } 132 c.Crowdsec.AcquisitionFiles[i] = f 133 } 134 135 if err = c.LoadAPIClient(); err != nil { 136 return fmt.Errorf("loading api client: %w", err) 137 } 138 139 return nil 140 } 141 142 func (c *CrowdsecServiceCfg) DumpContextConfigFile() error { 143 // XXX: MakeDirs 144 out, err := yaml.Marshal(c.ContextToSend) 145 if err != nil { 146 return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleContextPath, err) 147 } 148 149 if err = os.MkdirAll(filepath.Dir(c.ConsoleContextPath), 0700); err != nil { 150 return fmt.Errorf("while creating directories for %s: %w", c.ConsoleContextPath, err) 151 } 152 153 if err := os.WriteFile(c.ConsoleContextPath, out, 0600); err != nil { 154 return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleContextPath, err) 155 } 156 157 log.Infof("%s file saved", c.ConsoleContextPath) 158 159 return nil 160 }