Add docker-fence v1 with tests and docs
This commit is contained in:
commit
305cb562f8
46 files changed
+5473
No files matched your search
+423
@@ -0,0 +1,423 @@
|
||||
# shellcheck shell=bash
|
||||
# Commands that read or change after.rules and the firewall.
|
||||
|
||||
# ------------------------------------------------------------------ check
|
||||
|
||||
fence_cmd_check() {
|
||||
fence_env_check || return "$FENCE_EXIT_ENV"
|
||||
fence_info i_config_ok "$FENCE_CONFIG_FILE" "${CFG[mode]}" \
|
||||
"${#ADMIN_SRC[@]}" "${#ALLOW_PROTO[@]}" "${#C2H_PROTO[@]}"
|
||||
fence_generate_block
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------ install
|
||||
|
||||
# Prepares the new content of after.rules in file $1.
|
||||
# Reads: OPT_REPLACE_LEGACY, OPT_FORCE.
|
||||
# Returns 0, or an exit code.
|
||||
fence_prepare_rules() {
|
||||
local out="$1" rules rc block
|
||||
rules="$(fence_rules_path)"
|
||||
if [[ ! -f "$rules" || ! -r "$rules" ]]; then
|
||||
fence_error e_no_rules "$rules"
|
||||
return "$FENCE_EXIT_ENV"
|
||||
fi
|
||||
fence_read_lines "$rules"
|
||||
|
||||
rc=0
|
||||
fence_find_legacy || rc=$?
|
||||
if (( rc == 2 )); then
|
||||
fence_error "$FENCE_BLOCK_ERR" "$rules" "$FENCE_LEGACY_BEGIN"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
if (( rc == 0 )); then
|
||||
local known=1
|
||||
fence_legacy_is_known || known=0
|
||||
if (( ! OPT_REPLACE_LEGACY )); then
|
||||
fence_error e_legacy_present "$rules"
|
||||
fence_print_block >&2
|
||||
return "$FENCE_EXIT_LEGACY"
|
||||
fi
|
||||
if (( ! known )); then
|
||||
fence_warn w_legacy_unknown
|
||||
fence_legacy_diff >&2
|
||||
if (( ! OPT_FORCE )); then
|
||||
fence_error e_legacy_unknown
|
||||
return "$FENCE_EXIT_LEGACY"
|
||||
fi
|
||||
fi
|
||||
FENCE_LEGACY_CUT="$(fence_print_block)"
|
||||
fence_cut_block
|
||||
fi
|
||||
|
||||
rc=0
|
||||
fence_find_managed || rc=$?
|
||||
if (( rc == 2 )); then
|
||||
fence_error "$FENCE_BLOCK_ERR" "$rules" "${CFG[block_marker]}"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
(( rc == 0 )) && fence_cut_block
|
||||
fence_trim_trailing_blank
|
||||
|
||||
block="$(mktemp)"
|
||||
fence_generate_block >"$block"
|
||||
fence_compose "$block" >"$out"
|
||||
rm -f -- "$block"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Writes after.rules from the prepared file; backs up first.
|
||||
# fence_write_rules PREPARED_FILE; sets FENCE_BACKUP.
|
||||
fence_write_rules() {
|
||||
local new="$1" rules
|
||||
rules="$(fence_rules_path)"
|
||||
FENCE_BACKUP="$(fence_backup "$(fence_rules_basename)")" || {
|
||||
fence_error e_backup "$(fence_backup_dir)"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
}
|
||||
fence_info i_backup "$FENCE_BACKUP"
|
||||
if [[ -n "${FENCE_LEGACY_CUT:-}" ]]; then
|
||||
(umask 077 && printf '%s\n' "$FENCE_LEGACY_CUT" >"$(fence_backup_dir)/legacy-block.rules")
|
||||
fence_info i_legacy_saved "$(fence_backup_dir)/legacy-block.rules"
|
||||
fi
|
||||
if ! fence_atomic_write "$rules" <"$new"; then
|
||||
fence_error e_write "$rules"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
fence_info i_written "$rules"
|
||||
fence_log "write $rules backup=$FENCE_BACKUP mode=${CFG[mode]}"
|
||||
}
|
||||
|
||||
fence_cmd_install() {
|
||||
local new rc=0
|
||||
fence_env_check || return "$FENCE_EXIT_ENV"
|
||||
fence_refuse_if_pending || return
|
||||
new="$(mktemp)"
|
||||
fence_prepare_rules "$new" || rc=$?
|
||||
if (( rc != 0 )); then
|
||||
rm -f -- "$new"
|
||||
return "$rc"
|
||||
fi
|
||||
if cmp -s -- "$new" "$(fence_rules_path)"; then
|
||||
rm -f -- "$new"
|
||||
fence_info i_unchanged "$(fence_rules_path)"
|
||||
return 0
|
||||
fi
|
||||
fence_write_rules "$new" || rc=$?
|
||||
rm -f -- "$new"
|
||||
(( rc == 0 )) && fence_info i_install_done
|
||||
return "$rc"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------ kernel
|
||||
|
||||
fence_ufw_active() {
|
||||
"${CFG[bin_ufw]}" status 2>/dev/null | head -n 1 | grep -q '^Status: active'
|
||||
}
|
||||
|
||||
# Is the DOCKER-USER -> chain jump loaded?
|
||||
fence_kernel_jump_present() {
|
||||
"${CFG[bin_iptables]}" -C DOCKER-USER -j "${CFG[chain]}" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Checks that the block is loaded after ufw reload.
|
||||
fence_kernel_verify() {
|
||||
local rules
|
||||
if ! fence_kernel_jump_present; then
|
||||
fence_error e_verify_jump "${CFG[chain]}"
|
||||
return 1
|
||||
fi
|
||||
rules="$("${CFG[bin_iptables]}" -S "${CFG[chain]}" 2>/dev/null | grep -c '^-A ')" || true
|
||||
if (( ${rules:-0} == 0 )); then
|
||||
fence_error e_verify_empty "${CFG[chain]}"
|
||||
return 1
|
||||
fi
|
||||
local i
|
||||
for i in "${!C2H_PROTO[@]}"; do
|
||||
if ! "${CFG[bin_iptables]}" -C ufw-after-input -i "${C2H_IFACE[i]}" -p "${C2H_PROTO[i]}" \
|
||||
--dport "${C2H_PORT[i]}" -j ACCEPT >/dev/null 2>&1; then
|
||||
fence_error e_verify_c2h "${C2H_PROTO[i]}" "${C2H_PORT[i]}"
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
# Removes what is left of the block in the kernel when the file has none:
|
||||
# ufw reload (--noflush) only flushes the chains declared in after.rules.
|
||||
fence_kernel_cleanup() {
|
||||
local c="${CFG[chain]}" d ipt="${CFG[bin_iptables]}" n=0
|
||||
d="$(fence_deny_chain)"
|
||||
while "$ipt" -C DOCKER-USER -j "$c" >/dev/null 2>&1 && (( n < 50 )); do
|
||||
"$ipt" -D DOCKER-USER -j "$c" || break
|
||||
n=$((n + 1))
|
||||
done
|
||||
"$ipt" -F "$c" >/dev/null 2>&1 || true
|
||||
"$ipt" -F "$d" >/dev/null 2>&1 || true
|
||||
"$ipt" -X "$c" >/dev/null 2>&1 || true
|
||||
"$ipt" -X "$d" >/dev/null 2>&1 || true
|
||||
fence_detail i_kernel_cleaned "$c"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------- pending rollback
|
||||
|
||||
fence_pending_get() {
|
||||
local f
|
||||
f="$(fence_pending_file)"
|
||||
[[ -f "$f" ]] || return 1
|
||||
FENCE_PENDING="$(<"$f")"
|
||||
[[ -n "$FENCE_PENDING" ]]
|
||||
}
|
||||
|
||||
fence_refuse_if_pending() {
|
||||
if fence_pending_get; then
|
||||
fence_error e_pending "${CFG[unit_name]}"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
}
|
||||
|
||||
fence_stop_timer() {
|
||||
"${CFG[bin_systemctl]}" stop "${CFG[unit_name]}.timer" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
# Restores a backup, reloads UFW and cleans the kernel.
|
||||
# fence_restore BACKUP
|
||||
fence_restore() {
|
||||
local backup="$1" rules pre
|
||||
rules="$(fence_rules_path)"
|
||||
if [[ ! -f "$backup" || ! -r "$backup" ]]; then
|
||||
fence_error e_no_backup "$backup"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
fence_stop_timer || true
|
||||
pre="$(fence_backup "$(fence_rules_basename).pre-rollback")" || true
|
||||
[[ -n "$pre" ]] && fence_detail i_backup "$pre"
|
||||
if ! fence_atomic_write "$rules" <"$backup"; then
|
||||
fence_error e_write "$rules"
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
fence_info i_restored "$backup" "$rules"
|
||||
rm -f -- "$(fence_pending_file)"
|
||||
fence_log "rollback restored=$backup"
|
||||
if ! fence_run bin_ufw reload >/dev/null; then
|
||||
fence_error e_reload
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
fence_read_lines "$rules"
|
||||
local rc=0
|
||||
fence_find_managed || rc=$?
|
||||
if (( rc != 0 )); then
|
||||
fence_kernel_cleanup
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ apply
|
||||
|
||||
# --set options to pass to the automatic rollback.
|
||||
fence_override_args() {
|
||||
local k
|
||||
for k in "${FENCE_SETTINGS[@]}"; do
|
||||
if [[ "${CFG_ORIGIN[$k]}" == option ]]; then
|
||||
printf '%s\n' --set "$k=${CFG[$k]}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
fence_cmd_apply() {
|
||||
local new rc=0 rules
|
||||
rules="$(fence_rules_path)"
|
||||
fence_env_check || return "$FENCE_EXIT_ENV"
|
||||
fence_env_require_bins bin_iptables bin_ufw bin_systemd_run bin_systemctl || return "$FENCE_EXIT_ENV"
|
||||
fence_refuse_if_pending || return
|
||||
if ! fence_ufw_active; then
|
||||
fence_error e_ufw_inactive
|
||||
return "$FENCE_EXIT_ENV"
|
||||
fi
|
||||
new="$(mktemp)"
|
||||
fence_prepare_rules "$new" || rc=$?
|
||||
if (( rc != 0 )); then
|
||||
rm -f -- "$new"
|
||||
return "$rc"
|
||||
fi
|
||||
fence_write_rules "$new" || rc=$?
|
||||
rm -f -- "$new"
|
||||
(( rc == 0 )) || return "$rc"
|
||||
printf '%s\n' "$FENCE_BACKUP" >"$(fence_pending_file)"
|
||||
|
||||
# Rollback scheduled before the reload.
|
||||
local -a run_args=(
|
||||
"--unit=${CFG[unit_name]}"
|
||||
"--on-active=${CFG[rollback_seconds]}s"
|
||||
"--timer-property=AccuracySec=1s"
|
||||
"--description=docker-fence rollback"
|
||||
)
|
||||
[[ -n "${FENCE_ROOT:-}" ]] && run_args+=("--setenv=FENCE_ROOT=$FENCE_ROOT")
|
||||
local -a ov
|
||||
mapfile -t ov < <(fence_override_args)
|
||||
if ! fence_run bin_systemd_run "${run_args[@]}" "$FENCE_SELF" rollback --auto \
|
||||
--config "$FENCE_CONFIG_FILE" "${ov[@]}" >/dev/null 2>&1; then
|
||||
fence_error e_timer "${CFG[unit_name]}"
|
||||
fence_atomic_write "$rules" <"$FENCE_BACKUP" || fence_error e_write "$rules"
|
||||
rm -f -- "$(fence_pending_file)"
|
||||
fence_log "apply aborted: timer"
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
fence_info i_timer "${CFG[rollback_seconds]}" "${CFG[unit_name]}"
|
||||
|
||||
if ! fence_run bin_ufw reload >/dev/null; then
|
||||
fence_error e_reload
|
||||
fence_restore "$FENCE_BACKUP" || true
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
if ! fence_kernel_verify; then
|
||||
fence_restore "$FENCE_BACKUP" || true
|
||||
fence_error e_apply_failed
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
fence_log "apply ok mode=${CFG[mode]} pending=${CFG[unit_name]}"
|
||||
fence_info i_apply_done "${CFG[mode]}"
|
||||
fence_info i_confirm "${CFG[rollback_seconds]}"
|
||||
return 0
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- confirm
|
||||
|
||||
fence_cmd_confirm() {
|
||||
if ! fence_pending_get; then
|
||||
fence_info i_nothing_pending
|
||||
return 0
|
||||
fi
|
||||
if ! fence_stop_timer; then
|
||||
fence_warn w_timer_stop "${CFG[unit_name]}.timer"
|
||||
fi
|
||||
rm -f -- "$(fence_pending_file)"
|
||||
fence_log "confirm"
|
||||
fence_info i_confirmed
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------- rollback
|
||||
|
||||
fence_cmd_rollback() {
|
||||
local backup
|
||||
if (( OPT_AUTO )); then
|
||||
if ! fence_pending_get; then
|
||||
fence_info i_nothing_pending
|
||||
return 0
|
||||
fi
|
||||
backup="$FENCE_PENDING"
|
||||
fence_log "rollback --auto"
|
||||
elif [[ -n "${OPT_FILE:-}" ]]; then
|
||||
backup="$OPT_FILE"
|
||||
else
|
||||
backup="$(fence_latest_backup)"
|
||||
if [[ -z "$backup" ]]; then
|
||||
fence_error e_no_backups "$(fence_backup_dir)"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
fi
|
||||
fence_restore "$backup"
|
||||
}
|
||||
|
||||
# --------------------------------------------------------------- uninstall
|
||||
|
||||
fence_cmd_uninstall() {
|
||||
local rules rc=0 legacy new
|
||||
rules="$(fence_rules_path)"
|
||||
fence_refuse_if_pending || return
|
||||
if [[ ! -f "$rules" ]]; then
|
||||
fence_error e_no_rules "$rules"
|
||||
return "$FENCE_EXIT_ENV"
|
||||
fi
|
||||
fence_read_lines "$rules"
|
||||
fence_find_managed || rc=$?
|
||||
if (( rc == 2 )); then
|
||||
fence_error "$FENCE_BLOCK_ERR" "$rules" "${CFG[block_marker]}"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
if (( rc == 1 && ! OPT_RESTORE_LEGACY )); then
|
||||
fence_info i_not_installed "$rules"
|
||||
return 0
|
||||
fi
|
||||
(( rc == 0 )) && fence_cut_block
|
||||
fence_trim_trailing_blank
|
||||
|
||||
new="$(mktemp)"
|
||||
if (( OPT_RESTORE_LEGACY )); then
|
||||
legacy="$(fence_backup_dir)/legacy-block.rules"
|
||||
rc=0
|
||||
fence_find_legacy || rc=$?
|
||||
if (( rc != 1 )); then
|
||||
rm -f -- "$new"
|
||||
fence_error e_legacy_already "$rules"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
if [[ ! -r "$legacy" ]]; then
|
||||
rm -f -- "$new"
|
||||
fence_error e_no_legacy_saved "$legacy"
|
||||
return "$FENCE_EXIT_CONFIG"
|
||||
fi
|
||||
fence_compose "$legacy" >"$new"
|
||||
else
|
||||
fence_compose >"$new"
|
||||
fi
|
||||
rc=0
|
||||
fence_write_rules "$new" || rc=$?
|
||||
rm -f -- "$new"
|
||||
(( rc == 0 )) || return "$rc"
|
||||
fence_log "uninstall restore_legacy=$OPT_RESTORE_LEGACY"
|
||||
|
||||
if fence_have bin_ufw && fence_ufw_active; then
|
||||
if ! fence_run bin_ufw reload >/dev/null; then
|
||||
fence_error e_reload
|
||||
return "$FENCE_EXIT_APPLY"
|
||||
fi
|
||||
fence_kernel_cleanup
|
||||
fence_info i_uninstalled_reloaded
|
||||
else
|
||||
fence_info i_uninstalled
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ status
|
||||
|
||||
fence_cmd_status() {
|
||||
local rules mode="-" rc=0
|
||||
rules="$(fence_rules_path)"
|
||||
if [[ -r "$rules" ]]; then
|
||||
fence_read_lines "$rules"
|
||||
fence_find_managed || rc=$?
|
||||
if (( rc == 0 )); then
|
||||
if fence_print_block | grep -qF -- "--ctstate DNAT -j $(fence_deny_chain)"; then
|
||||
mode=enforce
|
||||
else
|
||||
mode=observe
|
||||
fi
|
||||
fence_info i_status_block "$rules" "$mode"
|
||||
else
|
||||
fence_info i_status_noblock "$rules"
|
||||
fi
|
||||
else
|
||||
fence_warn w_rules_unreadable "$rules"
|
||||
fi
|
||||
if fence_pending_get; then
|
||||
fence_info i_status_pending "${CFG[unit_name]}" "$FENCE_PENDING"
|
||||
fi
|
||||
if fence_have bin_iptables; then
|
||||
if fence_kernel_jump_present; then
|
||||
fence_info i_status_jump_yes "${CFG[chain]}"
|
||||
else
|
||||
fence_info i_status_jump_no "${CFG[chain]}"
|
||||
fi
|
||||
fence_info i_status_counters
|
||||
"${CFG[bin_iptables]}" -L "${CFG[chain]}" -n -v -x 2>&1 || true
|
||||
"${CFG[bin_iptables]}" -L "$(fence_deny_chain)" -n -v -x 2>&1 || true
|
||||
fi
|
||||
if fence_have bin_journalctl; then
|
||||
fence_info i_status_log
|
||||
"${CFG[bin_journalctl]}" -k --no-pager -o short-iso --since "${CFG[report_since]}" 2>/dev/null \
|
||||
| grep -F -e "${CFG[log_prefix_drop]} " -e "${CFG[log_prefix_observe]} " \
|
||||
| tail -n 20 || true
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user