115 lines
3.8 KiB
Bash
115 lines
3.8 KiB
Bash
# shellcheck shell=bash
|
|
# Configuration template (init) and effective configuration (show-config).
|
|
|
|
# Normalized port (a:b) back to the file format (a-b).
|
|
fence_port_conf() {
|
|
printf '%s' "${1/:/-}"
|
|
}
|
|
|
|
fence_template() {
|
|
local k
|
|
printf '# %s\n' "$(fence_fmt t_title)"
|
|
printf '# %s\n' "$(fence_fmt t_format)"
|
|
printf '# %s\n\n' "$(fence_fmt t_defaults)"
|
|
|
|
printf '# %s\n' "$(fence_fmt d_mode)"
|
|
printf 'mode %s\n\n' "${FENCE_DEFAULTS[mode]}"
|
|
|
|
printf '# admin <ipv4[/mask]> [iface <name>]\n'
|
|
printf '# %s\n' "$(fence_fmt t_admin)"
|
|
printf '# %s\n' "$(fence_fmt t_admin_doc)"
|
|
printf 'admin 192.0.2.10\n'
|
|
printf '# admin 192.0.2.11 iface wg0\n\n'
|
|
|
|
printf '# allow <tcp|udp> <port|a-b> from <ipv4[/mask]|any> [iface <name>]\n'
|
|
printf '# %s\n' "$(fence_fmt t_allow)"
|
|
printf '# allow tcp 443 from any\n'
|
|
printf '# allow tcp 5432 from 198.51.100.20\n'
|
|
printf '# allow tcp 9000-9010 from 203.0.113.0/24\n\n'
|
|
|
|
printf '# container_to_host <tcp|udp> <port|a-b> [from_iface <name>]\n'
|
|
printf '# %s\n' "$(fence_fmt t_c2h)"
|
|
printf '# container_to_host tcp 8080\n\n'
|
|
|
|
printf '# trusted_iface <name>\n'
|
|
printf '# %s\n' "$(fence_fmt t_trusted)"
|
|
printf '# trusted_iface br-+\n'
|
|
printf '# trusted_iface docker0\n\n'
|
|
|
|
printf '# %s\n' "$(fence_fmt t_settings)"
|
|
for k in "${FENCE_SETTINGS[@]}"; do
|
|
[[ "$k" == mode ]] && continue
|
|
printf '\n# %s\n' "$(fence_fmt "d_$k")"
|
|
printf '# %s %s\n' "$k" "${FENCE_DEFAULTS[$k]}"
|
|
done
|
|
}
|
|
|
|
fence_cmd_init() {
|
|
local out="${OPT_OUTPUT:-$(fence_path /etc/docker-fence/fence.conf)}"
|
|
if [[ "$out" == - ]]; then
|
|
fence_template
|
|
return 0
|
|
fi
|
|
if [[ -e "$out" && ! "$OPT_FORCE" -eq 1 ]]; then
|
|
fence_error e_exists "$out"
|
|
return "$FENCE_EXIT_CONFIG"
|
|
fi
|
|
local dir
|
|
dir="$(dirname -- "$out")"
|
|
if [[ ! -d "$dir" ]]; then
|
|
(umask 022 && mkdir -p -- "$dir") || { fence_error e_write "$out"; return "$FENCE_EXIT_CONFIG"; }
|
|
fi
|
|
if ! fence_template | (umask 022 && fence_atomic_write "$out"); then
|
|
fence_error e_write "$out"
|
|
return "$FENCE_EXIT_CONFIG"
|
|
fi
|
|
chmod 0644 -- "$out"
|
|
fence_info i_init_done "$out"
|
|
}
|
|
|
|
fence_origin_text() {
|
|
local o="$1"
|
|
case "$o" in
|
|
default) fence_fmt o_default ;;
|
|
option) fence_fmt o_option ;;
|
|
env) fence_fmt o_env ;;
|
|
file:*) fence_fmt o_file "${o#file:}" ;;
|
|
esac
|
|
}
|
|
|
|
fence_rule_origin() {
|
|
if [[ "$1" == default ]]; then
|
|
fence_fmt o_default
|
|
else
|
|
fence_fmt o_file "$1"
|
|
fi
|
|
}
|
|
|
|
# Prints the effective configuration, each line followed by its origin.
|
|
# The output is itself a valid configuration file.
|
|
fence_cmd_show_config() {
|
|
local k i r
|
|
printf '# %s\n' "$(fence_fmt o_header "$FENCE_CONFIG_FILE")"
|
|
for k in "${FENCE_SETTINGS[@]}"; do
|
|
printf '%-40s # %s\n' "$k ${CFG[$k]}" "$(fence_origin_text "${CFG_ORIGIN[$k]}")"
|
|
done
|
|
printf '\n'
|
|
for i in "${!ADMIN_SRC[@]}"; do
|
|
r="admin ${ADMIN_SRC[i]}"
|
|
[[ -n "${ADMIN_IFACE[i]}" ]] && r+=" iface ${ADMIN_IFACE[i]}"
|
|
printf '%-40s # %s\n' "$r" "$(fence_rule_origin "${ADMIN_LINE[i]}")"
|
|
done
|
|
for i in "${!ALLOW_PROTO[@]}"; do
|
|
r="allow ${ALLOW_PROTO[i]} $(fence_port_conf "${ALLOW_PORT[i]}") from ${ALLOW_SRC[i]}"
|
|
[[ -n "${ALLOW_IFACE[i]}" ]] && r+=" iface ${ALLOW_IFACE[i]}"
|
|
printf '%-40s # %s\n' "$r" "$(fence_rule_origin "${ALLOW_LINE[i]}")"
|
|
done
|
|
for i in "${!C2H_PROTO[@]}"; do
|
|
r="container_to_host ${C2H_PROTO[i]} $(fence_port_conf "${C2H_PORT[i]}") from_iface ${C2H_IFACE[i]}"
|
|
printf '%-40s # %s\n' "$r" "$(fence_rule_origin "${C2H_LINE[i]}")"
|
|
done
|
|
for i in "${!TRUSTED_IFACE[@]}"; do
|
|
printf '%-40s # %s\n' "trusted_iface ${TRUSTED_IFACE[i]}" "$(fence_rule_origin "${TRUSTED_LINE[i]}")"
|
|
done
|
|
}
|