#!/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