database.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package csconfig
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/crowdsecurity/crowdsec/pkg/types"
  6. log "github.com/sirupsen/logrus"
  7. )
  8. var DEFAULT_MAX_OPEN_CONNS = 100
  9. type DatabaseCfg struct {
  10. User string `yaml:"user"`
  11. Password string `yaml:"password"`
  12. DbName string `yaml:"db_name"`
  13. Sslmode string `yaml:"sslmode"`
  14. Host string `yaml:"host"`
  15. Port int `yaml:"port"`
  16. DbPath string `yaml:"db_path"`
  17. Type string `yaml:"type"`
  18. Flush *FlushDBCfg `yaml:"flush"`
  19. LogLevel *log.Level `yaml:"log_level"`
  20. MaxOpenConns *int `yaml:"max_open_conns,omitempty"`
  21. }
  22. type AuthGCCfg struct {
  23. Cert *string `yaml:"cert,omitempty"`
  24. CertDuration *time.Duration
  25. Api *string `yaml:"api_key,omitempty"`
  26. ApiDuration *time.Duration
  27. LoginPassword *string `yaml:"login_password,omitempty"`
  28. LoginPasswordDuration *time.Duration
  29. }
  30. type FlushDBCfg struct {
  31. MaxItems *int `yaml:"max_items,omitempty"`
  32. MaxAge *string `yaml:"max_age,omitempty"`
  33. BouncersGC *AuthGCCfg `yaml:"bouncers_autodelete,omitempty"`
  34. AgentsGC *AuthGCCfg `yaml:"agents_autodelete,omitempty"`
  35. }
  36. func (c *Config) LoadDBConfig() error {
  37. if c.DbConfig == nil {
  38. return fmt.Errorf("no database configuration provided")
  39. }
  40. if c.Cscli != nil {
  41. c.Cscli.DbConfig = c.DbConfig
  42. }
  43. if c.API != nil && c.API.Server != nil {
  44. c.API.Server.DbConfig = c.DbConfig
  45. }
  46. if c.DbConfig.MaxOpenConns == nil {
  47. c.DbConfig.MaxOpenConns = types.IntPtr(DEFAULT_MAX_OPEN_CONNS)
  48. }
  49. return nil
  50. }