database.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package database
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "os"
  7. "time"
  8. "entgo.io/ent/dialect"
  9. entsql "entgo.io/ent/dialect/sql"
  10. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  11. "github.com/crowdsecurity/crowdsec/pkg/database/ent"
  12. "github.com/crowdsecurity/crowdsec/pkg/types"
  13. "github.com/go-co-op/gocron"
  14. _ "github.com/go-sql-driver/mysql"
  15. _ "github.com/jackc/pgx/v4/stdlib"
  16. _ "github.com/lib/pq"
  17. _ "github.com/mattn/go-sqlite3"
  18. "github.com/pkg/errors"
  19. log "github.com/sirupsen/logrus"
  20. )
  21. type Client struct {
  22. Ent *ent.Client
  23. CTX context.Context
  24. Log *log.Logger
  25. CanFlush bool
  26. Type string
  27. WalMode *bool
  28. }
  29. func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig.DatabaseCfg) (*entsql.Driver, error) {
  30. db, err := sql.Open(dbtype, dsn)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if config.MaxOpenConns == nil {
  35. log.Warningf("MaxOpenConns is 0, defaulting to %d", csconfig.DEFAULT_MAX_OPEN_CONNS)
  36. config.MaxOpenConns = types.IntPtr(csconfig.DEFAULT_MAX_OPEN_CONNS)
  37. }
  38. db.SetMaxOpenConns(*config.MaxOpenConns)
  39. drv := entsql.OpenDB(dbdialect, db)
  40. return drv, nil
  41. }
  42. func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
  43. var client *ent.Client
  44. var err error
  45. if config == nil {
  46. return &Client{}, fmt.Errorf("DB config is empty")
  47. }
  48. /*The logger that will be used by db operations*/
  49. clog := log.New()
  50. if err := types.ConfigureLogger(clog); err != nil {
  51. return nil, errors.Wrap(err, "while configuring db logger")
  52. }
  53. if config.LogLevel != nil {
  54. clog.SetLevel(*config.LogLevel)
  55. }
  56. entLogger := clog.WithField("context", "ent")
  57. entOpt := ent.Log(entLogger.Debug)
  58. switch config.Type {
  59. case "sqlite":
  60. /*if it's the first startup, we want to touch and chmod file*/
  61. if _, err := os.Stat(config.DbPath); os.IsNotExist(err) {
  62. f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0600)
  63. if err != nil {
  64. return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
  65. }
  66. if err := f.Close(); err != nil {
  67. return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
  68. }
  69. }
  70. //Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)
  71. if err := setFilePerm(config.DbPath, 0600); err != nil {
  72. return &Client{}, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
  73. }
  74. if config.UseWal == nil {
  75. entLogger.Warn("you are using sqlite without WAL, this can have an impact of performance. 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.")
  76. }
  77. var sqliteConnectionStringParameters string
  78. if config.UseWal != nil && *config.UseWal {
  79. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1&_journal_mode=WAL"
  80. } else {
  81. sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1"
  82. }
  83. drv, err := getEntDriver("sqlite3", dialect.SQLite, fmt.Sprintf("file:%s?%s", config.DbPath, sqliteConnectionStringParameters), config)
  84. if err != nil {
  85. return &Client{}, errors.Wrapf(err, "failed opening connection to sqlite: %v", config.DbPath)
  86. }
  87. client = ent.NewClient(ent.Driver(drv), entOpt)
  88. case "mysql":
  89. drv, err := getEntDriver("mysql", dialect.MySQL, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", config.User, config.Password, config.Host, config.Port, config.DbName), config)
  90. if err != nil {
  91. return &Client{}, fmt.Errorf("failed opening connection to mysql: %v", err)
  92. }
  93. client = ent.NewClient(ent.Driver(drv), entOpt)
  94. case "postgres", "postgresql":
  95. drv, err := getEntDriver("postgres", dialect.Postgres, fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s", config.Host, config.Port, config.User, config.DbName, config.Password, config.Sslmode), config)
  96. if err != nil {
  97. return &Client{}, fmt.Errorf("failed opening connection to postgresql: %v", err)
  98. }
  99. client = ent.NewClient(ent.Driver(drv), entOpt)
  100. case "pgx":
  101. drv, err := getEntDriver("pgx", dialect.Postgres, fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=%s", config.User, config.Password, config.Host, config.Port, config.DbName, config.Sslmode), config)
  102. if err != nil {
  103. return &Client{}, fmt.Errorf("failed opening connection to pgx: %v", err)
  104. }
  105. client = ent.NewClient(ent.Driver(drv), entOpt)
  106. default:
  107. return &Client{}, fmt.Errorf("unknown database type '%s'", config.Type)
  108. }
  109. if config.LogLevel != nil && *config.LogLevel >= log.DebugLevel {
  110. clog.Debugf("Enabling request debug")
  111. client = client.Debug()
  112. }
  113. if err = client.Schema.Create(context.Background()); err != nil {
  114. return nil, fmt.Errorf("failed creating schema resources: %v", err)
  115. }
  116. return &Client{Ent: client, CTX: context.Background(), Log: clog, CanFlush: true, Type: config.Type, WalMode: config.UseWal}, nil
  117. }
  118. func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Scheduler, error) {
  119. maxItems := 0
  120. maxAge := ""
  121. if config.MaxItems != nil && *config.MaxItems <= 0 {
  122. return nil, fmt.Errorf("max_items can't be zero or negative number")
  123. }
  124. if config.MaxItems != nil {
  125. maxItems = *config.MaxItems
  126. }
  127. if config.MaxAge != nil && *config.MaxAge != "" {
  128. maxAge = *config.MaxAge
  129. }
  130. // Init & Start cronjob every minute for alerts
  131. scheduler := gocron.NewScheduler(time.UTC)
  132. job, err := scheduler.Every(1).Minute().Do(c.FlushAlerts, maxAge, maxItems)
  133. if err != nil {
  134. return nil, errors.Wrap(err, "while starting FlushAlerts scheduler")
  135. }
  136. job.SingletonMode()
  137. // Init & Start cronjob every hour for bouncers/agents
  138. if config.AgentsGC != nil {
  139. if config.AgentsGC.Cert != nil {
  140. duration, err := types.ParseDuration(*config.AgentsGC.Cert)
  141. if err != nil {
  142. return nil, errors.Wrap(err, "while parsing agents cert auto-delete duration")
  143. }
  144. config.AgentsGC.CertDuration = &duration
  145. }
  146. if config.AgentsGC.LoginPassword != nil {
  147. duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword)
  148. if err != nil {
  149. return nil, errors.Wrap(err, "while parsing agents login/password auto-delete duration")
  150. }
  151. config.AgentsGC.LoginPasswordDuration = &duration
  152. }
  153. if config.AgentsGC.Api != nil {
  154. log.Warning("agents auto-delete for API auth is not supported (use cert or login_password)")
  155. }
  156. }
  157. if config.BouncersGC != nil {
  158. if config.BouncersGC.Cert != nil {
  159. duration, err := types.ParseDuration(*config.BouncersGC.Cert)
  160. if err != nil {
  161. return nil, errors.Wrap(err, "while parsing bouncers cert auto-delete duration")
  162. }
  163. config.BouncersGC.CertDuration = &duration
  164. }
  165. if config.BouncersGC.Api != nil {
  166. duration, err := types.ParseDuration(*config.BouncersGC.Api)
  167. if err != nil {
  168. return nil, errors.Wrap(err, "while parsing bouncers api auto-delete duration")
  169. }
  170. config.BouncersGC.ApiDuration = &duration
  171. }
  172. if config.BouncersGC.LoginPassword != nil {
  173. log.Warning("bouncers auto-delete for login/password auth is not supported (use cert or api)")
  174. }
  175. }
  176. baJob, err := scheduler.Every(1).Minute().Do(c.FlushAgentsAndBouncers, config.AgentsGC, config.BouncersGC)
  177. if err != nil {
  178. return nil, errors.Wrap(err, "while starting FlushAgentsAndBouncers scheduler")
  179. }
  180. baJob.SingletonMode()
  181. scheduler.StartAsync()
  182. return scheduler, nil
  183. }