Skip to content

Commit

Permalink
bin/crowdsec: avoid writing errors twice when log_media=stdout (#2729)
Browse files Browse the repository at this point in the history
* bin/crowdsec: avoid writing errors twice when log_media=stdout
* lint
  • Loading branch information
mmetc authored Jan 12, 2024
1 parent fca8883 commit 0ef5f20
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
43 changes: 43 additions & 0 deletions cmd/crowdsec/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"io"
"os"

log "github.com/sirupsen/logrus"
)

type ConditionalHook struct {
Writer io.Writer
LogLevels []log.Level
Enabled bool
}

func (hook *ConditionalHook) Fire(entry *log.Entry) error {
if hook.Enabled {
line, err := entry.String()
if err != nil {
return err
}

_, err = hook.Writer.Write([]byte(line))

return err
}

return nil
}

func (hook *ConditionalHook) Levels() []log.Level {
return hook.LogLevels
}

// The primal logging hook is set up before parsing config.yaml.
// Once config.yaml is parsed, the primal hook is disabled if the
// configured logger is writing to stderr. Otherwise it's used to
// report fatal errors and panics to stderr in addition to the log file.
var primalHook = &ConditionalHook{
Writer: os.Stderr,
LogLevels: []log.Level{log.FatalLevel, log.PanicLevel},
Enabled: true,
}
15 changes: 15 additions & 0 deletions cmd/crowdsec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
err error
files []string
)

for _, hubScenarioItem := range hub.GetItemMap(cwhub.SCENARIOS) {
if hubScenarioItem.State.Installed {
files = append(files, hubScenarioItem.State.LocalPath)
}
}

buckets = leakybucket.NewBuckets()

log.Infof("Loading %d scenario files", len(files))
Expand All @@ -99,6 +101,7 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
holders[holderIndex].Profiling = true
}
}

return nil
}

Expand Down Expand Up @@ -143,8 +146,10 @@ func (l labelsMap) Set(label string) error {
if len(split) != 2 {
return fmt.Errorf("invalid format for label '%s', must be key:value", pair)
}

l[split[0]] = split[1]
}

return nil
}

Expand All @@ -168,9 +173,11 @@ func (f *Flags) Parse() {
flag.BoolVar(&f.DisableAPI, "no-api", false, "disable local API")
flag.BoolVar(&f.DisableCAPI, "no-capi", false, "disable communication with Central API")
flag.BoolVar(&f.OrderEvent, "order-event", false, "enforce event ordering with significant performance cost")

if runtime.GOOS == "windows" {
flag.StringVar(&f.WinSvc, "winsvc", "", "Windows service Action: Install, Remove etc..")
}

flag.StringVar(&dumpFolder, "dump-data", "", "dump parsers/buckets raw outputs")
flag.Parse()
}
Expand Down Expand Up @@ -205,6 +212,7 @@ func newLogLevel(curLevelPtr *log.Level, f *Flags) *log.Level {
// avoid returning a new ptr to the same value
return curLevelPtr
}

return &ret
}

Expand Down Expand Up @@ -238,6 +246,8 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
return nil, err
}

primalHook.Enabled = (cConfig.Common.LogMedia != "stdout")

if err := csconfig.LoadFeatureFlagsFile(configFile, log.StandardLogger()); err != nil {
return nil, err
}
Expand Down Expand Up @@ -282,6 +292,7 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
if cConfig.DisableAPI {
cConfig.Common.Daemonize = false
}

log.Infof("single file mode : log_media=%s daemonize=%t", cConfig.Common.LogMedia, cConfig.Common.Daemonize)
}

Expand All @@ -291,6 +302,7 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo

if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
log.Debug("Daemonization is not supported on Windows, disabling")

cConfig.Common.Daemonize = false
}

Expand All @@ -308,6 +320,8 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
var crowdsecT0 time.Time

func main() {
log.AddHook(primalHook)

if err := fflag.RegisterAllFeatures(); err != nil {
log.Fatalf("failed to register features: %s", err)
}
Expand Down Expand Up @@ -342,5 +356,6 @@ func main() {
if err != nil {
log.Fatal(err)
}

os.Exit(0)
}
16 changes: 4 additions & 12 deletions cmd/crowdsec/run_in_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ package main

import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"

"github.com/crowdsecurity/go-cs-lib/trace"
"github.com/crowdsecurity/go-cs-lib/version"
Expand All @@ -24,16 +22,6 @@ func StartRunSvc() error {

defer trace.CatchPanic("crowdsec/StartRunSvc")

// Set a default logger with level=fatal on stderr,
// in addition to the one we configure afterwards
log.AddHook(&writer.Hook{
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
},
})

if cConfig, err = LoadConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI, false); err != nil {
return err
}
Expand All @@ -46,6 +34,7 @@ func StartRunSvc() error {
// Enable profiling early
if cConfig.Prometheus != nil {
var dbClient *database.Client

var err error

if cConfig.DbConfig != nil {
Expand All @@ -55,8 +44,11 @@ func StartRunSvc() error {
return fmt.Errorf("unable to create database client: %s", err)
}
}

registerPrometheus(cConfig.Prometheus)

go servePrometheus(cConfig.Prometheus, dbClient, apiReady, agentReady)
}

return Serve(cConfig, apiReady, agentReady)
}

0 comments on commit 0ef5f20

Please sign in to comment.