mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
pkg/csconfig: use yaml.v3; deprecate yaml.v2 for new code (#2867)
* pkg/csconfig: use yaml.v3; deprecate yaml.v2 for new code * yaml.v3: handle empty files * Lint whitespace, errors
This commit is contained in:
parent
41b43733b0
commit
e7ecea764e
11 changed files with 153 additions and 43 deletions
|
@ -1,10 +1,13 @@
|
|||
package csconfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
|
||||
)
|
||||
|
@ -20,37 +23,51 @@ func (s *SimulationConfig) IsSimulated(scenario string) bool {
|
|||
if s.Simulation != nil && *s.Simulation {
|
||||
simulated = true
|
||||
}
|
||||
|
||||
for _, excluded := range s.Exclusions {
|
||||
if excluded == scenario {
|
||||
simulated = !simulated
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return simulated
|
||||
}
|
||||
|
||||
func (c *Config) LoadSimulation() error {
|
||||
simCfg := SimulationConfig{}
|
||||
|
||||
if c.ConfigPaths.SimulationFilePath == "" {
|
||||
c.ConfigPaths.SimulationFilePath = filepath.Clean(c.ConfigPaths.ConfigDir + "/simulation.yaml")
|
||||
}
|
||||
|
||||
patcher := yamlpatch.NewPatcher(c.ConfigPaths.SimulationFilePath, ".local")
|
||||
|
||||
rcfg, err := patcher.MergedPatchContent()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := yaml.UnmarshalStrict(rcfg, &simCfg); err != nil {
|
||||
return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
|
||||
|
||||
dec := yaml.NewDecoder(bytes.NewReader(rcfg))
|
||||
dec.KnownFields(true)
|
||||
|
||||
if err := dec.Decode(&simCfg); err != nil {
|
||||
if !errors.Is(err, io.EOF) {
|
||||
return fmt.Errorf("while unmarshaling simulation file '%s': %w", c.ConfigPaths.SimulationFilePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
if simCfg.Simulation == nil {
|
||||
simCfg.Simulation = new(bool)
|
||||
}
|
||||
|
||||
if c.Crowdsec != nil {
|
||||
c.Crowdsec.SimulationConfig = &simCfg
|
||||
}
|
||||
|
||||
if c.Cscli != nil {
|
||||
c.Cscli.SimulationConfig = &simCfg
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue