115 lines
3.6 KiB
Bash
115 lines
3.6 KiB
Bash
#!/usr/bin/env bats
|
|
# Block generation: comparison with golden files and real syntax check
|
|
# with iptables-restore --test.
|
|
|
|
load helpers
|
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
setup() {
|
|
setup_env
|
|
}
|
|
|
|
# golden CONFIG REFERENCE [OPTIONS...]
|
|
golden() {
|
|
local ref="$2"
|
|
# Copy: the configuration must belong to the current user.
|
|
cp "$1" "$CONF"
|
|
chmod 0644 "$CONF"
|
|
shift 2
|
|
run --separate-stderr fence "$@" check
|
|
[ "$status" -eq 0 ]
|
|
diff -u "$GOLDEN/$ref" <(printf '%s\n' "$output")
|
|
}
|
|
|
|
@test "example in observe mode" {
|
|
golden "$REPO/examples/fence.conf" example-observe.rules
|
|
}
|
|
|
|
@test "example in enforce mode" {
|
|
golden "$REPO/examples/fence.conf" example-enforce.rules --mode enforce
|
|
}
|
|
|
|
@test "minimal configuration: no second container_to_host block" {
|
|
golden "$FIXTURES/conf/minimal.conf" minimal-observe.rules
|
|
[[ "$output" != *"ufw-after-input"* ]]
|
|
}
|
|
|
|
@test "changed settings, interfaces, udp, ranges, French header" {
|
|
golden "$FIXTURES/conf/custom.conf" custom-enforce.rules
|
|
}
|
|
|
|
@test "standard output only carries the block" {
|
|
cp "$REPO/examples/fence.conf" "$CONF"; chmod 0644 "$CONF"
|
|
run --separate-stderr fence check
|
|
[ "${lines[0]}" = "# BEGIN DOCKER-FENCE (managed by docker-fence, do not edit by hand)" ]
|
|
[ "${lines[-1]}" = "# END DOCKER-FENCE" ]
|
|
[[ "$stderr" == *"configuration valid"* ]]
|
|
}
|
|
|
|
@test "one rule per source, never a comma-separated list" {
|
|
cp "$REPO/examples/fence.conf" "$CONF"; chmod 0644 "$CONF"
|
|
run --separate-stderr fence check
|
|
! grep -E -- '-s [^ ]*,' <<<"$output"
|
|
}
|
|
|
|
@test "from any drops the -s option" {
|
|
printf 'admin 192.0.2.10\nallow tcp 443 from any\n' | write_conf
|
|
run --separate-stderr fence check
|
|
grep -qxF -- '-A docker-fence -p tcp -m conntrack --ctorigdstport 443 -j RETURN' <<<"$output"
|
|
}
|
|
|
|
@test "check changes nothing" {
|
|
cp "$REPO/examples/fence.conf" "$CONF"; chmod 0644 "$CONF"
|
|
local before
|
|
before="$(sum_of "$RULES")"
|
|
run fence check
|
|
[ "$status" -eq 0 ]
|
|
[ "$(sum_of "$RULES")" = "$before" ]
|
|
[ ! -e "$BACKUPS" ]
|
|
[ ! -s "$KERNEL" ]
|
|
! grep -qE '^(ufw reload|iptables -[ADFX]|systemd-run)' "$STUB_LOG"
|
|
}
|
|
|
|
# Real syntax: needs iptables-restore and the NET_ADMIN capability (the test
|
|
# runs in the network namespace of the test container, nothing is loaded).
|
|
REAL_PATH=/usr/sbin:/sbin:/usr/bin:/bin
|
|
|
|
need_iptables_restore() {
|
|
PATH="$REAL_PATH" command -v iptables-restore >/dev/null || skip "iptables-restore not found"
|
|
PATH="$REAL_PATH" iptables -S >/dev/null 2>&1 || skip "not enough privileges (NET_ADMIN)"
|
|
}
|
|
|
|
# Returns the exit code of iptables-restore --test on the given block.
|
|
restore_test() {
|
|
local input="$BATS_TEST_TMPDIR/restore.rules"
|
|
{
|
|
# Chain declared by UFW earlier in after.rules.
|
|
printf '*filter\n:ufw-after-input - [0:0]\nCOMMIT\n'
|
|
cat "$1"
|
|
} >"$input"
|
|
PATH="$REAL_PATH" iptables-restore --test --noflush "$input"
|
|
}
|
|
|
|
@test "iptables-restore --test accepts the observe block" {
|
|
need_iptables_restore
|
|
restore_test "$GOLDEN/example-observe.rules"
|
|
}
|
|
|
|
@test "iptables-restore --test accepts the enforce block" {
|
|
need_iptables_restore
|
|
restore_test "$GOLDEN/example-enforce.rules"
|
|
}
|
|
|
|
@test "iptables-restore --test accepts the custom block" {
|
|
need_iptables_restore
|
|
restore_test "$GOLDEN/custom-enforce.rules"
|
|
}
|
|
|
|
@test "iptables-restore --test refuses a broken block (the check is meaningful)" {
|
|
need_iptables_restore
|
|
sed 's/--ctorigdstport 443/--ctorigdstport x443/' "$GOLDEN/example-observe.rules" >"$BATS_TEST_TMPDIR/bad.rules"
|
|
run restore_test "$BATS_TEST_TMPDIR/bad.rules"
|
|
[ "$status" -ne 0 ]
|
|
}
|