Skip to content

Commit

Permalink
cscli: remove global dbClient (#2985)
Browse files Browse the repository at this point in the history
* cscli: remove global dbClient

* lint (whitespace, errors)
  • Loading branch information
mmetc authored May 6, 2024
1 parent 659feec commit a2dcc0e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 44 deletions.
2 changes: 0 additions & 2 deletions cmd/crowdsec-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ import (
"github.com/crowdsecurity/go-cs-lib/trace"

"github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/database"
"github.com/crowdsecurity/crowdsec/pkg/fflag"
)

var (
ConfigFilePath string
csConfig *csconfig.Config
dbClient *database.Client
)

type configGetter func() *csconfig.Config
Expand Down
14 changes: 7 additions & 7 deletions cmd/crowdsec-cli/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func (cli *cliPapi) NewStatusCmd() *cobra.Command {
RunE: func(_ *cobra.Command, _ []string) error {
var err error
cfg := cli.cfg()
dbClient, err = database.NewClient(cfg.DbConfig)
db, err := database.NewClient(cfg.DbConfig)
if err != nil {
return fmt.Errorf("unable to initialize database client: %w", err)
}

apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
if err != nil {
return fmt.Errorf("unable to initialize API client: %w", err)
}

papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
papi, err := apiserver.NewPAPI(apic, db, cfg.API.Server.ConsoleConfig, log.GetLevel())
if err != nil {
return fmt.Errorf("unable to initialize PAPI client: %w", err)
}
Expand All @@ -82,7 +82,7 @@ func (cli *cliPapi) NewStatusCmd() *cobra.Command {
return fmt.Errorf("unable to get PAPI permissions: %w", err)
}
var lastTimestampStr *string
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
lastTimestampStr, err = db.GetConfigItem(apiserver.PapiPullKey)
if err != nil {
lastTimestampStr = ptr.Of("never")
}
Expand Down Expand Up @@ -113,19 +113,19 @@ func (cli *cliPapi) NewSyncCmd() *cobra.Command {
cfg := cli.cfg()
t := tomb.Tomb{}

dbClient, err = database.NewClient(cfg.DbConfig)
db, err := database.NewClient(cfg.DbConfig)
if err != nil {
return fmt.Errorf("unable to initialize database client: %w", err)
}

apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
if err != nil {
return fmt.Errorf("unable to initialize API client: %w", err)
}

t.Go(apic.Push)

papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
papi, err := apiserver.NewPAPI(apic, db, cfg.API.Server.ConsoleConfig, log.GetLevel())
if err != nil {
return fmt.Errorf("unable to initialize PAPI client: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/crowdsec-cli/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ cscli support dump -f /tmp/crowdsec-support.zip
outFile = "/tmp/crowdsec-support.zip"
}

dbClient, err = database.NewClient(csConfig.DbConfig)
dbClient, err := database.NewClient(csConfig.DbConfig)
if err != nil {
log.Warnf("Could not connect to database: %s", err)
skipDB = true
Expand Down
46 changes: 29 additions & 17 deletions pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,72 +35,84 @@ func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig.
if err != nil {
return nil, err
}

if config.MaxOpenConns == nil {
log.Warningf("MaxOpenConns is 0, defaulting to %d", csconfig.DEFAULT_MAX_OPEN_CONNS)
config.MaxOpenConns = ptr.Of(csconfig.DEFAULT_MAX_OPEN_CONNS)
}

db.SetMaxOpenConns(*config.MaxOpenConns)
drv := entsql.OpenDB(dbdialect, db)

return drv, nil
}

func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
var client *ent.Client
var err error

if config == nil {
return &Client{}, errors.New("DB config is empty")
return nil, errors.New("DB config is empty")
}
/*The logger that will be used by db operations*/
clog := log.New()
if err := types.ConfigureLogger(clog); err != nil {
return nil, fmt.Errorf("while configuring db logger: %w", err)
}

if config.LogLevel != nil {
clog.SetLevel(*config.LogLevel)
}
entLogger := clog.WithField("context", "ent")

entLogger := clog.WithField("context", "ent")
entOpt := ent.Log(entLogger.Debug)

typ, dia, err := config.ConnectionDialect()
if err != nil {
return &Client{}, err //unsupported database caught here
return nil, err // unsupported database caught here
}

if config.Type == "sqlite" {
/*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)
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return &Client{}, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
}

if err := f.Close(); err != nil {
return &Client{}, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err)
}
}
//Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)
if err := setFilePerm(config.DbPath, 0640); err != nil {
return &Client{}, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
// Always try to set permissions to simplify a bit the code for windows (as the permissions set by OpenFile will be garbage)
if err := setFilePerm(config.DbPath, 0o640); err != nil {
return nil, fmt.Errorf("unable to set perms on %s: %v", config.DbPath, err)
}
}

drv, err := getEntDriver(typ, dia, config.ConnectionString(), config)
if err != nil {
return &Client{}, fmt.Errorf("failed opening connection to %s: %v", config.Type, err)
return nil, fmt.Errorf("failed opening connection to %s: %v", config.Type, err)
}

client = ent.NewClient(ent.Driver(drv), entOpt)

if config.LogLevel != nil && *config.LogLevel >= log.DebugLevel {
clog.Debugf("Enabling request debug")

client = client.Debug()
}

if err = client.Schema.Create(context.Background()); err != nil {
return nil, fmt.Errorf("failed creating schema resources: %v", err)
}

return &Client{
Ent: client,
CTX: context.Background(),
Log: clog,
CanFlush: true,
Type: config.Type,
WalMode: config.UseWal,
Ent: client,
CTX: context.Background(),
Log: clog,
CanFlush: true,
Type: config.Type,
WalMode: config.UseWal,
decisionBulkSize: config.DecisionBulkSize,
}, nil
}
6 changes: 3 additions & 3 deletions pkg/leakybucket/manager_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func DumpBucketsStateAt(deadline time.Time, outputdir string, buckets *Buckets)
defer buckets.wgDumpState.Done()

if outputdir == "" {
return "", fmt.Errorf("empty output dir for dump bucket state")
return "", errors.New("empty output dir for dump bucket state")
}
tmpFd, err := os.CreateTemp(os.TempDir(), "crowdsec-buckets-dump-")
if err != nil {
Expand Down Expand Up @@ -132,11 +132,11 @@ func DumpBucketsStateAt(deadline time.Time, outputdir string, buckets *Buckets)
})
bbuckets, err := json.MarshalIndent(serialized, "", " ")
if err != nil {
return "", fmt.Errorf("Failed to unmarshal buckets : %s", err)
return "", fmt.Errorf("failed to unmarshal buckets: %s", err)
}
size, err := tmpFd.Write(bbuckets)
if err != nil {
return "", fmt.Errorf("failed to write temp file : %s", err)
return "", fmt.Errorf("failed to write temp file: %s", err)
}
log.Infof("Serialized %d live buckets (+%d expired) in %d bytes to %s", len(serialized), discard, size, tmpFd.Name())
serialized = nil
Expand Down
Loading

0 comments on commit a2dcc0e

Please sign in to comment.