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"
|
2022-03-09 13:45:36 +00:00
|
|
|
./instance-data load
|
|
|
|
./instance-crowdsec start
|
|
|
|
API_KEY=$(cscli bouncers add testbouncer -o raw)
|
|
|
|
export API_KEY
|
|
|
|
CROWDSEC_API_URL="http://localhost:8080"
|
|
|
|
export CROWDSEC_API_URL
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/teardown_file.sh"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
2022-04-13 13:17:49 +00:00
|
|
|
if is_db_mysql; then sleep 0.3; fi
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api() {
|
|
|
|
URI="$1"
|
|
|
|
curl -s -H "X-Api-Key: ${API_KEY}" "${CROWDSEC_API_URL}${URI}"
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "cli - first decisions list: must be empty" {
|
2022-04-13 13:17:49 +00:00
|
|
|
# delete community pull
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions delete --all
|
|
|
|
rune -0 cscli decisions list -o json
|
2023-08-03 10:51:50 +00:00
|
|
|
assert_json '[]'
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - first decisions list: must be empty" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions'
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output 'null'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "adding decision for 1.2.3.4" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions add -i '1.2.3.4'
|
2022-12-30 14:57:24 +00:00
|
|
|
assert_stderr --partial 'Decision successfully added'
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "CLI - all decisions" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -o json
|
|
|
|
rune -0 jq -r '.[0].decisions[0].value' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '1.2.3.4'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - all decisions" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions'
|
|
|
|
rune -0 jq -c '[ . | length, .[0].value ]' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '[1,"1.2.3.4"]'
|
|
|
|
}
|
|
|
|
|
|
|
|
# check ip match
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "CLI - decision for 1.2.3.4" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -i '1.2.3.4' -o json
|
|
|
|
rune -0 jq -r '.[0].decisions[0].value' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '1.2.3.4'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - decision for 1.2.3.4" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions?ip=1.2.3.4'
|
|
|
|
rune -0 jq -r '.[0].value' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '1.2.3.4'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "CLI - decision for 1.2.3.5" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -i '1.2.3.5' -o json
|
2023-08-03 10:51:50 +00:00
|
|
|
assert_json '[]'
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - decision for 1.2.3.5" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions?ip=1.2.3.5'
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output 'null'
|
|
|
|
}
|
|
|
|
|
|
|
|
## check outer range match
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "CLI - decision for 1.2.3.0/24" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -r '1.2.3.0/24' -o json
|
2023-08-03 10:51:50 +00:00
|
|
|
assert_json '[]'
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - decision for 1.2.3.0/24" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions?range=1.2.3.0/24'
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output 'null'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "CLI - decisions where IP in 1.2.3.0/24" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -r '1.2.3.0/24' --contained -o json
|
|
|
|
rune -0 jq -r '.[0].decisions[0].value' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '1.2.3.4'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "API - decisions where IP in 1.2.3.0/24" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 api '/v1/decisions?range=1.2.3.0/24&contains=false'
|
|
|
|
rune -0 jq -r '.[0].value' <(output)
|
2022-03-09 13:45:36 +00:00
|
|
|
assert_output '1.2.3.4'
|
|
|
|
}
|