Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint revive(deep-exit): refactor cmd/crowdsec #3063

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 10 additions & 93 deletions cmd/crowdsec/crowdsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
"context"
"fmt"
"os"
"path/filepath"
"sync"
"time"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/go-cs-lib/trace"

Expand All @@ -34,9 +32,8 @@
}

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

Check warning on line 36 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L36

Added line #L36 was not covered by tests
log.Warnf("unable to initialize GeoIP: %s", err)
}

Expand Down Expand Up @@ -79,7 +76,6 @@

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
}

Expand All @@ -96,7 +92,7 @@

bucketsTomb.Go(func() error {
bucketWg.Add(1)
/*restore previous state as well if present*/
// restore previous state as well if present

Check warning on line 95 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L95

Added line #L95 was not covered by tests
if cConfig.Crowdsec.BucketStateFile != "" {
log.Warningf("Restoring buckets state from %s", cConfig.Crowdsec.BucketStateFile)

Expand All @@ -109,12 +105,7 @@
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()
Expand All @@ -140,12 +131,7 @@
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()
Expand Down Expand Up @@ -190,104 +176,35 @@
}
}()

/*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
*/
waitOnTomb()
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)

Check warning on line 187 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L187

Added line #L187 was not covered by tests
}

log.Debugf("everything is dead, return crowdsecTomb")

if dumpStates {
dumpParserState()
dumpOverflowState()
dumpBucketsPour()
if err := dumpAllStates(); err != nil {
log.Fatal(err)
}

Check warning on line 195 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L194-L195

Added lines #L194 - L195 were not covered by tests
os.Exit(0)
}

return nil
})
}

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 {
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")
/*
Expand Down
56 changes: 56 additions & 0 deletions cmd/crowdsec/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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)

Check warning on line 17 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L17

Added line #L17 was not covered by tests
if err := dumpState(
filepath.Join(parser.DumpFolder, "parser-dump.yaml"),
parser.StageParseCache,
); err != nil {
return fmt.Errorf("while dumping parser state: %w", err)
}

Check warning on line 23 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L22-L23

Added lines #L22 - L23 were not covered by tests

if err := dumpState(
filepath.Join(parser.DumpFolder, "bucket-dump.yaml"),
bucketOverflows,
); err != nil {
return fmt.Errorf("while dumping bucket overflow state: %w", err)
}

Check warning on line 30 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L29-L30

Added lines #L29 - L30 were not covered by tests

if err := dumpState(
filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"),
leaky.BucketPourCache,
); err != nil {
return fmt.Errorf("while dumping bucket pour state: %w", err)
}

Check warning on line 37 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L36-L37

Added lines #L36 - L37 were not covered by tests

return nil
}

func dumpState(destPath string, obj any) error {
dir := filepath.Dir(destPath)

Check warning on line 44 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L44

Added line #L44 was not covered by tests
err := os.MkdirAll(dir, 0o755)
if err != nil {
return err
}

Check warning on line 48 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L47-L48

Added lines #L47 - L48 were not covered by tests

out, err := yaml.Marshal(obj)
if err != nil {
return err
}

Check warning on line 53 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L52-L53

Added lines #L52 - L53 were not covered by tests

return os.WriteFile(destPath, out, 0o666)
}
3 changes: 2 additions & 1 deletion cmd/crowdsec/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@
if flags.TestMode {
log.Infof("Configuration test done")
pluginBroker.Kill()
os.Exit(0)

Check warning on line 393 in cmd/crowdsec/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/serve.go#L393

Added line #L393 was not covered by tests
return nil
}

if cConfig.Common != nil && cConfig.Common.Daemonize {
Expand Down
Loading