Skip to content

Commit

Permalink
dependencies: replaced function calls to pkg/types, errors.Wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jun 1, 2023
1 parent f654496 commit ba96986
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 132 deletions.
14 changes: 8 additions & 6 deletions cmd/crowdsec-cli/papi.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions pkg/apiserver/papi_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -75,28 +77,28 @@ 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))

/*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
Expand All @@ -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
Expand Down
25 changes: 12 additions & 13 deletions pkg/csconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/csconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
33 changes: 17 additions & 16 deletions pkg/csconfig/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit ba96986

Please sign in to comment.