dependencies: replaced function calls to pkg/types, errors.Wrap (#2235)
we now use a generic pointer function, and slowly remove the deprecated pkg/errors
This commit is contained in:
parent
12c32d507c
commit
396dcf8e6e
5
.github/codecov.yml
vendored
5
.github/codecov.yml
vendored
|
@ -1,5 +1,10 @@
|
||||||
|
# we measure coverage but don't enforce it
|
||||||
|
# https://docs.codecov.com/docs/codecov-yaml
|
||||||
coverage:
|
coverage:
|
||||||
status:
|
status:
|
||||||
|
patch:
|
||||||
|
default:
|
||||||
|
target: 0%
|
||||||
project:
|
project:
|
||||||
default:
|
default:
|
||||||
target: 0%
|
target: 0%
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"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"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/tomb.v2"
|
"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 {
|
func NewPapiCmd() *cobra.Command {
|
||||||
|
@ -20,7 +22,7 @@ func NewPapiCmd() *cobra.Command {
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
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 {
|
if csConfig.API.Server.OnlineClient == nil {
|
||||||
log.Fatalf("no configuration for Central API in '%s'", *csConfig.FilePath)
|
log.Fatalf("no configuration for Central API in '%s'", *csConfig.FilePath)
|
||||||
|
@ -71,7 +73,7 @@ func NewPapiStatusCmd() *cobra.Command {
|
||||||
var lastTimestampStr *string
|
var lastTimestampStr *string
|
||||||
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lastTimestampStr = types.StrPtr("never")
|
lastTimestampStr = ptr.Of("never")
|
||||||
}
|
}
|
||||||
log.Infof("You can successfully interact with Polling API (PAPI)")
|
log.Infof("You can successfully interact with Polling API (PAPI)")
|
||||||
log.Infof("Console plan: %s", perms.Plan)
|
log.Infof("Console plan: %s", perms.Plan)
|
||||||
|
|
|
@ -5,11 +5,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"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/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type deleteDecisions struct {
|
type deleteDecisions struct {
|
||||||
|
@ -75,7 +77,7 @@ func AlertCmd(message *Message, p *Papi, sync bool) error {
|
||||||
alert := &models.Alert{}
|
alert := &models.Alert{}
|
||||||
|
|
||||||
if err := json.Unmarshal(data, alert); err != nil {
|
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))
|
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*/
|
/*Fix the alert with missing mandatory items*/
|
||||||
if alert.StartAt == nil || *alert.StartAt == "" {
|
if alert.StartAt == nil || *alert.StartAt == "" {
|
||||||
log.Warnf("Alert %d has no StartAt, setting it to now", alert.ID)
|
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 == "" {
|
if alert.StopAt == nil || *alert.StopAt == "" {
|
||||||
log.Warnf("Alert %d has no StopAt, setting it to now", alert.ID)
|
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.EventsCount = ptr.Of(int32(0))
|
||||||
alert.Capacity = types.Int32Ptr(0)
|
alert.Capacity = ptr.Of(int32(0))
|
||||||
alert.Leakspeed = types.StrPtr("")
|
alert.Leakspeed = ptr.Of("")
|
||||||
alert.Simulated = types.BoolPtr(false)
|
alert.Simulated = ptr.Of(false)
|
||||||
alert.ScenarioHash = types.StrPtr("")
|
alert.ScenarioHash = ptr.Of("")
|
||||||
alert.ScenarioVersion = types.StrPtr("")
|
alert.ScenarioVersion = ptr.Of("")
|
||||||
alert.Message = types.StrPtr("")
|
alert.Message = ptr.Of("")
|
||||||
alert.Scenario = types.StrPtr("")
|
alert.Scenario = ptr.Of("")
|
||||||
alert.Source = &models.Source{}
|
alert.Source = &models.Source{}
|
||||||
|
|
||||||
//if we're setting Source.Scope to types.ConsoleOrigin, it messes up the alert's value
|
//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
|
alert.Source.Value = alert.Decisions[0].Value
|
||||||
} else {
|
} else {
|
||||||
log.Warningf("No decision found in alert for Polling API (%s : %s)", message.Header.Source.User, message.Header.Message)
|
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.Source.Value = &message.Header.Source.User
|
||||||
}
|
}
|
||||||
alert.Scenario = &message.Header.Message
|
alert.Scenario = &message.Header.Message
|
||||||
|
|
|
@ -9,14 +9,13 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
"github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch"
|
"github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type APICfg struct {
|
type APICfg struct {
|
||||||
|
@ -83,11 +82,11 @@ func (o *OnlineApiClientCfg) Load() error {
|
||||||
o.Credentials = new(ApiCredentialsCfg)
|
o.Credentials = new(ApiCredentialsCfg)
|
||||||
fcontent, err := os.ReadFile(o.CredentialsFilePath)
|
fcontent, err := os.ReadFile(o.CredentialsFilePath)
|
||||||
if err != nil {
|
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)
|
err = yaml.UnmarshalStrict(fcontent, o.Credentials)
|
||||||
if err != nil {
|
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 == "" {
|
if o.Credentials.Login == "" || o.Credentials.Password == "" || o.Credentials.URL == "" {
|
||||||
log.Warningf("can't load CAPI credentials from '%s' (missing field)", o.CredentialsFilePath)
|
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)
|
err = yaml.UnmarshalStrict(fcontent, &l.Credentials)
|
||||||
if err != nil {
|
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 == "" {
|
if l.Credentials == nil || l.Credentials.URL == "" {
|
||||||
return fmt.Errorf("no credentials or URL found in api client configuration '%s'", l.CredentialsFilePath)
|
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 != "" {
|
if l.Credentials.CACertPath != "" {
|
||||||
caCert, err := os.ReadFile(l.Credentials.CACertPath)
|
caCert, err := os.ReadFile(l.Credentials.CACertPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to load cacert")
|
return fmt.Errorf("failed to load cacert: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
caCertPool, err := x509.SystemCertPool()
|
caCertPool, err := x509.SystemCertPool()
|
||||||
|
@ -147,7 +146,7 @@ func (l *LocalApiClientCfg) Load() error {
|
||||||
if l.Credentials.CertPath != "" && l.Credentials.KeyPath != "" {
|
if l.Credentials.CertPath != "" && l.Credentials.KeyPath != "" {
|
||||||
cert, err := tls.LoadX509KeyPair(l.Credentials.CertPath, l.Credentials.KeyPath)
|
cert, err := tls.LoadX509KeyPair(l.Credentials.CertPath, l.Credentials.KeyPath)
|
||||||
if err != nil {
|
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
|
apiclient.Cert = &cert
|
||||||
|
@ -251,7 +250,7 @@ func (c *Config) LoadAPIServer() error {
|
||||||
|
|
||||||
if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
|
if c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
|
||||||
if err := c.API.Server.OnlineClient.Load(); err != nil {
|
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 {
|
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 c.API.Server.Enable == nil {
|
||||||
// if the option is not present, it is enabled by default
|
// 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 {
|
if !*c.API.Server.Enable {
|
||||||
|
@ -300,18 +299,18 @@ func (c *Config) LoadAPIServer() error {
|
||||||
c.API.Server.UseForwardedForHeaders = true
|
c.API.Server.UseForwardedForHeaders = true
|
||||||
}
|
}
|
||||||
if err := c.API.Server.LoadProfiles(); err != nil {
|
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 == "" {
|
if c.API.Server.ConsoleConfigPath == "" {
|
||||||
c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
c.API.Server.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
||||||
}
|
}
|
||||||
if err := c.API.Server.LoadConsoleConfig(); err != nil {
|
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 c.API.Server.OnlineClient != nil && c.API.Server.OnlineClient.CredentialsFilePath != "" {
|
||||||
if err := c.API.Server.OnlineClient.Load(); err != nil {
|
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 {
|
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 c.API.CTI != nil {
|
||||||
if err := c.API.CTI.Load(); err != nil {
|
if err := c.API.CTI.Load(); err != nil {
|
||||||
return errors.Wrap(err, "loading CTI configuration")
|
return fmt.Errorf("loading CTI configuration: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,12 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch"
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/pkg/csstring"
|
"github.com/crowdsecurity/go-cs-lib/pkg/csstring"
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/go-cs-lib/pkg/yamlpatch"
|
||||||
)
|
)
|
||||||
|
|
||||||
// defaultConfigDir is the base path to all configuration files, to be overridden in the Makefile */
|
// 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 {
|
func (c *Config) Dump() error {
|
||||||
out, err := yaml.Marshal(c)
|
out, err := yaml.Marshal(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "failed marshaling config")
|
return fmt.Errorf("failed marshaling config: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s", string(out))
|
fmt.Printf("%s", string(out))
|
||||||
return nil
|
return nil
|
||||||
|
@ -65,7 +63,7 @@ func NewConfig(configFile string, disableAgent bool, disableAPI bool, quiet bool
|
||||||
err = yaml.UnmarshalStrict([]byte(configData), &cfg)
|
err = yaml.UnmarshalStrict([]byte(configData), &cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// this is actually the "merged" yaml
|
// this is actually the "merged" yaml
|
||||||
return nil, "", errors.Wrap(err, configFile)
|
return nil, "", fmt.Errorf("%s: %w", configFile, err)
|
||||||
}
|
}
|
||||||
return &cfg, configData, nil
|
return &cfg, configData, nil
|
||||||
}
|
}
|
||||||
|
@ -113,14 +111,14 @@ func NewDefaultConfig() *Config {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
CTI: &CTICfg{
|
CTI: &CTICfg{
|
||||||
Enabled: types.BoolPtr(false),
|
Enabled: ptr.Of(false),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
dbConfig := DatabaseCfg{
|
dbConfig := DatabaseCfg{
|
||||||
Type: "sqlite",
|
Type: "sqlite",
|
||||||
DbPath: DefaultDataPath("crowdsec.db"),
|
DbPath: DefaultDataPath("crowdsec.db"),
|
||||||
MaxOpenConns: types.IntPtr(DEFAULT_MAX_OPEN_CONNS),
|
MaxOpenConns: ptr.Of(DEFAULT_MAX_OPEN_CONNS),
|
||||||
}
|
}
|
||||||
|
|
||||||
globalCfg := Config{
|
globalCfg := Config{
|
||||||
|
|
|
@ -4,11 +4,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -35,11 +36,11 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
|
||||||
c.ConsoleConfig = &ConsoleConfig{}
|
c.ConsoleConfig = &ConsoleConfig{}
|
||||||
if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
|
if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
|
||||||
log.Debugf("no console configuration to load")
|
log.Debugf("no console configuration to load")
|
||||||
c.ConsoleConfig.ShareCustomScenarios = types.BoolPtr(true)
|
c.ConsoleConfig.ShareCustomScenarios = ptr.Of(true)
|
||||||
c.ConsoleConfig.ShareTaintedScenarios = types.BoolPtr(true)
|
c.ConsoleConfig.ShareTaintedScenarios = ptr.Of(true)
|
||||||
c.ConsoleConfig.ShareManualDecisions = types.BoolPtr(false)
|
c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
|
||||||
c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
|
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
|
||||||
c.ConsoleConfig.ShareContext = types.BoolPtr(false)
|
c.ConsoleConfig.ShareContext = ptr.Of(false)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,27 +55,27 @@ func (c *LocalApiServerCfg) LoadConsoleConfig() error {
|
||||||
|
|
||||||
if c.ConsoleConfig.ShareCustomScenarios == nil {
|
if c.ConsoleConfig.ShareCustomScenarios == nil {
|
||||||
log.Debugf("no share_custom scenarios found, setting to true")
|
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 {
|
if c.ConsoleConfig.ShareTaintedScenarios == nil {
|
||||||
log.Debugf("no share_tainted scenarios found, setting to true")
|
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 {
|
if c.ConsoleConfig.ShareManualDecisions == nil {
|
||||||
log.Debugf("no share_manual scenarios found, setting to false")
|
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() {
|
if !fflag.PapiClient.IsEnabled() {
|
||||||
c.ConsoleConfig.ConsoleManagement = types.BoolPtr(false)
|
c.ConsoleConfig.ConsoleManagement = ptr.Of(false)
|
||||||
} else if c.ConsoleConfig.ConsoleManagement == nil {
|
} else if c.ConsoleConfig.ConsoleManagement == nil {
|
||||||
log.Debugf("no console_management found, setting to false")
|
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 {
|
if c.ConsoleConfig.ShareContext == nil {
|
||||||
log.Debugf("no 'context' found, setting to false")
|
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)
|
log.Debugf("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
|
||||||
|
@ -87,7 +88,7 @@ func (c *LocalApiServerCfg) DumpConsoleConfig() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if out, err = yaml.Marshal(c.ConsoleConfig); err != nil {
|
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 == "" {
|
if c.ConsoleConfigPath == "" {
|
||||||
c.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
c.ConsoleConfigPath = DefaultConsoleConfigFilePath
|
||||||
|
@ -96,7 +97,7 @@ func (c *LocalApiServerCfg) DumpConsoleConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
|
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
|
return nil
|
||||||
|
|
|
@ -5,11 +5,10 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v2"
|
"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
|
// 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 c.Crowdsec.Enable == nil {
|
||||||
// if the option is not present, it is enabled by default
|
// 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 {
|
if !*c.Crowdsec.Enable {
|
||||||
|
@ -72,20 +71,20 @@ func (c *Config) LoadCrowdsec() error {
|
||||||
if c.Crowdsec.AcquisitionDirPath != "" {
|
if c.Crowdsec.AcquisitionDirPath != "" {
|
||||||
c.Crowdsec.AcquisitionDirPath, err = filepath.Abs(c.Crowdsec.AcquisitionDirPath)
|
c.Crowdsec.AcquisitionDirPath, err = filepath.Abs(c.Crowdsec.AcquisitionDirPath)
|
||||||
if err != nil {
|
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
|
var files []string
|
||||||
|
|
||||||
files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yaml")
|
files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yaml")
|
||||||
if err != nil {
|
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...)
|
c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...)
|
||||||
|
|
||||||
files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yml")
|
files, err = filepath.Glob(c.Crowdsec.AcquisitionDirPath + "/*.yml")
|
||||||
if err != nil {
|
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...)
|
c.Crowdsec.AcquisitionFiles = append(c.Crowdsec.AcquisitionFiles, files...)
|
||||||
}
|
}
|
||||||
|
@ -99,7 +98,7 @@ func (c *Config) LoadCrowdsec() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.LoadSimulation(); err != nil {
|
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
|
c.Crowdsec.ConfigDir = c.ConfigPaths.ConfigDir
|
||||||
|
@ -129,7 +128,7 @@ func (c *Config) LoadCrowdsec() error {
|
||||||
}
|
}
|
||||||
*k, err = filepath.Abs(*k)
|
*k, err = filepath.Abs(*k)
|
||||||
if err != nil {
|
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 {
|
for i, file := range c.Crowdsec.AcquisitionFiles {
|
||||||
f, err := filepath.Abs(file)
|
f, err := filepath.Abs(file)
|
||||||
if err != nil {
|
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
|
c.Crowdsec.AcquisitionFiles[i] = f
|
||||||
}
|
}
|
||||||
|
@ -147,7 +146,7 @@ func (c *Config) LoadCrowdsec() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.LoadHub(); err != nil {
|
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)
|
c.Crowdsec.ContextToSend = make(map[string][]string, 0)
|
||||||
|
@ -186,11 +185,11 @@ func (c *CrowdsecServiceCfg) DumpContextConfigFile() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if out, err = yaml.Marshal(c.ContextToSend); err != nil {
|
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 {
|
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)
|
log.Infof("%s file saved", c.ConsoleContextPath)
|
||||||
|
|
|
@ -5,8 +5,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"entgo.io/ent/dialect"
|
"entgo.io/ent/dialect"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DEFAULT_MAX_OPEN_CONNS = 100
|
var DEFAULT_MAX_OPEN_CONNS = 100
|
||||||
|
@ -56,7 +57,7 @@ func (c *Config) LoadDBConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.DbConfig.MaxOpenConns == nil {
|
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" {
|
if c.DbConfig.Type == "sqlite" {
|
||||||
|
|
|
@ -5,8 +5,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadDBConfig(t *testing.T) {
|
func TestLoadDBConfig(t *testing.T) {
|
||||||
|
@ -22,7 +23,7 @@ func TestLoadDBConfig(t *testing.T) {
|
||||||
DbConfig: &DatabaseCfg{
|
DbConfig: &DatabaseCfg{
|
||||||
Type: "sqlite",
|
Type: "sqlite",
|
||||||
DbPath: "./tests/test.db",
|
DbPath: "./tests/test.db",
|
||||||
MaxOpenConns: types.IntPtr(10),
|
MaxOpenConns: ptr.Of(10),
|
||||||
},
|
},
|
||||||
Cscli: &CscliCfg{},
|
Cscli: &CscliCfg{},
|
||||||
API: &APICfg{
|
API: &APICfg{
|
||||||
|
@ -32,7 +33,7 @@ func TestLoadDBConfig(t *testing.T) {
|
||||||
expectedResult: &DatabaseCfg{
|
expectedResult: &DatabaseCfg{
|
||||||
Type: "sqlite",
|
Type: "sqlite",
|
||||||
DbPath: "./tests/test.db",
|
DbPath: "./tests/test.db",
|
||||||
MaxOpenConns: types.IntPtr(10),
|
MaxOpenConns: ptr.Of(10),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,9 +11,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const validApiKey = "my-api-key"
|
const validApiKey = "my-api-key"
|
||||||
|
@ -202,12 +202,12 @@ func TestFireOk(t *testing.T) {
|
||||||
assert.Equal(t, len(data.Items), 3)
|
assert.Equal(t, len(data.Items), 3)
|
||||||
assert.Equal(t, data.Items[0].Ip, "1.2.3.4")
|
assert.Equal(t, data.Items[0].Ip, "1.2.3.4")
|
||||||
//page 1 is the default
|
//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, err, nil)
|
||||||
assert.Equal(t, len(data.Items), 3)
|
assert.Equal(t, len(data.Items), 3)
|
||||||
assert.Equal(t, data.Items[0].Ip, "1.2.3.4")
|
assert.Equal(t, data.Items[0].Ip, "1.2.3.4")
|
||||||
//page 2
|
//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, err, nil)
|
||||||
assert.Equal(t, len(data.Items), 3)
|
assert.Equal(t, len(data.Items), 3)
|
||||||
assert.Equal(t, data.Items[0].Ip, "4.2.3.4")
|
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, "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) {
|
func TestSmokeUnknownIP(t *testing.T) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
//func (c *SmokeItem) GetAttackDetails() []string {
|
//func (c *SmokeItem) GetAttackDetails() []string {
|
||||||
|
@ -16,16 +16,16 @@ func getSampleSmokeItem() SmokeItem {
|
||||||
emptyItem := SmokeItem{
|
emptyItem := SmokeItem{
|
||||||
IpRangeScore: 2.0,
|
IpRangeScore: 2.0,
|
||||||
Ip: "1.2.3.4",
|
Ip: "1.2.3.4",
|
||||||
IpRange: types.StrPtr("1.2.3.0/24"),
|
IpRange: ptr.Of("1.2.3.0/24"),
|
||||||
AsName: types.StrPtr("AS1234"),
|
AsName: ptr.Of("AS1234"),
|
||||||
AsNum: types.IntPtr(1234),
|
AsNum: ptr.Of(1234),
|
||||||
Location: CTILocationInfo{
|
Location: CTILocationInfo{
|
||||||
Country: types.StrPtr("FR"),
|
Country: ptr.Of("FR"),
|
||||||
City: types.StrPtr("Paris"),
|
City: ptr.Of("Paris"),
|
||||||
Latitude: &lat,
|
Latitude: &lat,
|
||||||
Longitude: &long,
|
Longitude: &long,
|
||||||
},
|
},
|
||||||
ReverseDNS: types.StrPtr("foo.bar.com"),
|
ReverseDNS: ptr.Of("foo.bar.com"),
|
||||||
Behaviors: []*CTIBehavior{
|
Behaviors: []*CTIBehavior{
|
||||||
{
|
{
|
||||||
Name: "ssh:bruteforce",
|
Name: "ssh:bruteforce",
|
||||||
|
@ -34,8 +34,8 @@ func getSampleSmokeItem() SmokeItem {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
History: CTIHistory{
|
History: CTIHistory{
|
||||||
FirstSeen: types.StrPtr("2022-12-05T17:45:00+00:00"),
|
FirstSeen: ptr.Of("2022-12-05T17:45:00+00:00"),
|
||||||
LastSeen: types.StrPtr("2022-12-06T19:15:00+00:00"),
|
LastSeen: ptr.Of("2022-12-06T19:15:00+00:00"),
|
||||||
FullAge: 3,
|
FullAge: 3,
|
||||||
DaysAge: 1,
|
DaysAge: 1,
|
||||||
},
|
},
|
||||||
|
@ -56,7 +56,7 @@ func getSampleSmokeItem() SmokeItem {
|
||||||
"GB": 14,
|
"GB": 14,
|
||||||
"US": 14,
|
"US": 14,
|
||||||
},
|
},
|
||||||
BackgroundNoiseScore: types.IntPtr(3),
|
BackgroundNoiseScore: ptr.Of(3),
|
||||||
Scores: CTIScores{
|
Scores: CTIScores{
|
||||||
Overall: CTIScore{
|
Overall: CTIScore{
|
||||||
Aggressiveness: 2,
|
Aggressiveness: 2,
|
||||||
|
|
|
@ -8,15 +8,17 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
entsql "entgo.io/ent/dialect/sql"
|
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-co-op/gocron"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/jackc/pgx/v4/stdlib"
|
_ "github.com/jackc/pgx/v4/stdlib"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/pkg/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
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 {
|
type Client struct {
|
||||||
|
@ -35,7 +37,7 @@ func getEntDriver(dbtype string, dbdialect string, dsn string, config *csconfig.
|
||||||
}
|
}
|
||||||
if config.MaxOpenConns == nil {
|
if config.MaxOpenConns == nil {
|
||||||
log.Warningf("MaxOpenConns is 0, defaulting to %d", csconfig.DEFAULT_MAX_OPEN_CONNS)
|
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)
|
db.SetMaxOpenConns(*config.MaxOpenConns)
|
||||||
drv := entsql.OpenDB(dbdialect, db)
|
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*/
|
/*The logger that will be used by db operations*/
|
||||||
clog := log.New()
|
clog := log.New()
|
||||||
if err := types.ConfigureLogger(clog); err != nil {
|
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 {
|
if config.LogLevel != nil {
|
||||||
clog.SetLevel(*config.LogLevel)
|
clog.SetLevel(*config.LogLevel)
|
||||||
|
@ -68,10 +70,10 @@ func NewClient(config *csconfig.DatabaseCfg) (*Client, error) {
|
||||||
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, 0600)
|
f, err := os.OpenFile(config.DbPath, os.O_CREATE|os.O_RDWR, 0600)
|
||||||
if err != nil {
|
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 {
|
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)
|
//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)
|
scheduler := gocron.NewScheduler(time.UTC)
|
||||||
job, err := scheduler.Every(1).Minute().Do(c.FlushAlerts, maxAge, maxItems)
|
job, err := scheduler.Every(1).Minute().Do(c.FlushAlerts, maxAge, maxItems)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while starting FlushAlerts scheduler")
|
return nil, fmt.Errorf("while starting FlushAlerts scheduler: %w", err)
|
||||||
}
|
}
|
||||||
job.SingletonMode()
|
job.SingletonMode()
|
||||||
// Init & Start cronjob every hour for bouncers/agents
|
// 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 {
|
if config.AgentsGC.Cert != nil {
|
||||||
duration, err := types.ParseDuration(*config.AgentsGC.Cert)
|
duration, err := types.ParseDuration(*config.AgentsGC.Cert)
|
||||||
if err != nil {
|
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
|
config.AgentsGC.CertDuration = &duration
|
||||||
}
|
}
|
||||||
if config.AgentsGC.LoginPassword != nil {
|
if config.AgentsGC.LoginPassword != nil {
|
||||||
duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword)
|
duration, err := types.ParseDuration(*config.AgentsGC.LoginPassword)
|
||||||
if err != nil {
|
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
|
config.AgentsGC.LoginPasswordDuration = &duration
|
||||||
}
|
}
|
||||||
|
@ -138,14 +140,14 @@ func (c *Client) StartFlushScheduler(config *csconfig.FlushDBCfg) (*gocron.Sched
|
||||||
if config.BouncersGC.Cert != nil {
|
if config.BouncersGC.Cert != nil {
|
||||||
duration, err := types.ParseDuration(*config.BouncersGC.Cert)
|
duration, err := types.ParseDuration(*config.BouncersGC.Cert)
|
||||||
if err != nil {
|
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
|
config.BouncersGC.CertDuration = &duration
|
||||||
}
|
}
|
||||||
if config.BouncersGC.Api != nil {
|
if config.BouncersGC.Api != nil {
|
||||||
duration, err := types.ParseDuration(*config.BouncersGC.Api)
|
duration, err := types.ParseDuration(*config.BouncersGC.Api)
|
||||||
if err != nil {
|
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
|
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)
|
baJob, err := scheduler.Every(1).Minute().Do(c.FlushAgentsAndBouncers, config.AgentsGC, config.BouncersGC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while starting FlushAgentsAndBouncers scheduler")
|
return nil, fmt.Errorf("while starting FlushAgentsAndBouncers scheduler: %w", err)
|
||||||
}
|
}
|
||||||
baJob.SingletonMode()
|
baJob.SingletonMode()
|
||||||
scheduler.StartAsync()
|
scheduler.StartAsync()
|
||||||
|
|
|
@ -9,9 +9,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cticlient"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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{
|
var sampledata = map[string]cticlient.SmokeItem{
|
||||||
|
@ -106,7 +108,7 @@ func smokeHandler(req *http.Request) *http.Response {
|
||||||
|
|
||||||
func TestInvalidAuth(t *testing.T) {
|
func TestInvalidAuth(t *testing.T) {
|
||||||
defer ShutdownCrowdsecCTI()
|
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)
|
t.Fatalf("failed to init CTI : %s", err)
|
||||||
}
|
}
|
||||||
//Replace the client created by InitCrowdsecCTI with one that uses a custom transport
|
//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) {
|
func TestCache(t *testing.T) {
|
||||||
defer ShutdownCrowdsecCTI()
|
defer ShutdownCrowdsecCTI()
|
||||||
cacheDuration := 1 * time.Second
|
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)
|
t.Fatalf("failed to init CTI : %s", err)
|
||||||
}
|
}
|
||||||
//Replace the client created by InitCrowdsecCTI with one that uses a custom transport
|
//Replace the client created by InitCrowdsecCTI with one that uses a custom transport
|
||||||
|
|
|
@ -20,15 +20,15 @@ import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/umahmood/haversine"
|
"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/cache"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
"github.com/wasilibs/go-re2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var dataFile map[string][]string
|
var dataFile map[string][]string
|
||||||
|
@ -91,13 +91,13 @@ func RegexpCacheInit(filename string, CacheCfg types.DataSource) error {
|
||||||
//cache is enabled
|
//cache is enabled
|
||||||
|
|
||||||
if CacheCfg.Size == nil {
|
if CacheCfg.Size == nil {
|
||||||
CacheCfg.Size = types.IntPtr(50)
|
CacheCfg.Size = ptr.Of(50)
|
||||||
}
|
}
|
||||||
|
|
||||||
gc := gcache.New(*CacheCfg.Size)
|
gc := gcache.New(*CacheCfg.Size)
|
||||||
|
|
||||||
if CacheCfg.Strategy == nil {
|
if CacheCfg.Strategy == nil {
|
||||||
CacheCfg.Strategy = types.StrPtr("LRU")
|
CacheCfg.Strategy = ptr.Of("LRU")
|
||||||
}
|
}
|
||||||
switch *CacheCfg.Strategy {
|
switch *CacheCfg.Strategy {
|
||||||
case "LRU":
|
case "LRU":
|
||||||
|
|
|
@ -3,8 +3,11 @@ package parser
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/pkg/ptr"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDateParse(t *testing.T) {
|
func TestDateParse(t *testing.T) {
|
||||||
|
@ -20,7 +23,7 @@ func TestDateParse(t *testing.T) {
|
||||||
StrTime: "2019-10-12T07:20:50.52Z",
|
StrTime: "2019-10-12T07:20:50.52Z",
|
||||||
},
|
},
|
||||||
expected_err: nil,
|
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",
|
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",
|
StrTime: "02/Jan/2006:15:04:05 -0700",
|
||||||
},
|
},
|
||||||
expected_err: nil,
|
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",
|
name: "Dec 17 08:17:43",
|
||||||
|
@ -37,7 +40,7 @@ func TestDateParse(t *testing.T) {
|
||||||
StrTimeFormat: "2006 X 2 zz 15X04X05 oneone Jan",
|
StrTimeFormat: "2006 X 2 zz 15X04X05 oneone Jan",
|
||||||
},
|
},
|
||||||
expected_err: nil,
|
expected_err: nil,
|
||||||
expected_strTime: types.StrPtr("2011-12-17T08:17:43Z"),
|
expected_strTime: ptr.Of("2011-12-17T08:17:43Z"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -176,22 +176,6 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) {
|
||||||
return
|
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 {
|
func UtcNow() time.Time {
|
||||||
return time.Now().UTC()
|
return time.Now().UTC()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue