fix(ssl): recover interrupted IP certificate migrations

Register recovery before retiring legacy renewal state and detach completed journals before deleting backups.
This commit is contained in:
Hamed Sanaei
2026-12-03 21:32:11 +03:30
parent 7af3fed69b
commit 5fe46c2e55
4 changed files with 162 additions and 51 deletions
+33 -16
View File
@@ -308,6 +308,8 @@ ip_ssl_retire_legacy() {
fi
done
((${#retire[@]})) || return 0
# A scheduled runner must be able to restore retired entries after SIGKILL.
ip_ssl_schedule "$state" "${acme%/*}" || return 1
if ip_ssl_legacy_busy "$acme"; then
printf 'Wait for the existing acme.sh command to finish, then retry IP certificate setup.\n' >&2
return 1
@@ -326,13 +328,17 @@ ip_ssl_retire_legacy() {
ip_ssl_recover() {
local dir="$1" state="$1/.xui-ip-ssl" pending="$1/.xui-ip-ssl/pending"
local gen='' home='' current='' entry held file name failed=0 previous=''
[[ -d "$pending" ]] || return 0
if [[ -f "$pending/generation" ]]; then
if ! IFS= read -r gen < "$pending/generation" || [[ ! "$gen" =~ ^gen\.[a-zA-Z0-9]+$ ]]; then
printf 'Invalid IP certificate generation journal. Keep recovery files at %s.\n' "$pending" >&2
return 1
fi
local gen home='' current='' entry held file name failed=0 previous=''
# These directories are unpublished work or journals already detached from recovery.
if ! rm -rf -- "$state/pending-init" "$state/pending-cleanup"; then
printf 'Could not remove disposable IP certificate files at %s.\n' "$state" >&2
return 1
fi
[[ -e "$pending" || -L "$pending" ]] || return 0
if [[ ! -d "$pending" || -L "$pending" || ! -f "$pending/generation" ]] ||
! IFS= read -r gen < "$pending/generation" || [[ ! "$gen" =~ ^gen\.[a-zA-Z0-9]+$ ]]; then
printf 'Invalid IP certificate generation journal. Keep recovery files at %s.\n' "$pending" >&2
return 1
fi
current=$(readlink -- "$state/current") || current=''
if [[ -f "$pending/home" ]] && ! IFS= read -r home < "$pending/home"; then
@@ -340,7 +346,7 @@ ip_ssl_recover() {
return 1
fi
if [[ -n "$gen" && "$current" == "$gen" ]]; then
if [[ "$current" == "$gen" ]]; then
# Commit is the pointer swap. Never roll back a committed generation.
if [[ -f "$pending/legacy" ]]; then
while IFS= read -r entry; do
@@ -370,7 +376,7 @@ ip_ssl_recover() {
# types are restored. A failed restore may still leave a live alias.
if ((!failed)) && [[ -f "$pending/legacy" ]]; then
while IFS= read -r entry; do
if ! ip_ssl_ipv4 "${entry%_ecc}" || [[ -z "$home" || -z "$gen" ]]; then failed=1; continue; fi
if ! ip_ssl_ipv4 "${entry%_ecc}" || [[ -z "$home" ]]; then failed=1; continue; fi
held="$home/.xui-retired-$gen-$entry"
if [[ -e "$held" ]]; then
if [[ -e "$home/$entry" || -L "$home/$entry" ]] ||
@@ -383,14 +389,20 @@ ip_ssl_recover() {
fi
done < "$pending/legacy"
fi
if ((!failed)) && [[ -n "$gen" ]]; then rm -rf -- "${state:?}/${gen:?}" || failed=1; fi
if ((!failed)); then rm -rf -- "${state:?}/${gen:?}" || failed=1; fi
fi
if ((failed)); then
printf 'IP certificate recovery could not complete. Keep recovery files at %s and retry after resolving the error.\n' "$pending" >&2
return 1
fi
if ! rm -f -- "$state/staging/fullchain.pem" "$state/staging/privkey.pem" || ! rm -rf -- "$pending"; then
printf 'Could not remove the IP certificate journal at %s\n' "$pending" >&2
# Remove the journal from the recovery path before deleting any of its files.
if ! rm -f -- "$state/staging/fullchain.pem" "$state/staging/privkey.pem" ||
! mv -T -- "$pending" "$state/pending-cleanup"; then
printf 'Could not detach the IP certificate journal at %s\n' "$pending" >&2
return 1
fi
if ! rm -rf -- "$state/pending-cleanup"; then
printf 'Could not remove the disposable IP certificate journal at %s\n' "$state/pending-cleanup" >&2
return 1
fi
}
@@ -487,18 +499,23 @@ ip_ssl_legacy_home() (
ip_ssl_transaction() (
local action="$1" acme="$2" ipv4="$3" ipv6="$4" port="$5" dir="$6" reload="$7"
local state="$dir/.xui-ip-ssl" pending="$dir/.xui-ip-ssl/pending" home="${acme%/*}"
local work gen file previous rc legacy_home stage="$dir/.xui-ip-ssl/staging"
local work gen file previous rc legacy_home stage="$state/staging" pending_init="$state/pending-init"
local -a scope domains=(-d "$ipv4")
mkdir -- "$pending" || return 1
mkdir -- "$pending_init" || return 1
# Recovery runs under the caller's flock, including on normal exit.
trap 'rc=$?; ip_ssl_recover "$dir" || rc=1; exit "$rc"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
trap 'exit 129' HUP
work=$(mktemp -d "$state/gen.XXXXXX") || return 1
# Publish only a journal with a complete generation record.
work=$(mktemp -d "$pending_init/gen.XXXXXX") || return 1
gen="${work##*/}"
printf '%s\n' "$gen" > "$pending/generation" && mkdir -- "$work/acme" "$pending/live" || return 1
printf '%s\n' "$gen" > "$pending_init/generation" &&
mkdir -- "$work/acme" "$pending_init/live" &&
mv -T -- "$pending_init" "$pending" &&
mv -T -- "$pending/$gen" "$state/$gen" || return 1
work="$state/$gen"
if [[ "$action" == issue ]]; then
legacy_home=$(ip_ssl_legacy_home "$home") || return 1
[[ "$legacy_home" == /* && "$legacy_home" != *[$'\n\r']* ]] || return 1
+63 -3
View File
@@ -63,6 +63,8 @@ printf 'unrelated certificate\n' > "$legacy/example.net_ecc/marker"
for suffix in '' _ecc; do
printf "Le_Alt='no'\nLe_NextRenewTime='1'\nLe_RealKeyPath='%s/privkey.pem'\nLe_RealFullChainPath='%s/fullchain.pem'\n" \
"$cert_dir" "$cert_dir" > "$legacy/$ipv4$suffix/$ipv4.conf"
cp "$TEMP/old.pem" "$legacy/$ipv4$suffix/fullchain.cer"
cp "$TEMP/old.key" "$legacy/$ipv4$suffix/$ipv4.key"
done
cp "$legacy/${ipv4}_ecc/$ipv4.conf" "$TEMP/legacy.conf"
cp "${acme%/*}/account.conf" "$TEMP/account.conf"
@@ -73,7 +75,7 @@ cat > "$TEMP/mockbin/crontab" <<'MOCK'
#!/usr/bin/env bash
case "$1" in
-l) cat "$CRON_FILE" ;;
-) cat > "$CRON_FILE" ;;
-) [[ "${CRON_FAIL:-0}" == 0 ]] && cat > "$CRON_FILE" ;;
*) exit 1 ;;
esac
MOCK
@@ -112,7 +114,10 @@ case "$action" in
mkdir -p "$entry"
if [[ "$action" == --issue ]]; then
printf "Le_Alt='%s'\n" "${san:-no}" > "$conf"
if [[ "$ISSUE_MODE" == fail ]]; then exit 1; fi
case "$ISSUE_MODE" in
fail) exit 1 ;;
interrupt) kill -KILL "$PPID"; exit 1 ;;
esac
else
if [[ "$RENEW_MODE" == skip ]]; then exit 2; fi
if [[ -n "${RENEW_BARRIER:-}" ]]; then
@@ -169,6 +174,24 @@ reject() {
}
fixture new
: > "$ACME_LOG"
CRON_FAIL=1 reject issue
if grep -Fxq 'arg=--issue' "$ACME_LOG"; then fail 'cron registration failure reached ACME issuance'; fi
if ! live_is old || ! legacy_is_intact; then fail 'cron registration failure changed legacy state'; fi
ISSUE_MODE=interrupt reject issue
if [[ ! -d "$state/pending" || -e "$legacy/$ipv4" || -e "$legacy/${ipv4}_ecc" ]]; then
fail 'issuance interruption did not leave retired legacy entries in the journal'
fi
cron_command=$(sed -n 's/^19 \* \* \* \* //p' "$CRON_FILE")
[[ -n "$cron_command" ]] || fail 'no recovery job was registered before retirement'
before=$(wc -l < "$ACME_LOG")
bash -c "$cron_command" || fail 'first scheduled recovery could not restore the legacy entries'
if ! live_is old || ! legacy_is_intact || [[ -e "$state/pending" || "$(wc -l < "$ACME_LOG")" != "$before" ]]; then
fail 'scheduled recovery changed the old pair or did not restore renewal metadata'
fi
printf 'PASS: failed cron registration preserves legacy state and scheduled recovery repairs interrupted issuance\n'
ISSUE_MODE=fail
reject issue
if ! live_is old || ! legacy_is_intact; then fail 'failed dual-stack order changed prior certificate or ACME state'; fi
@@ -250,7 +273,44 @@ printf 'PASS: restoration failures retain backups and interruptions recover on n
: > "$ACME_LOG"
RELOAD_FAIL=1
issue
# shellcheck disable=SC2317,SC2329
rm() {
local journal="${!#}" committed
case "$journal" in
"$state/pending"|"$state/pending-cleanup")
committed=$(readlink -- "$state/current") || committed=''
if [[ -f "$journal/live-changing" && -f "$journal/generation" &&
"$(cat "$journal/generation")" == "$committed" ]]; then
command rm -f -- "$journal/generation" || return 1
kill -KILL "$BASHPID"
fi
;;
esac
command rm "$@"
}
reject issue
unset -f rm
committed=$(readlink -- "$state/current")
if ! live_is new; then fail 'cleanup interruption changed the committed pair'; fi
recovery_status=0
RENEW_MODE=skip "$runner" || recovery_status=$?
if [[ "$(readlink -- "$state/current")" != "$committed" ||
"$(readlink -- "$cert_dir/fullchain.pem")" != .xui-ip-ssl/current/fullchain.pem ||
"$(readlink -- "$cert_dir/privkey.pem")" != .xui-ip-ssl/current/privkey.pem ]] || ! live_is new; then
fail 'scheduled recovery replaced the committed aliases after interrupted cleanup'
fi
((recovery_status == 0)) || fail 'scheduled recovery did not survive interrupted journal cleanup'
printf 'PASS: interrupted post-commit cleanup preserves the active aliases and scheduled renewal\n'
mkdir -p -- "$state/pending/live"
touch "$state/pending/live-changing"
cp -- "$TEMP/old.pem" "$state/pending/live/fullchain.pem"
reject "$runner"
grep -q 'Invalid IP certificate generation journal' "$TEMP/rejected" || fail 'malformed journal was not diagnosed'
if ! live_is new || [[ ! -L "$cert_dir/fullchain.pem" || ! -L "$cert_dir/privkey.pem" ]]; then
fail 'malformed journal changed the committed aliases'
fi
rm -rf -- "$state/pending"
if ! live_is new || [[ ! -f "$state/current/reload-pending" ]]; then fail 'reload failure discarded valid deployment'; fi
[[ "$(stat -Lc %a "$cert_dir/privkey.pem")" == 600 ]] || fail 'private key permissions changed'
[[ "$(stat -Lc %a "$cert_dir/fullchain.pem")" == 644 ]] || fail 'certificate permissions changed'