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
+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'