caddytls: keep ACME issuer network proxy after RebuildCertMagic (#8288)

* caddytls: keep ACME issuer network proxy across re-provisioning

LoadModule clears NetworkProxyRaw, so when the HTTP app provisions an
ACME issuer a second time (fillInACMEIssuer), the rebuilt template has
no HTTPProxy. Since #7510 that template is used via SetConfig, and ACME
requests bypass the configured proxy.

Keep the loaded proxy func on the issuer, like the DNS solver.

Fixes #8283

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2366825f2380383dacd8adeb1508e34c3e25634d)

* caddytls: test that ACME requests use the proxy after RebuildCertMagic

Replace the provision-twice check with a regression for the path the
HTTP app takes: Provision, re-provision in fillInACMEIssuer, then
RebuildCertMagic. The test issues against a local proxy and asserts the
ACME request goes through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: k4lleanka <257361691+k4lleanka@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
K4lleAnka 2026-10-28 10:37:00 +01:00 • committed by GitHub
parent 71c5812cfe
commit fead9c3f24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 97 additions and 5 deletions

View file

@ -109,8 +109,9 @@ type ACMEIssuer struct {
// Forward proxy module
NetworkProxyRaw json.RawMessage `json:"network_proxy,omitempty" caddy:"namespace=caddy.network_proxy inline_key=from"`
rootPool *x509.CertPool
logger *zap.Logger
rootPool *x509.CertPool
logger *zap.Logger
httpProxy func(*http.Request) (*url.URL, error)
template certmagic.ACMEIssuer // set at Provision
magic *certmagic.Config // set at PreCheck
@ -265,17 +266,20 @@ func (iss *ACMEIssuer) makeIssuerTemplate(ctx caddy.Context) (certmagic.ACMEIssu
Logger: iss.logger,
}
// LoadModule clears NetworkProxyRaw, so keep the proxy for when the
// issuer is provisioned again (e.g. by the HTTP app's auto-HTTPS)
if len(iss.NetworkProxyRaw) != 0 {
proxyMod, err := ctx.LoadModule(iss, "NetworkProxyRaw")
if err != nil {
return template, fmt.Errorf("failed to load network_proxy module: %v", err)
}
if m, ok := proxyMod.(caddy.ProxyFuncProducer); ok {
template.HTTPProxy = m.ProxyFunc()
iss.httpProxy = m.ProxyFunc()
} else {
return template, fmt.Errorf("network_proxy module is not `(func(*http.Request) (*url.URL, error))``")
}
}
template.HTTPProxy = iss.httpProxy
if iss.Challenges != nil {
if iss.Challenges.HTTP != nil {

View file

@ -1,9 +1,22 @@
package caddytls
import (
"github.com/caddyserver/caddy/v2"
"github.com/mholt/acmez/v3/acme"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
_ "github.com/caddyserver/caddy/v2/modules/internal/network"
"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/v3/acme"
"go.uber.org/zap"
)
func TestACMEIssuerExpandPlaceholders(t *testing.T) {
@ -41,3 +54,78 @@ func TestACMEIssuerExpandPlaceholders(t *testing.T) {
t.Errorf("ExternalAccount.MACKey: got %q, want %q", iss.ExternalAccount.MACKey, want)
}
}
// The HTTP app provisions ACME issuers again to fill in its ports and then
// rebuilds the CertMagic config; requests must still use the configured proxy (#8283).
func TestACMEIssuerNetworkProxyAfterRebuildCertMagic(t *testing.T) {
var proxied atomic.Int32
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodConnect && r.Host == "acme.invalid:443" {
proxied.Add(1)
}
http.Error(w, "test proxy", http.StatusBadGateway)
}))
defer proxy.Close()
certCacheMu.Lock()
prevCache := certCache
certCache = certmagic.NewCache(certmagic.CacheOptions{
GetConfigForCert: func(certmagic.Certificate) (*certmagic.Config, error) { return nil, nil },
})
certCacheMu.Unlock()
defer func() {
certCacheMu.Lock()
certCache.Stop()
certCache = prevCache
certCacheMu.Unlock()
}()
caddyCtx, cancel := caddy.NewContext(caddy.Context{Context: t.Context()})
defer cancel()
tlsApp := &TLS{ctx: caddyCtx, logger: zap.NewNop()}
iss := &ACMEIssuer{
CA: "https://acme.invalid/directory",
NetworkProxyRaw: []byte(`{"from":"url","url":"` + proxy.URL + `"}`),
}
if err := iss.Provision(caddyCtx); err != nil {
t.Fatalf("provisioning issuer: %v", err)
}
ap := &AutomationPolicy{
Issuers: []certmagic.Issuer{iss},
storage: &certmagic.FileStorage{Path: t.TempDir()},
}
if err := ap.Provision(tlsApp); err != nil {
t.Fatalf("provisioning automation policy: %v", err)
}
// what the HTTP app's fillInACMEIssuer and createAutomationPolicies do
if err := iss.Provision(caddyCtx); err != nil {
t.Fatalf("re-provisioning issuer: %v", err)
}
if err := ap.RebuildCertMagic(tlsApp); err != nil {
t.Fatalf("rebuilding certmagic config: %v", err)
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
csrDER, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{DNSNames: []string{"example.com"}}, key)
if err != nil {
t.Fatal(err)
}
csr, err := x509.ParseCertificateRequest(csrDER)
if err != nil {
t.Fatal(err)
}
ctx, cancelIssue := context.WithTimeout(t.Context(), 10*time.Second)
defer cancelIssue()
if _, err := iss.Issue(ctx, csr); err == nil {
t.Fatal("expected issuance to fail against the test proxy")
}
if proxied.Load() == 0 {
t.Error("ACME request did not go through the configured network proxy")
}
}