267 lines
8.3 KiB
Bash
267 lines
8.3 KiB
Bash
#!/usr/bin/env bats
|
|
# install, uninstall, original ufw-docker block, backups, environment.
|
|
|
|
load helpers
|
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
setup() {
|
|
setup_env
|
|
cp "$REPO/examples/fence.conf" "$CONF"
|
|
chmod 0644 "$CONF"
|
|
}
|
|
|
|
count_backups() {
|
|
find "$BACKUPS" -maxdepth 1 -name 'after.rules.[0-9]*' 2>/dev/null | wc -l
|
|
}
|
|
|
|
@test "install appends the block without touching the rest" {
|
|
run fence install
|
|
[ "$status" -eq 0 ]
|
|
# The start of the file is the original, byte for byte.
|
|
local n
|
|
n="$(wc -l <"$FIXTURES/after.rules")"
|
|
diff "$FIXTURES/after.rules" <(head -n "$n" "$RULES")
|
|
# Then an empty line and the golden block.
|
|
diff <(printf '\n'; cat "$GOLDEN/example-observe.rules") <(tail -n +"$((n + 1))" "$RULES")
|
|
}
|
|
|
|
@test "install does not reload UFW" {
|
|
run fence install
|
|
[ "$status" -eq 0 ]
|
|
! grep -q '^ufw reload' "$STUB_LOG"
|
|
! grep -q '^systemd-run' "$STUB_LOG"
|
|
[[ "$output" == *'next "ufw reload"'* ]]
|
|
}
|
|
|
|
@test "install is idempotent (byte for byte) and makes no useless backup" {
|
|
fence install
|
|
local first
|
|
first="$(sum_of "$RULES")"
|
|
[ "$(count_backups)" -eq 1 ]
|
|
run fence install
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"already up to date"* ]]
|
|
[ "$(sum_of "$RULES")" = "$first" ]
|
|
[ "$(count_backups)" -eq 1 ]
|
|
}
|
|
|
|
@test "install replaces the existing block, one block in the file" {
|
|
fence install
|
|
fence --mode enforce install
|
|
[ "$(grep -c '^# BEGIN DOCKER-FENCE' "$RULES")" -eq 1 ]
|
|
grep -qF -- '--ctstate DNAT -j docker-fence-deny' "$RULES"
|
|
! grep -qF -- 'DOCKER-WOULD-DROP' "$RULES"
|
|
# Back to observe mode: same file as after the first install.
|
|
fence install
|
|
local n
|
|
n="$(wc -l <"$FIXTURES/after.rules")"
|
|
diff <(cat "$FIXTURES/after.rules"; printf '\n'; cat "$GOLDEN/example-observe.rules") "$RULES"
|
|
}
|
|
|
|
@test "a block moved inside the file goes back to the end, once" {
|
|
{
|
|
cat "$GOLDEN/example-enforce.rules"
|
|
cat "$FIXTURES/after.rules"
|
|
} >"$RULES"
|
|
run fence install
|
|
[ "$status" -eq 0 ]
|
|
diff <(cat "$FIXTURES/after.rules"; printf '\n'; cat "$GOLDEN/example-observe.rules") "$RULES"
|
|
}
|
|
|
|
@test "file without a final newline" {
|
|
printf '*filter\nCOMMIT' >"$RULES"
|
|
fence install
|
|
diff <(printf '*filter\nCOMMIT\n\n'; cat "$GOLDEN/example-observe.rules") "$RULES"
|
|
}
|
|
|
|
@test "backup: original content, mode 0600, directory 0700" {
|
|
local before
|
|
before="$(sum_of "$RULES")"
|
|
fence install
|
|
local b
|
|
b="$(find "$BACKUPS" -name 'after.rules.[0-9]*')"
|
|
[ "$(sum_of "$b")" = "$before" ]
|
|
[ "$(stat -c %a "$b")" = 600 ]
|
|
[ "$(stat -c %a "$BACKUPS")" = 700 ]
|
|
}
|
|
|
|
@test "atomic write: mode and owner kept, no temporary file left" {
|
|
chmod 0604 "$RULES"
|
|
if [ "$EUID" -eq 0 ]; then
|
|
chown 4321:4322 "$RULES"
|
|
fi
|
|
fence install
|
|
[ "$(stat -c %a "$RULES")" = 604 ]
|
|
if [ "$EUID" -eq 0 ]; then
|
|
[ "$(stat -c %u:%g "$RULES")" = 4321:4322 ]
|
|
fi
|
|
[ -z "$(find "$FENCE_ROOT/etc/ufw" -name '.after.rules.*')" ]
|
|
}
|
|
|
|
@test "backup rotation (backup_keep)" {
|
|
printf 'backup_keep 3\n' >>"$CONF"
|
|
local i
|
|
for i in 1 2 3 4 5; do
|
|
fence --set rollback_seconds="$((100 + i))" --set log_burst_observe="$i" install
|
|
done
|
|
[ "$(count_backups)" -eq 3 ]
|
|
}
|
|
|
|
@test "missing after.rules: unsupported environment" {
|
|
rm "$RULES"
|
|
run fence install
|
|
[ "$status" -eq 2 ]
|
|
[[ "$output" == *"missing or unreadable"* ]]
|
|
}
|
|
|
|
@test "inconsistent managed block: start without end, two starts, end alone" {
|
|
local before
|
|
printf '\n# BEGIN DOCKER-FENCE\n*filter\n' >>"$RULES"
|
|
before="$(sum_of "$RULES")"
|
|
run fence install
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"without an end"* ]]
|
|
[ "$(sum_of "$RULES")" = "$before" ]
|
|
|
|
cp "$FIXTURES/after.rules" "$RULES"
|
|
cat "$GOLDEN/example-observe.rules" "$GOLDEN/example-observe.rules" >>"$RULES"
|
|
run fence install
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"twice"* ]]
|
|
|
|
cp "$FIXTURES/after.rules" "$RULES"
|
|
printf '# END DOCKER-FENCE\n' >>"$RULES"
|
|
run fence install
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"without a start"* ]]
|
|
}
|
|
|
|
@test "original block without --replace-legacy: exit code 3, nothing changes" {
|
|
add_legacy
|
|
local before
|
|
before="$(sum_of "$RULES")"
|
|
run fence install
|
|
[ "$status" -eq 3 ]
|
|
[[ "$output" == *"--replace-legacy"* ]]
|
|
[[ "$output" == *"# BEGIN UFW AND DOCKER"* ]]
|
|
[ "$(sum_of "$RULES")" = "$before" ]
|
|
[ ! -e "$BACKUPS" ]
|
|
}
|
|
|
|
@test "--replace-legacy removes and saves the original block, managed block at the end" {
|
|
add_legacy
|
|
run fence install --replace-legacy
|
|
[ "$status" -eq 0 ]
|
|
! grep -q 'UFW AND DOCKER' "$RULES"
|
|
diff <(cat "$FIXTURES/after.rules"; printf '\n'; cat "$GOLDEN/example-observe.rules") "$RULES"
|
|
diff "$FIXTURES/legacy-block.rules" "$BACKUPS/legacy-block.rules"
|
|
[ "$(stat -c %a "$BACKUPS/legacy-block.rules")" = 600 ]
|
|
}
|
|
|
|
@test "modified original block: reported with a diff, replaced only with --force" {
|
|
add_legacy
|
|
sed -i 's|-A DOCKER-USER -j RETURN -s 192.168.0.0/16|-A DOCKER-USER -j RETURN -s 192.168.1.0/24|' "$RULES"
|
|
local before
|
|
before="$(sum_of "$RULES")"
|
|
run fence install --replace-legacy
|
|
[ "$status" -eq 3 ]
|
|
[[ "$output" == *"differs from the known version"* ]]
|
|
[[ "$output" == *"+-A DOCKER-USER -j RETURN -s 192.168.1.0/24"* ]]
|
|
[ "$(sum_of "$RULES")" = "$before" ]
|
|
|
|
run fence install --replace-legacy --force
|
|
[ "$status" -eq 0 ]
|
|
! grep -q 'UFW AND DOCKER' "$RULES"
|
|
grep -q '192.168.1.0/24' "$BACKUPS/legacy-block.rules"
|
|
}
|
|
|
|
@test "an original block without an end is refused" {
|
|
printf '\n# BEGIN UFW AND DOCKER\n*filter\n' >>"$RULES"
|
|
run fence install --replace-legacy
|
|
[ "$status" -eq 1 ]
|
|
}
|
|
|
|
@test "uninstall removes the block, reloads UFW and cleans the kernel" {
|
|
fence install
|
|
"$REPO/tests/stubs/ufw" reload
|
|
grep -qxF -- '-A DOCKER-USER -j docker-fence' "$KERNEL"
|
|
run fence uninstall
|
|
[ "$status" -eq 0 ]
|
|
diff "$FIXTURES/after.rules" "$RULES"
|
|
grep -q '^ufw reload' "$STUB_LOG"
|
|
# ufw reload (--noflush) does not flush DOCKER-USER: the tool removes the jump.
|
|
! grep -qF -- '-j docker-fence' "$KERNEL"
|
|
! grep -q '^-A docker-fence' "$KERNEL"
|
|
grep -qx 'docker-fence' "$STUB_STATE/deleted-chains"
|
|
grep -qx 'docker-fence-deny' "$STUB_STATE/deleted-chains"
|
|
}
|
|
|
|
@test "uninstall without block: nothing to do" {
|
|
run fence uninstall
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"no managed block"* ]]
|
|
diff "$FIXTURES/after.rules" "$RULES"
|
|
}
|
|
|
|
@test "uninstall with UFW inactive: file changed, nothing reloaded" {
|
|
fence install
|
|
STUB_UFW_INACTIVE=1 run fence uninstall
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"nothing reloaded"* ]]
|
|
! grep -q '^ufw reload' "$STUB_LOG"
|
|
}
|
|
|
|
@test "uninstall --restore-legacy puts the original block back" {
|
|
add_legacy
|
|
local orig
|
|
orig="$(sum_of "$RULES")"
|
|
fence install --replace-legacy
|
|
run fence uninstall --restore-legacy
|
|
[ "$status" -eq 0 ]
|
|
[ "$(sum_of "$RULES")" = "$orig" ]
|
|
}
|
|
|
|
@test "uninstall --restore-legacy without a saved original block" {
|
|
fence install
|
|
run fence uninstall --restore-legacy
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"no saved original block"* ]]
|
|
}
|
|
|
|
@test "daemon.json: iptables disabled or nftables backend refused (exit code 2)" {
|
|
printf '{\n "iptables" : false\n}\n' >"$FENCE_ROOT/etc/docker/daemon.json"
|
|
run fence install
|
|
[ "$status" -eq 2 ]
|
|
[[ "$output" == *"disables iptables"* ]]
|
|
run fence check
|
|
[ "$status" -eq 2 ]
|
|
|
|
printf '{"firewall-backend": "nftables"}\n' >"$FENCE_ROOT/etc/docker/daemon.json"
|
|
run fence install
|
|
[ "$status" -eq 2 ]
|
|
[[ "$output" == *"native nftables"* ]]
|
|
[ ! -e "$BACKUPS" ]
|
|
}
|
|
|
|
@test "daemon.json: IPv6 enabled gives a warning" {
|
|
printf '{"ipv6": true, "iptables": true}\n' >"$FENCE_ROOT/etc/docker/daemon.json"
|
|
run fence check
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"warning: "*"IPv6"* ]]
|
|
}
|
|
|
|
@test "macvlan and ipvlan networks reported" {
|
|
printf 'lan\n' >"$STUB_DOCKER_DIR/macvlan"
|
|
printf 'l2a\nl2b\n' >"$STUB_DOCKER_DIR/ipvlan"
|
|
run fence check
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"macvlan networks found (lan)"* ]]
|
|
[[ "$output" == *"ipvlan networks found (l2a l2b)"* ]]
|
|
}
|
|
|
|
@test "versions are printed" {
|
|
run fence check
|
|
[[ "$output" == *"bash ${BASH_VERSION}; docker 29.0.0; iptables v1.8.10 (nf_tables); ufw 0.36.2"* ]]
|
|
}
|