2020-11-30 09:37:17 +00:00
package csconfig
2021-03-24 17:16:17 +00:00
import (
"fmt"
2022-06-08 14:05:52 +00:00
"time"
2021-03-24 17:16:17 +00:00
2022-02-17 16:52:04 +00:00
"github.com/crowdsecurity/crowdsec/pkg/types"
2021-03-24 17:16:17 +00:00
log "github.com/sirupsen/logrus"
)
2020-11-30 09:37:17 +00:00
2022-02-17 16:52:04 +00:00
var DEFAULT_MAX_OPEN_CONNS = 100
2020-11-30 09:37:17 +00:00
type DatabaseCfg struct {
2022-02-17 16:52:04 +00:00
User string ` yaml:"user" `
Password string ` yaml:"password" `
DbName string ` yaml:"db_name" `
Sslmode string ` yaml:"sslmode" `
Host string ` yaml:"host" `
Port int ` yaml:"port" `
DbPath string ` yaml:"db_path" `
Type string ` yaml:"type" `
Flush * FlushDBCfg ` yaml:"flush" `
LogLevel * log . Level ` yaml:"log_level" `
MaxOpenConns * int ` yaml:"max_open_conns,omitempty" `
2022-09-14 13:09:54 +00:00
UseWal * bool ` yaml:"use_wal,omitempty" `
2020-11-30 09:37:17 +00:00
}
2022-06-08 14:05:52 +00:00
type AuthGCCfg struct {
Cert * string ` yaml:"cert,omitempty" `
CertDuration * time . Duration
Api * string ` yaml:"api_key,omitempty" `
ApiDuration * time . Duration
LoginPassword * string ` yaml:"login_password,omitempty" `
LoginPasswordDuration * time . Duration
}
2020-11-30 09:37:17 +00:00
type FlushDBCfg struct {
2022-06-08 14:05:52 +00:00
MaxItems * int ` yaml:"max_items,omitempty" `
MaxAge * string ` yaml:"max_age,omitempty" `
BouncersGC * AuthGCCfg ` yaml:"bouncers_autodelete,omitempty" `
AgentsGC * AuthGCCfg ` yaml:"agents_autodelete,omitempty" `
2020-11-30 09:37:17 +00:00
}
2021-03-24 17:16:17 +00:00
func ( c * Config ) LoadDBConfig ( ) error {
if c . DbConfig == nil {
return fmt . Errorf ( "no database configuration provided" )
}
if c . Cscli != nil {
c . Cscli . DbConfig = c . DbConfig
}
if c . API != nil && c . API . Server != nil {
c . API . Server . DbConfig = c . DbConfig
}
2022-02-17 16:52:04 +00:00
if c . DbConfig . MaxOpenConns == nil {
c . DbConfig . MaxOpenConns = types . IntPtr ( DEFAULT_MAX_OPEN_CONNS )
}
2022-11-07 15:36:39 +00:00
if c . DbConfig . Type == "sqlite" {
if c . DbConfig . UseWal == nil {
2022-12-06 14:55:27 +00:00
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." )
2022-11-07 15:36:39 +00:00
}
}
2021-03-24 17:16:17 +00:00
return nil
}