database.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package metabase
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  8. )
  9. type Database struct {
  10. DBUrl string
  11. Model *Model
  12. Config *csconfig.DatabaseCfg
  13. Client *MBClient
  14. Details *Details
  15. // in case mysql host is 127.0.0.1 the ip address of mysql/pgsql host will be the docker gateway since metabase run in a container
  16. }
  17. type Details struct {
  18. Db string `json:"db"`
  19. Host string `json:"host"`
  20. Port int `json:"port"`
  21. Dbname string `json:"dbname"`
  22. User string `json:"user"`
  23. Password string `json:"password"`
  24. Ssl bool `json:"ssl"`
  25. AdditionalOptions interface{} `json:"additional-options"`
  26. TunnelEnabled bool `json:"tunnel-enabled"`
  27. }
  28. type Model struct {
  29. Engine string `json:"engine"`
  30. Name string `json:"name"`
  31. Details *Details `json:"details"`
  32. AutoRunQueries bool `json:"auto_run_queries"`
  33. IsFullSync bool `json:"is_full_sync"`
  34. IsOnDemand bool `json:"is_on_demand"`
  35. Schedules map[string]interface{} `json:"schedules"`
  36. }
  37. func NewDatabase(config *csconfig.DatabaseCfg, client *MBClient, remoteDBAddr string) (*Database, error) {
  38. var details *Details
  39. database := Database{}
  40. switch config.Type {
  41. case "mysql":
  42. return nil, fmt.Errorf("database '%s' is not supported yet", config.Type)
  43. case "sqlite":
  44. database.DBUrl = metabaseSQLiteDBURL
  45. localFolder := filepath.Dir(config.DbPath)
  46. // replace /var/lib/crowdsec/data/ with /metabase-data/
  47. dbPath := strings.Replace(config.DbPath, localFolder, containerSharedFolder, 1)
  48. details = &Details{
  49. Db: dbPath,
  50. }
  51. case "postgresql", "postgres", "pgsql":
  52. return nil, fmt.Errorf("database '%s' is not supported yet", config.Type)
  53. default:
  54. return nil, fmt.Errorf("database '%s' not supported", config.Type)
  55. }
  56. database.Details = details
  57. database.Client = client
  58. database.Config = config
  59. return &database, nil
  60. }
  61. func (d *Database) Update() error {
  62. success, errormsg, err := d.Client.Do("GET", routes[databaseEndpoint], nil)
  63. if err != nil {
  64. return err
  65. }
  66. if errormsg != nil {
  67. return fmt.Errorf("update sqlite db http error: %+v", errormsg)
  68. }
  69. data, err := json.Marshal(success)
  70. if err != nil {
  71. return fmt.Errorf("update sqlite db response (marshal): %w", err)
  72. }
  73. model := Model{}
  74. if err := json.Unmarshal(data, &model); err != nil {
  75. return fmt.Errorf("update sqlite db response (unmarshal): %w", err)
  76. }
  77. model.Details = d.Details
  78. _, errormsg, err = d.Client.Do("PUT", routes[databaseEndpoint], model)
  79. if err != nil {
  80. return err
  81. }
  82. if errormsg != nil {
  83. return fmt.Errorf("update sqlite db http error: %+v", errormsg)
  84. }
  85. return nil
  86. }