Backport #38554 by @Otto-Deviant1904 Fix #38519 When `ENABLE_ACME` renew fails during startup (e.g. CA unreachable), `ManageSync` currently aborts even if a still-valid certificate is on disk, so HTTPS never comes up. If `CacheManagedCertificate` finds a non-expired cert, log the manage error, continue with that cert, and kick `ManageAsync` for background retries. First-time install / expired-or-missing cert still fails closed. Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Harsh Satyajit Thakur <f20240223@goa.bits-pilani.ac.in> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
+15
-3
@@ -6,6 +6,7 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -93,10 +94,21 @@ func runACME(listenAddr string, m http.Handler) error {
|
|||||||
myACME := certmagic.NewACMEIssuer(magic, certmagic.DefaultACME)
|
myACME := certmagic.NewACMEIssuer(magic, certmagic.DefaultACME)
|
||||||
magic.Issuers = []certmagic.Issuer{myACME}
|
magic.Issuers = []certmagic.Issuer{myACME}
|
||||||
|
|
||||||
// this obtains certificates or renews them if necessary
|
// Obtain certificates or renew them if necessary. ManageSync fails closed on
|
||||||
err := magic.ManageSync(graceful.GetManager().HammerContext(), []string{setting.Domain})
|
// renewal errors even when a still-valid certificate is already on disk, which
|
||||||
|
// takes HTTPS down on restart (https://github.com/go-gitea/gitea/issues/38519).
|
||||||
|
// Prefer keeping the existing cert and retrying renewals asynchronously.
|
||||||
|
ctx := graceful.GetManager().ShutdownContext()
|
||||||
|
err := magic.ManageSync(ctx, []string{setting.Domain})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
cert, cacheErr := magic.CacheManagedCertificate(ctx, setting.Domain)
|
||||||
|
if cacheErr != nil || cert.Expired() {
|
||||||
|
return errors.Join(err, cacheErr)
|
||||||
|
}
|
||||||
|
log.Error("ACME certificate manage failed; continuing with existing certificate: %v", err)
|
||||||
|
if err := magic.ManageAsync(ctx, []string{setting.Domain}); err != nil {
|
||||||
|
log.Error("Failed to start async ACME management: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := magic.TLSConfig()
|
tlsConfig := magic.TLSConfig()
|
||||||
|
|||||||
Reference in New Issue
Block a user