From 5390b8ea4b121bc8949b885d1582bce9306a0c94 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:05:40 +0200 Subject: [PATCH] remove warning "maxopenconns is 0, default to 100" (#3129) * remove warning "maxopenconns is 0, default to 100" also don't store as pointer since value 0 is not useful * lint --- pkg/csconfig/api_test.go | 2 +- pkg/csconfig/config.go | 2 +- pkg/csconfig/database.go | 7 ++++--- pkg/csconfig/database_test.go | 11 ++++++----- pkg/database/database.go | 19 ++++++++----------- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/pkg/csconfig/api_test.go b/pkg/csconfig/api_test.go index 079936253a1..51a4c5ad602 100644 --- a/pkg/csconfig/api_test.go +++ b/pkg/csconfig/api_test.go @@ -191,7 +191,7 @@ func TestLoadAPIServer(t *testing.T) { DbConfig: &DatabaseCfg{ DbPath: "./testdata/test.db", Type: "sqlite", - MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS), + MaxOpenConns: DEFAULT_MAX_OPEN_CONNS, UseWal: ptr.Of(true), // autodetected DecisionBulkSize: defaultDecisionBulkSize, }, diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index ed71af4029a..3bbdf607187 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -158,7 +158,7 @@ func NewDefaultConfig() *Config { dbConfig := DatabaseCfg{ Type: "sqlite", DbPath: DefaultDataPath("crowdsec.db"), - MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS), + MaxOpenConns: DEFAULT_MAX_OPEN_CONNS, } globalCfg := Config{ diff --git a/pkg/csconfig/database.go b/pkg/csconfig/database.go index d30cc20a938..a24eb9e13c3 100644 --- a/pkg/csconfig/database.go +++ b/pkg/csconfig/database.go @@ -33,7 +33,7 @@ type DatabaseCfg struct { Type string `yaml:"type"` Flush *FlushDBCfg `yaml:"flush"` LogLevel *log.Level `yaml:"log_level"` - MaxOpenConns *int `yaml:"max_open_conns,omitempty"` + MaxOpenConns int `yaml:"max_open_conns,omitempty"` UseWal *bool `yaml:"use_wal,omitempty"` DecisionBulkSize int `yaml:"decision_bulk_size,omitempty"` } @@ -68,8 +68,8 @@ func (c *Config) LoadDBConfig(inCli bool) error { c.API.Server.DbConfig = c.DbConfig } - if c.DbConfig.MaxOpenConns == nil { - c.DbConfig.MaxOpenConns = ptr.Of(DEFAULT_MAX_OPEN_CONNS) + if c.DbConfig.MaxOpenConns == 0 { + c.DbConfig.MaxOpenConns = DEFAULT_MAX_OPEN_CONNS } if !inCli && c.DbConfig.Type == "sqlite" { @@ -134,6 +134,7 @@ func (d *DatabaseCfg) ConnectionString() string { } else { connString = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=True", d.User, d.Password, d.Host, d.Port, d.DbName) } + if d.Sslmode != "" { connString = fmt.Sprintf("%s&tls=%s", connString, d.Sslmode) } diff --git a/pkg/csconfig/database_test.go b/pkg/csconfig/database_test.go index 954b1c47fd7..4a1ef807f97 100644 --- a/pkg/csconfig/database_test.go +++ b/pkg/csconfig/database_test.go @@ -22,7 +22,7 @@ func TestLoadDBConfig(t *testing.T) { DbConfig: &DatabaseCfg{ Type: "sqlite", DbPath: "./testdata/test.db", - MaxOpenConns: ptr.Of(10), + MaxOpenConns: 10, }, Cscli: &CscliCfg{}, API: &APICfg{ @@ -30,10 +30,10 @@ func TestLoadDBConfig(t *testing.T) { }, }, expected: &DatabaseCfg{ - Type: "sqlite", - DbPath: "./testdata/test.db", - MaxOpenConns: ptr.Of(10), - UseWal: ptr.Of(true), + Type: "sqlite", + DbPath: "./testdata/test.db", + MaxOpenConns: 10, + UseWal: ptr.Of(true), DecisionBulkSize: defaultDecisionBulkSize, }, }, @@ -49,6 +49,7 @@ func TestLoadDBConfig(t *testing.T) { t.Run(tc.name, func(t *testing.T) { err := tc.input.LoadDBConfig(false) cstest.RequireErrorContains(t, err, tc.expectedErr) + if tc.expectedErr != "" { return } diff --git a/pkg/database/database.go b/pkg/database/database.go index 6f392c46d21..e513459199f 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -14,8 +14,6 @@ import ( _ "github.com/mattn/go-sqlite3" log "github.com/sirupsen/logrus" - "github.com/crowdsecurity/go-cs-lib/ptr" - "github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/database/ent" "github.com/crowdsecurity/crowdsec/pkg/types" @@ -37,12 +35,11 @@ func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig. 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) + if config.MaxOpenConns == 0 { + config.MaxOpenConns = csconfig.DEFAULT_MAX_OPEN_CONNS } - db.SetMaxOpenConns(*config.MaxOpenConns) + db.SetMaxOpenConns(config.MaxOpenConns) drv := entsql.OpenDB(dbdialect, db) return drv, nil @@ -74,7 +71,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro 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) { + if _, err = os.Stat(config.DbPath); os.IsNotExist(err) { f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0o600) if err != nil { return nil, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err) @@ -85,14 +82,14 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro } } // 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) + if err = setFilePerm(config.DbPath, 0o640); err != nil { + return nil, fmt.Errorf("unable to set perms on %s: %w", config.DbPath, err) } } drv, err := getEntDriver(typ, dia, config.ConnectionString(), config) if err != nil { - return nil, fmt.Errorf("failed opening connection to %s: %v", config.Type, err) + return nil, fmt.Errorf("failed opening connection to %s: %w", config.Type, err) } client = ent.NewClient(ent.Driver(drv), entOpt) @@ -104,7 +101,7 @@ func NewClient(ctx context.Context, config *csconfig.DatabaseCfg) (*Client, erro } if err = client.Schema.Create(ctx); err != nil { - return nil, fmt.Errorf("failed creating schema resources: %v", err) + return nil, fmt.Errorf("failed creating schema resources: %w", err) } return &Client{