caddytls: publish cache options when the TLS app starts

Provision replaced the shared certificate cache's GetConfigForCert
callback with one pointing at the app being provisioned. If cache
maintenance ran during that window it consulted a partially provisioned
app (nil automation config), panicking while holding the cache read lock.
A config that failed to load also left the cache pointing at it.

Only create the cache in Provision; switch its options to the new app in
Start, once the config is actually becoming active.

Fixes #8231 (Caddy side; the leaked read lock needs a CertMagic fix)
This commit is contained in:
sylvain 2026-11-12 09:47:34 +01:00
parent 7591cff1fb
commit 3c29904175
No known key found for this signature in database
2 changed files with 142 additions and 3 deletions

View file

@ -144,6 +144,7 @@ type TLS struct {
echRotateInterval time.Duration
logger *zap.Logger
events *caddyevents.App
cacheOpts certmagic.CacheOptions
serverNames map[string]serverNameRegistration
serverNamesMu *sync.Mutex
@ -197,7 +198,9 @@ func (t *TLS) Provision(ctx caddy.Context) error {
t.dns = dnsMod
}
// set up a new certificate cache; this (re)loads all certificates
// set up the certificate cache; if one already exists, it keeps using the
// running TLS app until this one is started, since a config that is still
// provisioning (or fails to) must not be consulted by cache maintenance
cacheOpts := certmagic.CacheOptions{
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
return t.getConfigForName(cert.Names[0]), nil
@ -215,11 +218,11 @@ func (t *TLS) Provision(ctx caddy.Context) error {
cacheOpts.Capacity = 10000
}
t.cacheOpts = cacheOpts
certCacheMu.Lock()
if certCache == nil {
certCache = certmagic.NewCache(cacheOpts)
} else {
certCache.SetOptions(cacheOpts)
}
certCacheMu.Unlock()
@ -416,6 +419,15 @@ func (t *TLS) Start() error {
}
}
// now that this config is becoming active, let the cache use it
if t.cacheOpts.GetConfigForCert != nil {
certCacheMu.RLock()
if certCache != nil {
certCache.SetOptions(t.cacheOpts)
}
certCacheMu.RUnlock()
}
if t.bgCtx == nil {
parentCtx := t.ctx.Context
if parentCtx == nil {

View file

@ -0,0 +1,127 @@
package caddytls
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"math/big"
"testing"
"time"
"github.com/caddyserver/certmagic"
"github.com/caddyserver/caddy/v2"
)
type testIssuer struct{}
func (testIssuer) IssuerKey() string { return "test" }
func (testIssuer) Issue(context.Context, *x509.CertificateRequest) (*certmagic.IssuedCertificate, error) {
return nil, errors.New("not implemented")
}
// TestProvisionDoesNotReplaceCacheCallback ensures that cache maintenance keeps
// using the running TLS app while a new one is provisioned, and only switches
// to the new app once it is started.
func TestProvisionDoesNotReplaceCacheCallback(t *testing.T) {
t.Setenv("XDG_DATA_HOME", t.TempDir())
certCacheMu.Lock()
origCache := certCache
certCacheMu.Unlock()
t.Cleanup(func() {
certCacheMu.Lock()
certCache = origCache
certCacheMu.Unlock()
})
// cache owned by the "running" app, holding one managed certificate
oldCalls := 0
errOldApp := errors.New("old app")
cache := certmagic.NewCache(certmagic.CacheOptions{
GetConfigForCert: func(certmagic.Certificate) (*certmagic.Config, error) {
oldCalls++
return nil, errOldApp
},
})
defer cache.Stop()
certCacheMu.Lock()
certCache = cache
certCacheMu.Unlock()
storage := &certmagic.FileStorage{Path: t.TempDir()}
storeTestCert(t, storage, "test", "example.com")
cfg := certmagic.New(cache, certmagic.Config{
Storage: storage,
Issuers: []certmagic.Issuer{testIssuer{}},
OCSP: certmagic.OCSPConfig{DisableStapling: true},
})
if _, err := cfg.CacheManagedCertificate(t.Context(), "example.com"); err != nil {
t.Fatalf("caching managed certificate: %v", err)
}
// provision (but do not start) a new TLS app
ctx, err := caddy.ProvisionContext(&caddy.Config{})
if err != nil {
t.Fatal(err)
}
app := &TLS{DisableStorageClean: true}
if err := app.Provision(ctx); err != nil {
t.Fatalf("provisioning: %v", err)
}
_ = cache.RenewManagedCertificates(t.Context())
if oldCalls != 1 {
t.Fatalf("expected the running app's callback to be used once, got %d calls", oldCalls)
}
// once started, the new app takes over
if err := app.Start(); err != nil {
t.Fatalf("starting: %v", err)
}
defer app.Stop()
_ = cache.RenewManagedCertificates(t.Context())
if oldCalls != 1 {
t.Fatalf("expected the started app's callback to be used, but the old one was called again")
}
}
func storeTestCert(t *testing.T, storage certmagic.Storage, issuerKey, name string) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: name},
DNSNames: []string{name},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(365 * 24 * time.Hour),
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
keyDER, err := x509.MarshalECPrivateKey(key)
if err != nil {
t.Fatal(err)
}
ctx := t.Context()
for k, v := range map[string][]byte{
certmagic.StorageKeys.SiteCert(issuerKey, name): pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}),
certmagic.StorageKeys.SitePrivateKey(issuerKey, name): pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}),
certmagic.StorageKeys.SiteMeta(issuerKey, name): []byte(`{}`),
} {
if err := storage.Store(ctx, k, v); err != nil {
t.Fatal(err)
}
}
}