2022-03-09 15:15:18 +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 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
2022-03-18 09:13:12 +00:00
|
|
|
load "../lib/teardown_file.sh"
|
2022-03-09 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
|
|
|
./instance-data load
|
|
|
|
./instance-crowdsec start
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
2022-04-13 13:44:23 +00:00
|
|
|
declare stderr
|
|
|
|
|
2022-03-09 15:15:18 +00:00
|
|
|
#----------
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "cscli alerts list, with and without --machine" {
|
2022-04-13 13:17:49 +00:00
|
|
|
is_db_postgres && skip
|
2022-03-09 15:15:18 +00:00
|
|
|
run -0 cscli decisions add -i 10.20.30.40 -t ban
|
|
|
|
|
|
|
|
run -0 cscli alerts list
|
|
|
|
refute_output --partial 'MACHINE'
|
|
|
|
# machine name appears quoted in the "REASON" column
|
2022-03-29 15:06:49 +00:00
|
|
|
assert_output --regexp "\| 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?' \|"
|
|
|
|
refute_output --regexp "\| githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})? \|"
|
2022-03-09 15:15:18 +00:00
|
|
|
|
2022-03-17 11:20:08 +00:00
|
|
|
run -0 cscli alerts list -m
|
|
|
|
assert_output --partial 'MACHINE'
|
2022-03-29 15:06:49 +00:00
|
|
|
assert_output --regexp "\| 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?' \|"
|
|
|
|
assert_output --regexp "\| githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})? \|"
|
2022-03-17 11:20:08 +00:00
|
|
|
|
2022-03-09 15:15:18 +00:00
|
|
|
run -0 cscli alerts list --machine
|
|
|
|
assert_output --partial 'MACHINE'
|
2022-03-29 15:06:49 +00:00
|
|
|
assert_output --regexp "\| 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?' \|"
|
|
|
|
assert_output --regexp "\| githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})? \|"
|
2022-03-09 15:15:18 +00:00
|
|
|
}
|
2022-04-13 13:44:23 +00:00
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "cscli alerts list, human/json/raw" {
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 cscli decisions add -i 10.20.30.40 -t ban
|
|
|
|
|
|
|
|
run -0 cscli alerts list -o human
|
|
|
|
assert_output --regexp ".* ID .* VALUE .* REASON .* COUNTRY .* AS .* DECISIONS .* CREATED AT .*"
|
|
|
|
assert_output --regexp ".*Ip:10.20.30.40.*manual 'ban' from.*ban:1.*"
|
|
|
|
|
|
|
|
run -0 cscli alerts list -o json
|
|
|
|
run -0 jq -c '.[].decisions[0] | [.origin, .scenario, .scope, .simulated, .type, .value]' <(output)
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp "\[\"cscli\",\"manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?'\",\"Ip\",false,\"ban\",\"10.20.30.40\"\]"
|
2022-04-13 13:44:23 +00:00
|
|
|
|
|
|
|
run -0 cscli alerts list -o raw
|
|
|
|
assert_line "id,scope,value,reason,country,as,decisions,created_at"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp ".*,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,\" \",ban:1,.*"
|
2022-04-13 13:44:23 +00:00
|
|
|
|
|
|
|
run -0 cscli alerts list -o raw --machine
|
|
|
|
assert_line "id,scope,value,reason,country,as,decisions,created_at,machine"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp "^[0-9]+,Ip,10.20.30.40,manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?',,\" \",ban:1,.*,githubciXXXXXXXXXXXXXXXXXXXXXXXX([a-zA-Z0-9]{16})?$"
|
2022-04-13 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "cscli alerts inspect" {
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 cscli decisions add -i 10.20.30.40 -t ban
|
|
|
|
run -0 cscli alerts list -o raw <(output)
|
|
|
|
run -0 grep 10.20.30.40 <(output)
|
|
|
|
run -0 cut -d, -f1 <(output)
|
2022-07-01 09:03:40 +00:00
|
|
|
ALERT_ID="${output}"
|
2022-04-13 13:44:23 +00:00
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
run -0 cscli alerts inspect "${ALERT_ID}" -o human
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp '^#+$'
|
2022-07-01 09:03:40 +00:00
|
|
|
assert_line --regexp "^ - ID *: ${ALERT_ID}$"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ - Date *: .*$"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp "^ - Machine *: githubciXXXXXXXXXXXXXXXXXXXXXXXX.*"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ - Simulation *: false$"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp "^ - Reason *: manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX.*'$"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ - Events Count *: 1$"
|
|
|
|
assert_line --regexp "^ - Scope:Value *: Ip:10.20.30.40$"
|
|
|
|
assert_line --regexp "^ - Country *: *$"
|
|
|
|
assert_line --regexp "^ - AS *: *$"
|
|
|
|
assert_line --regexp "^ - Begin *: .*$"
|
|
|
|
assert_line --regexp "^ - End *: .*$"
|
|
|
|
assert_line --regexp "^ - Active Decisions *:$"
|
|
|
|
assert_line --regexp "^.* ID .* SCOPE:VALUE .* ACTION .* EXPIRATION .* CREATED AT .*$"
|
|
|
|
assert_line --regexp "^.* Ip:10.20.30.40 .* ban .*$"
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
run -0 cscli alerts inspect "${ALERT_ID}" -o human --details
|
2022-04-13 13:44:23 +00:00
|
|
|
# XXX can we have something here?
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
run -0 cscli alerts inspect "${ALERT_ID}" -o raw
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ *capacity: 0$"
|
2022-07-01 09:03:40 +00:00
|
|
|
assert_line --regexp "^ *id: ${ALERT_ID}$"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ *origin: cscli$"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_line --regexp "^ *scenario: manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX.*'$"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_line --regexp "^ *scope: Ip$"
|
|
|
|
assert_line --regexp "^ *simulated: false$"
|
|
|
|
assert_line --regexp "^ *type: ban$"
|
|
|
|
assert_line --regexp "^ *value: 10.20.30.40$"
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
run -0 cscli alerts inspect "${ALERT_ID}" -o json
|
|
|
|
alert=${output}
|
|
|
|
run jq -c '.decisions[] | [.origin,.scenario,.scope,.simulated,.type,.value]' <<<"${alert}"
|
2022-04-28 12:42:42 +00:00
|
|
|
assert_output --regexp "\[\"cscli\",\"manual 'ban' from 'githubciXXXXXXXXXXXXXXXXXXXXXXXX.*'\",\"Ip\",false,\"ban\",\"10.20.30.40\"\]"
|
2022-07-01 09:03:40 +00:00
|
|
|
run jq -c '.source' <<<"${alert}"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_output '{"ip":"10.20.30.40","scope":"Ip","value":"10.20.30.40"}'
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "no active alerts" {
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 cscli alerts list --until 200d -o human
|
|
|
|
assert_output "No active alerts"
|
|
|
|
run -0 cscli alerts list --until 200d -o json
|
|
|
|
assert_output "null"
|
|
|
|
run -0 cscli alerts list --until 200d -o raw
|
|
|
|
assert_output "id,scope,value,reason,country,as,decisions,created_at"
|
|
|
|
run -0 cscli alerts list --until 200d -o raw --machine
|
|
|
|
assert_output "id,scope,value,reason,country,as,decisions,created_at,machine"
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "cscli alerts delete" {
|
2022-04-13 13:44:23 +00:00
|
|
|
run -0 --separate-stderr cscli alerts delete --all
|
2022-07-01 09:03:40 +00:00
|
|
|
run echo "${stderr}"
|
2022-04-13 13:44:23 +00:00
|
|
|
assert_output --partial 'alert(s) deleted'
|
|
|
|
|
|
|
|
# XXX TODO: delete by scope, id, value, scenario, range..
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "bad duration" {
|
2022-04-13 13:44:23 +00:00
|
|
|
skip 'TODO'
|
|
|
|
run -0 cscli decisions add -i 10.20.30.40 -t ban
|
|
|
|
run -9 cscli decisions list --ip 10.20.30.40 -o json
|
|
|
|
run -9 jq -r '.[].decisions[].id' <(output)
|
2022-07-01 09:03:40 +00:00
|
|
|
DECISION_ID="${output}"
|
2022-04-13 13:44:23 +00:00
|
|
|
|
|
|
|
./instance-crowdsec stop
|
2022-07-01 09:03:40 +00:00
|
|
|
run -0 ./instance-db exec_sql "UPDATE decisions SET ... WHERE id=${DECISION_ID}"
|
2022-04-13 13:44:23 +00:00
|
|
|
./instance-crowdsec start
|
|
|
|
}
|