Skip to content

Commit

Permalink
Lint whitespace, errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Mar 4, 2024
1 parent cbee069 commit ba93725
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 23 deletions.
12 changes: 6 additions & 6 deletions pkg/csconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (a *CTICfg) Load() error {
}

if a.Key != nil && *a.Key == "" {
return fmt.Errorf("empty cti key")
return errors.New("empty cti key")

Check warning on line 67 in pkg/csconfig/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/api.go#L67

Added line #L67 was not covered by tests
}

if a.Enabled == nil {
Expand Down Expand Up @@ -147,7 +147,7 @@ func (l *LocalApiClientCfg) Load() error {
}

if l.Credentials.Login != "" && (l.Credentials.CertPath != "" || l.Credentials.KeyPath != "") {
return fmt.Errorf("user/password authentication and TLS authentication are mutually exclusive")
return errors.New("user/password authentication and TLS authentication are mutually exclusive")

Check warning on line 150 in pkg/csconfig/api.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/api.go#L150

Added line #L150 was not covered by tests
}

if l.InsecureSkipVerify == nil {
Expand Down Expand Up @@ -274,7 +274,7 @@ func (c *Config) LoadAPIServer(inCli bool) error {
}

if c.API.Server.ListenURI == "" {
return fmt.Errorf("no listen_uri specified")
return errors.New("no listen_uri specified")
}

// inherit log level from common, then api->server
Expand Down Expand Up @@ -361,7 +361,7 @@ func parseCapiWhitelists(fd io.Reader) (*CapiWhitelist, error) {
decoder := yaml.NewDecoder(fd)
if err := decoder.Decode(&fromCfg); err != nil {
if errors.Is(err, io.EOF) {
return nil, fmt.Errorf("empty file")
return nil, errors.New("empty file")
}

return nil, err
Expand Down Expand Up @@ -400,7 +400,7 @@ func (s *LocalApiServerCfg) LoadCapiWhitelists() error {

fd, err := os.Open(s.CapiWhitelistsPath)
if err != nil {
return fmt.Errorf("while opening capi whitelist file: %s", err)
return fmt.Errorf("while opening capi whitelist file: %w", err)
}

defer fd.Close()
Expand All @@ -415,7 +415,7 @@ func (s *LocalApiServerCfg) LoadCapiWhitelists() error {

func (c *Config) LoadAPIClient() error {
if c.API == nil || c.API.Client == nil || c.API.Client.CredentialsFilePath == "" || c.DisableAgent {
return fmt.Errorf("no API client section in configuration")
return errors.New("no API client section in configuration")
}

if err := c.API.Client.Load(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/csconfig/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestLoadLocalApiClientCfg(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.Load()
cstest.RequireErrorContains(t, err, tc.expectedErr)

if tc.expectedErr != "" {
return
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.Load()
cstest.RequireErrorContains(t, err, tc.expectedErr)

if tc.expectedErr != "" {
return
}
Expand Down Expand Up @@ -246,6 +248,7 @@ func TestLoadAPIServer(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
err := tc.input.LoadAPIServer(false)
cstest.RequireErrorContains(t, err, tc.expectedErr)

if tc.expectedErr != "" {
return
}
Expand Down Expand Up @@ -309,6 +312,7 @@ func TestParseCapiWhitelists(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
wl, err := parseCapiWhitelists(strings.NewReader(tc.input))
cstest.RequireErrorContains(t, err, tc.expectedErr)

if tc.expectedErr != "" {
return
}
Expand Down
1 change: 0 additions & 1 deletion pkg/csconfig/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Package csconfig contains the configuration structures for crowdsec and cscli.

package csconfig

import (
Expand Down
10 changes: 8 additions & 2 deletions pkg/csconfig/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,44 @@ func (c *ConsoleConfig) IsPAPIEnabled() bool {
if c == nil || c.ConsoleManagement == nil {
return false
}

return *c.ConsoleManagement
}

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 = 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
}

yamlFile, err := os.ReadFile(c.ConsoleConfigPath)
if err != nil {
return fmt.Errorf("reading console config file '%s': %s", c.ConsoleConfigPath, err)
return fmt.Errorf("reading console config file '%s': %w", c.ConsoleConfigPath, err)

Check warning on line 64 in pkg/csconfig/console.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/console.go#L64

Added line #L64 was not covered by tests
}

err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
if err != nil {
return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err)
return fmt.Errorf("unmarshaling console config file '%s': %w", c.ConsoleConfigPath, err)

Check warning on line 69 in pkg/csconfig/console.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/console.go#L69

Added line #L69 was not covered by tests
}

if c.ConsoleConfig.ShareCustomScenarios == nil {
log.Debugf("no share_custom scenarios found, setting to 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 = ptr.Of(true)
}

if c.ConsoleConfig.ShareManualDecisions == nil {
log.Debugf("no share_manual scenarios found, setting to false")
c.ConsoleConfig.ShareManualDecisions = ptr.Of(false)
Expand Down
9 changes: 3 additions & 6 deletions pkg/csconfig/crowdsec_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,16 @@ func (c *Config) LoadCrowdsec() error {
}

if err = c.LoadAPIClient(); err != nil {
return fmt.Errorf("loading api client: %s", err)
return fmt.Errorf("loading api client: %w", err)

Check warning on line 136 in pkg/csconfig/crowdsec_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/crowdsec_service.go#L136

Added line #L136 was not covered by tests
}

return nil
}

func (c *CrowdsecServiceCfg) DumpContextConfigFile() error {
var out []byte
var err error

// XXX: MakeDirs

if out, err = yaml.Marshal(c.ContextToSend); err != nil {
out, err := yaml.Marshal(c.ContextToSend)
if err != nil {

Check warning on line 145 in pkg/csconfig/crowdsec_service.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/crowdsec_service.go#L144-L145

Added lines #L144 - L145 were not covered by tests
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleContextPath, err)
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/csconfig/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csconfig

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -53,7 +54,7 @@ type FlushDBCfg struct {

func (c *Config) LoadDBConfig(inCli bool) error {
if c.DbConfig == nil {
return fmt.Errorf("no database configuration provided")
return errors.New("no database configuration provided")

Check warning on line 57 in pkg/csconfig/database.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/database.go#L57

Added line #L57 was not covered by tests
}

if c.Cscli != nil {
Expand Down Expand Up @@ -87,6 +88,7 @@ func (c *Config) LoadDBConfig(inCli bool) error {

func (d *DatabaseCfg) ConnectionString() string {
connString := ""

switch d.Type {
case "sqlite":
var sqliteConnectionStringParameters string
Expand All @@ -95,6 +97,7 @@ func (d *DatabaseCfg) ConnectionString() string {
} else {
sqliteConnectionStringParameters = "_busy_timeout=100000&_fk=1"
}

connString = fmt.Sprintf("file:%s?%s", d.DbPath, sqliteConnectionStringParameters)
case "mysql":
if d.isSocketConfig() {
Expand All @@ -109,6 +112,7 @@ func (d *DatabaseCfg) ConnectionString() string {
connString = fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s", d.Host, d.Port, d.User, d.DbName, d.Password, d.Sslmode)
}
}

return connString
}

Expand All @@ -122,8 +126,10 @@ func (d *DatabaseCfg) ConnectionDialect() (string, string, error) {
if d.Type != "pgx" {
log.Debugf("database type '%s' is deprecated, switching to 'pgx' instead", d.Type)
}

return "pgx", dialect.Postgres, nil
}

return "", "", fmt.Errorf("unknown database type '%s'", d.Type)
}

Expand Down
19 changes: 13 additions & 6 deletions pkg/csconfig/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,50 @@ import (
type ProfileCfg struct {
Name string `yaml:"name,omitempty"`
Debug *bool `yaml:"debug,omitempty"`
Filters []string `yaml:"filters,omitempty"` //A list of OR'ed expressions. the models.Alert object
Filters []string `yaml:"filters,omitempty"` // A list of OR'ed expressions. the models.Alert object
Decisions []models.Decision `yaml:"decisions,omitempty"`
DurationExpr string `yaml:"duration_expr,omitempty"`
OnSuccess string `yaml:"on_success,omitempty"` //continue or break
OnFailure string `yaml:"on_failure,omitempty"` //continue or break
OnError string `yaml:"on_error,omitempty"` //continue, break, error, report, apply, ignore
OnSuccess string `yaml:"on_success,omitempty"` // continue or break
OnFailure string `yaml:"on_failure,omitempty"` // continue or break
OnError string `yaml:"on_error,omitempty"` // continue, break, error, report, apply, ignore
Notifications []string `yaml:"notifications,omitempty"`
}

func (c *LocalApiServerCfg) LoadProfiles() error {
if c.ProfilesPath == "" {
return fmt.Errorf("empty profiles path")
return errors.New("empty profiles path")

Check warning on line 37 in pkg/csconfig/profiles.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/profiles.go#L37

Added line #L37 was not covered by tests
}

patcher := yamlpatch.NewPatcher(c.ProfilesPath, ".local")

fcontent, err := patcher.PrependedPatchContent()
if err != nil {
return err
}

reader := bytes.NewReader(fcontent)

dec := yaml.NewDecoder(reader)
dec.KnownFields(true)

for {
t := ProfileCfg{}

err = dec.Decode(&t)
if err != nil {
if errors.Is(err, io.EOF) {
break
}

return fmt.Errorf("while decoding %s: %w", c.ProfilesPath, err)
}

c.Profiles = append(c.Profiles, &t)
}

if len(c.Profiles) == 0 {
return fmt.Errorf("zero profiles loaded for LAPI")
return errors.New("zero profiles loaded for LAPI")

Check warning on line 68 in pkg/csconfig/profiles.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/profiles.go#L68

Added line #L68 was not covered by tests
}

return nil
}
12 changes: 11 additions & 1 deletion pkg/csconfig/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,51 @@ func (s *SimulationConfig) IsSimulated(scenario string) bool {
if s.Simulation != nil && *s.Simulation {
simulated = true
}

for _, excluded := range s.Exclusions {
if excluded == scenario {
simulated = !simulated
break
}
}

return simulated
}

func (c *Config) LoadSimulation() error {
simCfg := SimulationConfig{}

if c.ConfigPaths.SimulationFilePath == "" {
c.ConfigPaths.SimulationFilePath = filepath.Clean(c.ConfigPaths.ConfigDir + "/simulation.yaml")
}

patcher := yamlpatch.NewPatcher(c.ConfigPaths.SimulationFilePath, ".local")

rcfg, err := patcher.MergedPatchContent()
if err != nil {
return err
}

dec := yaml.NewDecoder(bytes.NewReader(rcfg))
dec.KnownFields(true)

if err := dec.Decode(&simCfg); err != nil {
if !errors.Is(err, io.EOF) {
return fmt.Errorf("while unmarshaling simulation file '%s' : %s", c.ConfigPaths.SimulationFilePath, err)
return fmt.Errorf("while unmarshaling simulation file '%s': %w", c.ConfigPaths.SimulationFilePath, err)
}

Check warning on line 57 in pkg/csconfig/simulation.go

View check run for this annotation

Codecov / codecov/patch

pkg/csconfig/simulation.go#L55-L57

Added lines #L55 - L57 were not covered by tests
}

if simCfg.Simulation == nil {
simCfg.Simulation = new(bool)
}

if c.Crowdsec != nil {
c.Crowdsec.SimulationConfig = &simCfg
}

if c.Cscli != nil {
c.Cscli.SimulationConfig = &simCfg
}

return nil
}

0 comments on commit ba93725

Please sign in to comment.