From eab2925af3ad62a92b7493ab1e42acefc9807652 Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 6 Jun 2024 14:34:30 +0200 Subject: [PATCH 1/2] lint revive(deep-exit): refactor cmd/crowdsec --- .golangci.yml | 4 ++ cmd/crowdsec/crowdsec.go | 94 +++------------------------------------- cmd/crowdsec/dump.go | 57 ++++++++++++++++++++++++ cmd/crowdsec/serve.go | 2 +- 4 files changed, 68 insertions(+), 89 deletions(-) create mode 100644 cmd/crowdsec/dump.go diff --git a/.golangci.yml b/.golangci.yml index ba54bfbbd89..c096ef5f88a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -497,6 +497,10 @@ issues: - revive path: pkg/metabase/metabase.go + - linters: + - revive + path: pkg/metabase/container.go + - linters: - revive path: cmd/crowdsec-cli/copyfile.go diff --git a/cmd/crowdsec/crowdsec.go b/cmd/crowdsec/crowdsec.go index d226e3a5796..4430c246cb4 100644 --- a/cmd/crowdsec/crowdsec.go +++ b/cmd/crowdsec/crowdsec.go @@ -4,12 +4,10 @@ import ( "context" "fmt" "os" - "path/filepath" "sync" "time" log "github.com/sirupsen/logrus" - "gopkg.in/yaml.v3" "github.com/crowdsecurity/go-cs-lib/trace" @@ -79,7 +77,6 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H if err := runParse(inputLineChan, inputEventChan, *parsers.Ctx, parsers.Nodes); err != nil { // this error will never happen as parser.Parse is not able to return errors - log.Fatalf("starting parse error : %s", err) return err } @@ -109,12 +106,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H bucketsTomb.Go(func() error { defer trace.CatchPanic("crowdsec/runPour") - if err := runPour(inputEventChan, holders, buckets, cConfig); err != nil { - log.Fatalf("starting pour error : %s", err) - return err - } - - return nil + return runPour(inputEventChan, holders, buckets, cConfig) }) } bucketWg.Done() @@ -140,12 +132,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H outputsTomb.Go(func() error { defer trace.CatchPanic("crowdsec/runOutput") - if err := runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, apiClient); err != nil { - log.Fatalf("starting outputs error : %s", err) - return err - } - - return nil + return runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, apiClient) }) } outputWg.Done() @@ -198,15 +185,15 @@ func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub log.Debugf("Shutting down crowdsec routines") if err := ShutdownCrowdsecRoutines(); err != nil { - log.Fatalf("unable to shutdown crowdsec routines: %s", err) + return fmt.Errorf("unable to shutdown crowdsec routines: %w", err) } log.Debugf("everything is dead, return crowdsecTomb") if dumpStates { - dumpParserState() - dumpOverflowState() - dumpBucketsPour() + if err := dumpAllStates(); err != nil { + log.Fatal(err) + } os.Exit(0) } @@ -214,75 +201,6 @@ func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub }) } -func dumpBucketsPour() { - fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666) - if err != nil { - log.Fatalf("open: %s", err) - } - - out, err := yaml.Marshal(leaky.BucketPourCache) - if err != nil { - log.Fatalf("marshal: %s", err) - } - - b, err := fd.Write(out) - if err != nil { - log.Fatalf("write: %s", err) - } - - log.Tracef("wrote %d bytes", b) - - if err := fd.Close(); err != nil { - log.Fatalf(" close: %s", err) - } -} - -func dumpParserState() { - fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "parser-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666) - if err != nil { - log.Fatalf("open: %s", err) - } - - out, err := yaml.Marshal(parser.StageParseCache) - if err != nil { - log.Fatalf("marshal: %s", err) - } - - b, err := fd.Write(out) - if err != nil { - log.Fatalf("write: %s", err) - } - - log.Tracef("wrote %d bytes", b) - - if err := fd.Close(); err != nil { - log.Fatalf(" close: %s", err) - } -} - -func dumpOverflowState() { - fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucket-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666) - if err != nil { - log.Fatalf("open: %s", err) - } - - out, err := yaml.Marshal(bucketOverflows) - if err != nil { - log.Fatalf("marshal: %s", err) - } - - b, err := fd.Write(out) - if err != nil { - log.Fatalf("write: %s", err) - } - - log.Tracef("wrote %d bytes", b) - - if err := fd.Close(); err != nil { - log.Fatalf(" close: %s", err) - } -} - func waitOnTomb() { for { select { diff --git a/cmd/crowdsec/dump.go b/cmd/crowdsec/dump.go new file mode 100644 index 00000000000..ff481633cda --- /dev/null +++ b/cmd/crowdsec/dump.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + + log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" + + leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket" + "github.com/crowdsecurity/crowdsec/pkg/parser" +) + +func dumpAllStates() error { + log.Debugf("Dumping parser+bucket states to %s", parser.DumpFolder) + + if err := dumpState( + filepath.Join(parser.DumpFolder, "parser-dump.yaml"), + parser.StageParseCache, + ); err != nil { + return fmt.Errorf("while dumping parser state: %w", err) + } + + if err := dumpState( + filepath.Join(parser.DumpFolder, "bucket-dump.yaml"), + bucketOverflows, + ); err != nil { + return fmt.Errorf("while dumping bucket overflow state: %w", err) + + } + + if err := dumpState( + filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"), + leaky.BucketPourCache, + ); err != nil { + return fmt.Errorf("while dumping bucket pour state: %w", err) + } + + return nil +} + +func dumpState(destPath string, obj any) error { + dir := filepath.Dir(destPath) + + err := os.MkdirAll(dir, 0o755) + if err != nil { + return err + } + + out, err := yaml.Marshal(obj) + if err != nil { + return err + } + + return os.WriteFile(destPath, out, 0o666) +} diff --git a/cmd/crowdsec/serve.go b/cmd/crowdsec/serve.go index 497215d74a1..96271c8605d 100644 --- a/cmd/crowdsec/serve.go +++ b/cmd/crowdsec/serve.go @@ -390,7 +390,7 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error { if flags.TestMode { log.Infof("Configuration test done") pluginBroker.Kill() - os.Exit(0) + return nil } if cConfig.Common != nil && cConfig.Common.Daemonize { From 3482e4dca8888c8a57c53a5ac84f509910393d11 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 7 Jun 2024 10:47:55 +0200 Subject: [PATCH 2/2] lint --- cmd/crowdsec/crowdsec.go | 9 ++++----- cmd/crowdsec/dump.go | 1 - cmd/crowdsec/serve.go | 1 + 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/crowdsec/crowdsec.go b/cmd/crowdsec/crowdsec.go index 4430c246cb4..0fd269b3537 100644 --- a/cmd/crowdsec/crowdsec.go +++ b/cmd/crowdsec/crowdsec.go @@ -32,9 +32,8 @@ func initCrowdsec(cConfig *csconfig.Config, hub *cwhub.Hub) (*parser.Parsers, [] } err = exprhelpers.GeoIPInit(hub.GetDataDir()) - if err != nil { - //GeoIP databases are not mandatory, do not make crowdsec fail if they are not present + // GeoIP databases are not mandatory, do not make crowdsec fail if they are not present log.Warnf("unable to initialize GeoIP: %s", err) } @@ -93,7 +92,7 @@ func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.H bucketsTomb.Go(func() error { bucketWg.Add(1) - /*restore previous state as well if present*/ + // restore previous state as well if present if cConfig.Crowdsec.BucketStateFile != "" { log.Warningf("Restoring buckets state from %s", cConfig.Crowdsec.BucketStateFile) @@ -177,7 +176,7 @@ func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub } }() - /*we should stop in two cases : + /* we should stop in two cases : - crowdsecTomb has been Killed() : it might be shutdown or reload, so stop - acquisTomb is dead, it means that we were in "cat" mode and files are done reading, quit */ @@ -205,7 +204,7 @@ func waitOnTomb() { for { select { case <-acquisTomb.Dead(): - /*if it's acquisition dying it means that we were in "cat" mode. + /* if it's acquisition dying it means that we were in "cat" mode. while shutting down, we need to give time for all buckets to process in flight data*/ log.Info("Acquisition is finished, shutting down") /* diff --git a/cmd/crowdsec/dump.go b/cmd/crowdsec/dump.go index ff481633cda..33c65878b11 100644 --- a/cmd/crowdsec/dump.go +++ b/cmd/crowdsec/dump.go @@ -27,7 +27,6 @@ func dumpAllStates() error { bucketOverflows, ); err != nil { return fmt.Errorf("while dumping bucket overflow state: %w", err) - } if err := dumpState( diff --git a/cmd/crowdsec/serve.go b/cmd/crowdsec/serve.go index 96271c8605d..da79e50c427 100644 --- a/cmd/crowdsec/serve.go +++ b/cmd/crowdsec/serve.go @@ -390,6 +390,7 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error { if flags.TestMode { log.Infof("Configuration test done") pluginBroker.Kill() + return nil }