2019-10-08 20:03:56 +00:00
|
|
|
load 'test_helper/common'
|
|
|
|
|
|
|
|
function setup() {
|
|
|
|
run_setup_file_if_necessary
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardown() {
|
|
|
|
run_teardown_file_if_necessary
|
|
|
|
}
|
|
|
|
|
|
|
|
function setup_file() {
|
2020-10-20 07:33:47 +00:00
|
|
|
local PRIVATE_CONFIG
|
|
|
|
PRIVATE_CONFIG="$(duplicate_config_for_container .)"
|
2019-10-08 20:03:56 +00:00
|
|
|
docker run --rm -d --name mail_fail2ban \
|
2020-10-20 07:33:47 +00:00
|
|
|
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
|
2020-10-19 11:13:42 +00:00
|
|
|
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
|
2019-10-08 20:03:56 +00:00
|
|
|
-e ENABLE_FAIL2BAN=1 \
|
|
|
|
-e POSTSCREEN_ACTION=ignore \
|
|
|
|
--cap-add=NET_ADMIN \
|
2020-11-05 12:32:42 +00:00
|
|
|
-h mail.my-domain.com -t "${NAME}"
|
2019-10-08 20:03:56 +00:00
|
|
|
|
|
|
|
# Create a container which will send wrong authentications and should get banned
|
|
|
|
docker run --name fail-auth-mailer \
|
2020-11-05 12:32:42 +00:00
|
|
|
-e MAIL_FAIL2BAN_IP="$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' mail_fail2ban)" \
|
2019-10-08 20:03:56 +00:00
|
|
|
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test \
|
2020-11-05 12:32:42 +00:00
|
|
|
-d "${NAME}" \
|
2019-10-08 20:03:56 +00:00
|
|
|
tail -f /var/log/faillog
|
|
|
|
|
|
|
|
wait_for_finished_setup_in_container mail_fail2ban
|
2020-10-21 16:16:32 +00:00
|
|
|
|
2019-10-08 20:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function teardown_file() {
|
|
|
|
docker rm -f mail_fail2ban fail-auth-mailer
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "first" {
|
|
|
|
skip 'this test must come first to reliably identify when to run setup_file'
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# processes
|
|
|
|
#
|
|
|
|
|
|
|
|
@test "checking process: fail2ban (fail2ban server enabled)" {
|
|
|
|
run docker exec mail_fail2ban /bin/bash -c "ps aux --forest | grep -v grep | grep '/usr/bin/python3 /usr/bin/fail2ban-server'"
|
|
|
|
assert_success
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# fail2ban
|
|
|
|
#
|
|
|
|
|
|
|
|
@test "checking fail2ban: localhost is not banned because ignored" {
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client status postfix-sasl | grep 'IP list:.*127.0.0.1'"
|
|
|
|
assert_failure
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "grep 'ignoreip = 127.0.0.1/8' /etc/fail2ban/jail.conf"
|
|
|
|
assert_success
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "checking fail2ban: fail2ban-fail2ban.cf overrides" {
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client get loglevel | grep DEBUG"
|
|
|
|
assert_success
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "checking fail2ban: fail2ban-jail.cf overrides" {
|
2021-04-11 15:33:39 +00:00
|
|
|
FILTERS=(dovecot postfix postfix-sasl)
|
2019-10-08 20:03:56 +00:00
|
|
|
|
|
|
|
for FILTER in "${FILTERS[@]}"; do
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client get ${FILTER} bantime"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_output 1234
|
|
|
|
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client get ${FILTER} findtime"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_output 321
|
|
|
|
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client get ${FILTER} maxretry"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_output 2
|
2021-04-18 10:55:43 +00:00
|
|
|
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client -d | grep -F \"['set', 'dovecot', 'addaction', 'iptables-multiport']\""
|
|
|
|
assert_output "['set', 'dovecot', 'addaction', 'iptables-multiport']"
|
|
|
|
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client -d | grep -F \"['set', 'postfix', 'addaction', 'iptables-multiport']\""
|
|
|
|
assert_output "['set', 'postfix', 'addaction', 'iptables-multiport']"
|
|
|
|
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client -d | grep -F \"['set', 'postfix-sasl', 'addaction', 'iptables-multiport']\""
|
|
|
|
assert_output "['set', 'postfix-sasl', 'addaction', 'iptables-multiport']"
|
2019-10-08 20:03:56 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "checking fail2ban: ban ip on multiple failed login" {
|
|
|
|
# can't pipe the file as usual due to postscreen. (respecting postscreen_greet_wait time and talking in turn):
|
2020-11-05 12:32:42 +00:00
|
|
|
# shellcheck disable=SC1004
|
|
|
|
for _ in {1,2}
|
|
|
|
do
|
2019-10-08 20:03:56 +00:00
|
|
|
docker exec fail-auth-mailer /bin/bash -c \
|
2020-11-05 12:32:42 +00:00
|
|
|
'exec 3<>/dev/tcp/${MAIL_FAIL2BAN_IP}/25 && \
|
2019-10-08 20:03:56 +00:00
|
|
|
while IFS= read -r cmd; do \
|
|
|
|
head -1 <&3; \
|
2020-11-05 12:32:42 +00:00
|
|
|
[[ "${cmd}" == "EHLO"* ]] && sleep 6; \
|
|
|
|
echo ${cmd} >&3; \
|
2019-10-08 20:03:56 +00:00
|
|
|
done < "/tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt"'
|
|
|
|
done
|
|
|
|
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
FAIL_AUTH_MAILER_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' fail-auth-mailer)
|
|
|
|
# Checking that FAIL_AUTH_MAILER_IP is banned in mail_fail2ban
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client status postfix-sasl | grep '${FAIL_AUTH_MAILER_IP}'"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_success
|
|
|
|
|
2021-04-18 10:55:43 +00:00
|
|
|
# Checking that FAIL_AUTH_MAILER_IP is banned by iptables and blocktype set to DROP
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "iptables -n -L f2b-postfix-sasl"
|
|
|
|
assert_output --regexp "DROP.+all.+${FAIL_AUTH_MAILER_IP}"
|
2019-10-08 20:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "checking fail2ban: unban ip works" {
|
|
|
|
|
|
|
|
FAIL_AUTH_MAILER_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' fail-auth-mailer)
|
2020-11-05 12:32:42 +00:00
|
|
|
docker exec mail_fail2ban fail2ban-client set postfix-sasl unbanip "${FAIL_AUTH_MAILER_IP}"
|
2019-10-08 20:03:56 +00:00
|
|
|
|
|
|
|
sleep 5
|
|
|
|
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client status postfix-sasl | grep 'IP list:.*${FAIL_AUTH_MAILER_IP}'"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_failure
|
|
|
|
|
|
|
|
# Checking that FAIL_AUTH_MAILER_IP is unbanned by iptables
|
2020-11-05 12:32:42 +00:00
|
|
|
run docker exec mail_fail2ban /bin/sh -c "iptables -L f2b-postfix-sasl -n | grep REJECT | grep '${FAIL_AUTH_MAILER_IP}'"
|
2019-10-08 20:03:56 +00:00
|
|
|
assert_failure
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# debug
|
|
|
|
#
|
|
|
|
|
|
|
|
@test "checking setup.sh: setup.sh debug fail2ban" {
|
|
|
|
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client set dovecot banip 192.0.66.4"
|
|
|
|
run docker exec mail_fail2ban /bin/sh -c "fail2ban-client set dovecot banip 192.0.66.5"
|
|
|
|
sleep 10
|
|
|
|
run ./setup.sh -c mail_fail2ban debug fail2ban
|
2020-11-05 10:41:18 +00:00
|
|
|
assert_output --regexp "^Banned in dovecot: 192.0.66.5, 192.0.66.4.*"
|
2019-10-08 20:03:56 +00:00
|
|
|
run ./setup.sh -c mail_fail2ban debug fail2ban unban 192.0.66.4
|
2020-11-05 10:41:18 +00:00
|
|
|
assert_output --partial "Unbanned IP from dovecot: 192.0.66.4"
|
2019-10-08 20:03:56 +00:00
|
|
|
run ./setup.sh -c mail_fail2ban debug fail2ban
|
2020-11-05 10:41:18 +00:00
|
|
|
assert_output --regexp "^Banned in dovecot: 192.0.66.5.*"
|
2019-10-08 20:03:56 +00:00
|
|
|
run ./setup.sh -c mail_fail2ban debug fail2ban unban 192.0.66.5
|
2021-04-18 10:55:43 +00:00
|
|
|
assert_output --partial "Unbanned IP from dovecot: 192.0.66.5"
|
2019-10-08 20:03:56 +00:00
|
|
|
run ./setup.sh -c mail_fail2ban debug fail2ban unban
|
|
|
|
assert_output --partial "You need to specify an IP address. Run"
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# supervisor
|
|
|
|
#
|
|
|
|
|
|
|
|
@test "checking restart of process: fail2ban (fail2ban server enabled)" {
|
|
|
|
run docker exec mail_fail2ban /bin/bash -c "pkill fail2ban && sleep 10 && ps aux --forest | grep -v grep | grep '/usr/bin/python3 /usr/bin/fail2ban-server'"
|
|
|
|
assert_success
|
|
|
|
}
|
|
|
|
|
|
|
|
@test "last" {
|
|
|
|
skip 'this test is only there to reliably mark the end for the teardown_file'
|
2020-10-21 16:16:32 +00:00
|
|
|
}
|