Files
2026-12-05 15:32:27 +00:00

419 lines
14 KiB
Bash

#!/usr/bin/env bats
# Configuration parsing.
load helpers
bats_require_minimum_version 1.5.0
setup() {
setup_env
}
# Checks that a configuration line is refused, with its line number.
# expect_refused LINE PATTERN
expect_refused() {
printf 'admin 192.0.2.10\n%s\n' "$1" | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"fence.conf:2: "* ]]
[[ "$output" == *"$2"* ]]
}
@test "the example of the repository is valid" {
cp "$REPO/examples/fence.conf" "$CONF"
chmod 0644 "$CONF"
run fence check
[ "$status" -eq 0 ]
[[ "$output" == *"configuration valid"* ]]
}
@test "empty lines, comments and comment after a directive" {
write_conf <<'EOF'
# comment
# indented comment
admin 192.0.2.10 # admin workstation
mode enforce # tabs
EOF
run fence show-config
[ "$status" -eq 0 ]
[[ "$output" == *"admin 192.0.2.10/32"*"# line 4"* ]]
[[ "$output" == *"mode enforce"*"# line 5"* ]]
}
@test "a # stuck to a value is refused" {
expect_refused 'mode observe#x' 'must follow a blank'
}
@test "unknown directive refused with its line number" {
expect_refused 'alow tcp 443 from any' 'unknown directive: "alow"'
}
@test "\$ and backtick are refused, never interpreted" {
expect_refused 'admin $HOME' 'forbidden character: "$"'
expect_refused 'admin `id`' 'forbidden character: "`"'
expect_refused 'log_file /tmp/$(touch x)' 'forbidden character'
[ ! -e x ]
[ ! -e "$BATS_TEST_TMPDIR/x" ]
}
@test "quotes, semicolon and backslash are refused" {
expect_refused 'mode "observe"' 'forbidden character'
expect_refused 'mode observe; id' 'forbidden character: ";"'
expect_refused 'mode observe\' 'forbidden character'
}
@test "invalid IPv4 addresses" {
expect_refused 'admin 256.1.1.1' 'invalid IPv4 address'
expect_refused 'admin 192.0.2' 'invalid IPv4 address'
expect_refused 'admin 192.0.2.010' 'invalid IPv4 address'
expect_refused 'admin 192.0.2.1/33' 'invalid IPv4 address'
expect_refused 'admin 192.0.2.1/' 'invalid IPv4 address'
expect_refused 'admin 192.0.2.1/24' 'host bits are set'
expect_refused 'admin 0.0.0.0/0' 'covers every address'
expect_refused 'allow tcp 80 from 0.0.0.0/0' 'covers every address'
expect_refused 'admin fe80::1' 'invalid IPv4 address'
}
@test "invalid ports and ranges" {
expect_refused 'allow tcp 0 from any' 'invalid port:'
expect_refused 'allow tcp 65536 from any' 'invalid port:'
expect_refused 'allow tcp 080 from any' 'invalid port:'
expect_refused 'allow tcp http from any' 'invalid port:'
expect_refused 'allow tcp 10-5 from any' 'invalid port range'
expect_refused 'allow tcp 10-10 from any' 'invalid port range'
expect_refused 'allow tcp 1:5 from any' 'invalid port:'
expect_refused 'allow icmp 80 from any' 'invalid protocol'
}
@test "port boundaries are accepted" {
write_conf <<'EOF'
admin 192.0.2.10
allow tcp 1 from any
allow udp 65535 from any
allow tcp 1-65535 from 198.51.100.0/24
container_to_host udp 1-2
EOF
run fence show-config
[ "$status" -eq 0 ]
[[ "$output" == *"allow tcp 1-65535 from 198.51.100.0/24"* ]]
[[ "$output" == *"container_to_host udp 1-2 from_iface br-+"* ]]
}
@test "rule syntax" {
expect_refused 'admin' 'expected syntax: admin'
expect_refused 'admin 192.0.2.1 wg0' 'expected syntax: admin'
expect_refused 'admin 192.0.2.1 iface' 'expected syntax: admin'
expect_refused 'allow tcp 443 any' 'expected syntax: allow'
expect_refused 'allow tcp 443 to any' 'expected syntax: allow'
expect_refused 'allow tcp 443 from any iface' 'expected syntax: allow'
expect_refused 'container_to_host tcp' 'expected syntax: container_to_host'
expect_refused 'container_to_host tcp 80 iface br-+' 'expected syntax: container_to_host'
expect_refused 'trusted_iface' 'expected syntax: trusted_iface'
expect_refused 'trusted_iface a b' 'expected syntax: trusted_iface'
}
@test "interface names" {
expect_refused 'admin 192.0.2.1 iface wg0/1' 'invalid interface name'
expect_refused 'trusted_iface abcdefghijklmnop' 'invalid interface name'
expect_refused 'trusted_iface br+x' 'invalid interface name'
write_conf <<'EOF'
admin 192.0.2.10 iface abcdefghijklmno
trusted_iface br-+
trusted_iface veth.1_a-b
EOF
run fence check
[ "$status" -eq 0 ]
}
@test "repeated rules add up, in order" {
write_conf <<'EOF'
admin 192.0.2.10
admin 192.0.2.11
allow tcp 443 from any
allow tcp 443 from 198.51.100.20
EOF
run fence check
[ "$status" -eq 0 ]
[[ "$output" == *"-s 192.0.2.10/32 -j RETURN"*"-s 192.0.2.11/32 -j RETURN"* ]]
[[ "$output" == *"--ctorigdstport 443 -j RETURN"*"--ctorigdstport 443 -s 198.51.100.20/32 -j RETURN"* ]]
}
@test "duplicates refused with the line of the first rule" {
write_conf <<'EOF'
admin 192.0.2.10
admin 192.0.2.10/32
allow tcp 443 from any
allow tcp 443 from any
container_to_host tcp 8080
container_to_host tcp 8080 from_iface br-+
trusted_iface wg0
trusted_iface wg0
EOF
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"fence.conf:2: "*"same rule as line 1"* ]]
[[ "$output" == *"fence.conf:4: "*"same rule as line 3"* ]]
[[ "$output" == *"fence.conf:6: "*"same rule as line 5"* ]]
[[ "$output" == *"fence.conf:8: "*"same rule as line 7"* ]]
}
@test "the same source on another interface is not a duplicate" {
write_conf <<'EOF'
admin 192.0.2.10
admin 192.0.2.10 iface wg0
EOF
run fence check
[ "$status" -eq 0 ]
}
@test "a repeated setting takes the last value" {
write_conf <<'EOF'
admin 192.0.2.10
mode enforce
rollback_seconds 60
mode observe
EOF
run fence show-config
[ "$status" -eq 0 ]
[[ "$output" == *"mode observe"*"# line 4"* ]]
}
@test "a setting takes exactly one value" {
expect_refused 'mode observe enforce' '"mode" takes exactly one value'
expect_refused 'mode' '"mode" takes exactly one value'
}
@test "report_since accepts several words" {
printf 'admin 192.0.2.10\nreport_since 2 days ago\n' | write_conf
run fence show-config
[ "$status" -eq 0 ]
[[ "$output" == *"report_since 2 days ago"* ]]
}
@test "invalid setting values" {
expect_refused 'mode block' 'invalid mode'
expect_refused 'lang de' 'invalid language'
expect_refused 'rollback_seconds 29' 'integer expected between 30-86400'
expect_refused 'rollback_seconds 86401' 'integer expected'
expect_refused 'rollback_seconds -5' 'integer expected'
expect_refused 'log_level 8' 'integer expected between 0-7'
expect_refused 'log_burst_enforce 0' 'integer expected'
expect_refused 'backup_keep 0' 'integer expected'
expect_refused 'log_rate_observe 20' 'invalid rate'
expect_refused 'log_rate_observe 20/week' 'invalid rate'
expect_refused 'log_file var/log/x.log' 'invalid path'
expect_refused 'backup_dir /var/../etc' 'invalid path'
expect_refused 'ufw_after_rules /etc/ufw/' 'invalid path'
expect_refused 'bin_ufw ../ufw' 'invalid command'
expect_refused 'unit_name a.b' 'invalid unit name'
expect_refused 'block_marker A:B' 'invalid block marker'
}
@test "log prefix: 28 characters accepted, 29 refused" {
printf 'admin 192.0.2.10\nlog_prefix_drop %s\n' "$(printf 'A%.0s' {1..28})" | write_conf
run fence check
[ "$status" -eq 0 ]
[[ "$output" == *"--log-prefix \"$(printf 'A%.0s' {1..28}) \""* ]]
expect_refused "log_prefix_drop $(printf 'A%.0s' {1..29})" 'at most 28 characters'
expect_refused 'log_prefix_drop [UFW]' 'forbidden character'
}
@test "identical prefixes refused" {
printf 'admin 192.0.2.10\nlog_prefix_drop X\nlog_prefix_observe X\n' | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"must differ"* ]]
}
@test "chain name: <name>-deny must fit in 28 characters" {
printf 'admin 192.0.2.10\nchain %s\n' "$(printf 'c%.0s' {1..23})" | write_conf
run fence check
[ "$status" -eq 0 ]
[[ "$output" == *":$(printf 'c%.0s' {1..23})-deny - [0:0]"* ]]
expect_refused "chain $(printf 'c%.0s' {1..24})" 'at most 23 characters'
}
@test "reserved chain names" {
expect_refused 'chain DOCKER-USER' 'reserved chain name'
expect_refused 'chain DOCKER' 'reserved chain name'
expect_refused 'chain DOCKER-FORWARD' 'reserved chain name'
expect_refused 'chain ufw-x' 'reserved chain name'
expect_refused 'chain FORWARD' 'reserved chain name'
expect_refused 'chain drop' 'reserved chain name'
}
@test "every default value passes validation" {
source "$REPO/lib/messages.sh"
source "$REPO/lib/config.sh"
local k
for k in "${FENCE_SETTINGS[@]}"; do
fence_validate_setting "$k" "${FENCE_DEFAULTS[$k]}" || {
echo "invalid default: $k"
return 1
}
done
[ "${#FENCE_SETTINGS[@]}" -eq "${#FENCE_DEFAULTS[@]}" ]
}
@test "empty file or comments only: admin missing" {
: | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *'at least one "admin" directive is required'* ]]
printf '# nothing\n\n \n' | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *'at least one "admin" directive is required'* ]]
}
@test "at least one admin is required" {
printf 'mode observe\nallow tcp 443 from any\n' | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *'at least one "admin" directive is required'* ]]
}
@test "every error is reported, each with its line" {
write_conf <<'EOF'
admin 192.0.2.10
mode bad
allow tcp 99999 from 1.2.3
foo bar
EOF
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"fence.conf:2: "*"invalid mode"* ]]
[[ "$output" == *"fence.conf:3: "*"invalid port"* ]]
[[ "$output" == *"fence.conf:3: "*"invalid IPv4 address"* ]]
[[ "$output" == *"fence.conf:4: "*"unknown directive"* ]]
# Nothing is generated when the configuration is invalid.
[[ "$output" != *"BEGIN"* ]]
}
@test "file without a final newline" {
printf 'admin 192.0.2.10\nmode enforce' | write_conf
run fence show-config
[ "$status" -eq 0 ]
[[ "$output" == *"mode enforce"*"# line 2"* ]]
}
@test "Windows line endings refused" {
printf 'admin 192.0.2.10\r\n' | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"fence.conf:1: "*"forbidden character"* ]]
}
@test "priority: option > environment > file > default" {
printf 'admin 192.0.2.10\nmode enforce\nlang fr\n' | write_conf
run fence show-config
[[ "$output" == *"mode enforce"*"# ligne 2"* ]]
[[ "$output" == *"rollback_seconds 120"*"# défaut"* ]]
[[ "$output" == *"lang fr"*"# ligne 3"* ]]
FENCE_LANG=en run fence show-config
[[ "$output" == *"lang en"*"# environment"* ]]
FENCE_LANG=en run fence --lang fr --mode observe --set rollback_seconds=300 show-config
[ "$status" -eq 0 ]
[[ "$output" == *"lang fr"*"# option"* ]]
[[ "$output" == *"mode observe"*"# option"* ]]
[[ "$output" == *"rollback_seconds 300"*"# option"* ]]
}
@test "invalid --set options" {
minimal_conf
run fence --set nope=1 check
[ "$status" -eq 1 ]
[[ "$output" == *"invalid option: --set nope=1"* ]]
run fence --set mode=bad check
[ "$status" -eq 1 ]
run fence --set mode check
[ "$status" -eq 1 ]
run fence --mode block check
[ "$status" -eq 1 ]
FENCE_LANG=xx run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"invalid variable FENCE_LANG"* ]]
}
@test "messages in French" {
printf 'admin 192.0.2.10\nlang fr\nfoo\n' | write_conf
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"erreur : "*"fence.conf:3: directive inconnue : « foo »"* ]]
}
@test "search order: --config, FENCE_CONFIG, ./fence.conf, /etc" {
printf 'admin 192.0.2.10\nmode enforce\n' >"$CONF"; chmod 0644 "$CONF"
mkdir -p work && cd work
run "$FENCE" show-config
[[ "$output" == *"mode enforce"* ]]
printf 'admin 192.0.2.10\nrollback_seconds 31\n' >fence.conf; chmod 0644 fence.conf
run "$FENCE" show-config
[[ "$output" == *"rollback_seconds 31"* ]]
printf 'admin 192.0.2.10\nrollback_seconds 32\n' >env.conf; chmod 0644 env.conf
FENCE_CONFIG=env.conf run "$FENCE" show-config
[[ "$output" == *"rollback_seconds 32"* ]]
printf 'admin 192.0.2.10\nrollback_seconds 33\n' >opt.conf; chmod 0644 opt.conf
FENCE_CONFIG=env.conf run "$FENCE" --config opt.conf show-config
[[ "$output" == *"rollback_seconds 33"* ]]
[[ "$output" == *"$PWD/opt.conf"* ]]
}
@test "missing file" {
run "$FENCE" -c /nonexistent.conf check
[ "$status" -eq 1 ]
[[ "$output" == *"missing or unreadable: /nonexistent.conf"* ]]
}
@test "file writable by group or others refused" {
minimal_conf
chmod 0664 "$CONF"
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"writable by group or others (mode 664)"* ]]
chmod 0646 "$CONF"
run fence check
[ "$status" -eq 1 ]
chmod 0600 "$CONF"
run fence check
[ "$status" -eq 0 ]
}
@test "file owned by another user refused" {
[ "$EUID" -eq 0 ] || skip "chown needs root"
minimal_conf
chown 12345 "$CONF"
run fence check
[ "$status" -eq 1 ]
[[ "$output" == *"must be owned by root"* ]]
}
@test "show-config prints a valid, equivalent configuration" {
cp "$REPO/examples/fence.conf" "$CONF"; chmod 0644 "$CONF"
run fence check
local before="$output"
fence show-config >"$BATS_TEST_TMPDIR/again.conf"
chmod 0644 "$BATS_TEST_TMPDIR/again.conf"
run "$FENCE" -c "$BATS_TEST_TMPDIR/again.conf" check
[ "$status" -eq 0 ]
[ "${output#*# BEGIN}" = "${before#*# BEGIN}" ]
}
@test "default trusted_iface: br-+ and docker0" {
minimal_conf
run fence show-config
[[ "$output" == *"trusted_iface br-+"*"# default"* ]]
[[ "$output" == *"trusted_iface docker0"*"# default"* ]]
printf 'admin 192.0.2.10\ntrusted_iface br-custom\n' | write_conf
run fence check
[[ "$output" == *"-i br-custom -j RETURN"* ]]
[[ "$output" != *"-i docker0"* ]]
[[ "$output" != *"-i br-+ -j RETURN"* ]]
}