From ba93725def32428ae8f537e239166fd6eda508dd Mon Sep 17 00:00:00 2001 From: marco Date: Mon, 4 Mar 2024 13:56:20 +0100 Subject: [PATCH] Lint whitespace, errors --- pkg/csconfig/api.go | 12 ++++++------ pkg/csconfig/api_test.go | 4 ++++ pkg/csconfig/config.go | 1 - pkg/csconfig/console.go | 10 ++++++++-- pkg/csconfig/crowdsec_service.go | 9 +++------ pkg/csconfig/database.go | 8 +++++++- pkg/csconfig/profiles.go | 19 +++++++++++++------ pkg/csconfig/simulation.go | 12 +++++++++++- 8 files changed, 52 insertions(+), 23 deletions(-) diff --git a/pkg/csconfig/api.go b/pkg/csconfig/api.go index 67b52f1026e6..7fd1f5888973 100644 --- a/pkg/csconfig/api.go +++ b/pkg/csconfig/api.go @@ -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") } if a.Enabled == nil { @@ -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") } if l.InsecureSkipVerify == nil { @@ -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 @@ -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 @@ -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() @@ -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 { diff --git a/pkg/csconfig/api_test.go b/pkg/csconfig/api_test.go index 653610a37c66..b6febd4d4505 100644 --- a/pkg/csconfig/api_test.go +++ b/pkg/csconfig/api_test.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/csconfig/config.go b/pkg/csconfig/config.go index 729246e21444..0c960803e04c 100644 --- a/pkg/csconfig/config.go +++ b/pkg/csconfig/config.go @@ -1,5 +1,4 @@ // Package csconfig contains the configuration structures for crowdsec and cscli. - package csconfig import ( diff --git a/pkg/csconfig/console.go b/pkg/csconfig/console.go index c9731ba5a939..01e74a94db41 100644 --- a/pkg/csconfig/console.go +++ b/pkg/csconfig/console.go @@ -41,6 +41,7 @@ func (c *ConsoleConfig) IsPAPIEnabled() bool { if c == nil || c.ConsoleManagement == nil { return false } + return *c.ConsoleManagement } @@ -48,31 +49,36 @@ 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) } + 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) } 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) diff --git a/pkg/csconfig/crowdsec_service.go b/pkg/csconfig/crowdsec_service.go index 1ba27257ec43..7820595b46fa 100644 --- a/pkg/csconfig/crowdsec_service.go +++ b/pkg/csconfig/crowdsec_service.go @@ -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) } 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 { return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleContextPath, err) } diff --git a/pkg/csconfig/database.go b/pkg/csconfig/database.go index 9486be867bf4..2df2207859da 100644 --- a/pkg/csconfig/database.go +++ b/pkg/csconfig/database.go @@ -1,6 +1,7 @@ package csconfig import ( + "errors" "fmt" "time" @@ -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") } if c.Cscli != nil { @@ -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 @@ -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() { @@ -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 } @@ -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) } diff --git a/pkg/csconfig/profiles.go b/pkg/csconfig/profiles.go index e21abca37ef2..6fbb8ed8b212 100644 --- a/pkg/csconfig/profiles.go +++ b/pkg/csconfig/profiles.go @@ -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") } 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") } + return nil } diff --git a/pkg/csconfig/simulation.go b/pkg/csconfig/simulation.go index 620061567a20..bf121ef56f9b 100644 --- a/pkg/csconfig/simulation.go +++ b/pkg/csconfig/simulation.go @@ -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) } } + 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 }