simulation.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package csconfig
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "gopkg.in/yaml.v2"
  6. "github.com/crowdsecurity/go-cs-lib/yamlpatch"
  7. )
  8. type SimulationConfig struct {
  9. Simulation *bool `yaml:"simulation"`
  10. Exclusions []string `yaml:"exclusions,omitempty"`
  11. }
  12. func (s *SimulationConfig) IsSimulated(scenario string) bool {
  13. var simulated bool
  14. if s.Simulation != nil && *s.Simulation {
  15. simulated = true
  16. }
  17. for _, excluded := range s.Exclusions {
  18. if excluded == scenario {
  19. simulated = !simulated
  20. break
  21. }
  22. }
  23. return simulated
  24. }
  25. func (c *Config) LoadSimulation() error {
  26. simCfg := SimulationConfig{}
  27. if c.ConfigPaths.SimulationFilePath == "" {
  28. c.ConfigPaths.SimulationFilePath = filepath.Clean(c.ConfigPaths.ConfigDir + "/simulation.yaml")
  29. }
  30. patcher := yamlpatch.NewPatcher(c.ConfigPaths.SimulationFilePath, ".local")
  31. rcfg, err := patcher.MergedPatchContent()
  32. if err != nil {
  33. return err
  34. }
  35. if err := yaml.UnmarshalStrict(rcfg, &simCfg); err != nil {
  36. return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
  37. }
  38. if simCfg.Simulation == nil {
  39. simCfg.Simulation = new(bool)
  40. }
  41. if c.Crowdsec != nil {
  42. c.Crowdsec.SimulationConfig = &simCfg
  43. }
  44. if c.Cscli != nil {
  45. c.Cscli.SimulationConfig = &simCfg
  46. }
  47. return nil
  48. }