fix(ssl): preserve live certificates and renewal state across failed deployments

This commit is contained in:
Hamed Sanaei
2026-12-02 21:17:30 +03:30
parent 6387073b50
commit 85a8dd6725
9 changed files with 1709 additions and 354 deletions
+14 -6
View File
@@ -1,7 +1,7 @@
# IP certificate regression tests
Run from the repository root in Bash. The suite uses temporary files, mocks ACME
orders and service commands, and generates local self-signed certificate fixtures.
orders and service commands, and generates certificate fixtures from a local CA.
It never requests a certificate from a CA or executes the complete installer.
```bash
@@ -14,7 +14,7 @@ IP_SSL_LINUX_TEST=1 bash scripts/test/ip-ssl-listener-test.sh
bash scripts/test/ip-ssl-certificate-test.sh
```
Linux integration needs `socat`, `curl`, `openssl` (1.1.1+), and standard coreutils.
Linux integration needs `socat`, `curl`, `flock`, `openssl` (1.1.1+), and standard coreutils.
The listener tests bind unprivileged random ports on loopback. They need an IPv6
loopback interface, but no public IPv6 address, root privileges, or working
external echo service. Run the same commands on Debian/Ubuntu and AlmaLinux/RHEL;
@@ -22,9 +22,10 @@ install test dependencies in a disposable container if they are missing locally.
The tests cover real embedded installer/menu functions, discovery and consent,
unattended overrides, both socket families, actual IP SAN types and matching keys,
failed issuance/installation, rollback, permissions, and the persisted renewal
reload guard. Package installation, ACME orders, and panel/service operations are
mocked at their command boundaries.
failed issuance and installation, failed restoration, interruption, shared-path
locking, rejected automatic renewals, and the persisted renewal runner. Package
installation, ACME orders, and panel/service operations are mocked at their command
boundaries.
To check unchanged upstream acme.sh listener implementations separately:
@@ -33,14 +34,21 @@ curl -fsSLo /tmp/acme-3.1.5.sh https://raw.githubusercontent.com/acmesh-official
bash scripts/test/ip-ssl-acme-listener-test.sh /tmp/acme-3.1.5.sh 28085
curl -fsSLo /tmp/acme-3.1.6.sh https://raw.githubusercontent.com/acmesh-official/acme.sh/3.1.6/acme.sh
bash scripts/test/ip-ssl-acme-listener-test.sh /tmp/acme-3.1.6.sh 28085
bash scripts/test/ip-ssl-acme-lifecycle-test.sh /tmp/acme-3.1.5.sh /tmp/acme-3.1.6.sh
```
This invokes only the upstream listener dispatcher, sends independent loopback
The listener test invokes the upstream dispatcher, sends independent loopback
HTTP requests with `curl -4` and `curl -6`, prints the sockets with `ss`, then
stops the listeners. It does not issue ACME orders or change an installed client.
Run it in a disposable Linux environment with `iproute2`/`iproute` installed.
Test both `net.ipv6.bindv6only=0` and `1` container configurations where available.
The lifecycle test exercises stock `--installcert` and the retry path of stock
`--renew`. It reproduces the live-file overwrite on a failed upstream reload,
then checks the x-ui runner against the same behavior using isolated ACME state,
fixture certificates, and no CA requests. CI fetches the two upstream versions
by commit and runs this test alongside the lifecycle and concurrency regressions.
Loopback success does not establish public reachability. Real CA validation,
external port 80 forwarding, provider firewalls, and scheduled cron execution on
a deployed server require separate deployment testing.
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env bash
# Exercise the actual upstream install and renewal-retry commands without a CA.
set -euo pipefail
ROOT=$(cd "$(dirname "$0")/../.." && pwd)
# shellcheck disable=SC1091
source "$ROOT/scripts/lib/ip-ssl.sh"
test_acme() (
source_file="$1"
temp=$(mktemp -d)
trap 'rm -rf "$temp"' EXIT
ipv4=198.51.100.8
ipv6=2001:4860::1
export HOME="$temp/home" NO_ARI=1 SSL_CERT_FILE="$temp/ca.pem"
export CRON_FILE="$temp/crontab"
export PATH="$temp/bin:$PATH"
acme_home="$HOME/.acme.sh"
global="$temp/global-certs"
legacy="$temp/legacy-live"
managed="$temp/managed's live"
mkdir -p "$acme_home" "$global/${ipv4}_ecc" "$legacy" "$temp/bin"
cp "$source_file" "$acme_home/upstream.sh"
printf "CERT_HOME='%s'\n" "$global" > "$acme_home/account.conf"
cp "$acme_home/account.conf" "$temp/account.conf"
touch "$CRON_FILE"
cat > "$temp/bin/crontab" <<'CRON'
#!/usr/bin/env bash
case "$1" in -l) cat "$CRON_FILE";; -) cat > "$CRON_FILE";; *) exit 1;; esac
CRON
chmod +x "$temp/bin/crontab"
openssl req -new -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-days 3 -subj /CN=offline-ACME-CA -keyout "$temp/ca.key" -out "$SSL_CERT_FILE" >/dev/null 2>&1
serial=0
for name in good invalid renewed; do
san="IP:$ipv4,IP:$ipv6"
[[ "$name" != invalid ]] || san="IP:$ipv4"
printf 'subjectAltName=%s\n' "$san" > "$temp/ext"
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-subj "/CN=$name" -keyout "$temp/$name.key" -out "$temp/$name.csr" >/dev/null 2>&1
openssl x509 -req -in "$temp/$name.csr" -CA "$SSL_CERT_FILE" -CAkey "$temp/ca.key" \
-set_serial "$((++serial))" -days 2 -extfile "$temp/ext" -out "$temp/$name.cer" >/dev/null 2>&1
cat "$temp/$name.cer" "$SSL_CERT_FILE" > "$temp/$name.fullchain"
done
seed_order() {
local entry="$1" fixture="$2"
mkdir -p "$entry"
cp "$temp/$fixture.cer" "$entry/$ipv4.cer"
cp "$temp/$fixture.key" "$entry/$ipv4.key"
cp "$temp/$fixture.fullchain" "$entry/fullchain.cer"
cp "$SSL_CERT_FILE" "$entry/ca.cer"
}
version=$(sh "$acme_home/upstream.sh" --version | tail -n1)
entry="$global/${ipv4}_ecc"
seed_order "$entry" invalid
printf "Le_Domain='%s'\nLe_NextRenewTime='4000000000'\nLe_CertCreateTime='2'\nLe_InstallCertSuccessTime='1'\n" "$ipv4" > "$entry/$ipv4.conf"
cp "$temp/good.fullchain" "$legacy/fullchain.pem"
cp "$temp/good.key" "$legacy/privkey.pem"
# Old design: the guard fails after installcert has copied both files.
if sh "$acme_home/upstream.sh" --installcert --home "$acme_home" -d "$ipv4" --ecc \
--key-file "$legacy/privkey.pem" --fullchain-file "$legacy/fullchain.pem" --reloadcmd false > "$temp/legacy.log" 2>&1; then
printf 'FAIL: upstream accepted a failing reload command (%s)\n' "$version" >&2
return 1
fi
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
cmp "$temp/invalid.key" "$legacy/privkey.pem"
grep -q "^Le_NextRenewTime='4000000000'" "$entry/$ipv4.conf"
grep -q "^Le_InstallCertSuccessTime='1'" "$entry/$ipv4.conf"
grep -Fq "Le_RealKeyPath='$legacy/privkey.pem'" "$entry/$ipv4.conf"
cp "$temp/good.fullchain" "$legacy/fullchain.pem"
if sh "$acme_home/upstream.sh" --renew --home "$acme_home" -d "$ipv4" --ecc > "$temp/legacy-retry.log" 2>&1; then
printf 'FAIL: upstream accepted a failing install retry (%s)\n' "$version" >&2
return 1
fi
grep -q 'retrying install cert' "$temp/legacy-retry.log"
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
printf 'PASS: upstream %s reproduces live overwrite on failed reload, including a saved-path retry\n' "$version"
# Only the order is replaced by local fixtures. Stock installcert and
# renew execute through the actual persistent runner with the original home.
export TEST_ACME_DIR="$temp" TEST_ACME_IP="$ipv4" TEST_ACME_ORIGINAL="$acme_home/upstream.sh"
wrapper="$acme_home/acme.sh"
cat > "$wrapper" <<'WRAPPER'
#!/usr/bin/env bash
set -euo pipefail
if [[ "$1" != --issue ]]; then exec sh "$TEST_ACME_ORIGINAL" "$@"; fi
while [[ "$1" != --cert-home ]]; do shift; done
domain="$TEST_ACME_IP"
entry="$2/${domain}_ecc"
mkdir -p "$entry"
cp "$TEST_ACME_DIR/good.cer" "$entry/$domain.cer"
cp "$TEST_ACME_DIR/good.key" "$entry/$domain.key"
cp "$TEST_ACME_DIR/good.fullchain" "$entry/fullchain.cer"
cp "$SSL_CERT_FILE" "$entry/ca.cer"
printf "Le_Domain='%s'\nLe_CertCreateTime='1'\nLe_NextRenewTime='4000000000'\n" "$domain" > "$entry/$domain.conf"
WRAPPER
chmod +x "$wrapper"
ip_ssl_issue_and_install "$wrapper" "$ipv4" "$ipv6" "$((20000+RANDOM))" "$managed" true
cmp "$temp/good.fullchain" "$managed/fullchain.pem"
cmp "$temp/good.key" "$managed/privkey.pem"
cmp "$temp/account.conf" "$acme_home/account.conf"
cmp "$temp/invalid.fullchain" "$legacy/fullchain.pem"
state="$managed/.xui-ip-ssl"
entry="$state/current/acme/${ipv4}_ecc"
grep -Fxq "Le_RealKeyPath='staging/privkey.pem'" "$entry/$ipv4.conf"
seed_order "$entry" invalid
# Simulate an already-created certificate with a pending upstream install
# retry. NO_ARI and a future timestamp keep stock --renew entirely offline.
sed -i '/^Le_InstallCertSuccessTime=/d; /^Le_CertCreateTime=/d' "$entry/$ipv4.conf"
printf "Le_InstallCertSuccessTime='1'\nLe_CertCreateTime='2'\n" >> "$entry/$ipv4.conf"
cp "$entry/$ipv4.conf" "$temp/before-retry.conf"
if "$state/renew-v1" > "$temp/retry.log" 2>&1; then
printf 'FAIL: managed retry accepted candidate without IPv6 SAN (%s)\n' "$version" >&2
return 1
fi
grep -q 'retrying install cert' "$temp/retry.log"
grep -q 'Staged IP certificate failed' "$temp/retry.log"
cmp "$temp/good.fullchain" "$managed/fullchain.pem"
cmp "$temp/good.key" "$managed/privkey.pem"
cmp "$temp/before-retry.conf" "$entry/$ipv4.conf"
seed_order "$entry" renewed
"$state/renew-v1" > "$temp/valid-retry.log" 2>&1
cmp "$temp/renewed.fullchain" "$managed/fullchain.pem"
cmp "$temp/renewed.key" "$managed/privkey.pem"
cmp "$temp/account.conf" "$acme_home/account.conf"
printf 'PASS: managed upstream %s installs privately, rejects failed retry, and promotes valid retry\n' "$version"
)
if (($# == 0)); then printf 'Pass verified upstream acme.sh source paths as arguments.\n' >&2; exit 2; fi
for acme_source in "$@"; do test_acme "$acme_source"; done
+341 -87
View File
@@ -6,115 +6,369 @@ source "${IP_SSL_TEST_HELPERS:-$ROOT/scripts/lib/ip-ssl.sh}"
TEMP=$(mktemp -d)
trap 'rm -rf "$TEMP"' EXIT
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
export ACME_LOG="$TEMP/arguments" RELOAD_LOG="$TEMP/reloads"
export ISSUED_CERT="$TEMP/new.pem" ISSUED_KEY="$TEMP/new.key"
export ACME_ISSUE_FAIL=0 ACME_INSTALL_FAIL=0 ACME_RELOAD_FAIL=0
for name in old new v4 dns; do
san='IP:198.51.100.8,IP:2001:4860::1'
[[ "$name" != v4 ]] || san='IP:198.51.100.8'
[[ "$name" != dns ]] || san='DNS:198.51.100.8,DNS:2001:4860::1'
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 2 -subj /CN=ip-test -addext "subjectAltName=$san" -keyout "$TEMP/$name.key" -out "$TEMP/$name.pem" > /dev/null 2>&1
export ACME_LOG="$TEMP/arguments" RELOAD_LOG="$TEMP/reloads" CRON_FILE="$TEMP/crontab"
export ISSUE_MODE=ok RENEW_MODE=ok INSTALL_MODE=ok RELOAD_FAIL=0
export SSL_CERT_FILE="$TEMP/ca.pem"
ipv4=198.51.100.8
ipv6=2001:4860::1
port=$((20000+RANDOM))
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 3 \
-subj /CN=IP-fixture-CA -keyout "$TEMP/ca.key" -out "$SSL_CERT_FILE" >/dev/null 2>&1
serial=0
touch "$TEMP/ca-index"
printf '01\n' > "$TEMP/ca-serial"
cat > "$TEMP/ca.conf" <<EOF
[ca]
default_ca = fixture
[fixture]
database = $TEMP/ca-index
serial = $TEMP/ca-serial
new_certs_dir = $TEMP
default_md = sha256
policy = subject
[subject]
commonName = supplied
EOF
for name in old new later v4 dns expired; do
san="IP:$ipv4,IP:$ipv6"
case "$name" in
v4) san="IP:$ipv4" ;;
dns) san="DNS:$ipv4,DNS:$ipv6" ;;
esac
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes \
-subj "/CN=$name" -keyout "$TEMP/$name.key" -out "$TEMP/$name.csr" >/dev/null 2>&1
printf 'subjectAltName=%s\n' "$san" > "$TEMP/ext"
if [[ "$name" == expired ]]; then
openssl ca -config "$TEMP/ca.conf" -in "$TEMP/$name.csr" -cert "$SSL_CERT_FILE" -keyfile "$TEMP/ca.key" \
-startdate 20200101000000Z -enddate 20200102000000Z -batch -notext \
-extfile "$TEMP/ext" -out "$TEMP/$name.pem" >/dev/null 2>&1
else
openssl x509 -req -in "$TEMP/$name.csr" -CA "$SSL_CERT_FILE" -CAkey "$TEMP/ca.key" \
-set_serial "$((++serial))" -days 2 -extfile "$TEMP/ext" -out "$TEMP/$name.pem" >/dev/null 2>&1
fi
cat "$SSL_CERT_FILE" >> "$TEMP/$name.pem"
done
mkdir "$TEMP/cert's directory"
mkdir "$TEMP/mockbin" "$TEMP/acme home" "$TEMP/legacy certificates"
export PATH="$TEMP/mockbin:$PATH"
printf '13 0 * * * /unrelated-cron\n' > "$CRON_FILE"
acme="$TEMP/acme home/acme.sh"
legacy="$TEMP/legacy certificates"
cert_dir="$TEMP/cert's directory"
state="$cert_dir/.xui-ip-ssl"
runner="$state/renew-v1"
mkdir -p "$cert_dir" "$legacy/$ipv4" "$legacy/${ipv4}_ecc" "$legacy/example.net_ecc"
printf "CERT_HOME='%s'\n" "$legacy" > "${acme%/*}/account.conf"
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"
done
cp "$legacy/${ipv4}_ecc/$ipv4.conf" "$TEMP/legacy.conf"
cp "${acme%/*}/account.conf" "$TEMP/account.conf"
cp "$TEMP/old.pem" "$cert_dir/fullchain.pem"
cp "$TEMP/old.key" "$cert_dir/privkey.pem"
cat > "$TEMP/mock acme" <<'MOCK'
cat > "$TEMP/mockbin/crontab" <<'MOCK'
#!/usr/bin/env bash
case "$1" in
-l) cat "$CRON_FILE" ;;
-) cat > "$CRON_FILE" ;;
*) exit 1 ;;
esac
MOCK
cat > "$acme" <<'MOCK'
#!/usr/bin/env bash
set -euo pipefail
action=$1
printf 'CALL\n' >> "$ACME_LOG"
printf 'arg=%s\n' "$@" >> "$ACME_LOG"
case "$1" in
shift
home='' cert_home='' account='' domain='' san='' key='' chain='' reload=''
while (($#)); do
case "$1" in
--home) home=$2; shift ;;
--cert-home) cert_home=$2; shift ;;
--accountconf) account=$2; shift ;;
-d)
if [[ -z "$domain" ]]; then domain=$2; else san=$2; fi
shift ;;
--key-file) key=$2; shift ;;
--fullchain-file) chain=$2; shift ;;
--reloadcmd) reload=$2; shift ;;
esac
shift
done
# Like upstream, load the account config after applying command-line arguments.
CERT_HOME=${cert_home:-${home:-${0%/*}}}
if [[ -z "$account" ]]; then account="${home:-${0%/*}}/account.conf"; fi
# shellcheck disable=SC1090
if [[ -f "$account" ]]; then source "$account"; fi
entry="$CERT_HOME/${domain}_ecc"
conf="$entry/$domain.conf"
case "$action" in
--version) printf 'v3.1.5\n' ;;
--issue)
# Like acme.sh, record the requested SAN set before the order can fail.
mkdir -p "$ACME_HOME/198.51.100.8_ecc"
printf "Le_Alt='%s'\n" "$*" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf"
[[ "$ACME_ISSUE_FAIL" != 1 ]]
--issue|--renew)
mkdir -p "$entry"
if [[ "$action" == --issue ]]; then
printf "Le_Alt='%s'\n" "${san:-no}" > "$conf"
if [[ "$ISSUE_MODE" == fail ]]; then exit 1; fi
else
if [[ "$RENEW_MODE" == skip ]]; then exit 2; fi
if [[ -n "${RENEW_BARRIER:-}" ]]; then
: > "$RENEW_BARRIER.ready"
read -r _ < "$RENEW_BARRIER"
fi
# Renew reuses stored destinations and advances the schedule before copying.
# shellcheck disable=SC1090
source "$conf"
printf "Le_NextRenewTime='advanced'\n" >> "$conf"
cp "$ISSUED_KEY" "$Le_RealKeyPath"
cp "$ISSUED_CERT" "$Le_RealFullChainPath"
if [[ "$RENEW_MODE" == fail ]]; then exit 1; fi
exit 0
fi
printf "Le_NextRenewTime='issued'\n" >> "$conf"
;;
--upgrade|--register-account) : ;;
--installcert)
key=''; cert=''; reload=''
while (($#)); do
case "$1" in --key-file) key="$2"; shift;; --fullchain-file) cert="$2"; shift;; --reloadcmd) reload="$2"; shift;; esac
shift
done
if [[ "$ACME_INSTALL_FAIL" == 1 ]]; then printf 'broken certificate\n' > "$cert"; exit 1; fi
cp "$ISSUED_KEY" "$key"; cp "$ISSUED_CERT" "$cert"
printf '%s' "$reload" > "$RELOAD_LOG.command"
sh -c "$reload"
[[ "$ACME_RELOAD_FAIL" != 1 ]]
printf 'Le_RealKeyPath=%q\nLe_RealFullChainPath=%q\n' "$key" "$chain" >> "$conf"
[[ -z "$account" || "$account" == */pending/account.conf ]] || exit 1
if [[ "$INSTALL_MODE" == fail ]]; then printf 'broken chain\n' > "$chain"; exit 1; fi
if [[ "$INSTALL_MODE" != no-key ]]; then cp "$ISSUED_KEY" "$key"; fi
if [[ "$INSTALL_MODE" != no-chain ]]; then cp "$ISSUED_CERT" "$chain"; fi
if [[ -n "$reload" ]]; then sh -c "$reload"; fi
;;
*) printf 'Unexpected ACME operation: %s\n' "$1" >&2; exit 1 ;;
*) printf 'Unexpected ACME operation: %s\n' "$action" >&2; exit 1 ;;
esac
MOCK
chmod +x "$TEMP/mock acme"
acme="$TEMP/mock acme"
export ACME_HOME="$TEMP"
reload="printf 'reloaded\\n' >> '$RELOAD_LOG'"
port=$((20000+RANDOM))
env chmod +x "$acme" "$TEMP/mockbin/crontab"
reload="test \"\$RELOAD_FAIL\" = 0 && printf 'reloaded\\n' >> '$RELOAD_LOG'"
ACME_ISSUE_FAIL=1
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'ACME failure reported success'; fi
cmp "$TEMP/old.pem" "$cert_dir/fullchain.pem" || fail 'issuance failure removed existing certificate'
cmp "$TEMP/old.key" "$cert_dir/privkey.pem" || fail 'issuance failure removed existing key'
[[ ! -f "$RELOAD_LOG" ]] || fail 'issuance failure reloaded panel'
[[ ! -e "$ACME_HOME/198.51.100.8_ecc" ]] || fail 'a failed first order left a renewal entry that cron would retry every day'
printf 'PASS: ACME failure preserves existing certificate and key\n'
fixture() {
export ISSUED_CERT="$TEMP/$1.pem" ISSUED_KEY="$TEMP/${2:-$1}.key"
}
mkdir -p "$ACME_HOME/198.51.100.8_ecc"
printf "Le_Alt='no'\nLe_NextRenewTime='1'\n" > "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf"
cp "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" "$TEMP/working.conf"
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"; then fail 'ACME failure reported success'; fi
cmp "$TEMP/working.conf" "$ACME_HOME/198.51.100.8_ecc/198.51.100.8.conf" || fail 'a failed IPv6 attempt rewrote the working certificate renewal entry'
[[ -z "$(find "$ACME_HOME" -maxdepth 1 -name '.xui-acme.*')" ]] || fail 'the renewal entry backup was left behind'
printf 'PASS: a failed order restores the renewal entry of the working certificate\n'
rm -rf "$ACME_HOME/198.51.100.8_ecc"
: > "$ACME_LOG"
ACME_ISSUE_FAIL=0
issue() {
ip_ssl_issue_and_install "$acme" "${1:-$ipv4}" "$ipv6" "$port" "${2:-$cert_dir}" "$reload"
}
ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'new certificate not installed'
[[ "$(stat -c %a "$cert_dir/privkey.pem")" == 600 ]] || fail 'private key permissions changed'
[[ "$(stat -c %a "$cert_dir/fullchain.pem")" == 644 ]] || fail 'certificate permissions changed'
openssl verify -trusted "$cert_dir/fullchain.pem" -partial_chain -verify_ip 198.51.100.8 "$cert_dir/fullchain.pem" > /dev/null
openssl verify -trusted "$cert_dir/fullchain.pem" -partial_chain -verify_ip 2001:4860::1 "$cert_dir/fullchain.pem" > /dev/null
if ! grep -qx 'arg=--listen-v4' "$ACME_LOG" || ! grep -qx 'arg=--listen-v6' "$ACME_LOG"; then fail 'dual-stack flags absent'; fi
[[ "$(grep -cx 'arg=2001:4860::1' "$ACME_LOG")" == 1 ]] || fail 'IPv6 SAN duplicated or missing'
[[ "$(cat "$RELOAD_LOG")" == reloaded ]] || fail 'valid certificate did not reload'
sh -c "$(cat "$RELOAD_LOG.command")"
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'saved renewal reload command failed'
printf 'PASS: both IP SANs, quoting, install permissions and saved reload command\n'
live_is() {
cmp "$TEMP/$1.pem" "$cert_dir/fullchain.pem" && cmp "$TEMP/$1.key" "$cert_dir/privkey.pem"
}
cp "$TEMP/v4.pem" "$cert_dir/fullchain.pem"
cp "$TEMP/v4.key" "$cert_dir/privkey.pem"
if sh -c "$(cat "$RELOAD_LOG.command")" > /dev/null 2>&1; then fail 'renewal guard accepted a missing IPv6 SAN'; fi
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'invalid renewed certificate reloaded the panel'
cp "$TEMP/new.pem" "$cert_dir/fullchain.pem"
cp "$TEMP/new.key" "$cert_dir/privkey.pem"
printf 'PASS: saved renewal guard blocks reload after a missing IPv6 SAN\n'
legacy_is_intact() {
cmp "$TEMP/legacy.conf" "$legacy/$ipv4/$ipv4.conf" &&
cmp "$TEMP/legacy.conf" "$legacy/${ipv4}_ecc/$ipv4.conf" &&
cmp "$TEMP/account.conf" "${acme%/*}/account.conf" &&
grep -qx 'unrelated certificate' "$legacy/example.net_ecc/marker"
}
for bad in v4 dns; do
ISSUED_CERT="$TEMP/$bad.pem"; ISSUED_KEY="$TEMP/$bad.key"
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '2001:4860::1' "$port" "$cert_dir" "$reload"; then fail 'missing IP SAN accepted'; fi
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'bad SAN replaced working certificate'
cmp "$TEMP/new.key" "$cert_dir/privkey.pem" || fail 'bad SAN replaced working key'
[[ "$(wc -l < "$RELOAD_LOG")" == 2 ]] || fail 'invalid IP SAN reloaded panel'
reject() {
if "$@" > "$TEMP/rejected" 2>&1; then fail "unexpected success: $*"; fi
}
fixture new
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
reject issue 198.51.100.9 "$TEMP/first"
[[ ! -e "$legacy/198.51.100.9_ecc" && ! -e "$TEMP/first/fullchain.pem" ]] || fail 'failed first order left public files or a renewable entry'
ISSUE_MODE=ok
for mode in fail no-key no-chain; do
INSTALL_MODE="$mode"
reject issue
if ! live_is old || ! legacy_is_intact; then fail "$mode installation changed prior certificate or ACME state"; fi
done
ISSUED_CERT="$TEMP/new.pem"; ISSUED_KEY="$TEMP/old.key"
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'mismatched private key accepted'; fi
cmp "$TEMP/new.key" "$cert_dir/privkey.pem" || fail 'mismatched key was not rolled back'
ACME_INSTALL_FAIL=1
if ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"; then fail 'partial installation accepted'; fi
cmp "$TEMP/new.pem" "$cert_dir/fullchain.pem" || fail 'partial install was not rolled back'
ACME_INSTALL_FAIL=0
ISSUED_CERT="$TEMP/old.pem"; ISSUED_KEY="$TEMP/old.key"; ACME_RELOAD_FAIL=1
ip_ssl_issue_and_install "$acme" 198.51.100.8 '' "$port" "$cert_dir" "$reload"
cmp "$TEMP/old.pem" "$cert_dir/fullchain.pem" || fail 'reload error discarded valid newly installed cert'
printf 'PASS: SAN/key mismatch and partial-install rollback; valid copy survives reload errors\n'
INSTALL_MODE=ok
for invalid in v4 dns; do
fixture "$invalid"
reject issue
if ! live_is old || ! legacy_is_intact; then fail 'missing IP SAN changed prior certificate or ACME state'; fi
done
fixture new old
reject issue
if ! live_is old || ! legacy_is_intact; then fail 'wrong key changed prior certificate or ACME state'; fi
fixture new
# Fault hooks are called indirectly by the sourced lifecycle functions.
# shellcheck disable=SC2317
chmod() {
if [[ "$1" == 600 && "$*" == *'/gen.'* ]]; then return 1; fi
command chmod "$@"
}
reject issue
unset -f chmod
if ! live_is old || ! legacy_is_intact; then fail 'chmod failure changed prior certificate or ACME state'; fi
printf 'PASS: issuance, installation, SAN, key, partial and permission failures preserve prior state\n'
# shellcheck disable=SC2317
mv() {
if [[ "$*" == *'.xui-retired-'* && "${!#}" == "$legacy/${ipv4}_ecc" ]]; then return 1; fi
command mv "$@"
}
ISSUE_MODE=fail
reject issue
grep -Fq "$state/pending" "$TEMP/rejected" || fail 'journal location missing from recovery error'
compgen -G "$legacy/.xui-retired-*-${ipv4}_ecc" >/dev/null || fail 'only renewal recovery copy was discarded'
unset -f mv
reject issue
legacy_is_intact || fail 'subsequent attempt did not finish ACME recovery'
ISSUE_MODE=ok
# shellcheck disable=SC2317
mv() {
if [[ "${!#}" == "$cert_dir/privkey.pem" || ( "$*" == *'/pending/restore '* && "${!#}" == "$cert_dir/fullchain.pem" ) ]]; then
return 1
fi
command mv "$@"
}
reject issue
grep -Fq "$state/pending" "$TEMP/rejected" || fail 'live recovery error omitted journal location'
cmp "$TEMP/old.pem" "$state/pending/live/fullchain.pem" || fail 'live recovery copy was lost'
unset -f mv
ISSUE_MODE=fail
reject issue
if ! live_is old || ! legacy_is_intact; then fail 'subsequent attempt did not finish live restoration'; fi
[[ ! -L "$cert_dir/fullchain.pem" ]] || fail 'original regular file type was not recovered'
ISSUE_MODE=ok
# shellcheck disable=SC2317
mv() {
command mv "$@" || return
if [[ "${!#}" == "$cert_dir/fullchain.pem" && "$*" == *'/pending/link '* ]]; then kill -KILL "$BASHPID"; fi
}
reject issue
unset -f mv
if [[ ! -d "$state/pending" ]] || ! live_is old; then fail 'interrupted transition exposed a mixed pair'; fi
ISSUE_MODE=fail
reject issue
if ! live_is old || ! legacy_is_intact; then fail 'interrupted transition failed to recover'; fi
ISSUE_MODE=ok
printf 'PASS: restoration failures retain backups and interruptions recover on next run\n'
: > "$ACME_LOG"
RELOAD_FAIL=1
issue
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'
[[ ! -e "$legacy/$ipv4" && ! -e "$legacy/${ipv4}_ecc" ]] || fail 'old cron can still target live paths'
[[ "$(grep -cx 'arg=--listen-v4' "$ACME_LOG")" == 1 && "$(grep -cx 'arg=--listen-v6' "$ACME_LOG")" == 1 ]] || fail 'dual-stack flags absent'
[[ "$(grep -cx "arg=$ipv6" "$ACME_LOG")" == 1 ]] || fail 'IPv6 SAN duplicated or missing'
grep -Fq '/unrelated-cron' "$CRON_FILE" || fail 'crontab lost an unrelated entry'
[[ "$(grep -c 'x-ui managed IP certificate' "$CRON_FILE")" == 1 ]] || fail 'persistent cron missing or duplicated'
cmp "$TEMP/account.conf" "${acme%/*}/account.conf" || fail 'global account config changed'
grep -qx 'unrelated certificate' "$legacy/example.net_ecc/marker"
export RELOAD_FAIL=0 RENEW_MODE=skip
"$runner"
[[ ! -f "$state/current/reload-pending" && "$(cat "$RELOAD_LOG")" == reloaded ]] || fail 'fresh-shell reload retry failed'
printf 'PASS: validated commit preserves modes, retires old live destinations and retries panel reload\n'
working=$(readlink "$state/current")
working_conf="$state/current/acme/${ipv4}_ecc/$ipv4.conf"
cp "$working_conf" "$TEMP/working.conf"
RENEW_MODE=ok
for invalid in v4 expired; do fixture "$invalid"; reject "$runner"; live_is new || fail "$invalid renewal exposed invalid files"; done
fixture later old
reject "$runner"
live_is new || fail 'wrong renewal key replaced live pair'
fixture later
for mode in no-key no-chain; do INSTALL_MODE="$mode"; reject "$runner"; done
INSTALL_MODE=ok
if ! live_is new || ! cmp "$TEMP/working.conf" "$working_conf"; then fail 'rejected renewal committed advanced ACME state'; fi
[[ "$(readlink "$state/current")" == "$working" ]] || fail 'rejected renewal changed generation'
# Start a fresh TLS process using the unchanged panel paths after rejecting a renewal.
(
openssl s_server -quiet -www -accept "127.0.0.1:$port" -cert "$cert_dir/fullchain.pem" \
-key "$cert_dir/privkey.pem" > "$TEMP/tls.log" 2>&1 &
tls_pid=$!
trap 'if kill -0 "$tls_pid" 2>/dev/null; then kill "$tls_pid"; fi; wait "$tls_pid" 2>/dev/null || [[ $? == 143 ]]' EXIT
for ((attempt=0; attempt<100; attempt++)); do
kill -0 "$tls_pid" || break
if curl --noproxy '*' --silent --fail --cacert "$SSL_CERT_FILE" --connect-timeout 1 --max-time 3 \
--connect-to "$ipv4:$port:127.0.0.1:$port" "https://$ipv4:$port/" >/dev/null; then exit 0; fi
sleep .01
done
cat "$TEMP/tls.log" >&2
fail 'TLS restart could not load the last known-good certificate'
)
# shellcheck disable=SC2317
mv() {
if [[ "${!#}" == "$state/current" ]]; then return 1; fi
command mv "$@"
}
reject ip_ssl_scheduled "$cert_dir"
unset -f mv
if ! live_is new || ! cmp "$TEMP/working.conf" "$working_conf"; then fail 'promotion failure damaged the previous generation'; fi
"$runner"
if ! live_is later || ! grep -q advanced "$working_conf"; then fail 'valid persistent renewal did not commit files and ACME state'; fi
printf 'PASS: actual TLS startup can read last known-good pair after rejected renewals; valid renewal promotes both files\n'
# Pause renewal while it holds the destination lock. An unrelated destination
# completes during the wait, while two competing manual requests share the lock.
export RENEW_BARRIER="$TEMP/renewal"
mkfifo "$RENEW_BARRIER"
fixture v4
"$runner" > "$TEMP/concurrent-renewal" 2>&1 &
renew_pid=$!
for ((attempt=0; attempt<500; attempt++)); do
[[ ! -f "$RENEW_BARRIER.ready" ]] || break
sleep .01
done
[[ -f "$RENEW_BARRIER.ready" ]] || fail 'renewal did not reach the deterministic barrier'
if flock -n "$state/lock" true; then fail 'renewal was not holding the destination lock'; fi
fixture old
issue "$ipv4" > "$TEMP/concurrent-manual" 2>&1 &
manual_pid=$!
fixture new
issue 198.51.100.9 > "$TEMP/concurrent-invalid" 2>&1 &
invalid_pid=$!
issue "$ipv4" "$TEMP/independent"
live_is later || fail 'a competing request changed live files while renewal held the lock'
printf 'continue\n' > "$RENEW_BARRIER"
if wait "$renew_pid"; then fail 'invalid concurrent renewal succeeded'; fi
wait "$manual_pid" || { cat "$TEMP/concurrent-manual"; fail 'concurrent manual issuance failed'; }
if wait "$invalid_pid"; then fail 'wrong identifier unexpectedly committed'; fi
live_is old || fail 'failed operation rolled back newer successful deployment'
unset RENEW_BARRIER
printf 'PASS: manual, renewal and shared-destination promotion serialize; unrelated destination progresses\n'
# Fail the first pointer swap while a second valid operation is waiting to
# promote. Its new state must survive cleanup of the earlier transaction.
promotion="$TEMP/promotion"
mkfifo "$promotion"
# shellcheck disable=SC2317
mv() {
if [[ "${!#}" == "$state/current" && "${FAIL_PROMOTION:-0}" == 1 ]]; then
: > "$promotion.ready"
read -r _ < "$promotion"
return 1
fi
command mv "$@"
}
fixture new
FAIL_PROMOTION=1 issue > "$TEMP/failed-promotion" 2>&1 &
failed_pid=$!
for ((attempt=0; attempt<500; attempt++)); do
[[ ! -f "$promotion.ready" ]] || break
sleep .01
done
[[ -f "$promotion.ready" ]] || fail 'first operation did not reach promotion'
fixture later
issue > "$TEMP/waiting-promotion" 2>&1 &
waiting_pid=$!
if flock -n "$state/lock" true; then fail 'promotion was not holding the destination lock'; fi
live_is old || fail 'uncommitted promotion changed live files'
printf 'continue\n' > "$promotion"
if wait "$failed_pid"; then fail 'injected promotion failure reported success'; fi
wait "$waiting_pid" || { cat "$TEMP/waiting-promotion"; fail 'second promotion failed'; }
unset -f mv
live_is later || fail 'rollback overwrote a newer promotion'
printf 'PASS: a failed promotion cannot roll back another operation on the same paths\n'
before=$(wc -l < "$ACME_LOG")
if ip_ssl_issue_and_install "$acme" 198.51.100.8 "2001:4860::1;touch $TEMP/injected" "$port" "$cert_dir" "$reload"; then fail 'injection accepted'; fi
reject ip_ssl_issue_and_install "$acme" "$ipv4" "$ipv6;touch $TEMP/injected" "$port" "$cert_dir" "$reload"
[[ ! -e "$TEMP/injected" && "$(wc -l < "$ACME_LOG")" == "$before" ]] || fail 'invalid IP reached shell or ACME'
printf 'PASS: invalid SAN never reaches ACME or a shell command\n'
@@ -147,7 +401,7 @@ ip() {
[[ "$*" != '-6 route get '* ]] || return 0
printf '2: eth0 inet6 2001:4860::1/64 scope global valid_lft forever preferred_lft forever\n'
}
ACME_RELOAD_FAIL=0; ISSUED_CERT="$TEMP/new.pem"; ISSUED_KEY="$TEMP/new.key"
fixture new
source_function "$ROOT/x-ui.sh" confirm
source_function "$ROOT/x-ui.sh" ssl_cert_issue_for_ip
if ! ssl_cert_issue_for_ip "$TEMP/menu-cert" <<< "$(printf 'y\ny\n%s\ny\n' "$port")" > "$TEMP/menu-output" 2>&1; then
+3
View File
@@ -24,6 +24,9 @@ prompt_and_setup_ssl 2053 / 198.51.100.8 > "$TEMP/invalid" 2>&1 || fail 'an inva
grep -q 'Invalid public IPv6' "$TEMP/invalid" || fail 'invalid environment value has no clear diagnostic'
[[ "$SSL_HOST" == 198.51.100.8 ]] || fail 'the installer lost its access host after skipping the certificate'
unset XUI_SSL_IPV6
called=0; stopped=0
prompt_and_setup_ssl 2053 / 198.51.100.900 > "$TEMP/invalid-v4" 2>&1 || fail 'an invalid IPv4 aborted the installer'
[[ "$called" == 0 && "$stopped" == 0 ]] || fail 'invalid IPv4 stopped panel or reached issuance'
called=0
prompt_and_setup_ssl 2053 / 198.51.100.8 </dev/null > "$TEMP/unattended" 2>&1
[[ "$called" == 1 && -z "$captured" ]] || fail 'unattended unset override did not retain IPv4-only issuance'