database.go 5.5 KB

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