forked from crowdsecurity/cs-custom-bouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (129 loc) · 3.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/coreos/go-systemd/daemon"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"github.com/crowdsecurity/crowdsec-custom-bouncer/pkg/version"
csbouncer "github.com/crowdsecurity/go-cs-bouncer"
"gopkg.in/tomb.v2"
)
const (
name = "crowdsec-custom-bouncer"
)
var t tomb.Tomb
func termHandler(sig os.Signal, custom *customBouncer) error {
if err := custom.ShutDown(); err != nil {
return err
}
return nil
}
func HandleSignals(custom *customBouncer) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan,
syscall.SIGTERM)
exitChan := make(chan int)
go func() {
for {
s := <-signalChan
switch s {
// kill -SIGTERM XXXX
case syscall.SIGTERM:
if err := termHandler(s, custom); err != nil {
log.Fatalf("shutdown fail: %s", err)
}
exitChan <- 0
}
}
}()
code := <-exitChan
log.Infof("Shutting down custom-bouncer service")
os.Exit(code)
}
func main() {
var err error
log.Infof("crowdsec-custom-bouncer %s", version.VersionStr())
configPath := flag.String("c", "", "path to crowdsec-custom-bouncer.yaml")
verbose := flag.Bool("v", false, "set verbose mode")
flag.Parse()
if configPath == nil || *configPath == "" {
log.Fatalf("configuration file is required")
}
log.AddHook(&writer.Hook{ // Send logs with level fatal to stderr
Writer: os.Stderr,
LogLevels: []log.Level{
log.PanicLevel,
log.FatalLevel,
},
})
config, err := NewConfig(*configPath)
if err != nil {
log.Fatalf("unable to load configuration: %s", err)
}
if *verbose {
log.SetLevel(log.DebugLevel)
}
custom, err := newCustomBouncer(config.BinPath)
if err != nil {
log.Fatalf(err.Error())
}
if err := custom.Init(); err != nil {
log.Fatalf(err.Error())
}
bouncer := &csbouncer.StreamBouncer{
APIKey: config.APIKey,
APIUrl: config.APIUrl,
TickerInterval: config.UpdateFrequency,
UserAgent: fmt.Sprintf("%s/%s", name, version.VersionStr()),
}
if err := bouncer.Init(); err != nil {
log.Fatalf(err.Error())
}
cacheResetTicker := time.NewTicker(config.CacheRetentionDuration)
go bouncer.Run()
t.Go(func() error {
log.Printf("Processing new and deleted decisions . . .")
for {
select {
case <-t.Dying():
log.Infoln("terminating bouncer process")
return nil
case decisions := <-bouncer.Stream:
log.Infof("deleting '%d' decisions", len(decisions.Deleted))
for _, decision := range decisions.Deleted {
if err := custom.Delete(decision); err != nil {
log.Errorf("unable to delete decision for '%s': %s", *decision.Value, err)
} else {
log.Debugf("deleted '%s'", *decision.Value)
}
}
log.Infof("adding '%d' decisions", len(decisions.New))
for _, decision := range decisions.New {
if err := custom.Add(decision); err != nil {
log.Errorf("unable to insert decision for '%s': %s", *decision.Value, err)
} else {
log.Debugf("Adding '%s' for '%s'", *decision.Value, *decision.Duration)
}
}
case <-cacheResetTicker.C:
custom.ResetCache()
}
}
})
if config.Daemon == true {
sent, err := daemon.SdNotify(false, "READY=1")
if !sent && err != nil {
log.Errorf("Failed to notify: %v", err)
}
HandleSignals(custom)
}
err = t.Wait()
if err != nil {
log.Fatalf("process return with error: %s", err)
}
}