diff --git a/hscontrol/app.go b/hscontrol/app.go index c1b1bf092..8f4950097 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -1094,8 +1094,6 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) { acmeCfg := h.cfg.TLS.ACME logger := zlog.Zap(log.Logger).Named("acme") - // An empty CA means Let's Encrypt, and certmagic then retries failures - // against Let's Encrypt staging to spare the production rate limits. issuer := certmagic.ACMEIssuer{ CA: acmeCfg.URL, Email: acmeCfg.Email, @@ -1164,6 +1162,9 @@ func (h *Headscale) acmeTLSSettings(ctx context.Context) (*tlsBundle, error) { } acmeIssuer := certmagic.NewACMEIssuer(magic, issuer) + // For Let's Encrypt, certmagic retries against Let's Encrypt staging, + // registering an account there; retries stay on tls.acme.url. + acmeIssuer.TestCA = "" magic.Issuers = []certmagic.Issuer{acmeIssuer} bundle.Config = magic.TLSConfig() diff --git a/hscontrol/app_tls_test.go b/hscontrol/app_tls_test.go index dc5da52ad..870d1bd4e 100644 --- a/hscontrol/app_tls_test.go +++ b/hscontrol/app_tls_test.go @@ -11,12 +11,14 @@ import ( "log" "math/big" "net" + "net/http" "net/http/httptest" "os" "path/filepath" "strconv" "strings" "sync" + "sync/atomic" "testing" "time" @@ -276,6 +278,77 @@ func TestACMETLSSettings(t *testing.T) { } } +// TestACMERetryStaysOnConfiguredCA pins that a failed issuance is retried +// against tls.acme.url and nothing else. The default tls.acme.url is +// certmagic's default CA, Let's Encrypt production, for which certmagic +// retries against Let's Encrypt staging and registers an account there +// (#3589). Pebble stands in for production, so the test waits for +// certmagic's first retry interval. +func TestACMERetryStaysOnConfiguredCA(t *testing.T) { + if testing.Short() { + t.Skip("waits for certmagic's first retry interval") + } + + acme := startTestACME(t, freePort(t), freePort(t)) + + var stagingHits atomic.Int32 + + staging := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + stagingHits.Add(1) + w.WriteHeader(http.StatusServiceUnavailable) + })) + t.Cleanup(staging.Close) + + prevCA, prevTestCA := certmagic.DefaultACME.CA, certmagic.DefaultACME.TestCA + certmagic.DefaultACME.CA, certmagic.DefaultACME.TestCA = acme.directory, staging.URL + + t.Cleanup(func() { certmagic.DefaultACME.CA, certmagic.DefaultACME.TestCA = prevCA, prevTestCA }) + + // The CA resolves TXT from acme.provider, so records published here are + // never seen and every validation fails, like a mistyped DNS delegation. + published := &dnstest.Provider{} + + h := &Headscale{cfg: &types.Config{ + ServerURL: "https://" + acmeTestHost, + Addr: net.JoinHostPort("127.0.0.1", strconv.Itoa(freePort(t))), + TLS: types.TLSConfig{ACME: types.ACMEConfig{ + Hostname: acmeTestHost, + ChallengeType: types.DNS01ChallengeType, + URL: acme.directory, + CacheDir: t.TempDir(), + }}, + DNSChallenge: types.DNSChallengeConfig{ + Provider: published, + Resolvers: []string{acme.resolver}, + }, + }} + + bundle, err := h.getTLSSettings(t.Context()) + require.NoError(t, err) + t.Cleanup(bundle.Stop) + + require.NoError(t, bundle.Manage(t.Context())) + + attempts := func() int { + n := 0 + + for _, c := range published.Calls() { + if c.Method == "append" { + n++ + } + } + + return n + } + + assert.EventuallyWithT(t, func(c *assert.CollectT) { + assert.True(c, attempts() >= 2 || stagingHits.Load() > 0) + }, 2*time.Minute, time.Second, "certmagic should retry the failed issuance") + + assert.Zero(t, stagingHits.Load(), "retry must not leave tls.acme.url") + assert.GreaterOrEqual(t, attempts(), 2, "retry must reach tls.acme.url") +} + // testKeypair returns a new certificate as DER, and as PEM with its key. func testKeypair(t *testing.T, notBefore, notAfter time.Time) ([]byte, []byte, []byte) { t.Helper()