diff --git a/.github/codecov.yml b/.github/codecov.yml index b902d548a..5f721427d 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -1,5 +1,10 @@ +# we measure coverage but don't enforce it +# https://docs.codecov.com/docs/codecov-yaml coverage: status: + patch: + default: + target: 0% project: default: target: 0% diff --git a/cmd/crowdsec-cli/papi.go b/cmd/crowdsec-cli/papi.go index 176b81370..d38da0df9 100644 --- a/cmd/crowdsec-cli/papi.go +++ b/cmd/crowdsec-cli/papi.go @@ -1,15 +1,17 @@ package main import ( + "fmt" "time" - "github.com/crowdsecurity/crowdsec/pkg/apiserver" - "github.com/crowdsecurity/crowdsec/pkg/database" - "github.com/crowdsecurity/crowdsec/pkg/types" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "gopkg.in/tomb.v2" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + + "github.com/crowdsecurity/crowdsec/pkg/apiserver" + "github.com/crowdsecurity/crowdsec/pkg/database" ) func NewPapiCmd() *cobra.Command { @@ -20,7 +22,7 @@ func NewPapiCmd() *cobra.Command { DisableAutoGenTag: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI { - return errors.Wrap(err, "Local API is disabled, please run this command on the local API machine") + return fmt.Errorf("Local API is disabled, please run this command on the local API machine: %w", err) } if csConfig.API.Server.OnlineClient == nil { log.Fatalf("no configuration for Central API in '%s'", *csConfig.FilePath) @@ -71,7 +73,7 @@ func NewPapiStatusCmd() *cobra.Command { var lastTimestampStr *string lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey) if err != nil { - lastTimestampStr = types.StrPtr("never") + lastTimestampStr = ptr.Of("never") } log.Infof("You can successfully interact with Polling API (PAPI)") log.Infof("Console plan: %s", perms.Plan) diff --git a/pkg/apiserver/papi_cmd.go b/pkg/apiserver/papi_cmd.go index 3635bc207..4cb9603b7 100644 --- a/pkg/apiserver/papi_cmd.go +++ b/pkg/apiserver/papi_cmd.go @@ -5,11 +5,13 @@ import ( "fmt" "time" + log "github.com/sirupsen/logrus" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + "github.com/crowdsecurity/crowdsec/pkg/apiclient" "github.com/crowdsecurity/crowdsec/pkg/models" "github.com/crowdsecurity/crowdsec/pkg/types" - "github.com/pkg/errors" - log "github.com/sirupsen/logrus" ) type deleteDecisions struct { @@ -75,7 +77,7 @@ func AlertCmd(message *Message, p *Papi, sync bool) error { alert := &models.Alert{} if err := json.Unmarshal(data, alert); err != nil { - return errors.Wrapf(err, "message for '%s' contains bad alert format", message.Header.OperationType) + return fmt.Errorf("message for '%s' contains bad alert format: %w", message.Header.OperationType, err) } log.Infof("Received order %s from PAPI (%d decisions)", alert.UUID, len(alert.Decisions)) @@ -83,20 +85,20 @@ func AlertCmd(message *Message, p *Papi, sync bool) error { /*Fix the alert with missing mandatory items*/ if alert.StartAt == nil || *alert.StartAt == "" { log.Warnf("Alert %d has no StartAt, setting it to now", alert.ID) - alert.StartAt = types.StrPtr(time.Now().UTC().Format(time.RFC3339)) + alert.StartAt = ptr.Of(time.Now().UTC().Format(time.RFC3339)) } if alert.StopAt == nil || *alert.StopAt == "" { log.Warnf("Alert %d has no StopAt, setting it to now", alert.ID) - alert.StopAt = types.StrPtr(time.Now().UTC().Format(time.RFC3339)) + alert.StopAt = ptr.Of(time.Now().UTC().Format(time.RFC3339)) } - alert.EventsCount = types.Int32Ptr(0) - alert.Capacity = types.Int32Ptr(0) - alert.Leakspeed = types.StrPtr("") - alert.Simulated = types.BoolPtr(false) - alert.ScenarioHash = types.StrPtr("") - alert.ScenarioVersion = types.StrPtr("") - alert.Message = types.StrPtr("") - alert.Scenario = types.StrPtr("") + alert.EventsCount = ptr.Of(int32(0)) + alert.Capacity = ptr.Of(int32(0)) + alert.Leakspeed = ptr.Of("") + alert.Simulated = ptr.Of(false) + alert.ScenarioHash = ptr.Of("") + alert.ScenarioVersion = ptr.Of("") + alert.Message = ptr.Of("") + alert.Scenario = ptr.Of("") alert.Source = &models.Source{} //if we're setting Source.Scope to types.ConsoleOrigin, it messes up the alert's value @@ -105,7 +107,7 @@ func AlertCmd(message *Message, p *Papi, sync bool) error { alert.Source.Value = alert.Decisions[0].Value } else { log.Warningf("No decision found in alert for Polling API (%s : %s)", message.Header.Source.User, message.Header.Message) - alert.Source.Scope = types.StrPtr(types.ConsoleOrigin) + alert.Source.Scope = ptr.Of(types.ConsoleOrigin) alert.Source.Value = &message.Header.Source.User } alert.Scenario = &message.Header.Message diff --git a/pkg/csconfig/api.go b/pkg/csconfig/api.go index bdeb4cca3..06d6a9712 100644 --- a/pkg/csconfig/api.go +++ b/pkg/csconfig/api.go @@ -9,14 +9,13 @@ import ( "strings" "time" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" "github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch" "github.com/crowdsecurity/crowdsec/pkg/apiclient" - "github.com/crowdsecurity/crowdsec/pkg/types" ) type APICfg struct { @@ -83,11 +82,11 @@ func (o *OnlineApiClientCfg) Load() error { o.Credentials = new(ApiCredentialsCfg) fcontent, err := os.ReadFile(o.CredentialsFilePath) if err != nil { - return errors.Wrapf(err, "failed to read api server credentials configuration file '%s'", o.CredentialsFilePath) + return fmt.Errorf("failed to read api server credentials configuration file '%s': %w", o.CredentialsFilePath, err) } err = yaml.UnmarshalStrict(fcontent, o.Credentials) if err != nil { - return errors.Wrapf(err, "failed unmarshaling api server credentials configuration file '%s'", o.CredentialsFilePath) + return fmt.Errorf("failed unmarshaling api server credentials configuration file '%s': %w", o.CredentialsFilePath, err) } if o.Credentials.Login == "" || o.Credentials.Password == "" || o.Credentials.URL == "" { log.Warningf("can't load CAPI credentials from '%s' (missing field)", o.CredentialsFilePath) @@ -105,7 +104,7 @@ func (l *LocalApiClientCfg) Load() error { } err = yaml.UnmarshalStrict(fcontent, &l.Credentials) if err != nil { - return errors.Wrapf(err, "failed unmarshaling api client credential configuration file '%s'", l.CredentialsFilePath) + return fmt.Errorf("failed unmarshaling api client credential configuration file '%s': %w", l.CredentialsFilePath, err) } if l.Credentials == nil || l.Credentials.URL == "" { return fmt.Errorf("no credentials or URL found in api client configuration '%s'", l.CredentialsFilePath) @@ -130,7 +129,7 @@ func (l *LocalApiClientCfg) Load() error { if l.Credentials.CACertPath != "" { caCert, err := os.ReadFile(l.Credentials.CACertPath) if err != nil { - return errors.Wrapf(err, "failed to load cacert") + return fmt.Errorf("failed to load cacert: %w", err) } caCertPool, err := x509.SystemCertPool() @@ -147,7 +146,7 @@ func (l *LocalApiClientCfg) Load() error { if l.Credentials.CertPath != "" && l.Credentials.KeyPath != "" { cert, err := tls.LoadX509KeyPair(l.Credentials.CertPath, l.Credentials.KeyPath) if err != nil { - return errors.Wrapf(err, "failed to load api client certificate") + return fmt.Errorf("failed to load api client certificate: %w", err) } apiclient.Cert = &cert @@ -251,7 +250,7 @@ func (c *Config) LoadAPIServer() error { if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" { if err := c.API.Server.OnlineClient.Load(); err != nil { - return errors.Wrap(err, "loading online client credentials") + return fmt.Errorf("loading online client credentials: %w", err) } } if c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil { @@ -271,7 +270,7 @@ func (c *Config) LoadAPIServer() error { if c.API.Server.Enable == nil { // if the option is not present, it is enabled by default - c.API.Server.Enable = types.BoolPtr(true) + c.API.Server.Enable = ptr.Of(true) } if !*c.API.Server.Enable { @@ -300,18 +299,18 @@ func (c *Config) LoadAPIServer() error { c.API.Server.UseForwardedForHeaders = true } if err := c.API.Server.LoadProfiles(); err != nil { - return errors.Wrap(err, "while loading profiles for LAPI") + return fmt.Errorf("while loading profiles for LAPI: %w", err) } if c.API.Server.ConsoleConfigPath == "" { c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath } if err := c.API.Server.LoadConsoleConfig(); err != nil { - return errors.Wrap(err, "while loading console options") + return fmt.Errorf("while loading console options: %w", err) } if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" { if err := c.API.Server.OnlineClient.Load(); err != nil { - return errors.Wrap(err, "loading online client credentials") + return fmt.Errorf("loading online client credentials: %w", err) } } if c.API.Server.OnlineClient == nil || c.API.Server.OnlineClient.Credentials == nil { @@ -320,7 +319,7 @@ func (c *Config) LoadAPIServer() error { if c.API.CTI != nil { if err := c.API.CTI.Load(); err != nil { - return errors.Wrap(err, "loading CTI configuration") + return fmt.Errorf("loading CTI configuration: %w", err) } } diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index b94a2e975..d4b4aa4af 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -5,14 +5,12 @@ import ( "os" "path/filepath" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" - "github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch" "github.com/crowdsecurity/go-cs-lib/pkg/csstring" - - "github.com/crowdsecurity/crowdsec/pkg/types" + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + "github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch" ) // defaultConfigDir is the base path to all configuration files, to be overridden in the Makefile */ @@ -42,7 +40,7 @@ type Config struct { func (c *Config) Dump() error { out, err := yaml.Marshal(c) if err != nil { - return errors.Wrap(err, "failed marshaling config") + return fmt.Errorf("failed marshaling config: %w", err) } fmt.Printf("%s", string(out)) return nil @@ -65,7 +63,7 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool err = yaml.UnmarshalStrict([]byte(configData), &cfg) if err != nil { // this is actually the "merged" yaml - return nil, "", errors.Wrap(err, configFile) + return nil, "", fmt.Errorf("%s: %w", configFile, err) } return &cfg, configData, nil } @@ -113,14 +111,14 @@ func NewDefaultConfig() *Config { }, }, CTI: &CTICfg{ - Enabled: types.BoolPtr(false), + Enabled: ptr.Of(false), }, } dbConfig := DatabaseCfg{ Type: "sqlite", DbPath: DefaultDataPath("crowdsec.db"), - MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS), + MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS), } globalCfg := Config{ diff --git a/pkg/csconfig/console.go b/pkg/csconfig/console.go index 18d175365..5adf6ca37 100644 --- a/pkg/csconfig/console.go +++ b/pkg/csconfig/console.go @@ -4,11 +4,12 @@ import ( "fmt" "os" - "github.com/crowdsecurity/crowdsec/pkg/fflag" - "github.com/crowdsecurity/crowdsec/pkg/types" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + + "github.com/crowdsecurity/crowdsec/pkg/fflag" ) const ( @@ -35,11 +36,11 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error { c.ConsoleConfig = &ConsoleConfig{} if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) { log.Debugf("no console configuration to load") - c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true) - c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true) - c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false) - c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false) - c.ConsoleConfig.ShareContext = types.BoolPtr(false) + c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true) + c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true) + c.ConsoleConfig.ShareManualDecisions = ptr.Of(false) + c.ConsoleConfig.ConsoleManagement = ptr.Of(false) + c.ConsoleConfig.ShareContext = ptr.Of(false) return nil } @@ -54,27 +55,27 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error { if c.ConsoleConfig.ShareCustomScenarios == nil { log.Debugf("no share_custom scenarios found, setting to true") - c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true) + c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true) } if c.ConsoleConfig.ShareTaintedScenarios == nil { log.Debugf("no share_tainted scenarios found, setting to true") - c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true) + c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true) } if c.ConsoleConfig.ShareManualDecisions == nil { log.Debugf("no share_manual scenarios found, setting to false") - c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false) + c.ConsoleConfig.ShareManualDecisions = ptr.Of(false) } if !fflag.PapiClient.IsEnabled() { - c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false) + c.ConsoleConfig.ConsoleManagement = ptr.Of(false) } else if c.ConsoleConfig.ConsoleManagement == nil { log.Debugf("no console_management found, setting to false") - c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false) + c.ConsoleConfig.ConsoleManagement = ptr.Of(false) } if c.ConsoleConfig.ShareContext == nil { log.Debugf("no 'context' found, setting to false") - c.ConsoleConfig.ShareContext = types.BoolPtr(false) + c.ConsoleConfig.ShareContext = ptr.Of(false) } log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath) @@ -87,7 +88,7 @@ func (c *LocalApiServerCfg) DumpConsoleConfig() error { var err error if out, err = yaml.Marshal(c.ConsoleConfig); err != nil { - return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", c.ConsoleConfigPath) + return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleConfigPath, err) } if c.ConsoleConfigPath == "" { c.ConsoleConfigPath = DefaultConsoleConfigFilePath @@ -96,7 +97,7 @@ func (c *LocalApiServerCfg) DumpConsoleConfig() error { } if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil { - return errors.Wrapf(err, "while dumping console config to %s", c.ConsoleConfigPath) + return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleConfigPath, err) } return nil diff --git a/pkg/csconfig/crowdsec_service.go b/pkg/csconfig/crowdsec_service.go index 8bcc13852..2642603cf 100644 --- a/pkg/csconfig/crowdsec_service.go +++ b/pkg/csconfig/crowdsec_service.go @@ -5,11 +5,10 @@ import ( "os" "path/filepath" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" - "github.com/crowdsecurity/crowdsec/pkg/types" + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" ) // CrowdsecServiceCfg contains the location of parsers/scenarios/... and acquisition files @@ -48,7 +47,7 @@ func (c *Config) LoadCrowdsec() error { if c.Crowdsec.Enable == nil { // if the option is not present, it is enabled by default - c.Crowdsec.Enable = types.BoolPtr(true) + c.Crowdsec.Enable = ptr.Of(true) } if !*c.Crowdsec.Enable { @@ -72,20 +71,20 @@ func (c *Config) LoadCrowdsec() error { if c.Crowdsec.AcquisitionDirPath != "" { c.Crowdsec.AcquisitionDirPath, err = filepath.Abs(c.Crowdsec.AcquisitionDirPath) if err != nil { - return errors.Wrapf(err, "can't get absolute path of '%s'", c.Crowdsec.AcquisitionDirPath) + return fmt.Errorf("can't get absolute path of '%s': %w", c.Crowdsec.AcquisitionDirPath, err) } var files []string files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yaml") if err != nil { - return errors.Wrap(err, "while globbing acquis_dir") + return fmt.Errorf("while globbing acquis_dir: %w", err) } c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...) files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yml") if err != nil { - return errors.Wrap(err, "while globbing acquis_dir") + return fmt.Errorf("while globbing acquis_dir: %w", err) } c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...) } @@ -99,7 +98,7 @@ func (c *Config) LoadCrowdsec() error { } if err = c.LoadSimulation(); err != nil { - return errors.Wrap(err, "load error (simulation)") + return fmt.Errorf("load error (simulation): %w", err) } c.Crowdsec.ConfigDir = c.ConfigPaths.ConfigDir @@ -129,7 +128,7 @@ func (c *Config) LoadCrowdsec() error { } *k, err = filepath.Abs(*k) if err != nil { - return errors.Wrapf(err, "failed to get absolute path of '%s'", *k) + return fmt.Errorf("failed to get absolute path of '%s': %w", *k, err) } } @@ -137,7 +136,7 @@ func (c *Config) LoadCrowdsec() error { for i, file := range c.Crowdsec.AcquisitionFiles { f, err := filepath.Abs(file) if err != nil { - return errors.Wrapf(err, "failed to get absolute path of '%s'", file) + return fmt.Errorf("failed to get absolute path of '%s': %w", file, err) } c.Crowdsec.AcquisitionFiles[i] = f } @@ -147,7 +146,7 @@ func (c *Config) LoadCrowdsec() error { } if err := c.LoadHub(); err != nil { - return errors.Wrap(err, "while loading hub") + return fmt.Errorf("while loading hub: %w", err) } c.Crowdsec.ContextToSend = make(map[string][]string, 0) @@ -186,11 +185,11 @@ func (c *CrowdsecServiceCfg) DumpContextConfigFile() error { var err error if out, err = yaml.Marshal(c.ContextToSend); err != nil { - return errors.Wrapf(err, "while marshaling ConsoleConfig (for %s)", c.ConsoleContextPath) + return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleContextPath, err) } if err := os.WriteFile(c.ConsoleContextPath, out, 0600); err != nil { - return errors.Wrapf(err, "while dumping console config to %s", c.ConsoleContextPath) + return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleContextPath, err) } log.Infof("%s file saved", c.ConsoleContextPath) diff --git a/pkg/csconfig/database.go b/pkg/csconfig/database.go index e7f67d474..0f8866824 100644 --- a/pkg/csconfig/database.go +++ b/pkg/csconfig/database.go @@ -5,8 +5,9 @@ import ( "time" "entgo.io/ent/dialect" - "github.com/crowdsecurity/crowdsec/pkg/types" log "github.com/sirupsen/logrus" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" ) var DEFAULT_MAX_OPEN_CONNS = 100 @@ -56,7 +57,7 @@ func (c *Config) LoadDBConfig() error { } if c.DbConfig.MaxOpenConns == nil { - c.DbConfig.MaxOpenConns = types.IntPtr(DEFAULT_MAX_OPEN_CONNS) + c.DbConfig.MaxOpenConns = ptr.Of(DEFAULT_MAX_OPEN_CONNS) } if c.DbConfig.Type == "sqlite" { diff --git a/pkg/csconfig/database_test.go b/pkg/csconfig/database_test.go index b029f3883..d33c54424 100644 --- a/pkg/csconfig/database_test.go +++ b/pkg/csconfig/database_test.go @@ -5,8 +5,9 @@ import ( "strings" "testing" - "github.com/crowdsecurity/crowdsec/pkg/types" "github.com/stretchr/testify/assert" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" ) func TestLoadDBConfig(t *testing.T) { @@ -22,7 +23,7 @@ func TestLoadDBConfig(t *testing.T) { DbConfig: &DatabaseCfg{ Type: "sqlite", DbPath: "./tests/test.db", - MaxOpenConns: types.IntPtr(10), + MaxOpenConns: ptr.Of(10), }, Cscli: &CscliCfg{}, API: &APICfg{ @@ -32,7 +33,7 @@ func TestLoadDBConfig(t *testing.T) { expectedResult: &DatabaseCfg{ Type: "sqlite", DbPath: "./tests/test.db", - MaxOpenConns: types.IntPtr(10), + MaxOpenConns: ptr.Of(10), }, }, { diff --git a/pkg/cticlient/client_test.go b/pkg/cticlient/client_test.go index a487f1098..a8f22e094 100644 --- a/pkg/cticlient/client_test.go +++ b/pkg/cticlient/client_test.go @@ -11,9 +11,9 @@ import ( "testing" log "github.com/sirupsen/logrus" - - "github.com/crowdsecurity/crowdsec/pkg/types" "github.com/stretchr/testify/assert" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" ) const validApiKey = "my-api-key" @@ -202,12 +202,12 @@ func TestFireOk(t *testing.T) { assert.Equal(t, len(data.Items), 3) assert.Equal(t, data.Items[0].Ip, "1.2.3.4") //page 1 is the default - data, err = cticlient.Fire(FireParams{Page: types.IntPtr(1)}) + data, err = cticlient.Fire(FireParams{Page: ptr.Of(1)}) assert.Equal(t, err, nil) assert.Equal(t, len(data.Items), 3) assert.Equal(t, data.Items[0].Ip, "1.2.3.4") //page 2 - data, err = cticlient.Fire(FireParams{Page: types.IntPtr(2)}) + data, err = cticlient.Fire(FireParams{Page: ptr.Of(2)}) assert.Equal(t, err, nil) assert.Equal(t, len(data.Items), 3) assert.Equal(t, data.Items[0].Ip, "4.2.3.4") @@ -250,7 +250,7 @@ func TestSmokeInfoValidIP(t *testing.T) { } assert.Equal(t, "1.1.1.1", resp.Ip) - assert.Equal(t, types.StrPtr("1.1.1.0/24"), resp.IpRange) + assert.Equal(t, ptr.Of("1.1.1.0/24"), resp.IpRange) } func TestSmokeUnknownIP(t *testing.T) { diff --git a/pkg/cticlient/types_test.go b/pkg/cticlient/types_test.go index 046d28692..1ec58cc78 100644 --- a/pkg/cticlient/types_test.go +++ b/pkg/cticlient/types_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/crowdsecurity/crowdsec/pkg/types" + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" ) //func (c *SmokeItem) GetAttackDetails() []string { @@ -16,16 +16,16 @@ func getSampleSmokeItem() SmokeItem { emptyItem := SmokeItem{ IpRangeScore: 2.0, Ip: "1.2.3.4", - IpRange: types.StrPtr("1.2.3.0/24"), - AsName: types.StrPtr("AS1234"), - AsNum: types.IntPtr(1234), + IpRange: ptr.Of("1.2.3.0/24"), + AsName: ptr.Of("AS1234"), + AsNum: ptr.Of(1234), Location: CTILocationInfo{ - Country: types.StrPtr("FR"), - City: types.StrPtr("Paris"), + Country: ptr.Of("FR"), + City: ptr.Of("Paris"), Latitude: &lat, Longitude: &long, }, - ReverseDNS: types.StrPtr("foo.bar.com"), + ReverseDNS: ptr.Of("foo.bar.com"), Behaviors: []*CTIBehavior{ { Name: "ssh:bruteforce", @@ -34,8 +34,8 @@ func getSampleSmokeItem() SmokeItem { }, }, History: CTIHistory{ - FirstSeen: types.StrPtr("2022-12-05T17:45:00+00:00"), - LastSeen: types.StrPtr("2022-12-06T19:15:00+00:00"), + FirstSeen: ptr.Of("2022-12-05T17:45:00+00:00"), + LastSeen: ptr.Of("2022-12-06T19:15:00+00:00"), FullAge: 3, DaysAge: 1, }, @@ -56,7 +56,7 @@ func getSampleSmokeItem() SmokeItem { "GB": 14, "US": 14, }, - BackgroundNoiseScore: types.IntPtr(3), + BackgroundNoiseScore: ptr.Of(3), Scores: CTIScores{ Overall: CTIScore{ Aggressiveness: 2, diff --git a/pkg/database/database.go b/pkg/database/database.go index 3c0828705..46de4e73a 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -8,15 +8,17 @@ import ( "time" entsql "entgo.io/ent/dialect/sql" - "github.com/crowdsecurity/crowdsec/pkg/csconfig" - "github.com/crowdsecurity/crowdsec/pkg/database/ent" - "github.com/crowdsecurity/crowdsec/pkg/types" "github.com/go-co-op/gocron" _ "github.com/go-sql-driver/mysql" _ "github.com/jackc/pgx/v4/stdlib" _ "github.com/mattn/go-sqlite3" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + + "github.com/crowdsecurity/crowdsec/pkg/csconfig" + "github.com/crowdsecurity/crowdsec/pkg/database/ent" + "github.com/crowdsecurity/crowdsec/pkg/types" ) type Client struct { @@ -35,7 +37,7 @@ func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig. } if config.MaxOpenConns == nil { log.Warningf("MaxOpenConns is 0, defaulting to %d", csconfig.DEFAULT_MAX_OPEN_CONNS) - config.MaxOpenConns = types.IntPtr(csconfig.DEFAULT_MAX_OPEN_CONNS) + config.MaxOpenConns = ptr.Of(csconfig.DEFAULT_MAX_OPEN_CONNS) } db.SetMaxOpenConns(*config.MaxOpenConns) drv := entsql.OpenDB(dbdialect, db) @@ -51,7 +53,7 @@ func NewClient(config *csconfig.DatabaseCfg) (*Client, error) { /*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") + return nil, fmt.Errorf("while configuring db logger: %w", err) } if config.LogLevel != nil { clog.SetLevel(*config.LogLevel) @@ -68,10 +70,10 @@ func NewClient(config *csconfig.DatabaseCfg) (*Client, error) { 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) + return &Client{}, fmt.Errorf("failed to create SQLite database file %q: %w", config.DbPath, err) } if err := f.Close(); err != nil { - return &Client{}, errors.Wrapf(err, "failed to create SQLite database file %q", config.DbPath) + return &Client{}, 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) @@ -111,7 +113,7 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched scheduler := gocron.NewScheduler(time.UTC) job, err := scheduler.Every(1).Minute().Do(c.FlushAlerts, maxAge, maxItems) if err != nil { - return nil, errors.Wrap(err, "while starting FlushAlerts scheduler") + return nil, fmt.Errorf("while starting FlushAlerts scheduler: %w", err) } job.SingletonMode() // Init & Start cronjob every hour for bouncers/agents @@ -119,14 +121,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched if config.AgentsGC.Cert != nil { duration, err := types.ParseDuration(*config.AgentsGC.Cert) if err != nil { - return nil, errors.Wrap(err, "while parsing agents cert auto-delete duration") + return nil, fmt.Errorf("while parsing agents cert auto-delete duration: %w", err) } config.AgentsGC.CertDuration = &duration } if config.AgentsGC.LoginPassword != nil { duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword) if err != nil { - return nil, errors.Wrap(err, "while parsing agents login/password auto-delete duration") + return nil, fmt.Errorf("while parsing agents login/password auto-delete duration: %w", err) } config.AgentsGC.LoginPasswordDuration = &duration } @@ -138,14 +140,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched if config.BouncersGC.Cert != nil { duration, err := types.ParseDuration(*config.BouncersGC.Cert) if err != nil { - return nil, errors.Wrap(err, "while parsing bouncers cert auto-delete duration") + return nil, fmt.Errorf("while parsing bouncers cert auto-delete duration: %w", err) } config.BouncersGC.CertDuration = &duration } if config.BouncersGC.Api != nil { duration, err := types.ParseDuration(*config.BouncersGC.Api) if err != nil { - return nil, errors.Wrap(err, "while parsing bouncers api auto-delete duration") + return nil, fmt.Errorf("while parsing bouncers api auto-delete duration: %w", err) } config.BouncersGC.ApiDuration = &duration } @@ -155,7 +157,7 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched } baJob, err := scheduler.Every(1).Minute().Do(c.FlushAgentsAndBouncers, config.AgentsGC, config.BouncersGC) if err != nil { - return nil, errors.Wrap(err, "while starting FlushAgentsAndBouncers scheduler") + return nil, fmt.Errorf("while starting FlushAgentsAndBouncers scheduler: %w", err) } baJob.SingletonMode() scheduler.StartAsync() diff --git a/pkg/exprhelpers/crowdsec_cti_test.go b/pkg/exprhelpers/crowdsec_cti_test.go index 41afcd6ee..51ab5f8a3 100644 --- a/pkg/exprhelpers/crowdsec_cti_test.go +++ b/pkg/exprhelpers/crowdsec_cti_test.go @@ -9,9 +9,11 @@ import ( "testing" "time" - "github.com/crowdsecurity/crowdsec/pkg/cticlient" - "github.com/crowdsecurity/crowdsec/pkg/types" "github.com/stretchr/testify/assert" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + + "github.com/crowdsecurity/crowdsec/pkg/cticlient" ) var sampledata = map[string]cticlient.SmokeItem{ @@ -106,7 +108,7 @@ func smokeHandler(req *http.Request) *http.Response { func TestInvalidAuth(t *testing.T) { defer ShutdownCrowdsecCTI() - if err := InitCrowdsecCTI(types.StrPtr("asdasd"), nil, nil, nil); err != nil { + if err := InitCrowdsecCTI(ptr.Of("asdasd"), nil, nil, nil); err != nil { t.Fatalf("failed to init CTI : %s", err) } //Replace the client created by InitCrowdsecCTI with one that uses a custom transport @@ -148,7 +150,7 @@ func TestNoKey(t *testing.T) { func TestCache(t *testing.T) { defer ShutdownCrowdsecCTI() cacheDuration := 1 * time.Second - if err := InitCrowdsecCTI(types.StrPtr(validApiKey), &cacheDuration, nil, nil); err != nil { + if err := InitCrowdsecCTI(ptr.Of(validApiKey), &cacheDuration, nil, nil); err != nil { t.Fatalf("failed to init CTI : %s", err) } //Replace the client created by InitCrowdsecCTI with one that uses a custom transport diff --git a/pkg/exprhelpers/helpers.go b/pkg/exprhelpers/helpers.go index 2a0c9fd31..a5f45c4b0 100644 --- a/pkg/exprhelpers/helpers.go +++ b/pkg/exprhelpers/helpers.go @@ -20,15 +20,15 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" - "github.com/umahmood/haversine" + "github.com/wasilibs/go-re2" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" "github.com/crowdsecurity/crowdsec/pkg/cache" "github.com/crowdsecurity/crowdsec/pkg/database" "github.com/crowdsecurity/crowdsec/pkg/fflag" "github.com/crowdsecurity/crowdsec/pkg/types" - - "github.com/wasilibs/go-re2" ) var dataFile map[string][]string @@ -91,13 +91,13 @@ func RegexpCacheInit(filename string, CacheCfg types.DataSource) error { //cache is enabled if CacheCfg.Size == nil { - CacheCfg.Size = types.IntPtr(50) + CacheCfg.Size = ptr.Of(50) } gc := gcache.New(*CacheCfg.Size) if CacheCfg.Strategy == nil { - CacheCfg.Strategy = types.StrPtr("LRU") + CacheCfg.Strategy = ptr.Of("LRU") } switch *CacheCfg.Strategy { case "LRU": diff --git a/pkg/parser/enrich_date_test.go b/pkg/parser/enrich_date_test.go index 5a3e08ffd..0a9ac67f8 100644 --- a/pkg/parser/enrich_date_test.go +++ b/pkg/parser/enrich_date_test.go @@ -3,8 +3,11 @@ package parser import ( "testing" - "github.com/crowdsecurity/crowdsec/pkg/types" log "github.com/sirupsen/logrus" + + "github.com/crowdsecurity/go-cs-lib/pkg/ptr" + + "github.com/crowdsecurity/crowdsec/pkg/types" ) func TestDateParse(t *testing.T) { @@ -20,7 +23,7 @@ func TestDateParse(t *testing.T) { StrTime: "2019-10-12T07:20:50.52Z", }, expected_err: nil, - expected_strTime: types.StrPtr("2019-10-12T07:20:50.52Z"), + expected_strTime: ptr.Of("2019-10-12T07:20:50.52Z"), }, { name: "02/Jan/2006:15:04:05 -0700", @@ -28,7 +31,7 @@ func TestDateParse(t *testing.T) { StrTime: "02/Jan/2006:15:04:05 -0700", }, expected_err: nil, - expected_strTime: types.StrPtr("2006-01-02T15:04:05-07:00"), + expected_strTime: ptr.Of("2006-01-02T15:04:05-07:00"), }, { name: "Dec 17 08:17:43", @@ -37,7 +40,7 @@ func TestDateParse(t *testing.T) { StrTimeFormat: "2006 X 2 zz 15X04X05 oneone Jan", }, expected_err: nil, - expected_strTime: types.StrPtr("2011-12-17T08:17:43Z"), + expected_strTime: ptr.Of("2011-12-17T08:17:43Z"), }, } diff --git a/pkg/types/utils.go b/pkg/types/utils.go index ddb5752c5..caed6961e 100644 --- a/pkg/types/utils.go +++ b/pkg/types/utils.go @@ -176,22 +176,6 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) { return } -func StrPtr(s string) *string { - return &s -} - -func IntPtr(i int) *int { - return &i -} - -func Int32Ptr(i int32) *int32 { - return &i -} - -func BoolPtr(b bool) *bool { - return &b -} - func UtcNow() time.Time { return time.Now().UTC() }