database.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package csconfig
  2. import (
  3. "fmt"
  4. "time"
  5. "entgo.io/ent/dialect"
  6. log "github.com/sirupsen/logrus"
  7. "github.com/crowdsecurity/go-cs-lib/ptr"
  8. )
  9. const (
  10. DEFAULT_MAX_OPEN_CONNS = 100
  11. defaultDecisionBulkSize = 1000
  12. // we need an upper bound due to the sqlite limit of 32k variables in a query
  13. // we have 15 variables per decision, so 32768/15 = 2184.5333
  14. maxDecisionBulkSize = 2000
  15. )
  16. type DatabaseCfg struct {
  17. User string `yaml:"user"`
  18. Password string `yaml:"password"`
  19. DbName string `yaml:"db_name"`
  20. Sslmode string `yaml:"sslmode"`
  21. Host string `yaml:"host"`
  22. Port int `yaml:"port"`
  23. DbPath string `yaml:"db_path"`
  24. Type string `yaml:"type"`
  25. Flush *FlushDBCfg `yaml:"flush"`
  26. LogLevel *log.Level `yaml:"log_level"`
  27. MaxOpenConns *int `yaml:"max_open_conns,omitempty"`
  28. UseWal *bool `yaml:"use_wal,omitempty"`
  29. DecisionBulkSize int `yaml:"decision_bulk_size,omitempty"`
  30. }
  31. type AuthGCCfg struct {
  32. Cert *string `yaml:"cert,omitempty"`
  33. CertDuration *time.Duration
  34. Api *string `yaml:"api_key,omitempty"`
  35. ApiDuration *time.Duration
  36. LoginPassword *string `yaml:"login_password,omitempty"`
  37. LoginPasswordDuration *time.Duration
  38. }
  39. type FlushDBCfg struct {
  40. MaxItems *int `yaml:"max_items,omitempty"`
  41. MaxAge *string `yaml:"max_age,omitempty"`
  42. BouncersGC *AuthGCCfg `yaml:"bouncers_autodelete,omitempty"`
  43. AgentsGC *AuthGCCfg `yaml:"agents_autodelete,omitempty"`
  44. }
  45. func (c *Config) LoadDBConfig() error {
  46. if c.DbConfig == nil {
  47. return fmt.Errorf("no database configuration provided")
  48. }
  49. if c.Cscli != nil {
  50. c.Cscli.DbConfig = c.DbConfig
  51. }
  52. if c.API != nil && c.API.Server != nil {
  53. c.API.Server.DbConfig = c.DbConfig
  54. }
  55. if c.DbConfig.MaxOpenConns == nil {
  56. c.DbConfig.MaxOpenConns = ptr.Of(DEFAULT_MAX_OPEN_CONNS)
  57. }
  58. if c.DbConfig.DecisionBulkSize == 0 {
  59. log.Tracef("No decision_bulk_size value provided, using default value of %d", defaultDecisionBulkSize)
  60. c.DbConfig.DecisionBulkSize = defaultDecisionBulkSize
  61. }
  62. if c.DbConfig.DecisionBulkSize > maxDecisionBulkSize {
  63. log.Warningf("decision_bulk_size too high (%d), setting to the maximum value of %d", c.DbConfig.DecisionBulkSize, maxDecisionBulkSize)
  64. c.DbConfig.DecisionBulkSize = maxDecisionBulkSize
  65. }
  66. if c.DbConfig.Type == "sqlite" {
  67. if c.DbConfig.UseWal == nil {
  68. log.Warning("You are using sqlite without WAL, this can have a performance impact. If you do not store the database in a network share, set db_config.use_wal to true. Set explicitly to false to disable this warning.")
  69. }
  70. }
  71. return nil
  72. }
  73. func (d *DatabaseCfg) ConnectionString() string {
  74. connString := ""
  75. switch d.Type {
  76. case "sqlite":
  77. var sqliteConnectionStringParameters string
  78. if d.UseWal != nil && *d.UseWal {
  79. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1&_journal_mode=WAL"
  80. } else {
  81. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1"
  82. }
  83. connString = fmt.Sprintf("file:%s?%s", d.DbPath, sqliteConnectionStringParameters)
  84. case "mysql":
  85. if d.isSocketConfig() {
  86. connString = fmt.Sprintf("%s:%s@unix(%s)/%s?parseTime=True", d.User, d.Password, d.DbPath, d.DbName)
  87. } else {
  88. connString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", d.User, d.Password, d.Host, d.Port, d.DbName)
  89. }
  90. case "postgres", "postgresql", "pgx":
  91. if d.isSocketConfig() {
  92. connString = fmt.Sprintf("host=%s user=%s dbname=%s password=%s", d.DbPath, d.User, d.DbName, d.Password)
  93. } else {
  94. connString = fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s", d.Host, d.Port, d.User, d.DbName, d.Password, d.Sslmode)
  95. }
  96. }
  97. return connString
  98. }
  99. func (d *DatabaseCfg) ConnectionDialect() (string, string, error) {
  100. switch d.Type {
  101. case "sqlite":
  102. return "sqlite3", dialect.SQLite, nil
  103. case "mysql":
  104. return "mysql", dialect.MySQL, nil
  105. case "pgx", "postgresql", "postgres":
  106. if d.Type != "pgx" {
  107. log.Debugf("database type '%s' is deprecated, switching to 'pgx' instead", d.Type)
  108. }
  109. return "pgx", dialect.Postgres, nil
  110. }
  111. return "", "", fmt.Errorf("unknown database type '%s'", d.Type)
  112. }
  113. func (d *DatabaseCfg) isSocketConfig() bool {
  114. return d.Host == "" && d.Port == 0 && d.DbPath != ""
  115. }