Skip to content

Commit e5c3e8c

Browse files
authored
Read from input + use go 1.19 + add prometheus (#36)
1 parent a54984e commit e5c3e8c

File tree

8 files changed

+206
-52
lines changed

8 files changed

+206
-52
lines changed

.github/workflows/build-binary-package.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
name: Build and upload binary package
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Set up Go 1.13
13+
- name: Set up Go 1.19
1414
uses: actions/setup-go@v1
1515
with:
16-
go-version: 1.13
16+
go-version: 1.19
1717
id: go
1818
- name: Check out code into the Go module directory
1919
uses: actions/checkout@v2

.github/workflows/go.yml

-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ jobs:
2323
run: make build
2424
- name: Test
2525
run: go test -v
26-
27-
28-
29-
30-

config.go

+27-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,33 @@ import (
1313
"gopkg.in/yaml.v2"
1414
)
1515

16+
type PrometheusConfig struct {
17+
Enabled bool `yaml:"enabled"`
18+
ListenAddress string `yaml:"listen_addr"`
19+
ListenPort string `yaml:"listen_port"`
20+
}
21+
1622
type bouncerConfig struct {
17-
BinPath string `yaml:"bin_path"` // path to binary
18-
PidDir string `yaml:"piddir"`
19-
UpdateFrequency string `yaml:"update_frequency"`
20-
Daemon bool `yaml:"daemonize"`
21-
LogMode string `yaml:"log_mode"`
22-
LogDir string `yaml:"log_dir"`
23-
LogLevel log.Level `yaml:"log_level"`
24-
CompressLogs *bool `yaml:"compress_logs,omitempty"`
25-
LogMaxSize int `yaml:"log_max_size,omitempty"`
26-
LogMaxFiles int `yaml:"log_max_files,omitempty"`
27-
LogMaxAge int `yaml:"log_max_age,omitempty"`
28-
CacheRetentionDuration time.Duration `yaml:"cache_retention_duration"`
23+
BinPath string `yaml:"bin_path"` // path to binary
24+
PidDir string `yaml:"piddir"`
25+
UpdateFrequency string `yaml:"update_frequency"`
26+
IncludeScenariosContaining []string `yaml:"include_scenarios_containing"`
27+
ExcludeScenariosContaining []string `yaml:"exclude_scenarios_containing"`
28+
OnlyIncludeDecisionsFrom []string `yaml:"only_include_decisions_from"`
29+
Daemon bool `yaml:"daemonize"`
30+
LogMode string `yaml:"log_mode"`
31+
LogDir string `yaml:"log_dir"`
32+
LogLevel log.Level `yaml:"log_level"`
33+
LogMaxSize int `yaml:"log_max_size,omitempty"`
34+
LogMaxFiles int `yaml:"log_max_files,omitempty"`
35+
LogMaxAge int `yaml:"log_max_age,omitempty"`
36+
CompressLogs *bool `yaml:"compress_logs,omitempty"`
37+
APIUrl string `yaml:"api_url"`
38+
APIKey string `yaml:"api_key"`
39+
CacheRetentionDuration time.Duration `yaml:"cache_retention_duration"`
40+
FeedViaStdin bool `yaml:"feed_via_stdin"`
41+
TotalRetries int `yaml:"total_retries"`
42+
PrometheusConfig PrometheusConfig `yaml:"prometheus"`
2943
}
3044

3145
func NewConfig(configPath string) (*bouncerConfig, error) {
@@ -56,7 +70,7 @@ func NewConfig(configPath string) (*bouncerConfig, error) {
5670
}
5771

5872
/*Configure logging*/
59-
if err = types.SetDefaultLoggerConfig(config.LogMode, config.LogDir, config.LogLevel, config.LogMaxSize, config.LogMaxFiles, config.LogMaxAge, config.CompressLogs, false); err != nil {
73+
if err := types.SetDefaultLoggerConfig(config.LogMode, config.LogDir, config.LogLevel, config.LogMaxSize, config.LogMaxFiles, config.LogMaxAge, config.CompressLogs, false); err != nil {
6074
log.Fatal(err.Error())
6175
}
6276
if config.LogMode == "file" {

config/crowdsec-custom-bouncer.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
bin_path: ${BINARY_PATH}
2+
feed_via_stdin: false # Invokes binary once and feeds incoming decisions to it's stdin.
3+
total_retries: 0 # number of times to restart binary. relevant if feed_via_stdin=true . Set to -1 for infinite retries.
4+
include_scenarios_containing: [] # ignore IPs banned for triggering scenarios not containing either of provided word, eg ["ssh", "http"]
5+
exclude_scenarios_containing: [] # ignore IPs banned for triggering scenarios containing either of provided word
6+
only_include_decisions_from: []
27
piddir: /var/run/
38
update_frequency: 10s
49
cache_retention_duration: 10s
510
daemonize: true
611
log_mode: file
712
log_dir: /var/log/
813
log_level: info
14+
log_compression: true
15+
log_max_size: 100
16+
log_max_backups: 3
17+
log_max_age: 30
918
api_url: http://localhost:8080/
1019
api_key: ${API_KEY}
20+
21+
prometheus:
22+
enabled: true
23+
listen_addr: 127.0.0.1
24+
listen_port: 60602

custom.go

+34-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"os/exec"
78
"strconv"
89
"time"
@@ -17,15 +18,23 @@ type DecisionKey struct {
1718
Type string
1819
}
1920

21+
type DecisionWithAction struct {
22+
models.Decision
23+
Action string `json:"action,omitempty"`
24+
}
25+
2026
type customBouncer struct {
2127
path string
28+
binaryStdin io.Writer
29+
feedViaStdin bool
2230
newDecisionValueSet map[DecisionKey]struct{}
2331
expiredDecisionValueSet map[DecisionKey]struct{}
2432
}
2533

26-
func newCustomBouncer(path string) (*customBouncer, error) {
34+
func newCustomBouncer(cfg *bouncerConfig) (*customBouncer, error) {
2735
return &customBouncer{
28-
path: path,
36+
path: cfg.BinPath,
37+
feedViaStdin: cfg.FeedViaStdin,
2938
}, nil
3039
}
3140

@@ -53,10 +62,19 @@ func (c *customBouncer) Add(decision *models.Decision) error {
5362
return err
5463
}
5564
log.Debugf("custom [%s] : add ban on %s for %s sec (%s)", c.path, *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario)
56-
str, err := serializeDecision(decision)
65+
var str string
66+
if c.feedViaStdin {
67+
str, err = serializeDecision(decision, "add")
68+
} else {
69+
str, err = serializeDecision(decision, "")
70+
}
5771
if err != nil {
5872
log.Warningf("serialize: %s", err)
5973
}
74+
if c.feedViaStdin {
75+
fmt.Fprintln(c.binaryStdin, str)
76+
return nil
77+
}
6078
cmd := exec.Command(c.path, "add", *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario, str)
6179
if out, err := cmd.CombinedOutput(); err != nil {
6280
log.Errorf("Error in 'add' command (%s): %v --> %s", cmd.String(), err, string(out))
@@ -73,7 +91,16 @@ func (c *customBouncer) Delete(decision *models.Decision) error {
7391
if err != nil {
7492
return err
7593
}
76-
str, err := serializeDecision(decision)
94+
var str string
95+
if c.feedViaStdin {
96+
str, err = serializeDecision(decision, "del")
97+
} else {
98+
str, err = serializeDecision(decision, "")
99+
}
100+
if c.feedViaStdin {
101+
fmt.Fprintln(c.binaryStdin, str)
102+
return nil
103+
}
77104
if err != nil {
78105
log.Warningf("serialize: %s", err)
79106
}
@@ -90,8 +117,9 @@ func (c *customBouncer) ShutDown() error {
90117
return nil
91118
}
92119

93-
func serializeDecision(decision *models.Decision) (string, error) {
94-
serbyte, err := json.Marshal(decision)
120+
func serializeDecision(decision *models.Decision, action string) (string, error) {
121+
d := DecisionWithAction{Decision: *decision, Action: action}
122+
serbyte, err := json.Marshal(d)
95123
if err != nil {
96124
return "", fmt.Errorf("serialize error : %s", err)
97125
}

go.mod

+16-7
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ go 1.19
55
require (
66
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
77
github.com/crowdsecurity/crowdsec v1.4.1
8-
github.com/crowdsecurity/go-cs-bouncer v0.0.0-20220808104920-19304be490bc
8+
github.com/crowdsecurity/go-cs-bouncer v0.0.0-20220817075151-29237cbe9873
9+
github.com/go-openapi/swag v0.22.3 // indirect
10+
github.com/prometheus/client_golang v1.13.0
911
github.com/sirupsen/logrus v1.9.0
12+
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
13+
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
1014
gopkg.in/natefinch/lumberjack.v2 v2.0.0
1115
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
1216
gopkg.in/yaml.v2 v2.4.0
@@ -15,25 +19,30 @@ require (
1519
require (
1620
github.com/antonmedv/expr v1.9.0 // indirect
1721
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
22+
github.com/beorn7/perks v1.0.1 // indirect
23+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
1824
github.com/crowdsecurity/grokky v0.1.0 // indirect
1925
github.com/go-openapi/analysis v0.21.4 // indirect
20-
github.com/go-openapi/errors v0.20.2 // indirect
26+
github.com/go-openapi/errors v0.20.3 // indirect
2127
github.com/go-openapi/jsonpointer v0.19.5 // indirect
2228
github.com/go-openapi/jsonreference v0.20.0 // indirect
23-
github.com/go-openapi/loads v0.21.1 // indirect
24-
github.com/go-openapi/spec v0.20.6 // indirect
29+
github.com/go-openapi/loads v0.21.2 // indirect
30+
github.com/go-openapi/spec v0.20.7 // indirect
2531
github.com/go-openapi/strfmt v0.21.3 // indirect
26-
github.com/go-openapi/swag v0.22.0 // indirect
2732
github.com/go-openapi/validate v0.22.0 // indirect
33+
github.com/golang/protobuf v1.5.2 // indirect
2834
github.com/google/go-querystring v1.1.0 // indirect
2935
github.com/hashicorp/go-version v1.6.0 // indirect
3036
github.com/josharian/intern v1.0.0 // indirect
3137
github.com/mailru/easyjson v0.7.7 // indirect
38+
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
3239
github.com/mitchellh/mapstructure v1.5.0 // indirect
3340
github.com/oklog/ulid v1.3.1 // indirect
3441
github.com/pkg/errors v0.9.1 // indirect
42+
github.com/prometheus/client_model v0.2.0 // indirect
43+
github.com/prometheus/common v0.37.0 // indirect
44+
github.com/prometheus/procfs v0.8.0 // indirect
3545
go.mongodb.org/mongo-driver v1.10.1 // indirect
36-
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
37-
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
46+
google.golang.org/protobuf v1.28.1 // indirect
3847
gopkg.in/yaml.v3 v3.0.1 // indirect
3948
)

0 commit comments

Comments
 (0)