2022-05-18 08:08:37 +00:00
|
|
|
#!/usr/bin/env bats
|
|
|
|
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
|
|
|
|
|
|
|
|
set -u
|
|
|
|
|
|
|
|
fake_log() {
|
|
|
|
for _ in $(seq 1 6); do
|
|
|
|
echo "$(LC_ALL=C date '+%b %d %H:%M:%S ')"'sd-126005 sshd[12422]: Invalid user netflix from 1.1.1.172 port 35424'
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_file() {
|
|
|
|
load "../lib/setup_file.sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown_file() {
|
|
|
|
load "../lib/teardown_file.sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
load "../lib/setup.sh"
|
|
|
|
./instance-data load
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 config_get '.api.client.credentials_path'
|
2022-06-24 13:55:21 +00:00
|
|
|
LOCAL_API_CREDENTIALS="${output}"
|
|
|
|
export LOCAL_API_CREDENTIALS
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "config.yaml.local - cscli (log_level)" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set '.common.log_level="warning"'
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli config show --key Config.Common.LogLevel
|
2023-12-05 09:38:21 +00:00
|
|
|
assert_output "&3"
|
2022-05-18 08:08:37 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "{'common':{'log_level':'debug'}}" >"${CONFIG_YAML}.local"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli config show --key Config.Common.LogLevel
|
2023-12-05 09:38:21 +00:00
|
|
|
assert_output "&5"
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "config.yaml.local - cscli (log_level - with envvar)" {
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set '.common.log_level="warning"'
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli config show --key Config.Common.LogLevel
|
2023-12-05 09:38:21 +00:00
|
|
|
assert_output "&3"
|
2022-05-18 08:08:37 +00:00
|
|
|
|
|
|
|
export CROWDSEC_LOG_LEVEL=debug
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "{'common':{'log_level':'${CROWDSEC_LOG_LEVEL}'}}" >"${CONFIG_YAML}.local"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli config show --key Config.Common.LogLevel
|
2023-12-05 09:38:21 +00:00
|
|
|
assert_output "&5"
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "config.yaml.local - crowdsec (listen_url)" {
|
2022-06-24 13:55:21 +00:00
|
|
|
# disable the agent or we'll need to patch api client credentials too
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 config_disable_agent
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec start
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 wait-for-port -q 8080
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec stop
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -1 wait-for-port -q 8080
|
2022-05-18 08:08:37 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "{'api':{'server':{'listen_uri':127.0.0.1:8083}}}" >"${CONFIG_YAML}.local"
|
2022-06-24 13:55:21 +00:00
|
|
|
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec start
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 wait-for-port -q 8083
|
|
|
|
rune -1 wait-for-port -q 8080
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec stop
|
2022-05-18 08:08:37 +00:00
|
|
|
|
|
|
|
rm -f "${CONFIG_YAML}.local"
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec start
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -1 wait-for-port -q 8083
|
|
|
|
rune -0 wait-for-port -q 8080
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "local_api_credentials.yaml.local" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 config_disable_agent
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "{'api':{'server':{'listen_uri':127.0.0.1:8083}}}" >"${CONFIG_YAML}.local"
|
2022-06-22 08:00:51 +00:00
|
|
|
./instance-crowdsec start
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 wait-for-port -q 8083
|
2022-05-18 08:08:37 +00:00
|
|
|
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -1 cscli decisions list
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "{'url':'http://127.0.0.1:8083'}" >"${LOCAL_API_CREDENTIALS}.local"
|
2022-06-24 13:55:21 +00:00
|
|
|
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "simulation.yaml.local" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 config_get '.config_paths.simulation_path'
|
2022-05-18 08:08:37 +00:00
|
|
|
refute_output null
|
|
|
|
SIMULATION="${output}"
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "simulation: off" >"${SIMULATION}"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli simulation status -o human
|
|
|
|
assert_stderr --partial "global simulation: disabled"
|
2022-05-18 08:08:37 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "simulation: on" >"${SIMULATION}"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli simulation status -o human
|
|
|
|
assert_stderr --partial "global simulation: enabled"
|
2022-05-18 08:08:37 +00:00
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
echo "simulation: off" >"${SIMULATION}.local"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli simulation status -o human
|
|
|
|
assert_stderr --partial "global simulation: disabled"
|
2022-05-18 08:08:37 +00:00
|
|
|
|
|
|
|
rm -f "${SIMULATION}.local"
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli simulation status -o human
|
|
|
|
assert_stderr --partial "global simulation: enabled"
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 09:03:40 +00:00
|
|
|
@test "profiles.yaml.local" {
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 config_get '.api.server.profiles_path'
|
2022-05-18 08:08:37 +00:00
|
|
|
refute_output null
|
|
|
|
PROFILES="${output}"
|
|
|
|
|
2022-06-13 19:54:47 +00:00
|
|
|
cat <<-EOT >"${PROFILES}.local"
|
2022-05-18 08:08:37 +00:00
|
|
|
name: default_ip_remediation
|
|
|
|
filters:
|
|
|
|
- Alert.Remediation == true && Alert.GetScope() == "Ip"
|
|
|
|
decisions:
|
|
|
|
- type: captcha
|
|
|
|
duration: 2h
|
|
|
|
on_success: break
|
|
|
|
EOT
|
|
|
|
|
|
|
|
tmpfile=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp)
|
|
|
|
touch "${tmpfile}"
|
2022-07-01 20:45:55 +00:00
|
|
|
ACQUIS_YAML=$(config_get '.crowdsec_service.acquisition_path')
|
2022-05-18 08:08:37 +00:00
|
|
|
echo -e "---\nfilename: ${tmpfile}\nlabels:\n type: syslog\n" >>"${ACQUIS_YAML}"
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
rune -0 cscli collections install crowdsecurity/sshd
|
|
|
|
rune -0 cscli parsers install crowdsecurity/syslog-logs
|
|
|
|
|
2022-05-18 08:08:37 +00:00
|
|
|
./instance-crowdsec start
|
2022-08-04 09:25:34 +00:00
|
|
|
sleep .5
|
2022-05-18 08:08:37 +00:00
|
|
|
fake_log >>"${tmpfile}"
|
2022-07-29 08:34:03 +00:00
|
|
|
|
|
|
|
# this could be simplified, but some systems are slow and we don't want to
|
|
|
|
# wait more than required
|
2022-08-04 09:25:34 +00:00
|
|
|
for ((i=0;i<30;i++)); do
|
2022-07-29 08:34:03 +00:00
|
|
|
sleep .5
|
2023-06-01 07:42:27 +00:00
|
|
|
rune -0 cscli decisions list -o json
|
|
|
|
rune -0 jq --exit-status '.[].decisions[0] | [.value,.type] == ["1.1.1.172","captcha"]' <(output) && break
|
2022-07-29 08:34:03 +00:00
|
|
|
done
|
2022-05-18 08:08:37 +00:00
|
|
|
rm -f -- "${tmpfile}"
|
2022-07-29 08:34:03 +00:00
|
|
|
[[ "${status}" -eq 0 ]] || fail "captcha not triggered"
|
2022-05-18 08:08:37 +00:00
|
|
|
}
|