2020-07-16 14:05:03 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2020-11-30 09:37:17 +00:00
|
|
|
"context"
|
2020-07-16 14:05:03 +00:00
|
|
|
"fmt"
|
2021-02-02 13:15:13 +00:00
|
|
|
"os"
|
2020-07-16 14:05:03 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
2020-07-16 14:05:03 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/go-co-op/gocron"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/lib/pq"
|
2020-07-16 14:05:03 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2020-11-30 09:37:17 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-07-16 14:05:03 +00:00
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
type Client struct {
|
|
|
|
Ent *ent.Client
|
|
|
|
CTX context.Context
|
|
|
|
Log *log.Logger
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
|
|
|
|
var client *ent.Client
|
|
|
|
var err error
|
|
|
|
if config == nil {
|
|
|
|
return &Client{}, fmt.Errorf("DB config is empty")
|
|
|
|
}
|
|
|
|
switch config.Type {
|
2020-07-16 14:05:03 +00:00
|
|
|
case "sqlite":
|
2021-02-02 13:15:13 +00:00
|
|
|
|
|
|
|
/*if it's the first startup, we want to touch and chmod file*/
|
|
|
|
if _, err := os.Stat(config.DbPath); os.IsNotExist(err) {
|
|
|
|
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath)
|
|
|
|
}
|
|
|
|
} else { /*ensure file perms*/
|
2021-02-10 08:23:33 +00:00
|
|
|
if err := os.Chmod(config.DbPath, 0660); err != nil {
|
2021-02-02 13:15:13 +00:00
|
|
|
return &Client{}, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
client, err = ent.Open("sqlite3", fmt.Sprintf("file:%s?_busy_timeout=100000&_fk=1", config.DbPath))
|
|
|
|
if err != nil {
|
|
|
|
return &Client{}, fmt.Errorf("failed opening connection to sqlite: %v", err)
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
case "mysql":
|
2020-11-30 09:37:17 +00:00
|
|
|
client, err = ent.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", config.User, config.Password, config.Host, config.Port, config.DbName))
|
|
|
|
if err != nil {
|
|
|
|
return &Client{}, fmt.Errorf("failed opening connection to mysql: %v", err)
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
case "postgres", "postgresql":
|
2021-05-19 15:03:23 +00:00
|
|
|
client, err = ent.Open("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))
|
2020-11-30 09:37:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return &Client{}, fmt.Errorf("failed opening connection to postgres: %v", err)
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-11-30 09:37:17 +00:00
|
|
|
return &Client{}, fmt.Errorf("unknown database type")
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
/*The logger that will be used by db operations*/
|
|
|
|
clog := log.New()
|
|
|
|
if err := types.ConfigureLogger(clog); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "while configuring db logger")
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if config.LogLevel != nil {
|
|
|
|
clog.SetLevel(*config.LogLevel)
|
2021-03-12 14:10:56 +00:00
|
|
|
if *config.LogLevel >= log.DebugLevel {
|
|
|
|
clog.Debugf("Enabling request debug")
|
2020-11-30 09:37:17 +00:00
|
|
|
client = client.Debug()
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if err = client.Schema.Create(context.Background()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed creating schema resources: %v", err)
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
return &Client{Ent: client, CTX: context.Background(), Log: clog}, nil
|
|
|
|
}
|
2020-07-16 14:05:03 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Scheduler, error) {
|
|
|
|
maxItems := 0
|
|
|
|
maxAge := ""
|
|
|
|
if config.MaxItems != nil && *config.MaxItems <= 0 {
|
|
|
|
return nil, fmt.Errorf("max_items can't be zero or negative number")
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
if config.MaxItems != nil {
|
|
|
|
maxItems = *config.MaxItems
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
if config.MaxAge != nil && *config.MaxAge != "" {
|
|
|
|
maxAge = *config.MaxAge
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
// Init & Start cronjob every minute
|
|
|
|
scheduler := gocron.NewScheduler(time.UTC)
|
|
|
|
scheduler.Every(1).Minute().Do(c.FlushAlerts, maxAge, maxItems)
|
|
|
|
scheduler.StartAsync()
|
2020-07-16 14:05:03 +00:00
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
return scheduler, nil
|
2020-07-16 14:05:03 +00:00
|
|
|
}
|