Add docker-fence v1 with tests and docs

This commit is contained in:
Djeex committed 2026-12-05 15:32:27 +00:00
commit 305cb562f8
46 files changed
+5473

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Fake docker: answers read from files in $STUB_DOCKER_DIR.
set -euo pipefail
printf 'docker %s\n' "$*" >>"$STUB_LOG"
d="${STUB_DOCKER_DIR:-/nonexistent}"
case "$1 ${2:-}" in
"version --format") echo "29.0.0" ;;
"ps -q") cat "$d/ps" 2>/dev/null || true ;;
"inspect --format") cat "$d/inspect" 2>/dev/null || true ;;
"network ls")
case "$*" in
*driver=macvlan*) cat "$d/macvlan" 2>/dev/null || true ;;
*driver=ipvlan*) cat "$d/ipvlan" 2>/dev/null || true ;;
*driver=bridge*) cat "$d/bridges" 2>/dev/null || true ;;
esac
;;
"network inspect") grep -- "^${*: -1} " "$d/subnets" 2>/dev/null | cut -d' ' -f2- || true ;;
*) exit 1 ;;
esac
+37
View File
@@ -0,0 +1,37 @@
#!/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
+4
View File
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail
printf 'journalctl %s\n' "$*" >>"$STUB_LOG"
cat "${STUB_JOURNAL:-/dev/null}"
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -euo pipefail
printf 'systemctl %s\n' "$*" >>"$STUB_LOG"
if [[ "${1:-}" == stop ]]; then
[[ -f "$STUB_STATE/timer_cmd" ]] || exit 5
rm -f "$STUB_STATE/timer_cmd"
fi
+11
View File
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Fake systemd-run: records the timer command without running it.
set -euo pipefail
printf 'systemd-run %s\n' "$*" >>"$STUB_LOG"
[[ "${STUB_SYSTEMD_RUN_RC:-0}" == 0 ]] || exit "$STUB_SYSTEMD_RUN_RC"
# systemd-run options come before the scheduled command.
while (( $# > 0 )) && [[ "$1" == --* ]]; do
[[ "$1" == --setenv=* ]] && printf '%s\n' "${1#--setenv=}" >"$STUB_STATE/timer_env"
shift
done
printf '%s\n' "$@" >"$STUB_STATE/timer_cmd"
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Fake ufw: simulates loading after.rules with iptables-restore
# --noflush (only declared chains are flushed, then filled).
set -euo pipefail
printf 'ufw %s\n' "$*" >>"$STUB_LOG"
kernel="$STUB_STATE/kernel"
touch "$kernel"
case "${1:-}" in
status)
# Like the real one (Python gettext), LANGUAGE wins over LC_ALL.
if [[ "${LANGUAGE:-}" == fr* ]]; then
echo "État : actif"
exit 0
fi
if [[ "${STUB_UFW_INACTIVE:-0}" == 1 ]]; then
echo "Status: inactive"
elif [[ "${2:-}" == verbose && -n "${STUB_UFW_STATUS:-}" ]]; then
cat "$STUB_UFW_STATUS"
else
echo "Status: active"
fi
;;
version) echo "ufw 0.36.2" ;;
reload)
[[ "${STUB_UFW_RELOAD_RC:-0}" == 0 ]] || exit "$STUB_UFW_RELOAD_RC"
[[ "${STUB_UFW_RELOAD_NOOP:-0}" == 1 ]] && exit 0
rules="$FENCE_ROOT/etc/ufw/after.rules"
declared="$(grep -E '^:[^ ]+ ' "$rules" | sed 's/^:\([^ ]*\) .*/\1/' | sort -u || true)"
tmp="$(mktemp)"
while IFS= read -r line; do
chain="$(printf '%s' "$line" | awk '{print $2}')"
if ! grep -qxF -- "$chain" <<<"$declared"; then
printf '%s\n' "$line"
fi
done <"$kernel" >"$tmp"
grep -E '^-A ' "$rules" >>"$tmp" || true
if [[ "${STUB_UFW_DROP_C2H:-0}" == 1 ]]; then
grep -v -- '-j ACCEPT$' "$tmp" >"$tmp.2" || true
mv "$tmp.2" "$tmp"
fi
mv "$tmp" "$kernel"
;;
show)
[[ -n "${STUB_UFW_ADDED:-}" ]] && cat "$STUB_UFW_ADDED"
;;
*) exit 1 ;;
esac