From fe689146281049440b57a4de584cf225a0ea76a2 Mon Sep 17 00:00:00 2001 From: Thibault bui Koechlin Date: Wed, 20 May 2020 11:00:25 +0200 Subject: [PATCH] more linter fixes (simplicity mostly) --- cmd/crowdsec-cli/api.go | 8 ++------ cmd/crowdsec/acquisition.go | 3 ++- pkg/acquisition/file_reader.go | 4 ++-- pkg/leakybucket/bucket.go | 4 ++-- pkg/parser/node.go | 4 ++-- pkg/parser/runtime.go | 6 ++---- plugins/backend/sqlite.go | 6 +----- 7 files changed, 13 insertions(+), 22 deletions(-) diff --git a/cmd/crowdsec-cli/api.go b/cmd/crowdsec-cli/api.go index 1b9e73159..7ad35b554 100644 --- a/cmd/crowdsec-cli/api.go +++ b/cmd/crowdsec-cli/api.go @@ -10,7 +10,6 @@ import ( "github.com/crowdsecurity/crowdsec/pkg/cwhub" "github.com/crowdsecurity/crowdsec/pkg/outputs" - "github.com/crowdsecurity/crowdsec/pkg/sqlite" "github.com/crowdsecurity/crowdsec/pkg/types" "github.com/denisbrodbeck/machineid" @@ -28,13 +27,10 @@ var ( var ( apiConfigFile = "api.yaml" + userID string // for flag parsing + outputCTX *outputs.Output ) -var userID string // for flag parsing -var dbctx *sqlite.Context - -var outputCTX *outputs.Output - func dumpCredentials() error { if config.output == "json" { credsYaml, err := json.Marshal(&outputCTX.API.Creds) diff --git a/cmd/crowdsec/acquisition.go b/cmd/crowdsec/acquisition.go index 55b1d411d..246cf3945 100644 --- a/cmd/crowdsec/acquisition.go +++ b/cmd/crowdsec/acquisition.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/crowdsecurity/crowdsec/pkg/acquisition" ) @@ -25,7 +26,7 @@ func loadAcquisition() (*acquisition.FileAcquisCtx, error) { if acquisitionCTX == nil { return nil, fmt.Errorf("no inputs to process") } - if cConfig.Profiling == true { + if cConfig.Profiling { acquisitionCTX.Profiling = true } diff --git a/pkg/acquisition/file_reader.go b/pkg/acquisition/file_reader.go index 186c27223..3c09a2a31 100644 --- a/pkg/acquisition/file_reader.go +++ b/pkg/acquisition/file_reader.go @@ -172,7 +172,7 @@ func AcquisStartReading(ctx *FileAcquisCtx, output chan types.Event, AcquisTomb /* start one go routine reading for each file, and pushing to chan output */ for idx, fctx := range ctx.Files { log.Printf("starting reader file %d/%d : %s", idx, len(ctx.Files), fctx.Filename) - if ctx.Profiling == true { + if ctx.Profiling { fctx.Profiling = true } fctx := fctx @@ -227,7 +227,7 @@ LOOP: if line.Text == "" { //skip empty lines continue } - if ctx.Profiling == true { + if ctx.Profiling { ReaderHits.With(prometheus.Labels{"source": ctx.Filename}).Inc() } l.Raw = line.Text diff --git a/pkg/leakybucket/bucket.go b/pkg/leakybucket/bucket.go index 183fed00b..18913f191 100644 --- a/pkg/leakybucket/bucket.go +++ b/pkg/leakybucket/bucket.go @@ -405,9 +405,9 @@ func FormatOverflow(l *Leaky, queue *Queue) types.SignalOccurence { am = fmt.Sprintf("%d IPs", len(sig.Sources)) } else if len(sig.Sources) == 1 { if sig.Source != nil { - am = fmt.Sprintf("%s", sig.Source.Ip.String()) + am = sig.Source.Ip.String() } else { - am = fmt.Sprintf("??") + am = "??" } } else { am = "UNKNOWN" diff --git a/pkg/parser/node.go b/pkg/parser/node.go index d51ce407c..98623561d 100644 --- a/pkg/parser/node.go +++ b/pkg/parser/node.go @@ -185,10 +185,10 @@ func (n *Node) process(p *types.Event, ctx UnixParserCtx) (bool, error) { clog.Debugf("Event leaving node : ko") return false, nil } - switch output.(type) { + switch out := output.(type) { case bool: /* filter returned false, don't process Node */ - if output.(bool) { + if out { clog.Infof("Event is whitelisted by Expr !") p.Whitelisted = true set = true diff --git a/pkg/parser/runtime.go b/pkg/parser/runtime.go index e87871886..cfe6b2981 100644 --- a/pkg/parser/runtime.go +++ b/pkg/parser/runtime.go @@ -39,10 +39,9 @@ func SetTargetByName(target string, value string, evt *types.Event) bool { if evt == nil { return false } + //it's a hack, we do it for the user - if strings.HasPrefix(target, "evt.") { - target = target[4:] - } + target = strings.TrimPrefix(target, "evt.") log.Debugf("setting target %s to %s", target, value) defer func() { @@ -68,7 +67,6 @@ func SetTargetByName(target string, value string, evt *types.Event) bool { /*if we're in a map and the field doesn't exist, the user wants to add it :) */ if (tmp == reflect.Value{}) || tmp.IsZero() { log.Debugf("map entry is zero in '%s'", target) - //return false } iter.SetMapIndex(reflect.ValueOf(f), reflect.ValueOf(value)) return true diff --git a/plugins/backend/sqlite.go b/plugins/backend/sqlite.go index 8b8579c74..0af1343eb 100644 --- a/plugins/backend/sqlite.go +++ b/plugins/backend/sqlite.go @@ -28,7 +28,7 @@ func (p *pluginDB) Delete(target string) (int, error) { if err != nil { return 0, err } - log.Debugf("deleted '%s' entry from database", nbDel) + log.Debugf("deleted '%d' entry from database", nbDel) return nbDel, nil } @@ -65,9 +65,5 @@ func (p *pluginDB) ReadAT(timeAT time.Time) ([]map[string]string, error) { return ret, nil } -func New() interface{} { - return &pluginDB{} -} - // empty main function is mandatory since we are in a main package func main() {}