2022-03-09 13:45:36 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
setup_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/setup_file.sh"
|
|
|
|
# eval "$(debug)"
|
2022-03-09 13:45:36 +00:00
|
|
|
./instance-data load
|
|
|
|
|
|
|
|
MOCK_OUT="${LOG_DIR}/mock-http.out"
|
|
|
|
export MOCK_OUT
|
2022-03-15 09:40:01 +00:00
|
|
|
MOCK_PORT="9999"
|
|
|
|
MOCK_URL="http://localhost:${MOCK_PORT}"
|
|
|
|
export MOCK_URL
|
2022-07-01 20:45:55 +00:00
|
|
|
PLUGIN_DIR=$(config_get '.config_paths.plugin_dir')
|
2022-07-01 08:16:45 +00:00
|
|
|
# could have a trailing slash
|
2022-07-13 08:10:08 +00:00
|
|
|
PLUGIN_DIR=$(realpath "${PLUGIN_DIR}")
|
2022-05-19 11:27:24 +00:00
|
|
|
export PLUGIN_DIR
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-15 09:40:01 +00:00
|
|
|
# https://mikefarah.gitbook.io/yq/operators/env-variable-operators
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "$(config_get '.config_paths.notification_dir')/http.yaml" '
|
2022-03-15 09:40:01 +00:00
|
|
|
.url=strenv(MOCK_URL) |
|
2022-03-09 13:45:36 +00:00
|
|
|
.group_wait="5s" |
|
|
|
|
.group_threshold=2
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set "$(config_get '.api.server.profiles_path')" '
|
2022-03-09 13:45:36 +00:00
|
|
|
.notifications=["http_default"] |
|
|
|
|
.filters=["Alert.GetScope() == \"Ip\""]
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set '
|
2022-03-09 13:45:36 +00:00
|
|
|
.plugin_config.user="" |
|
|
|
|
.plugin_config.group=""
|
2022-07-01 20:45:55 +00:00
|
|
|
'
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
rm -f -- "${MOCK_OUT}"
|
|
|
|
|
|
|
|
./instance-crowdsec start
|
2022-03-15 09:40:01 +00:00
|
|
|
./instance-mock-http start "${MOCK_PORT}"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/teardown_file.sh"
|
2022-05-19 11:27:24 +00:00
|
|
|
./instance-crowdsec stop
|
2022-03-09 13:45:36 +00:00
|
|
|
./instance-mock-http stop
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "add two bans" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 cscli decisions add --ip 1.2.3.4 --duration 30s
|
|
|
|
assert_output --partial 'Decision successfully added'
|
|
|
|
|
|
|
|
run -0 cscli decisions add --ip 1.2.3.5 --duration 30s
|
|
|
|
assert_output --partial 'Decision successfully added'
|
2022-04-19 17:12:23 +00:00
|
|
|
sleep 5
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "expected 1 log line from http server" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 wc -l <"${MOCK_OUT}"
|
2022-03-10 12:59:10 +00:00
|
|
|
# wc can pad with spaces on some platforms
|
|
|
|
run -0 tr -d ' ' < <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output 1
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "expected to receive 2 alerts in the request body from plugin" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 jq -r '.request_body' <"${MOCK_OUT}"
|
|
|
|
run -0 jq -r 'length' <(output)
|
|
|
|
assert_output 2
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "expected to receive IP 1.2.3.4 as value of first decision" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 jq -r '.request_body[0].decisions[0].value' <"${MOCK_OUT}"
|
|
|
|
assert_output 1.2.3.4
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "expected to receive IP 1.2.3.5 as value of second decision" {
|
2022-03-09 13:45:36 +00:00
|
|
|
run -0 jq -r '.request_body[1].decisions[0].value' <"${MOCK_OUT}"
|
|
|
|
assert_output 1.2.3.5
|
|
|
|
}
|