Files
docker-fence/lib/report.sh
T
2026-12-05 15:32:27 +00:00

177 lines
7.1 KiB
Bash

# shellcheck shell=bash
# Observation report (report) and host rules plan (ufw-plan).
# Neither changes anything.
# Container table: IP -> name, and (IP, internal port, proto) -> ports
# published on the host (IPv4). Only the name, addresses and ports are
# requested from docker inspect: never the environment nor the arguments.
fence_container_table() {
declare -gA NAME_BY_IP=() PUB_BY_TARGET=()
fence_have bin_docker || return 1
local -a ids
mapfile -t ids < <("${CFG[bin_docker]}" ps -q 2>/dev/null)
(( ${#ids[@]} > 0 )) || return 0
# shellcheck disable=SC2016 # Go template, not a shell expansion
local fmt='{{.Name}}|{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}|{{range $p, $c := .NetworkSettings.Ports}}{{if $c}}{{$p}}={{range $c}}{{.HostIp}}:{{.HostPort}},{{end}} {{end}}{{end}}'
local line name ips ports ip tok key b hip hport
while IFS= read -r line; do
IFS='|' read -r name ips ports <<<"$line"
name="${name#/}"
for ip in $ips; do
[[ "$ip" =~ ^[0-9.]+$ ]] || continue
NAME_BY_IP[$ip]="$name"
for tok in $ports; do
key="${tok%%=*}" # 80/tcp
[[ "$key" =~ ^([0-9]+)/(tcp|udp)$ ]] || continue
local cport="${BASH_REMATCH[1]}" cproto="${BASH_REMATCH[2]}"
local -a binds
IFS=',' read -r -a binds <<<"${tok#*=}"
for b in "${binds[@]}"; do
[[ -n "$b" ]] || continue
hport="${b##*:}"
hip="${b%:*}"
[[ "$hip" == *:* ]] && continue # IPv6, out of scope
local t="$ip|$cport|$cproto"
if [[ " ${PUB_BY_TARGET[$t]:-} " != *" $hport "* ]]; then
PUB_BY_TARGET[$t]="${PUB_BY_TARGET[$t]:+${PUB_BY_TARGET[$t]} }$hport"
fi
done
done
done
done < <("${CFG[bin_docker]}" inspect --format "$fmt" "${ids[@]}" 2>/dev/null)
}
fence_cmd_report() {
local since="${OPT_SINCE:-${CFG[report_since]}}"
if ! fence_v_since "$since"; then
fence_error e_option "--since $since"
return "$FENCE_EXIT_CONFIG"
fi
fence_env_require_bins bin_journalctl || return "$FENCE_EXIT_ENV"
if ! fence_container_table; then
fence_warn w_no_docker
fi
declare -A count=()
local line src dst proto dpt total=0
local re_src='[[:space:]]SRC=([0-9.]+)' re_dst='[[:space:]]DST=([0-9.]+)'
local re_proto='[[:space:]]PROTO=([A-Za-z]+)' re_dpt='[[:space:]]DPT=([0-9]+)'
while IFS= read -r line; do
[[ "$line" =~ $re_src ]] || continue
src="${BASH_REMATCH[1]}"
[[ "$line" =~ $re_dst ]] || continue
dst="${BASH_REMATCH[1]}"
[[ "$line" =~ $re_proto ]] || continue
proto="${BASH_REMATCH[1],,}"
[[ "$line" =~ $re_dpt ]] || continue
dpt="${BASH_REMATCH[1]}"
count["$src|$dst|$dpt|$proto"]=$(( ${count["$src|$dst|$dpt|$proto"]:-0} + 1 ))
total=$((total + 1))
done < <("${CFG[bin_journalctl]}" -k --no-pager -o short-iso --since "$since" 2>/dev/null \
| grep -F -e "${CFG[log_prefix_observe]} " -e "${CFG[log_prefix_drop]} " || true)
fence_info i_report_head "$since" "$total"
if (( total == 0 )); then
return 0
fi
local k n name pubs p
local -a sorted suggestions=()
declare -A suggested=()
mapfile -t sorted < <(for k in "${!count[@]}"; do printf '%s|%s\n' "${count[$k]}" "$k"; done \
| sort -t '|' -k1,1nr -k2,2 -k3,3 -k4,4n)
printf '%8s %-18s %-32s %s\n' "$(fence_fmt r_count)" "$(fence_fmt r_source)" \
"$(fence_fmt r_target)" "$(fence_fmt r_published)"
for line in "${sorted[@]}"; do
IFS='|' read -r n src dst dpt proto <<<"$line"
name="${NAME_BY_IP[$dst]:-$dst}"
pubs="${PUB_BY_TARGET[$dst|$dpt|$proto]:-}"
printf '%8s %-18s %-32s %s\n' "$n" "$src" "$name:$dpt/$proto" "${pubs:-?}"
if [[ -z "$pubs" ]]; then
suggestions+=("# $(fence_fmt r_no_published "$src" "$name:$dpt/$proto")")
continue
fi
for p in $pubs; do
k="$proto|$p|$src"
[[ -n "${suggested[$k]+x}" ]] && continue
suggested[$k]=1
suggestions+=("$(printf 'allow %s %s from %s' "$proto" "$p" "$src") # $name")
done
done
printf '\n%s\n' "$(fence_fmt r_suggestions)"
printf '%s\n' "${suggestions[@]}"
}
# ----------------------------------------------------------------- ufw-plan
# Address without /32, for readable ufw commands.
fence_short_ip() {
printf '%s' "${1%/32}"
}
fence_cmd_ufw_plan() {
local i port
printf '# %s\n' "$(fence_fmt p_intro)"
printf '# %s\n\n' "$(fence_fmt p_never_run)"
printf '# 1. %s\n' "$(fence_fmt p_admin)"
for i in "${!ADMIN_SRC[@]}"; do
if [[ -n "${ADMIN_IFACE[i]}" ]]; then
printf 'ufw allow in on %s from %s\n' "${ADMIN_IFACE[i]}" "$(fence_short_ip "${ADMIN_SRC[i]}")"
else
printf 'ufw allow from %s\n' "$(fence_short_ip "${ADMIN_SRC[i]}")"
fi
done
printf '\n# 2. %s\n' "$(fence_fmt p_allow)"
for i in "${!ALLOW_PROTO[@]}"; do
local from=any in=""
[[ "${ALLOW_SRC[i]}" != any ]] && from="$(fence_short_ip "${ALLOW_SRC[i]}")"
[[ -n "${ALLOW_IFACE[i]}" ]] && in="in on ${ALLOW_IFACE[i]} "
printf '# ufw allow %sproto %s from %s to any port %s\n' \
"$in" "${ALLOW_PROTO[i]}" "$from" "${ALLOW_PORT[i]}"
done
(( ${#ALLOW_PROTO[@]} > 0 )) || printf '# -\n'
printf '\n# 3. %s\n' "$(fence_fmt p_c2h)"
if (( ${#C2H_PROTO[@]} == 0 )); then
printf '# -\n'
else
local -a subnets=()
if fence_have bin_docker; then
mapfile -t subnets < <("${CFG[bin_docker]}" network ls --filter driver=bridge -q 2>/dev/null \
| while IFS= read -r id; do
"${CFG[bin_docker]}" network inspect --format '{{range .IPAM.Config}}{{.Subnet}} {{end}}' "$id" 2>/dev/null
done | tr ' ' '\n' | grep -E '^[0-9.]+/[0-9]+$' | sort -u)
fi
if (( ${#subnets[@]} == 0 )); then
printf '# %s\n' "$(fence_fmt p_no_subnets)"
fi
local s
for i in "${!C2H_PROTO[@]}"; do
port="${C2H_PORT[i]}"
for s in "${subnets[@]}"; do
printf '# ufw allow proto %s from %s to any port %s\n' "${C2H_PROTO[i]}" "$s" "$port"
done
done
fi
printf '\n# 4. %s\n' "$(fence_fmt p_delete)"
printf '# %s\n' "$(fence_fmt p_ssh_warning)"
local -a added=()
if fence_have bin_ufw; then
mapfile -t added < <("${CFG[bin_ufw]}" show added 2>/dev/null | grep -E '^ufw ' || true)
fi
if (( ${#added[@]} == 0 )); then
printf '# %s\n' "$(fence_fmt p_no_added)"
fi
local r
for r in "${added[@]}"; do
if [[ "$r" =~ ^ufw\ route\ || ( "$r" =~ ^ufw\ (allow|limit)\ && "$r" != *" from "* ) ]]; then
printf '# ufw --force delete %s\n' "${r#ufw }"
fi
done
printf '\n# %s\n' "$(fence_fmt p_host_network)"
}