carlos / platform Public

edge: serve mirrored wildcards without a DNS provider

Keep a serve-only wildcard manager on gated edges without DNS credentials.
Load mirrored certificates before standing down, refuse orders even while
holding the issuer lease, and rate-limit the diagnostic to once an hour.
Keep ungated and devbox provider refusals intact.

Add real TLS mirror coverage and providerless ordering and refusal tests.
The new regression fails with the 98c43eea implementation restored.
Implemented-by: Astra (codex)
claude pushed by paul@keymail.dev c7b1df8f73cd36c2aa3844acbfd87a03b7ef4fb8 parent 15118be
3 files changed, +232 −9
  • internal/edge/devbox.go +1 −1
  • internal/edge/wildcard.go +36 −8
  • internal/edge/wildcard_serveonly_test.go +195 −0

Intent

Human

Fallback: check the W1-W4 investigation agent (INVESTIGATION.md in $TMPDIR/g2rfix) and continue the G2R fix SDD build.

Attributed to this commit: 8.33 g CO₂e (954,984 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on region-ha-g2r-fixes, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/edge/devbox.go b/internal/edge/devbox.go
index 6987d871f..0caff03a5 100644
--- a/internal/edge/devbox.go
+++ b/internal/edge/devbox.go
@@ -188,7 +188,7 @@ func serveDevbox(ctx context.Context, cfg DevboxConfig, seams devboxSeams) error
// No issuer gate (e.issuer is nil, so ungated): a devbox is a deployment
// of one, and its manager is the only thing that orders.
w := newWildcardFromEnv(e, certCache)
- if w == nil {
+ if w == nil || w.dns == nil {
// newWildcardFromEnv has logged why (no DNS provider, or its config
// failed to load).
return errors.New("edge: devbox: wildcard certificates are off; devbox mode needs CARLOS_DNS_PROVIDER and its credential")
diff --git a/internal/edge/wildcard.go b/internal/edge/wildcard.go
index 067c090d5..e67c2f328 100644
--- a/internal/edge/wildcard.go
+++ b/internal/edge/wildcard.go
@@ -110,9 +110,9 @@ type wildcardManager struct {
// is never gated — that is the whole point of the shared cache.
issuer IssuerGate
- // lastNotIssuerLog rate-limits the one line a non-issuer prints when it
- // stands down from a pass. Read/written under mu.
+ // Stand-down diagnostics repeat at most hourly. Read/written under mu.
lastNotIssuerLog time.Time
+ lastNoDNSLog time.Time
}
func newWildcardManager(staticDomains []string, appsDomain string, hosts func() []string, delegated func() []string, delegationZone string, cache autocert.Cache, dns dnsProvider) *wildcardManager {
@@ -521,6 +521,10 @@ func (w *wildcardManager) passWithClock(now func() time.Time) time.Duration {
if !w.needsObtain(p, now()) {
continue
}
+ if w.dns == nil {
+ standDowns++
+ continue
+ }
// The order gate (certsync.go): one elected issuer talks to the CA.
// A non-issuer stands down HERE, after the cache load and before the
// CA — so it still picks up the issuer's wildcard, and never arms the
@@ -559,7 +563,11 @@ func (w *wildcardManager) passWithClock(now func() time.Time) time.Duration {
log.Printf("edge: wildcard *.%s obtained (valid until %s)", p, cert.Leaf.NotAfter.Format(time.RFC3339))
}
if standDowns > 0 {
- w.logStandDown(standDowns)
+ if w.dns == nil {
+ w.logNoDNSStandDown(standDowns, now())
+ } else {
+ w.logStandDown(standDowns)
+ }
// Come back in an hour, not twelve. A non-issuer's next pass is what
// LOADS the wildcard the issuer has meanwhile minted into this box's
// cache — certFor serves from the in-memory map loadCached fills — and
@@ -615,6 +623,23 @@ func (w *wildcardManager) logStandDown(parents int) {
log.Printf("edge: wildcard: %d parent(s) need issuance but this box is not the deployment's certificate issuer — serving what the issuer publishes", parents)
}
+func (w *wildcardManager) logNoDNSStandDown(parents int, now time.Time) {
+ w.mu.Lock()
+ quiet := now.Sub(w.lastNoDNSLog) < time.Hour
+ if !quiet {
+ w.lastNoDNSLog = now
+ }
+ w.mu.Unlock()
+ if quiet {
+ return
+ }
+ if _, held := w.issuerGate(); held {
+ log.Printf("edge: wildcard: warning: this box holds the issuer lease but cannot renew wildcards (no DNS provider); %d parent(s) need issuance", parents)
+ return
+ }
+ log.Printf("edge: wildcard: %d parent(s) need issuance but this box has no DNS provider; serving what the issuer publishes", parents)
+}
+
// noZoneRetry is how long a parent whose challenge zone the DNS provider
// does not hold waits before it is looked at again (see pass).
const noZoneRetry = 12 * time.Hour
@@ -799,9 +824,9 @@ func (w *wildcardManager) accountKey(ctx context.Context) (*ecdsa.PrivateKey, er
// newWildcardFromEnv builds the manager from the environment, or returns nil
// when wildcards are off: no CARLOS_WILDCARD_DOMAINS and no
// PLATFORM_APPS_DOMAIN (nothing to cover — silent, the historical default),
-// or no DNS provider configured (logged: half a config deserves a reason in
-// the journal). nil means RunWithActivator wires nothing and the edge
-// behaves exactly as before this feature existed.
+// or no DNS provider configured on an ungated box (logged). A gated box
+// without a provider still serves mirrored wildcards. nil means
+// RunWithActivator wires nothing and the edge keeps its per-host behaviour.
func newWildcardFromEnv(e *Edge, cache autocert.Cache) *wildcardManager {
static := demoWildcardDomains(os.Getenv)
appsDomain := strings.ToLower(strings.TrimSpace(os.Getenv("PLATFORM_APPS_DOMAIN")))
@@ -876,8 +901,11 @@ func newWildcardFromEnv(e *Edge, cache autocert.Cache) *wildcardManager {
}
dns, reason := dnsProviderFromEnv()
if dns == nil {
- log.Printf("edge: wildcard certs off (%s) — per-host issuance continues", reason)
- return nil
+ if e.issuer == nil {
+ log.Printf("edge: wildcard certs off (%s) — per-host issuance continues", reason)
+ return nil
+ }
+ log.Printf("edge: wildcard certs on, serve only (%s); this box cannot order wildcards", reason)
}
// Delegated parents (spec §4): the SOURCE is claim-derived and lands
// with the claim-derivation pass (Plan C); until then the zone is
diff --git a/internal/edge/wildcard_serveonly_test.go b/internal/edge/wildcard_serveonly_test.go
new file mode 100644
index 000000000..2599cd77f
--- /dev/null
+++ b/internal/edge/wildcard_serveonly_test.go
@@ -0,0 +1,195 @@
+package edge
+
+import (
+ "bytes"
+ "context"
+ "crypto/tls"
+ "crypto/x509"
+ "errors"
+ "log"
+ "net"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "golang.org/x/crypto/acme/autocert"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+func wildcardServeOnlyEnv(t *testing.T) {
+ t.Helper()
+ t.Setenv("CARLOS_WILDCARD_DOMAINS", "wildcard.example.test")
+ t.Setenv("PLATFORM_APPS_DOMAIN", "")
+ t.Setenv("CARLOS_DNS_PROVIDER", "")
+ t.Setenv("CARLOS_DNSIMPLE_TOKEN", "")
+ t.Setenv("CARLOS_ACME_DELEGATION_ZONE", "")
+ t.Setenv("CARLOS_STEERING_ZONE_ID", "")
+}
+
+func TestWildcardNonIssuerWithoutDNSServesMirroredTLS(t *testing.T) {
+ wildcardServeOnlyEnv(t)
+ const parent = "wildcard.example.test"
+ const host = "x." + parent
+ store := release.NewDirStore(t.TempDir())
+ rec := provision.Record{Schema: 1, Host: host, Slug: "x", App: "shop", Account: release.OpsAccount, Channel: "stable", Region: "uk", Pool: "ha", HA: true}
+ if err := provision.PutManifest(t.Context(), store, rec.Account, rec.App, provision.Manifest{Schema: 1, App: rec.App, Domain: parent}); err != nil {
+ t.Fatal(err)
+ }
+ if err := provision.CreateRecord(t.Context(), store, rec); err != nil {
+ t.Fatal(err)
+ }
+ view := provision.NewRecordView(store, release.NewChangeWatch(store))
+ if err := view.Rebuild(t.Context()); err != nil {
+ t.Fatal(err)
+ }
+ routes, err := view.Snapshot().HAIngressRoutes("uk")
+ if err != nil {
+ t.Fatal(err)
+ }
+ route, ok := routes[host]
+ if !ok {
+ t.Fatal("record view did not produce the host route")
+ }
+ route.Hibernate = true
+ route.Socket = "/run/carlos/" + host + ".sock"
+ reg := openReg(t)
+ if err := reg.Upsert(route); err != nil {
+ t.Fatal(err)
+ }
+ e, err := New(reg, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(e.probers.Stop)
+ e.staticStore = store
+ WithIssuerGate(func() (context.Context, bool) { return nil, false })(e)
+ if !e.Allowed(host) {
+ t.Fatal("edge must admit the provisioned host")
+ }
+ local := autocert.DirCache(t.TempDir())
+ cache := e.armCertIssuance(local)
+ mirror := e.newCertMirror(local)
+ cert, key := selfSignedWildcard(t, parent, time.Now().Add(60*24*time.Hour))
+ raw, err := encodeWildcardPEM(cert, key)
+ if err != nil {
+ t.Fatal(err)
+ }
+ putBucket(t, store, wildcardPEMKey(parent), raw)
+ if err := mirror.Refresh(t.Context()); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := local.Get(t.Context(), wildcardPEMKey(parent)); err != nil {
+ t.Fatalf("mirror did not populate the local wildcard cache: %v", err)
+ }
+ get := mirror.wrapGetCertificate(offlineManager(t, cache, e.issuerHostPolicy()).TLSConfig().GetCertificate)
+ if w := newWildcardFromEnv(e, cache); w != nil {
+ get = wrapGetCertificate(w, get)
+ e.OnReload(w.refresh)
+ attachWildcardToMirror(mirror, w)
+ w.refresh()
+ w.pass()
+ }
+ server := httptest.NewUnstartedServer(e)
+ server.TLS = &tls.Config{GetCertificate: get, MinVersion: tls.VersionTLS12}
+ server.StartTLS()
+ t.Cleanup(server.Close)
+ roots := x509.NewCertPool()
+ roots.AddCert(cert.Leaf)
+ conn, err := tls.DialWithDialer(&net.Dialer{Timeout: 5 * time.Second}, "tcp", server.Listener.Addr().String(), &tls.Config{ServerName: host, RootCAs: roots, MinVersion: tls.VersionTLS12})
+ if err != nil {
+ t.Fatalf("non-issuer without DNS must serve the mirrored wildcard for %s: %v", host, err)
+ }
+ defer conn.Close()
+ if got := conn.ConnectionState().PeerCertificates[0].Subject.CommonName; got != "*."+parent {
+ t.Fatalf("peer CN = %q, want %q", got, "*."+parent)
+ }
+}
+
+func TestWildcardWithoutDNSNeverOrders(t *testing.T) {
+ for _, held := range []bool{false, true} {
+ name := "non-issuer"
+ if held {
+ name = "issuer"
+ }
+ t.Run(name, func(t *testing.T) {
+ ca, requests := countingCA(t)
+ cache := autocert.DirCache(t.TempDir())
+ w := newWildcardManager([]string{"a.example.test", "b.example.test"}, "", func() []string { return nil }, nil, "", cache, nil)
+ w.directory = ca.URL
+ w.issuer = func() (context.Context, bool) { return context.Background(), held }
+ w.refresh()
+ var logs bytes.Buffer
+ old := log.Writer()
+ log.SetOutput(&logs)
+ defer log.SetOutput(old)
+ now := time.Now()
+ clock := func() time.Time { return now }
+ for i := 0; i < 3; i++ {
+ if delay := w.passWithClock(clock); delay != time.Hour {
+ t.Errorf("pass delay = %s, want 1h", delay)
+ }
+ }
+ if n := requests.Load(); n != 0 {
+ t.Errorf("CA received %d requests without a DNS provider", n)
+ }
+ if _, err := cache.Get(t.Context(), wildcardAccountKeyName); !errors.Is(err, autocert.ErrCacheMiss) {
+ t.Errorf("obtain touched the ACME account key: %v", err)
+ }
+ for _, p := range w.currentParents() {
+ if w.backingOff(p, time.Now()) {
+ t.Errorf("stand-down armed the order failure backoff for %s", p)
+ }
+ }
+ want := "no DNS provider"
+ if held {
+ want = "holds the issuer lease but cannot renew wildcards"
+ }
+ if got := strings.Count(logs.String(), want); got != 1 {
+ t.Errorf("stand-down diagnostic count = %d, want 1 across parents and passes: %s", got, logs.String())
+ }
+ now = now.Add(time.Hour - time.Nanosecond)
+ w.passWithClock(clock)
+ if got := strings.Count(logs.String(), want); got != 1 {
+ t.Errorf("stand-down repeated before the hourly window: %s", logs.String())
+ }
+ now = now.Add(time.Nanosecond)
+ w.passWithClock(clock)
+ if got := strings.Count(logs.String(), want); got != 2 {
+ t.Errorf("stand-down diagnostic count = %d, want 2 after one hour: %s", got, logs.String())
+ }
+ })
+ }
+}
+
+func TestWildcardUngatedWithoutDNSStaysOff(t *testing.T) {
+ wildcardServeOnlyEnv(t)
+ for _, noStore := range []bool{false, true} {
+ e, err := New(openReg(t), nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(e.probers.Stop)
+ cache := autocert.Cache(autocert.DirCache(t.TempDir()))
+ if noStore {
+ WithIssuerGate(func() (context.Context, bool) { return nil, false })(e)
+ cache = e.armCertIssuance(cache)
+ }
+ if w := newWildcardFromEnv(e, cache); w != nil {
+ t.Fatalf("ungated edge built a wildcard manager without DNS (no store: %v)", noStore)
+ }
+ }
+}
+
+func TestDevboxWithoutDNSStillRefuses(t *testing.T) {
+ wildcardServeOnlyEnv(t)
+ t.Setenv("CARLOS_CERT_CACHE", "")
+ ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
+ defer cancel()
+ err := ServeDevbox(ctx, DevboxConfig{Registry: openReg(t), ListenAddr: "127.0.0.1:0", CertDir: t.TempDir(), SpoolRoot: t.TempDir()})
+ if err == nil || !strings.Contains(err.Error(), "devbox mode needs CARLOS_DNS_PROVIDER and its credential") || ctx.Err() != nil {
+ t.Fatalf("devbox without DNS = %v (context %v), want immediate provider refusal", err, ctx.Err())
+ }
+}