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 1 commit
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
94 changes: 6 additions & 88 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 Down Expand Up @@ -79,7 +77,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 Down Expand Up @@ -109,12 +106,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 +132,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 @@ -198,91 +185,22 @@
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 188 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L188

Added line #L188 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 196 in cmd/crowdsec/crowdsec.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/crowdsec.go#L195-L196

Added lines #L195 - L196 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 {
Expand Down
57 changes: 57 additions & 0 deletions cmd/crowdsec/dump.go
Original file line number Diff line number Diff line change
@@ -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)

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 31 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L29-L31

Added lines #L29 - L31 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 38 in cmd/crowdsec/dump.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L37-L38

Added lines #L37 - L38 were not covered by tests

return nil
}

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L45

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L48-L49

Added lines #L48 - L49 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec/dump.go#L53-L54

Added lines #L53 - L54 were not covered by tests

return os.WriteFile(destPath, out, 0o666)
}
2 changes: 1 addition & 1 deletion cmd/crowdsec/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading