database.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/pkg/ptr"
  8. )
  9. var DEFAULT_MAX_OPEN_CONNS = 100
  10. type DatabaseCfg struct {
  11. User string `yaml:"user"`
  12. Password string `yaml:"password"`
  13. DbName string `yaml:"db_name"`
  14. Sslmode string `yaml:"sslmode"`
  15. Host string `yaml:"host"`
  16. Port int `yaml:"port"`
  17. DbPath string `yaml:"db_path"`
  18. Type string `yaml:"type"`
  19. Flush *FlushDBCfg `yaml:"flush"`
  20. LogLevel *log.Level `yaml:"log_level"`
  21. MaxOpenConns *int `yaml:"max_open_conns,omitempty"`
  22. UseWal *bool `yaml:"use_wal,omitempty"`
  23. }
  24. type AuthGCCfg struct {
  25. Cert *string `yaml:"cert,omitempty"`
  26. CertDuration *time.Duration
  27. Api *string `yaml:"api_key,omitempty"`
  28. ApiDuration *time.Duration
  29. LoginPassword *string `yaml:"login_password,omitempty"`
  30. LoginPasswordDuration *time.Duration
  31. }
  32. type FlushDBCfg struct {
  33. MaxItems *int `yaml:"max_items,omitempty"`
  34. MaxAge *string `yaml:"max_age,omitempty"`
  35. BouncersGC *AuthGCCfg `yaml:"bouncers_autodelete,omitempty"`
  36. AgentsGC *AuthGCCfg `yaml:"agents_autodelete,omitempty"`
  37. }
  38. func (c *Config) LoadDBConfig() error {
  39. if c.DbConfig == nil {
  40. return fmt.Errorf("no database configuration provided")
  41. }
  42. if c.Cscli != nil {
  43. c.Cscli.DbConfig = c.DbConfig
  44. }
  45. if c.API != nil && c.API.Server != nil {
  46. c.API.Server.DbConfig = c.DbConfig
  47. }
  48. if c.DbConfig.MaxOpenConns == nil {
  49. c.DbConfig.MaxOpenConns = ptr.Of(DEFAULT_MAX_OPEN_CONNS)
  50. }
  51. if c.DbConfig.Type == "sqlite" {
  52. if c.DbConfig.UseWal == nil {
  53. 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.")
  54. }
  55. }
  56. return nil
  57. }
  58. func (d *DatabaseCfg) ConnectionString() string {
  59. connString := ""
  60. switch d.Type {
  61. case "sqlite":
  62. var sqliteConnectionStringParameters string
  63. if d.UseWal != nil && *d.UseWal {
  64. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1&_journal_mode=WAL"
  65. } else {
  66. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1"
  67. }
  68. connString = fmt.Sprintf("file:%s?%s", d.DbPath, sqliteConnectionStringParameters)
  69. case "mysql":
  70. if d.isSocketConfig() {
  71. connString = fmt.Sprintf("%s:%s@unix(%s)/%s?parseTime=True", d.User, d.Password, d.DbPath, d.DbName)
  72. } else {
  73. connString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", d.User, d.Password, d.Host, d.Port, d.DbName)
  74. }
  75. case "postgres", "postgresql", "pgx":
  76. if d.isSocketConfig() {
  77. connString = fmt.Sprintf("host=%s user=%s dbname=%s password=%s", d.DbPath, d.User, d.DbName, d.Password)
  78. } else {
  79. 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)
  80. }
  81. }
  82. return connString
  83. }
  84. func (d *DatabaseCfg) ConnectionDialect() (string, string, error) {
  85. switch d.Type {
  86. case "sqlite":
  87. return "sqlite3", dialect.SQLite, nil
  88. case "mysql":
  89. return "mysql", dialect.MySQL, nil
  90. case "pgx", "postgresql", "postgres":
  91. if d.Type != "pgx" {
  92. log.Debugf("database type '%s' is deprecated, switching to 'pgx' instead", d.Type)
  93. }
  94. return "pgx", dialect.Postgres, nil
  95. }
  96. return "", "", fmt.Errorf("unknown database type '%s'", d.Type)
  97. }
  98. func (d *DatabaseCfg) isSocketConfig() bool {
  99. return d.Host == "" && d.Port == 0 && d.DbPath != ""
  100. }