#!/usr/bin/env bats # report, ufw-plan, status: read only. load helpers bats_require_minimum_version 1.5.0 setup() { setup_env cp "$REPO/examples/fence.conf" "$CONF" chmod 0644 "$CONF" cp "$FIXTURES"/docker/* "$STUB_DOCKER_DIR/" export STUB_JOURNAL="$FIXTURES/journal.log" } @test "report aggregates by source and container, most frequent first" { run --separate-stderr fence report [ "$status" -eq 0 ] [[ "$stderr" == *'report since "24 hours ago": 6 logged packets'* ]] # Header line, then the sorted flows. [[ "${lines[1]}" =~ ^\ +3\ +203\.0\.113\.5\ +web:80/tcp\ +8080$ ]] [[ "$output" =~ 1\ +198\.51\.100\.20\ +db:5432/tcp\ +15432\ 25432 ]] [[ "$output" =~ 1\ +198\.51\.100\.20\ +web:53/udp\ +5353 ]] [[ "$output" =~ 1\ +192\.0\.2\.99\ +172\.30\.0\.9:9999/tcp\ +\? ]] } @test "report suggests allow lines with the published port, not the internal one" { run --separate-stderr fence report [[ "$output" == *"allow tcp 8080 from 203.0.113.5 # web"* ]] [[ "$output" == *"allow tcp 15432 from 198.51.100.20 # db"* ]] [[ "$output" == *"allow tcp 25432 from 198.51.100.20 # db"* ]] [[ "$output" == *"allow udp 5353 from 198.51.100.20 # web"* ]] [[ "$output" == *"# 192.0.2.99 -> 172.30.0.9:9999/tcp: published port not found"* ]] [[ "$output" != *"allow tcp 80 "* ]] [[ "$output" != *"allow tcp 5432 "* ]] # One line per suggestion, even for three packets. [ "$(grep -c 'allow tcp 8080 from 203.0.113.5' <<<"$output")" -eq 1 ] } @test "suggested allow lines are valid configuration lines" { run --separate-stderr fence report { printf 'admin 192.0.2.10\n'; grep '^allow ' <<<"$output"; } >"$BATS_TEST_TMPDIR/proposed.conf" chmod 0644 "$BATS_TEST_TMPDIR/proposed.conf" run "$FENCE" -c "$BATS_TEST_TMPDIR/proposed.conf" check [ "$status" -eq 0 ] } @test "report never asks for the environment or arguments of containers" { fence report >/dev/null 2>&1 grep -q '^docker inspect --format' "$STUB_LOG" ! grep -qiE 'Env|Args|Cmd|Entrypoint|Config' <(grep '^docker inspect' "$STUB_LOG") } @test "report --since is validated and passed to journalctl" { fence report --since "2026-12-01 08:00" >/dev/null 2>&1 grep -qF -- '--since 2026-12-01 08:00' "$STUB_LOG" run fence report --since '$(id)' [ "$status" -eq 1 ] } @test "report with no logged packet" { STUB_JOURNAL=/dev/null run fence report [ "$status" -eq 0 ] [[ "$output" == *": 0 logged packets"* ]] } @test "report without docker: IPs are still shown" { run --separate-stderr fence --set bin_docker=/nonexistent/docker report [ "$status" -eq 0 ] [[ "$stderr" == *"docker unreachable"* ]] [[ "$output" =~ 3\ +203\.0\.113\.5\ +172\.18\.0\.2:80/tcp\ +\? ]] } @test "ufw-plan: safe order, nothing runs" { export STUB_UFW_ADDED="$FIXTURES/ufw-added" run --separate-stderr fence ufw-plan [ "$status" -eq 0 ] local admin allow c2h del admin="$(grep -n '^# 1\. ' <<<"$output" | cut -d: -f1)" allow="$(grep -n '^# 2\. ' <<<"$output" | cut -d: -f1)" c2h="$(grep -n '^# 3\. ' <<<"$output" | cut -d: -f1)" del="$(grep -n '^# 4\. ' <<<"$output" | cut -d: -f1)" [ "$admin" -lt "$allow" ] && [ "$allow" -lt "$c2h" ] && [ "$c2h" -lt "$del" ] # Only reads were made. ! grep -qE '^ufw (allow|delete|deny|route|--force)' "$STUB_LOG" grep -q '^ufw show added' "$STUB_LOG" } @test "ufw-plan: content of the four sections" { export STUB_UFW_ADDED="$FIXTURES/ufw-added" run --separate-stderr fence ufw-plan # 1. Admin: active commands. grep -qx 'ufw allow from 192.0.2.10' <<<"$output" grep -qx 'ufw allow in on wg0 from 192.0.2.11' <<<"$output" # 2. allow: commented out. grep -qx '# ufw allow proto tcp from any to any port 443' <<<"$output" grep -qx '# ufw allow proto tcp from 198.51.100.20 to any port 5432' <<<"$output" grep -qx '# ufw allow proto tcp from 203.0.113.0/24 to any port 9000:9010' <<<"$output" # 3. container_to_host fallback: one IPv4 network per line. grep -qx '# ufw allow proto tcp from 172.17.0.0/16 to any port 8080' <<<"$output" grep -qx '# ufw allow proto tcp from 172.18.0.0/16 to any port 8080' <<<"$output" ! grep -q 'fd00::' <<<"$output" # 4. Too broad rules, spelled out with --force. grep -qx '# ufw --force delete allow 22/tcp' <<<"$output" grep -qx '# ufw --force delete limit 2222/tcp' <<<"$output" grep -qx '# ufw --force delete route allow proto tcp from any to any port 80' <<<"$output" ! grep -q 'delete allow from 192.0.2.10' <<<"$output" ! grep -q 'delete allow proto tcp from 198.51.100.0/24' <<<"$output" [[ "$output" == *"Never remove the SSH access rule"* ]] [[ "$output" == *"network_mode: host"* ]] } @test "ufw-plan without root nor docker" { run --separate-stderr fence --set bin_docker=/nonexistent/docker ufw-plan [ "$status" -eq 0 ] [[ "$output" == *"ufw show added"*"root"* ]] [[ "$output" == *"no Docker subnet found"* ]] } @test "status after apply: block, pending, jump, counters, log" { fence apply run fence status [ "$status" -eq 0 ] [[ "$output" == *"block installed in $RULES (mode observe)"* ]] [[ "$output" == *"rollback pending (docker-fence-rollback)"* ]] [[ "$output" == *"jump DOCKER-USER -> docker-fence: present"* ]] grep -q '^iptables -L docker-fence -n -v -x' "$STUB_LOG" [[ "$output" == *"SRC=198.51.100.20 DST=172.20.0.3"* ]] [[ "$output" != *"[UFW BLOCK]"* ]] } @test "status without block" { run fence status [ "$status" -eq 0 ] [[ "$output" == *"no block installed"* ]] [[ "$output" == *"jump DOCKER-USER -> docker-fence: missing"* ]] } @test "status recognizes enforce mode" { fence --mode enforce install run fence status [[ "$output" == *"(mode enforce)"* ]] }