Add dnsbl linux test

This commit is contained in:
Théophile Diot 2023-09-23 08:20:41 +01:00
parent a29ac80e4b
commit 2244f734fa
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
3 changed files with 181 additions and 63 deletions

View file

@ -1,5 +1,6 @@
from contextlib import suppress
from ipaddress import IPv4Address
from os import getenv, sep
from pathlib import Path
from traceback import format_exc
from selenium import webdriver
@ -40,6 +41,12 @@ try:
print(" Checking the DNSBL servers for a banned IP ...", flush=True)
output_path = (
Path(sep, "output", "dnsbl_ip.txt")
if getenv("TEST_TYPE", "docker") == "docker"
else Path(".", "dnsbl_ip.txt")
)
for ip_address in [IPv4Address(f"{x}.0.0.3") for x in range(1, 256)]:
for dnsbl_server in dnsbl_servers:
with suppress(gaierror):
@ -47,10 +54,10 @@ try:
f"{ip_address.reverse_pointer.replace('.in-addr.arpa', '')}.{dnsbl_server}"
)
print(
f"{ip_address} is banned on {dnsbl_server}, saving it to /output/dnsbl_ip.txt",
f"{ip_address} is banned on {dnsbl_server}, saving it to {output_path}",
flush=True,
)
Path("/output/dnsbl_ip.txt").write_text(f"{ip_address} {dnsbl_server}")
output_path.write_text(f"{ip_address} {dnsbl_server}")
exit(0)
except SystemExit as e:
exit(e.code)

View file

@ -39,7 +39,13 @@ try:
)
status_code = get(
f"http://www.example.com", headers={"Host": "www.example.com"}
f"http://www.example.com",
headers={"Host": "www.example.com"}
| (
{"X-Forwarded-For": getenv("IP_ADDRESS", "")}
if getenv("TEST_TYPE", "docker") == "linux"
else {}
),
).status_code
if status_code == 403:

View file

@ -1,17 +1,38 @@
#!/bin/bash
echo "🚫 Building dnsbl stack ..."
integration=$1
# Starting stack
docker compose pull bw-docker
if [ $? -ne 0 ] ; then
echo "🚫 Pull failed ❌"
if [ -z "$integration" ] ; then
echo "🤖 Please provide an integration name as argument ❌"
exit 1
elif [ "$integration" != "docker" ] && [ "$integration" != "linux" ] ; then
echo "🤖 Integration \"$integration\" is not supported ❌"
exit 1
fi
docker compose -f docker-compose.test.yml build
if [ $? -ne 0 ] ; then
echo "🚫 Build failed ❌"
exit 1
echo "🚫 Building dnsbl stack for integration \"$integration\" ..."
# Starting stack
if [ "$integration" = "docker" ] ; then
docker compose pull bw-docker
if [ $? -ne 0 ] ; then
echo "🚫 Pull failed ❌"
exit 1
fi
docker compose -f docker-compose.test.yml build
if [ $? -ne 0 ] ; then
echo "🚫 Build failed ❌"
exit 1
fi
else
sudo systemctl stop bunkerweb
echo "USE_REAL_IP=yes" | sudo tee -a /etc/bunkerweb/variables.env
echo "REAL_IP_FROM=127.0.0.0/24" | sudo tee -a /etc/bunkerweb/variables.env
echo "USE_DNSBL=yes" | sudo tee -a /etc/bunkerweb/variables.env
echo "DNSBL_LIST=bl.blocklist.de problems.dnsbl.sorbs.net" | sudo tee -a /etc/bunkerweb/variables.env
sudo touch /var/www/html/index.html
export TEST_TYPE="linux"
fi
manual=0
@ -19,12 +40,19 @@ end=0
cleanup_stack () {
exit_code=$?
if [[ $end -eq 1 || $exit_code = 1 ]] || [[ $end -eq 0 && $exit_code = 0 ]] && [ $manual = 0 ] ; then
rm -rf init/output
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "no"@USE_DNSBL: "yes"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@ipv4_address: [0-9][0-9]*\.0@ipv4_address: 192.168@' {} \;
sed -i 's@subnet: [0-9][0-9]*\.0@subnet: 192.168@' docker-compose.yml
sed -i 's@www.example.com:[0-9][0-9]*\.0@www.example.com:192.168@' docker-compose.test.yml
if [ "$integration" == "docker" ] ; then
rm -rf init/output
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "no"@USE_DNSBL: "yes"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@ipv4_address: [0-9][0-9]*\.0@ipv4_address: 192.168@' {} \;
sed -i 's@subnet: [0-9][0-9]*\.0@subnet: 192.168@' docker-compose.yml
sed -i 's@www.example.com:[0-9][0-9]*\.0@www.example.com:192.168@' docker-compose.test.yml
else
sudo sed -i 's@USE_DNSBL=.*$@USE_DNSBL=yes@' /etc/bunkerweb/variables.env
sudo sed -i 's@DNSBL_LIST=.*$@DNSBL_LIST=bl.blocklist.de problems.dnsbl.sorbs.net@' /etc/bunkerweb/variables.env
unset USE_DNSBL
unset DNSBL_LIST
fi
if [[ $end -eq 1 && $exit_code = 0 ]] ; then
return
fi
@ -32,10 +60,15 @@ cleanup_stack () {
echo "🚫 Cleaning up current stack ..."
docker compose down -v --remove-orphans
if [ "$integration" == "docker" ] ; then
docker compose down -v --remove-orphans
else
sudo systemctl stop bunkerweb
sudo truncate -s 0 /var/log/bunkerweb/error.log
fi
if [ $? -ne 0 ] ; then
echo "🚫 Down failed ❌"
echo "🤖 Cleanup failed ❌"
exit 1
fi
@ -46,18 +79,32 @@ cleanup_stack () {
trap cleanup_stack EXIT
echo "🚫 Initializing workspace ..."
rm -rf init/output
mkdir -p init/output
docker compose -f docker-compose.init.yml up --build
if [ $? -ne 0 ] ; then
echo "🚫 Build failed ❌"
exit 1
elif ! [[ -f "init/output/dnsbl_ip.txt" ]] ; then
echo "🚫 Initialization failed, dnsbl_ip.txt not found ❌"
exit 1
if [ "$integration" = "docker" ] ; then
rm -rf init/output
mkdir -p init/output
docker compose -f docker-compose.init.yml up --build
if [ $? -ne 0 ] ; then
echo "🚫 Build failed ❌"
exit 1
elif ! [[ -f "init/output/dnsbl_ip.txt" ]] ; then
echo "🚫 Initialization failed, dnsbl_ip.txt not found ❌"
exit 1
fi
content=($(cat init/output/dnsbl_ip.txt))
else
python3 init/main.py
if [ $? -ne 0 ] ; then
echo "🚫 Initialization failed ❌"
exit 1
elif ! [[ -f "dnsbl_ip.txt" ]] ; then
echo "🚫 Initialization failed, dnsbl_ip.txt not found ❌"
exit 1
fi
content=($(cat dnsbl_ip.txt))
fi
content=($(cat init/output/dnsbl_ip.txt))
ip=${content[0]}
server=${content[1]}
@ -68,29 +115,54 @@ for test in "activated" "deactivated" "list"
do
if [ "$test" = "activated" ] ; then
echo "🚫 Running tests with DNSBL activated and the server $server added to the list ..."
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net '"$server"'"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@ipv4_address: 192.168@ipv4_address: '"${ip%%.*}"'.0@' {} \;
sed -i 's@subnet: 192.168@subnet: '"${ip%%.*}"'.0@' docker-compose.yml
sed -i 's@www.example.com:192.168@www.example.com:'"${ip%%.*}"'.0@' docker-compose.test.yml
if [ "$integration" = "docker" ] ; then
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net '"$server"'"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@ipv4_address: 192.168@ipv4_address: '"${ip%%.*}"'.0@' {} \;
sed -i 's@subnet: 192.168@subnet: '"${ip%%.*}"'.0@' docker-compose.yml
sed -i 's@www.example.com:192.168@www.example.com:'"${ip%%.*}"'.0@' docker-compose.test.yml
else
sudo sed -i 's@DNSBL_LIST=.*$@DNSBL_LIST=bl.blocklist.de problems.dnsbl.sorbs.net '"$server"'@' /etc/bunkerweb/variables.env
export IP_ADDRESS="$ip"
fi
elif [ "$test" = "deactivated" ] ; then
echo "🚫 Running tests without DNSBL ..."
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "yes"@USE_DNSBL: "no"@' {} \;
if [ "$integration" = "docker" ] ; then
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "yes"@USE_DNSBL: "no"@' {} \;
else
sudo sed -i 's@USE_DNSBL=.*$@USE_DNSBL=no@' /etc/bunkerweb/variables.env
export USE_DNSBL="no"
fi
elif [ "$test" = "list" ] ; then
echo "🚫 Running tests with DNSBL activated and without the server $server added to the list ..."
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "no"@USE_DNSBL: "yes"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net"@' {} \;
if [ "$integration" = "docker" ] ; then
find . -type f -name 'docker-compose.*' -exec sed -i 's@USE_DNSBL: "no"@USE_DNSBL: "yes"@' {} \;
find . -type f -name 'docker-compose.*' -exec sed -i 's@DNSBL_LIST: ".*"@DNSBL_LIST: "bl.blocklist.de problems.dnsbl.sorbs.net"@' {} \;
else
sudo sed -i 's@USE_DNSBL=.*$@USE_DNSBL=yes@' /etc/bunkerweb/variables.env
sudo sed -i 's@DNSBL_LIST=.*$@DNSBL_LIST=bl.blocklist.de problems.dnsbl.sorbs.net@' /etc/bunkerweb/variables.env
unset USE_DNSBL
unset DNSBL_LIST
fi
fi
echo "🚫 Starting stack ..."
docker compose up -d
if [ $? -ne 0 ] ; then
echo "🚫 Up failed, retrying ... ⚠️"
manual=1
cleanup_stack
manual=0
if [ "$integration" == "docker" ] ; then
docker compose up -d
if [ $? -ne 0 ] ; then
echo "🚫 Up failed ❌"
echo "🤖 Up failed, retrying ... ⚠️"
manual=1
cleanup_stack
manual=0
docker compose up -d
if [ $? -ne 0 ] ; then
echo "🤖 Up failed ❌"
exit 1
fi
fi
else
sudo systemctl start bunkerweb
if [ $? -ne 0 ] ; then
echo "🤖 Start failed ❌"
exit 1
fi
fi
@ -98,37 +170,70 @@ do
# Check if stack is healthy
echo "🚫 Waiting for stack to be healthy ..."
i=0
while [ $i -lt 120 ] ; do
containers=("dnsbl-bw-1" "dnsbl-bw-scheduler-1")
healthy="true"
for container in "${containers[@]}" ; do
check="$(docker inspect --format "{{json .State.Health }}" $container | grep "healthy")"
if [ "$check" = "" ] ; then
healthy="false"
if [ "$integration" == "docker" ] ; then
while [ $i -lt 120 ] ; do
containers=("dnsbl-bw-1" "dnsbl-bw-scheduler-1")
healthy="true"
for container in "${containers[@]}" ; do
check="$(docker inspect --format "{{json .State.Health }}" $container | grep "healthy")"
if [ "$check" = "" ] ; then
healthy="false"
break
fi
done
if [ "$healthy" = "true" ] ; then
echo "🚫 Docker stack is healthy ✅"
break
fi
sleep 1
i=$((i+1))
done
if [ "$healthy" = "true" ] ; then
echo "🚫 Docker stack is healthy ✅"
break
if [ $i -ge 120 ] ; then
docker compose logs
echo "🚫 Docker stack is not healthy ❌"
exit 1
fi
else
while [ $i -lt 120 ] ; do
check="$(sudo cat /var/log/bunkerweb/error.log | grep "BunkerWeb is ready")"
if ! [ -z "$check" ] ; then
echo "🤖 Linux stack is healthy ✅"
break
fi
sleep 1
i=$((i+1))
done
if [ $i -ge 120 ] ; then
sudo journalctl -u bunkerweb --no-pager
echo "🛡️ Showing BunkerWeb error logs ..."
sudo cat /var/log/bunkerweb/error.log
echo "🛡️ Showing BunkerWeb access logs ..."
sudo cat /var/log/bunkerweb/access.log
echo "🤖 Linux stack is not healthy ❌"
exit 1
fi
sleep 1
i=$((i+1))
done
if [ $i -ge 120 ] ; then
docker compose logs
echo "🚫 Docker stack is not healthy ❌"
exit 1
fi
# Start tests
docker compose -f docker-compose.test.yml up --abort-on-container-exit --exit-code-from tests
if [ "$integration" == "docker" ] ; then
docker compose -f docker-compose.test.yml up --abort-on-container-exit --exit-code-from tests
else
python3 main.py
fi
if [ $? -ne 0 ] ; then
echo "🚫 Test \"$test\" failed ❌"
echo "🛡️ Showing BunkerWeb and BunkerWeb Scheduler logs ..."
docker compose logs bw bw-scheduler
if [ "$integration" == "docker" ] ; then
docker compose logs bw bw-scheduler
else
sudo journalctl -u bunkerweb --no-pager
echo "🛡️ Showing BunkerWeb error logs ..."
sudo cat /var/log/bunkerweb/error.log
echo "🛡️ Showing BunkerWeb access logs ..."
sudo cat /var/log/bunkerweb/access.log
fi
exit 1
else
echo "🚫 Test \"$test\" succeeded ✅"