Skip to content

Commit c0b28ff

Browse files
LupulScIT-Raphael
authored andcommitted
Fix LE renew: retry downloading of certs on unexpected http status code
1 parent a817523 commit c0b28ff

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

bin/v-add-letsencrypt-domain

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ query_le_v2() {
5555
# Save http response to file passed as "$4" arg or print to stdout if not provided
5656
# http response headers are always sent to stdout
5757
local save_to_file=${4:-"/dev/stdout"}
58-
curl --silent --dump-header /dev/stdout --data "$post_data" "$1" --header "$content" --output "$save_to_file"
58+
curl -k --retry 5 --retry-connrefused --silent --dump-header /dev/stdout --data "$post_data" "$1" --header "$content" --output "$save_to_file"
59+
debug_log "API call" "exit status: $?"
5960
}
6061

6162

@@ -410,10 +411,31 @@ if [[ "$status" -ne 200 ]]; then
410411
fi
411412

412413
# Downloading signed certificate / STEP 7
413-
answer=$(query_le_v2 "$certificate" "" "$nonce" "$ssl_dir/$domain.pem")
414-
status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
414+
status=0
415+
retry=0
416+
417+
while [[ $status != 200 && $retry -lt 3 ]]; do
418+
419+
answer=$(query_le_v2 "$certificate" "" "$nonce" "$ssl_dir/$domain.pem")
420+
status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
421+
422+
debug_log "Step 7" "- status: ${status}\n- retry: ${retry}\n- answer: ${answer}"
423+
424+
if [[ $status != 200 ]]; then
425+
retry=$((retry + 1))
426+
sleep $((retry * 2)) # Sleep for 2s, 4s, 6s, 8s
427+
fi
428+
429+
done
430+
431+
# Fallback on depreciated download method for certs (unauthenticated GET)
432+
if [[ $status != 200 ]]; then
433+
answer=$(curl -k --retry 5 --retry-connrefused --silent --dump-header /dev/stdout "$certificate" --output "$ssl_dir/$domain.pem")
434+
status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
435+
436+
debug_log "Step 7 - Fallback" "- status: ${status}\n- answer: ${answer}"
437+
fi
415438

416-
debug_log "Step 7" "- status: ${status}\n- answer: ${answer}"
417439
debug_log "CERT DIR" "$(ls -las "$ssl_dir/")"
418440
debug_log "CERT PEM" "$(cat "$ssl_dir/$domain.pem")"
419441

0 commit comments

Comments
 (0)