config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package csconfig
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "github.com/pkg/errors"
  7. log "github.com/sirupsen/logrus"
  8. "gopkg.in/yaml.v2"
  9. "github.com/crowdsecurity/crowdsec/pkg/csstring"
  10. "github.com/crowdsecurity/crowdsec/pkg/types"
  11. "github.com/crowdsecurity/crowdsec/pkg/yamlpatch"
  12. )
  13. // defaultConfigDir is the base path to all configuration files, to be overridden in the Makefile */
  14. var defaultConfigDir = "/etc/crowdsec"
  15. // defaultDataDir is the base path to all data files, to be overridden in the Makefile */
  16. var defaultDataDir = "/var/lib/crowdsec/data/"
  17. // Config contains top-level defaults -> overridden by configuration file -> overridden by CLI flags
  18. type Config struct {
  19. //just a path to ourself :p
  20. FilePath *string `yaml:"-"`
  21. Self []byte `yaml:"-"`
  22. Common *CommonCfg `yaml:"common,omitempty"`
  23. Prometheus *PrometheusCfg `yaml:"prometheus,omitempty"`
  24. Crowdsec *CrowdsecServiceCfg `yaml:"crowdsec_service,omitempty"`
  25. Cscli *CscliCfg `yaml:"cscli,omitempty"`
  26. DbConfig *DatabaseCfg `yaml:"db_config,omitempty"`
  27. API *APICfg `yaml:"api,omitempty"`
  28. ConfigPaths *ConfigurationPaths `yaml:"config_paths,omitempty"`
  29. PluginConfig *PluginCfg `yaml:"plugin_config,omitempty"`
  30. DisableAPI bool `yaml:"-"`
  31. DisableAgent bool `yaml:"-"`
  32. Hub *Hub `yaml:"-"`
  33. }
  34. func (c *Config) Dump() error {
  35. out, err := yaml.Marshal(c)
  36. if err != nil {
  37. return errors.Wrap(err, "failed marshaling config")
  38. }
  39. fmt.Printf("%s", string(out))
  40. return nil
  41. }
  42. func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool) (*Config, error) {
  43. patcher := yamlpatch.NewPatcher(configFile, ".local")
  44. patcher.SetQuiet(quiet)
  45. fcontent, err := patcher.MergedPatchContent()
  46. if err != nil {
  47. return nil, err
  48. }
  49. configData := csstring.StrictExpand(string(fcontent), os.LookupEnv)
  50. cfg := Config{
  51. FilePath: &configFile,
  52. DisableAgent: disableAgent,
  53. DisableAPI: disableAPI,
  54. }
  55. err = yaml.UnmarshalStrict([]byte(configData), &cfg)
  56. if err != nil {
  57. // this is actually the "merged" yaml
  58. return nil, errors.Wrap(err, configFile)
  59. }
  60. return &cfg, nil
  61. }
  62. func NewDefaultConfig() *Config {
  63. logLevel := log.InfoLevel
  64. commonCfg := CommonCfg{
  65. Daemonize: false,
  66. PidDir: "/tmp/",
  67. LogMedia: "stdout",
  68. //LogDir unneeded
  69. LogLevel: &logLevel,
  70. WorkingDir: ".",
  71. }
  72. prometheus := PrometheusCfg{
  73. Enabled: true,
  74. Level: "full",
  75. }
  76. configPaths := ConfigurationPaths{
  77. ConfigDir: DefaultConfigPath("."),
  78. DataDir: DefaultDataPath("."),
  79. SimulationFilePath: DefaultConfigPath("simulation.yaml"),
  80. HubDir: DefaultConfigPath("hub"),
  81. HubIndexFile: DefaultConfigPath("hub", ".index.json"),
  82. }
  83. crowdsecCfg := CrowdsecServiceCfg{
  84. AcquisitionFilePath: DefaultConfigPath("acquis.yaml"),
  85. ParserRoutinesCount: 1,
  86. }
  87. cscliCfg := CscliCfg{
  88. Output: "human",
  89. Color: "auto",
  90. }
  91. apiCfg := APICfg{
  92. Client: &LocalApiClientCfg{
  93. CredentialsFilePath: DefaultConfigPath("lapi-secrets.yaml"),
  94. },
  95. Server: &LocalApiServerCfg{
  96. ListenURI: "127.0.0.1:8080",
  97. UseForwardedForHeaders: false,
  98. OnlineClient: &OnlineApiClientCfg{
  99. CredentialsFilePath: DefaultConfigPath("config", "online-api-secrets.yaml"),
  100. },
  101. },
  102. CTI: &CTICfg{
  103. Enabled: types.BoolPtr(false),
  104. },
  105. }
  106. dbConfig := DatabaseCfg{
  107. Type: "sqlite",
  108. DbPath: DefaultDataPath("crowdsec.db"),
  109. MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
  110. }
  111. globalCfg := Config{
  112. Common: &commonCfg,
  113. Prometheus: &prometheus,
  114. Crowdsec: &crowdsecCfg,
  115. Cscli: &cscliCfg,
  116. API: &apiCfg,
  117. ConfigPaths: &configPaths,
  118. DbConfig: &dbConfig,
  119. }
  120. return &globalCfg
  121. }
  122. // DefaultConfigPath returns the default path for a configuration resource
  123. // "elem" parameters are path components relative to the default cfg directory.
  124. func DefaultConfigPath(elem ...string) string {
  125. elem = append([]string{defaultConfigDir}, elem...)
  126. return filepath.Join(elem...)
  127. }
  128. // DefaultDataPath returns the default path for a data resource.
  129. // "elem" parameters are path components relative to the default data directory.
  130. func DefaultDataPath(elem ...string) string {
  131. elem = append([]string{defaultDataDir}, elem...)
  132. return filepath.Join(elem...)
  133. }