Skip to content

Commit 8de00cc

Browse files
committed
Prevent duplicate decisions to call script
Signed-off-by: Shivam Sandbhor <[email protected]>
1 parent f85781f commit 8de00cc

File tree

5 files changed

+68
-79
lines changed

5 files changed

+68
-79
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ sudo ./install.sh
4343

4444
### Start
4545

46-
If your bouncer run on the same machine as your crowdsec local API, you can start the service directly since the `install.sh` took care of the configuration.
46+
If your bouncer runs on the same machine as your crowdsec local API, you can start the service directly since the `install.sh` took care of the configuration.
4747
```sh
4848
sudo systemctl start cs-custom-bouncer
4949
```
5050

5151
## Upgrade
5252

53-
## Upgrade
54-
5553
If you already have `cs-custom-bouncer` installed, please download the [latest release](https://github.com/crowdsecurity/cs-custom-bouncer/releases) and run the following commands to upgrade it:
5654

5755
```bash

custom.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ import (
1212
"github.com/crowdsecurity/crowdsec/pkg/models"
1313
)
1414

15+
type DecisionKey struct {
16+
Value string
17+
Type string
18+
}
19+
1520
type customBouncer struct {
16-
path string
21+
path string
22+
newDecisionValueSet map[DecisionKey]struct{}
23+
expiredDecisionValueSet map[DecisionKey]struct{}
1724
}
1825

1926
func newCustomBouncer(path string) (*customBouncer, error) {
@@ -22,17 +29,25 @@ func newCustomBouncer(path string) (*customBouncer, error) {
2229
}, nil
2330
}
2431

32+
func (c *customBouncer) ResetCache() {
33+
c.newDecisionValueSet = make(map[DecisionKey]struct{})
34+
c.expiredDecisionValueSet = make(map[DecisionKey]struct{})
35+
}
36+
2537
func (c *customBouncer) Init() error {
38+
c.ResetCache()
2639
return nil
2740
}
2841

2942
func (c *customBouncer) Add(decision *models.Decision) error {
43+
if _, exists := c.newDecisionValueSet[decisionToDecisionKey(decision)]; exists {
44+
return nil
45+
}
3046
banDuration, err := time.ParseDuration(*decision.Duration)
3147
if err != nil {
3248
return err
3349
}
3450
log.Printf("custom [%s] : add ban on %s for %s sec (%s)", c.path, *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario)
35-
3651
str, err := serializeDecision(decision)
3752
if err != nil {
3853
log.Warningf("serialize: %s", err)
@@ -41,15 +56,18 @@ func (c *customBouncer) Add(decision *models.Decision) error {
4156
if out, err := cmd.CombinedOutput(); err != nil {
4257
log.Infof("Error in 'add' command (%s): %v --> %s", cmd.String(), err, string(out))
4358
}
59+
c.newDecisionValueSet[decisionToDecisionKey(decision)] = struct{}{}
4460
return nil
4561
}
4662

4763
func (c *customBouncer) Delete(decision *models.Decision) error {
64+
if _, exists := c.expiredDecisionValueSet[decisionToDecisionKey(decision)]; exists {
65+
return nil
66+
}
4867
banDuration, err := time.ParseDuration(*decision.Duration)
4968
if err != nil {
5069
return err
5170
}
52-
5371
str, err := serializeDecision(decision)
5472
if err != nil {
5573
log.Warningf("serialize: %s", err)
@@ -59,6 +77,7 @@ func (c *customBouncer) Delete(decision *models.Decision) error {
5977
if out, err := cmd.CombinedOutput(); err != nil {
6078
log.Infof("Error in 'del' command (%s): %v --> %s", cmd.String(), err, string(out))
6179
}
80+
c.expiredDecisionValueSet[decisionToDecisionKey(decision)] = struct{}{}
6281
return nil
6382
}
6483

@@ -73,3 +92,10 @@ func serializeDecision(decision *models.Decision) (string, error) {
7392
}
7493
return string(serbyte), nil
7594
}
95+
96+
func decisionToDecisionKey(decision *models.Decision) DecisionKey {
97+
return DecisionKey{
98+
Value: *decision.Value,
99+
Type: *decision.Type,
100+
}
101+
}

go.mod

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ module github.com/crowdsecurity/cs-custom-bouncer
33
go 1.14
44

55
require (
6-
github.com/KyleBanks/depth v1.2.1 // indirect
7-
github.com/antonmedv/expr v1.8.9 // indirect
86
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
97
github.com/crowdsecurity/crowdsec v1.0.2
108
github.com/crowdsecurity/go-cs-bouncer v0.0.0-20210113162030-7eec88c1afa8
119
github.com/go-openapi/validate v0.20.0 // indirect
12-
github.com/hashicorp/go-version v1.2.1 // indirect
13-
github.com/jinzhu/gorm v1.9.16 // indirect
1410
github.com/mitchellh/mapstructure v1.4.1 // indirect
15-
github.com/rogpeppe/godef v1.1.2 // indirect
1611
github.com/sirupsen/logrus v1.7.0
12+
github.com/vjeantet/grok v1.0.1 // indirect
1713
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
1814
golang.org/x/sys v0.0.0-20210113131315-ba0562f347e0 // indirect
1915
golang.org/x/text v0.3.5 // indirect

0 commit comments

Comments
 (0)