Skip to content

Commit

Permalink
Correct backend cleanup and return code (#268)
Browse files Browse the repository at this point in the history
The backend cleanup was only called from the signal handler, defer was ignored. Now there's proper messages and return codes

time="03-05-2023 12:43:02" level=info msg="6987 decisions added"
time="03-05-2023 12:43:10" level=info msg="Shutting down backend"
time="03-05-2023 12:43:10" level=info msg="removing 'crowdsec' table"
time="03-05-2023 12:43:10" level=info msg="removing 'crowdsec6' table"
time="03-05-2023 12:43:10" level=fatal msg="process terminating with error: received SIGTERM"
  • Loading branch information
mmetc authored May 4, 2023
1 parent 9f27212 commit 8b3009d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
49 changes: 22 additions & 27 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,23 @@ const (
name = "crowdsec-firewall-bouncer"
)

func termHandler(sig os.Signal, backend *backend.BackendCTX) error {
if err := backend.ShutDown(); err != nil {
return err
}
return nil
}

func backendCleanup(backend *backend.BackendCTX) {
log.Info("Shutting down backend")
if err := backend.ShutDown(); err != nil {
log.Errorf("unable to shutdown backend: %s", err)
}
}

func HandleSignals(backend *backend.BackendCTX) {
func HandleSignals(ctx context.Context) error {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM)

s := <-signalChan
if err := termHandler(s, backend); err != nil {
log.Fatalf("shutdown fail: %s", err)
select {
case <-signalChan:
return fmt.Errorf("received SIGTERM")
case <-ctx.Done():
return ctx.Err()
}
log.Infof("Shutting down firewall-bouncer service")
os.Exit(0)
}

func deleteDecisions(backend *backend.BackendCTX, decisions []*models.Decision, config *cfg.BouncerConfig) {
Expand Down Expand Up @@ -119,7 +113,7 @@ func addDecisions(backend *backend.BackendCTX, decisions []*models.Decision, con
}
}

func Execute() {
func Execute() error {
var err error
configPath := flag.String("c", "", "path to crowdsec-firewall-bouncer.yaml")
verbose := flag.Bool("v", false, "set verbose mode")
Expand All @@ -136,17 +130,17 @@ func Execute() {
log.Infof("crowdsec-firewall-bouncer %s", version.VersionStr())

if configPath == nil || *configPath == "" {
log.Fatalf("configuration file is required")
return fmt.Errorf("configuration file is required")
}

configBytes, err := cfg.MergedConfig(*configPath)
if err != nil {
log.Fatalf("unable to read config file: %s", err)
return fmt.Errorf("unable to read config file: %w", err)
}

config, err := cfg.NewConfig(bytes.NewReader(configBytes))
if err != nil {
log.Fatalf("unable to load configuration: %s", err)
return fmt.Errorf("unable to load configuration: %w", err)
}

if *verbose {
Expand All @@ -155,7 +149,7 @@ func Execute() {

backend, err := backend.NewBackend(config)
if err != nil {
log.Fatal(err)
return err
}

if *testConfig {
Expand All @@ -164,21 +158,19 @@ func Execute() {
}

if err = backend.Init(); err != nil {
log.Fatal(err)
return err
}
// No call to fatalf after this point

defer backendCleanup(backend)

bouncer := &csbouncer.StreamBouncer{}
err = bouncer.ConfigReader(bytes.NewReader(configBytes))
if err != nil {
log.Errorf("unable to configure bouncer: %s", err)
return
return fmt.Errorf("unable to configure bouncer: %w", err)
}
bouncer.UserAgent = fmt.Sprintf("%s/%s", name, version.VersionStr())
if err := bouncer.Init(); err != nil {
log.Error(err)
return
return err
}

if bouncer.InsecureSkipVerify != nil {
Expand Down Expand Up @@ -213,7 +205,6 @@ func Execute() {
for {
select {
case <-ctx.Done():
log.Info("terminating bouncer process")
return nil
case decisions := <-bouncer.Stream:
if decisions == nil {
Expand All @@ -230,10 +221,14 @@ func Execute() {
if !sent && err != nil {
log.Errorf("Failed to notify: %v", err)
}
go HandleSignals(backend)
g.Go(func() error {
return HandleSignals(ctx)
})
}

if err := g.Wait(); err != nil {
log.Errorf("process return with error: %s", err)
return fmt.Errorf("process terminated with error: %w", err)
}

return nil
}
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package main

import "github.com/crowdsecurity/cs-firewall-bouncer/cmd"
import (
log "github.com/sirupsen/logrus"

"github.com/crowdsecurity/cs-firewall-bouncer/cmd"
)

func main() {
cmd.Execute()
err := cmd.Execute()
if err != nil {
log.Fatal(err)
}
}

0 comments on commit 8b3009d

Please sign in to comment.