#!/usr/bin/env bats # rules: table of native UFW rules and docker-fence rules. Read only. load helpers bats_require_minimum_version 1.5.0 setup() { setup_env cp "$REPO/examples/fence.conf" "$CONF" chmod 0644 "$CONF" export STUB_UFW_STATUS="$FIXTURES/ufw-status-verbose" } # Line of the table that contains every given text, in order. # row TEXT... row() { local pattern="" t for t in "$@"; do pattern+="*$t" done local line for line in "${lines[@]}"; do # shellcheck disable=SC2053 [[ "$line" == $pattern* ]] && { echo "$line"; return 0; } done echo "no row matching: $*" >&2 return 1 } # Number of the first line that contains a text. line_of() { local i for i in "${!lines[@]}"; do [[ "${lines[i]}" == *"$1"* ]] && { echo "$i"; return 0; } done return 1 } @test "rules lists the native UFW rules" { run --separate-stderr fence rules [ "$status" -eq 0 ] [[ "${lines[0]}" =~ ^ORIGIN\ +TRAFFIC\ +ACTION\ +TO\ +FROM\ +STATE\ +NOTE$ ]] [[ "${lines[1]}" =~ ^-+\ +-+ ]] row ufw "host input" ALLOW "22/tcp" any active ssh row ufw "host input" ALLOW "any on wg0" "192.0.2.10" active row ufw "host input" DENY "5432/tcp" "198.51.100.0/24" active row ufw "host input" LIMIT "2222/tcp" any active row ufw "host input" ALLOW "22/tcp" any active "IPv6, ssh" row ufw routed ALLOW "80/tcp" any active row ufw "host output" ALLOW 53 any active } @test "rules ends the UFW rules with the default policies" { run --separate-stderr fence rules row ufw "host input" DENY any any active "default policy" row ufw "host output" ALLOW any any active "default policy" row ufw routed DENY any any active "default policy" [ "$(line_of "53 ")" -lt "$(line_of "default policy")" ] } @test "rules before apply: docker-fence rules are not loaded" { run --separate-stderr fence rules [ "$status" -eq 0 ] row fence "published ports" ALLOW any any "not loaded" "replies to established connections" row fence "published ports" ALLOW "all published ports" "192.0.2.10/32" "not loaded" admin row fence "container -> host" ALLOW "8080/tcp" "iface br-+" "not loaded" container_to_host } @test "rules after apply (observe): every docker-fence rule is loaded, in block order" { fence apply run --separate-stderr fence rules [ "$status" -eq 0 ] row fence "published ports" ALLOW any any loaded "replies to established connections" row fence "published ports" ALLOW any "iface br-+" loaded "trusted containers" row fence "published ports" ALLOW any "iface docker0" loaded "trusted containers" row fence "published ports" ALLOW "all published ports" "192.0.2.10/32" loaded admin row fence "published ports" ALLOW "all published ports" "192.0.2.11/32 on wg0" loaded admin row fence "published ports" ALLOW "443/tcp" any loaded allow row fence "published ports" ALLOW "5432/tcp" "198.51.100.20/32" loaded allow row fence "published ports" ALLOW "9000-9010/tcp" "203.0.113.0/24" loaded allow row fence "published ports" LOG "other published ports" any loaded "observe mode: logged, not blocked" row fence "container -> host" ALLOW "8080/tcp" "iface br-+" loaded container_to_host # Order: UFW first, then docker-fence in evaluation order. [ "$(line_of "default policy")" -lt "$(line_of "replies to established")" ] [ "$(line_of "192.0.2.10/32")" -lt "$(line_of "443/tcp")" ] [ "$(line_of "443/tcp")" -lt "$(line_of "other published ports")" ] [ "$(line_of "other published ports")" -lt "$(line_of "container -> host")" ] [[ "$output" != *"not loaded"* ]] } @test "rules after apply (enforce): the last rule drops" { fence --mode enforce apply run --separate-stderr fence --mode enforce rules row fence "published ports" DROP "other published ports" any loaded "enforce mode: logged and dropped" } @test "rules after uninstall: docker-fence rules are no longer loaded" { fence apply fence confirm fence uninstall run --separate-stderr fence rules row fence "published ports" ALLOW "443/tcp" any "not loaded" allow row fence "container -> host" ALLOW "8080/tcp" "iface br-+" "not loaded" } @test "rules: state unknown when the firewall cannot be read" { fence apply STUB_IPT_CHECK_RC=4 run --separate-stderr fence rules [ "$status" -eq 0 ] row fence "published ports" ALLOW "443/tcp" any "unknown (root?)" allow row fence "container -> host" ALLOW "8080/tcp" "iface br-+" "unknown (root?)" } @test "rules: columns are aligned, also with accents (French)" { fence apply local lang offsets for lang in en fr; do run --separate-stderr fence --lang "$lang" rules [ "$status" -eq 0 ] # Start of each column in the header, in characters. local header="${lines[0]}" col local -a starts=() local LC_ALL=C.UTF-8 local i prev=" " for (( i = 0; i < ${#header}; i++ )); do [[ "$prev" == " " && "${header:i:1}" != " " ]] && starts+=("$i") prev="${header:i:1}" done [ "${#starts[@]}" -eq 7 ] local line for line in "${lines[@]:1}"; do for col in "${starts[@]:1}"; do # An empty last cell ends the line early (no trailing blanks). (( ${#line} > col )) || continue # Every column starts after two blanks at least. [[ "${line:col-2:2}" == " " && "${line:col:1}" != " " ]] \ || { echo "misaligned ($lang): $line"; return 1; } done done done [[ "$output" == "ORIGINE"*"TRAFIC"*"ÉTAT"* ]] } @test "rules without UFW access or with UFW inactive still lists docker-fence" { run --separate-stderr fence --set bin_ufw=/nonexistent/ufw rules [ "$status" -eq 0 ] [[ "$stderr" == *"command not found: /nonexistent/ufw"* ]] row fence "published ports" ALLOW "443/tcp" STUB_UFW_INACTIVE=1 run --separate-stderr fence rules [ "$status" -eq 0 ] [[ "$stderr" == *"UFW is inactive: no UFW rule applies"* ]] [[ "$output" != *"host input"* ]] row fence "published ports" ALLOW "443/tcp" } @test "rules changes nothing" { fence apply fence confirm local before kernel before="$(sum_of "$RULES")" kernel="$(sum_of "$KERNEL")" : >"$STUB_LOG" run fence rules [ "$status" -eq 0 ] [ "$(sum_of "$RULES")" = "$before" ] [ "$(sum_of "$KERNEL")" = "$kernel" ] ! grep -qE '^(ufw (reload|allow|delete|route)|iptables -[ADFXIN]|systemd-run|systemctl)' "$STUB_LOG" } @test "rules: chain loaded but DOCKER-USER jump missing means not loaded" { fence apply # Something removed the jump (for example a manual iptables -D). "$REPO/tests/stubs/iptables" -D DOCKER-USER -j docker-fence grep -qF -- '-A docker-fence -s 192.0.2.10/32 -j RETURN' "$KERNEL" run --separate-stderr fence rules row fence "published ports" ALLOW "443/tcp" any "not loaded" allow row fence "published ports" ALLOW "all published ports" "192.0.2.10/32" "not loaded" admin # container_to_host rules do not depend on the jump. row fence "container -> host" ALLOW "8080/tcp" "iface br-+" loaded } @test "rules in French translates the UFW interface marker too" { run --separate-stderr fence --lang fr rules [ "$status" -eq 0 ] row ufw "entrant hôte" ALLOW "tout via wg0" "192.0.2.10" actif [[ "$output" != *" on wg0"* ]] } @test "rules: no colors when standard output is not a terminal" { fence apply run --separate-stderr fence rules [ "$status" -eq 0 ] [[ "$output" != *$'\e['* ]] run --separate-stderr fence --color never rules [[ "$output" != *$'\e['* ]] } @test "rules --color always: bold header, colored actions and states" { fence apply run --separate-stderr fence --color always rules [ "$status" -eq 0 ] [[ "${lines[0]}" == $'\e[1mORIGIN\e[0m'* ]] [[ "${lines[1]}" == $'\e[2m-'* ]] [[ "$output" == *$'\e[1;32mALLOW\e[0m'* ]] [[ "$output" == *$'\e[1;31mDENY\e[0m'* ]] [[ "$output" == *$'\e[1;33mLIMIT\e[0m'* ]] [[ "$output" == *$'\e[1;33mLOG\e[0m'* ]] [[ "$output" == *$'\e[32mloaded\e[0m'* ]] [[ "$output" == *$'\e[32mactive\e[0m'* ]] [[ "$output" == *$'\e[36mufw\e[0m'* ]] [[ "$output" == *$'\e[35mfence\e[0m'* ]] fence confirm fence --mode enforce apply run --separate-stderr fence --mode enforce --color=always rules [[ "$output" == *$'\e[1;31mDROP\e[0m'* ]] } @test "rules --color always: states not loaded and unknown" { run --separate-stderr fence --color always rules [[ "$output" == *$'\e[31mnot loaded\e[0m'* ]] STUB_IPT_CHECK_RC=4 run --separate-stderr fence --color always rules [[ "$output" == *$'\e[33munknown (root?)\e[0m'* ]] run --separate-stderr fence --lang fr --color always rules [[ "$output" == *$'\e[31mnon chargée\e[0m'* ]] } @test "rules --color always: same table as without colors once codes are removed" { fence apply local plain colored for lang in en fr; do plain="$(fence --lang "$lang" --color never rules 2>/dev/null)" colored="$(fence --lang "$lang" --color always rules 2>/dev/null | sed -E $'s/\e\\[[0-9;]*m//g')" [ "$plain" = "$colored" ] done } @test "rules --color: invalid value and other commands refused" { run fence --color sometimes rules [ "$status" -eq 1 ] run fence --color always check [ "$status" -eq 1 ] } @test "rules as root before apply: missing chain (iptables -C exits 2) means not loaded, not unknown" { run "$REPO/tests/stubs/iptables" -C DOCKER-USER -j docker-fence [ "$status" -eq 2 ] run --separate-stderr fence rules [ "$status" -eq 0 ] [[ "$output" != *"unknown"* ]] row fence "published ports" ALLOW any any "not loaded" "replies to established connections" row fence "container -> host" ALLOW "8080/tcp" "iface br-+" "not loaded" } @test "localized system (LANGUAGE=fr): UFW is still read as active" { LANGUAGE=fr_FR:fr run --separate-stderr fence rules [ "$status" -eq 0 ] [[ "$stderr" != *"inactive"* ]] row ufw "host input" ALLOW "22/tcp" any active ssh LANGUAGE=fr_FR:fr run --separate-stderr fence apply [ "$status" -eq 0 ] }