Files
docker-fence/tests/stubs/iptables
T
2026-12-05 15:32:27 +00:00

38 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Fake iptables: kernel state is a list of "-A CHAIN ..." lines.
set -euo pipefail
printf 'iptables %s\n' "$*" >>"$STUB_LOG"
kernel="$STUB_STATE/kernel"
touch "$kernel"
op="${1:-}"
shift || true
case "$op" in
--version) echo "iptables v1.8.10 (nf_tables)" ;;
# Like the real one, -C compares arguments, not quoting, and exits 2 when
# the target chain does not exist. STUB_IPT_CHECK_RC simulates an error on
# every read (4: permission denied).
-C)
[[ -z "${STUB_IPT_CHECK_RC:-}" ]] || exit "$STUB_IPT_CHECK_RC"
args=("$@")
for (( i = 0; i < ${#args[@]} - 1; i++ )); do
if [[ "${args[i]}" == -j && "${args[i+1]}" =~ [a-z] ]] \
&& ! grep -q -- "^-A ${args[i+1]} " "$kernel"; then
echo "iptables: Couldn't load target \`${args[i+1]}':No such file or directory" >&2
exit 2
fi
done
tr -d '"' <"$kernel" | grep -qxF -- "-A $*"
;;
-D)
line="-A $*"
n="$(grep -nxF -- "$line" "$kernel" | head -n 1 | cut -d: -f1)"
[[ -n "$n" ]] || exit 1
sed -i "${n}d" "$kernel"
;;
-F) grep -v -- "^-A $1 " "$kernel" >"$kernel.tmp" || true; mv "$kernel.tmp" "$kernel" ;;
-X) printf '%s\n' "$1" >>"$STUB_STATE/deleted-chains" ;;
-S) [[ -z "${STUB_IPT_CHECK_RC:-}" ]] || exit "$STUB_IPT_CHECK_RC"; echo "-N $1"; grep -- "^-A $1 " "$kernel" || true ;;
-L) echo "Chain $1"; grep -- "^-A $1 " "$kernel" || true ;;
*) exit 1 ;;
esac