Skip to content

Commit eacc819

Browse files
authored
cmd/crowdsec: assign overflow after parsing (#4225)
1 parent 6787990 commit eacc819

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

cmd/crowdsec/output.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,8 @@ func runOutput(
105105
}
106106
return nil
107107
case event := <-overflow:
108-
ov := event.Overflow
109108
// if alert is empty and mapKey is present, the overflow is just to cleanup bucket
110-
if ov.Alert == nil && ov.Mapkey != "" {
109+
if event.Overflow.Alert == nil && event.Overflow.Mapkey != "" {
111110
buckets.Bucket_map.Delete(event.Overflow.Mapkey)
112111
break
113112
}
@@ -118,6 +117,7 @@ func runOutput(
118117
return fmt.Errorf("postoverflow failed: %w", err)
119118
}
120119

120+
ov := event.Overflow
121121
log.Info(*ov.Alert.Message)
122122

123123
// if the Alert is nil, it's to signal bucket is ready for GC, don't track this

test/bats/41-postoverflows.bats

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bats
2+
3+
set -u
4+
5+
fake_log() {
6+
for _ in $(seq 1 6); do
7+
echo "$(LC_ALL=C date '+%b %d %H:%M:%S ')"'sd-126005 sshd[12422]: Invalid user netflix from 1.1.1.172 port 35424'
8+
done
9+
}
10+
11+
setup_file() {
12+
load "../lib/setup_file.sh"
13+
# we reset config and data, and only run the daemon once for all the tests in this file
14+
./instance-data load
15+
16+
cscli collections install crowdsecurity/sshd --error >/dev/null
17+
cscli parsers install crowdsecurity/syslog-logs --error >/dev/null
18+
cscli parsers install crowdsecurity/dateparse-enrich --error >/dev/null
19+
20+
./instance-crowdsec start
21+
}
22+
23+
teardown_file() {
24+
load "../lib/teardown_file.sh"
25+
}
26+
27+
setup() {
28+
load "../lib/setup.sh"
29+
}
30+
31+
#----------
32+
33+
@test "apply postoverflow" {
34+
CONFIG_DIR=$(dirname "$CONFIG_YAML")
35+
mkdir -p "$CONFIG_DIR"/postoverflows/s01-whitelist
36+
cat > "$CONFIG_DIR"/postoverflows/s01-whitelist/po-test.yaml <<-EOT
37+
name: crowdsecurity/po-test
38+
description: "foo"
39+
whitelist:
40+
reason: "foo"
41+
expression:
42+
- "evt.Overflow.Alert.Source.IP == '1.1.1.172'"
43+
EOT
44+
45+
rune -0 "$CROWDSEC" -dsn file://<(fake_log) -type syslog -no-api
46+
refute_output
47+
assert_stderr --regexp "Adding file .* to filelist"
48+
assert_stderr --regexp "reading .* at once"
49+
assert_stderr --partial "Ban for 1.1.1.172 whitelisted"
50+
assert_stderr --regexp "Acquisition is finished, shutting down"
51+
assert_stderr --regexp "Killing parser routines"
52+
assert_stderr --regexp "Bucket routine exiting"
53+
assert_stderr --regexp "crowdsec shutdown"
54+
}
55+
56+
@test "we have no decision" {
57+
rune -0 cscli decisions list -o json
58+
rune -0 jq '. | length' <(output)
59+
assert_output 0
60+
}

0 commit comments

Comments
 (0)