From 02ff240db9d56d3a862ae08a7f60c37d5a932455 Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 14 Mar 2024 09:40:09 +0100 Subject: [PATCH] lint --- .github/workflows/lint.yml | 2 +- cmd/root.go | 13 ++++++++----- pkg/cfg/config.go | 5 +++-- pkg/cfg/logging.go | 4 ++-- pkg/server/logging.go | 2 ++ pkg/server/server.go | 2 +- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f723912..bb8d672 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -39,7 +39,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - version: v1.55 + version: v1.56 args: --issues-exit-code=1 --timeout 10m only-new-issues: false # the cache is already managed above, enabling it here diff --git a/cmd/root.go b/cmd/root.go index 3120941..dd13701 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "context" + "errors" "flag" "fmt" "os" @@ -33,9 +34,9 @@ func HandleSignals(ctx context.Context) error { case s := <-signalChan: switch s { case syscall.SIGTERM: - return fmt.Errorf("received SIGTERM") + return errors.New("received SIGTERM") case os.Interrupt: // cross-platform SIGINT - return fmt.Errorf("received interrupt") + return errors.New("received interrupt") } case <-ctx.Done(): return ctx.Err() @@ -60,7 +61,7 @@ func Execute() error { } if configPath == nil || *configPath == "" { - return fmt.Errorf("configuration file is required") + return errors.New("configuration file is required") } configBytes, err := cfg.MergedConfig(*configPath) @@ -101,7 +102,7 @@ func Execute() error { ScenariosNotContaining: strings.Join(config.CrowdsecConfig.ExcludeScenariosContaining, ","), Origins: strings.Join(config.CrowdsecConfig.OnlyIncludeDecisionsFrom, ","), }, - UserAgent: fmt.Sprintf("crowdsec-blocklist-mirror/%s", version.String()), + UserAgent: "crowdsec-blocklist-mirror/" + version.String(), CertPath: config.CrowdsecConfig.CertPath, KeyPath: config.CrowdsecConfig.KeyPath, CAPath: config.CrowdsecConfig.CAPath, @@ -121,7 +122,7 @@ func Execute() error { g.Go(func() error { decisionStreamer.Run(ctx) - return fmt.Errorf("bouncer stream halted") + return errors.New("bouncer stream halted") }) g.Go(func() error { @@ -148,10 +149,12 @@ func Execute() error { if decisions == nil { continue } + if len(decisions.New) > 0 { log.Infof("received %d new decisions", len(decisions.New)) registry.GlobalDecisionRegistry.AddDecisions(decisions.New) } + if len(decisions.Deleted) > 0 { log.Infof("received %d expired decisions", len(decisions.Deleted)) registry.GlobalDecisionRegistry.DeleteDecisions(decisions.Deleted) diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index ece5652..4ba3e32 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -1,6 +1,7 @@ package cfg import ( + "errors" "fmt" "io" "os" @@ -66,11 +67,11 @@ type Config struct { func (cfg *Config) ValidateAndSetDefaults() error { if cfg.CrowdsecConfig.LapiKey == "" && cfg.CrowdsecConfig.CertPath == "" { - return fmt.Errorf("one of lapi_key or cert_path is required") + return errors.New("one of lapi_key or cert_path is required") } if cfg.CrowdsecConfig.LapiURL == "" { - return fmt.Errorf("lapi_url is required") + return errors.New("lapi_url is required") } if !strings.HasSuffix(cfg.CrowdsecConfig.LapiURL, "/") { diff --git a/pkg/cfg/logging.go b/pkg/cfg/logging.go index 496d656..942cc8c 100644 --- a/pkg/cfg/logging.go +++ b/pkg/cfg/logging.go @@ -1,7 +1,7 @@ package cfg import ( - "fmt" + "errors" "io" "os" "path/filepath" @@ -75,7 +75,7 @@ func (c *LoggingConfig) setDefaults() { func (c *LoggingConfig) validate() error { if c.LogMedia != "stdout" && c.LogMedia != "file" { - return fmt.Errorf("log_media should be either 'stdout' or 'file'") + return errors.New("log_media should be either 'stdout' or 'file'") } return nil diff --git a/pkg/server/logging.go b/pkg/server/logging.go index c16d641..ab76b21 100644 --- a/pkg/server/logging.go +++ b/pkg/server/logging.go @@ -138,12 +138,14 @@ func appendQuoted(buf []byte, s string) []byte { if r == rune('"') || r == '\\' { // always backslashed buf = append(buf, '\\') buf = append(buf, byte(r)) + continue } if strconv.IsPrint(r) { n := utf8.EncodeRune(runeTmp[:], r) buf = append(buf, runeTmp[:n]...) + continue } diff --git a/pkg/server/server.go b/pkg/server/server.go index 2cf1f2c..1c5e808 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -109,7 +109,7 @@ func satisfiesBasicAuth(r *http.Request, user, password string) bool { return false } - expectedVal := fmt.Sprintf("Basic %s", basicAuth(user, password)) + expectedVal := "Basic " + basicAuth(user, password) foundVal := r.Header[http.CanonicalHeaderKey("Authorization")][0] log.WithFields(log.Fields{ "expected": expectedVal,