fix(iplimit): install the iptables tools the ban action needs

The 3x-ipl Fail2ban action bans with iptables and ip6tables, but the setup installed only fail2ban and nftables. Debian 12/13 and Ubuntu 22.04/24.04 do not pull iptables in with fail2ban, so on a host without it every ban logged "iptables: not found", showed up in the jail's banned list and applied no rule, while the setup reported success.

A configured 3x-ipl jail now always has its ban tools. Writing the action installs them when they are missing, and so does an update over a jail an earlier release set up, whether the choice is stored or not. A jail that is already running is restarted after the install so bans it recorded without a rule get one. When the tools cannot be installed the setup fails with the reason instead of writing a jail that enforces nothing.

The lifecycle fixture now models the tools as present, so its cases no longer depend on the host running the tests.
This commit is contained in:
Sanaei
2026-11-29 11:02:51 +03:30
parent 428dfe7709
commit 2db45393cf
2 changed files with 130 additions and 3 deletions
+98 -2
View File
@@ -107,7 +107,11 @@ systemctl() {
}
apt-get() { rm -f "$TEST_DIR/installed"; }
command() {
if [[ $* == '-v fail2ban-client' ]]; then [[ -f "$TEST_DIR/installed" ]]; else builtin command "$@"; fi
case "$*" in
'-v fail2ban-client') [[ -f "$TEST_DIR/installed" ]] ;;
'-v iptables' | '-v ip6tables') [[ -f "$TEST_DIR/$2" ]] ;;
*) builtin command "$@" ;;
esac
}
fail2ban-client() {
case "$*" in
@@ -139,7 +143,7 @@ setup_fail2ban_iplimit() {
"confirm", "read_fail2ban_preference", "persist_fail2ban_preference",
"iplimit_jail_configured", "setup_fail2ban_for_lifecycle", "apply_iplimit_preference",
"iplimit_remove_conflicts", "iplimit_can_uninstall", "remove_iplimit_jail",
"install_iplimit", "remove_iplimit",
"install_iplimit", "remove_iplimit", "iplimit_ban_tools_present", "ensure_iplimit_ban_tools",
} {
if strings.Contains(string(source), "\n"+name+"() {\n") {
driver += strings.ReplaceAll(scriptFunction(t, "x-ui.sh", name), "/etc/fail2ban", filepath.Join(dir, "fail2ban"))
@@ -159,9 +163,101 @@ setup_fail2ban_iplimit() {
driver += wrapper
}
driver += "\nexport NONINTERACTIVE=1\n"
writeFail2banFixture(t, dir, "iptables", "")
writeFail2banFixture(t, dir, "ip6tables", "")
return driver
}
func TestIPLimitBanToolsAreEnsured(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("requires a POSIX shell")
}
bash, err := exec.LookPath("bash")
if err != nil {
t.Skip("requires bash")
}
const legacyJail = "[3x-ipl]\nenabled = true\nbantime = 47m\n"
const install = "update\ninstall -y iptables\n"
for _, tc := range []struct {
name, command, env, wantPackages, wantRestarts string
present, unavailable, jail, wantFailure bool
}{
{name: "writing the action installs the missing tools", command: "create_iplimit_jails", wantPackages: install},
{name: "present tools need no package and no restart", command: "create_iplimit_jails", present: true, jail: true},
{name: "an update repairs a running legacy jail", command: "setup_fail2ban_for_lifecycle update", jail: true, wantPackages: install, wantRestarts: "restart 3x-ipl\n"},
{name: "an update repairs a stored opt-in", command: "setup_fail2ban_for_lifecycle update", env: "XUI_ENABLE_FAIL2BAN=true\n", jail: true, wantPackages: install, wantRestarts: "restart 3x-ipl\n"},
{name: "tools that cannot be installed fail the setup", command: "create_iplimit_jails", unavailable: true, wantPackages: install, wantFailure: true},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
driver := setupFail2banScriptFixture(t, dir) + `
iplimit_log_path="$TEST_DIR/3xipl.log" iplimit_banned_log_path="$TEST_DIR/3xipl-banned.log" xui_folder="$TEST_DIR/app"
apt-get() {
printf '%s\n' "$*" >> "$TEST_DIR/packages"
if [[ $* == *iptables* && ! -f "$TEST_DIR/unavailable" ]]; then touch "$TEST_DIR/iptables" "$TEST_DIR/ip6tables"; fi
}
fail2ban-client() {
case "$*" in
'status 3x-ipl') [[ -f "$TEST_DIR/managed-running" ]] ;;
'restart 3x-ipl') printf '%s\n' "$*" >> "$TEST_DIR/restarts" ;;
*) return 1 ;;
esac
}
` + strings.ReplaceAll(scriptFunction(t, "x-ui.sh", "create_iplimit_jails"), "/etc/fail2ban", filepath.Join(dir, "fail2ban"))
for _, sub := range []string{"jail.d", "filter.d", "action.d"} {
if err := os.MkdirAll(filepath.Join(dir, "fail2ban", sub), 0o755); err != nil {
t.Fatal(err)
}
}
if !tc.present {
for _, tool := range []string{"iptables", "ip6tables"} {
if err := os.Remove(filepath.Join(dir, tool)); err != nil {
t.Fatal(err)
}
}
}
if tc.unavailable {
writeFail2banFixture(t, dir, "unavailable", "")
}
if tc.jail {
writeFail2banFixture(t, dir, "fail2ban/jail.d/3x-ipl.conf", legacyJail)
writeFail2banFixture(t, dir, "installed", "")
writeFail2banFixture(t, dir, "managed-running", "")
}
writeFail2banFixture(t, dir, "env", tc.env)
writeFail2banFixture(t, dir, "packages", "")
writeFail2banFixture(t, dir, "restarts", "")
cmd := exec.CommandContext(t.Context(), bash, "-c", driver+"\n"+tc.command)
for _, e := range os.Environ() {
if !strings.HasPrefix(e, "XUI_") && !strings.HasPrefix(e, "TEST_DIR=") {
cmd.Env = append(cmd.Env, e)
}
}
cmd.Env = append(cmd.Env, "TEST_DIR="+dir)
out, err := cmd.CombinedOutput()
if (err != nil) != tc.wantFailure {
t.Fatalf("shell flow error = %v, want failure %t\n%s", err, tc.wantFailure, out)
}
for _, tool := range []string{"iptables", "ip6tables"} {
_, statErr := os.Stat(filepath.Join(dir, tool))
if (statErr == nil) == tc.wantFailure {
t.Errorf("%s present = %t after the flow, want %t", tool, statErr == nil, !tc.wantFailure)
}
}
if got := readScriptState(t, dir, "packages"); got != tc.wantPackages {
t.Errorf("package manager calls = %q, want %q", got, tc.wantPackages)
}
if got := readScriptState(t, dir, "restarts"); got != tc.wantRestarts {
t.Errorf("jail restarts = %q, want %q", got, tc.wantRestarts)
}
_, actionErr := os.Stat(filepath.Join(dir, "fail2ban", "action.d", "3x-ipl.conf"))
if tc.wantFailure && (actionErr == nil || !strings.Contains(string(out), "iptables and ip6tables")) {
t.Errorf("setup without ban tools wrote the action (stat %v) or hid the reason: %s", actionErr, out)
}
})
}
}
func TestNativeFail2banStockSSHBackend(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("requires a POSIX shell")