carlos / platform Public

edge: fresh DNS look in the issuer's cert warm gate (#724)

The issuer's cached resolver (and the VPC resolver behind it) can hold a
"no such name" for a new customer hostname for the zone's SOA minimum, so
the cert waited that long after the record existed. When the cached
lookup has no answer, the gate now takes one fresh look through
domains.FreshResolver.ResolveAll and applies the same all-in-the-edge-set
rule, both families. The empty edge set still refuses first; a positive
cached answer still decides alone.

Bounds are hard counts held by the caller: 10 fresh looks per pass, 4 per
probe. Only names in their first hour of DNS misses take one. Failed
looks back off per name for 5 minutes inside FreshResolver; an answer
from that backoff asks nobody and does not spend the count. Nothing is
retained between calls.

A probe that sees a name pointing here hands the pass it triggers the
names that pointed and its start time: the pass gates those first and
judges age as of the probe's start, so the pass repeats the probe's look
and orders the name. newIssuerWarm is the production construction.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev d3e313f2fa536f2aad4d9ee7c6d76f812601734b parent 1b459f7
3 files changed, +717 −19
  • internal/edge/certwarm.go +218 −17
  • internal/edge/certwarm_fresh_test.go +497 −0
  • internal/edge/edge.go +2 −2
diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go
index 789903d3e..bb98aaccc 100644
--- a/internal/edge/certwarm.go
+++ b/internal/edge/certwarm.go
@@ -38,6 +38,7 @@ import (
"golang.org/x/crypto/acme"
"github.com/carlosframework/platform/internal/certwarm"
+ "github.com/carlosframework/platform/internal/domains"
"github.com/carlosframework/platform/internal/release"
)
@@ -78,6 +79,15 @@ const (
warmDNSFastFor = 10 * time.Minute
warmDNSRetrySlow = 5 * time.Minute
warmDNSSlowFor = time.Hour
+ // The fresh look's bounds (#724, issuerDNSGate). Hard counts, not
+ // rates: a pass or probe starts with its count and never gets more,
+ // however long it runs. Only names in their first warmDNSSlowFor of
+ // misses take one (freshYoung). A successful look is never held back; a
+ // failed one makes FreshResolver answer that name from its backoff,
+ // asking nobody, for warmFreshBackoff.
+ warmFreshPerPass = 10
+ warmFreshPerProbe = 4
+ warmFreshBackoff = 5 * time.Minute
// warmGateRecheck is how often the loop looks at the issuer gate between
// passes (#386). The gate is the housekeeper lease, held in memory, so a
// look costs nothing; a box that takes the lease over (or takes it BACK
@@ -132,6 +142,11 @@ type certWarm struct {
// Ordering for a half-pointed name burns the shared account's
// failed-validation budget; see dnsGate.
pointsHere func(host string) bool
+ // freshGate, when set, is used instead of pointsHere: the same test,
+ // plus one fresh look at the name's zone when the cache has no answer,
+ // taken from the caller's count (issuerDNSGate, #724). A nil count
+ // takes none. newIssuerWarm sets it; nil keeps the cached gate alone.
+ freshGate func(host string, fresh *freshCount) bool
// issuer gates ORDERING, exactly as it gates the wildcard manager's.
issuer IssuerGate
// store is the deployment bucket, for the durable outcome records
@@ -288,7 +303,10 @@ func (c *certWarm) loop(stop <-chan struct{}) {
// without moving the schedule, and this loop would come
// straight back to the same overdue re-check until gateDue.
if c.isIssuer() {
- full = c.probeDNS()
+ if d, ran := c.probeThenPass(); ran {
+ passDue = time.Now().Add(d)
+ issuer = c.isIssuer()
+ }
} else {
issuer = false
}
@@ -301,8 +319,10 @@ func (c *certWarm) loop(stop <-chan struct{}) {
}
}
-func (c *certWarm) runPass() time.Duration {
- d := c.pass()
+func (c *certWarm) runPass() time.Duration { return c.runPassAfter(probed{}) }
+
+func (c *certWarm) runPassAfter(p probed) time.Duration {
+ d := c.passAfter(p)
if c.passDone != nil {
c.passDone()
}
@@ -398,27 +418,107 @@ func (c *certWarm) dnsWait() (time.Duration, bool) {
// the answer, so a host that points but cannot be ordered yet (the order cap)
// is looked at again on its schedule rather than in a loop.
func (c *certWarm) probeDNS() bool {
+ return len(c.probe().pointed) > 0
+}
+
+// probed is what a probe hands the pass it triggers: the names that pointed
+// here, and when the probe started.
+type probed struct {
+ pointed []string
+ at time.Time
+}
+
+// probe is probeDNS, reporting which hosts pointed. It takes at most
+// warmFreshPerProbe fresh looks, youngest names first: the newest attach is
+// the likeliest to have just had its record added, and the oldest are about
+// to leave the schedule anyway.
+func (c *certWarm) probe() probed {
if !c.isIssuer() {
- return false
+ return probed{}
}
now := c.now()
c.mu.Lock()
var due []string
+ first := map[string]time.Time{}
for h, m := range c.dnsMisses {
if !m.next.IsZero() && !now.Before(m.next) {
due = append(due, h)
+ first[h] = m.first
}
}
c.mu.Unlock()
- sort.Strings(due)
- points := false
+ sort.Slice(due, func(i, j int) bool {
+ if fi, fj := first[due[i]], first[due[j]]; !fi.Equal(fj) {
+ return fi.After(fj)
+ }
+ return due[i] < due[j]
+ })
+ fresh := &freshCount{left: warmFreshPerProbe}
+ out := probed{at: now}
for _, h := range due {
- if c.pointsHere(h) {
- points = true
+ if c.gateOpen(h, fresh, now) {
+ out.pointed = append(out.pointed, h)
}
c.dnsMissed(h, now)
}
- return points
+ return out
+}
+
+// probeThenPass is the loop's DNS re-check: a probe, and when a pending host
+// now points here, the pass it calls for. That pass gates the hosts that
+// pointed first and judges fresh-look age as of the probe's start (see
+// passAfter), so a name the probe saw pointing here on a fresh look is
+// ordered by it, however many other new names the set holds and however
+// slow the lookups in between. Only the order cap can still hold it back.
+func (c *certWarm) probeThenPass() (time.Duration, bool) {
+ p := c.probe()
+ if len(p.pointed) == 0 {
+ return 0, false
+ }
+ return c.runPassAfter(p), true
+}
+
+// freshCount is how many fresh looks one pass or probe may still take. It
+// starts at the caller's bound and is never refilled; the one exception is
+// giveBack, for an answer that asked nobody.
+type freshCount struct{ left int }
+
+// take spends one look, if any are left. Safe on nil, which has none.
+func (f *freshCount) take() bool {
+ if f == nil || f.left <= 0 {
+ return false
+ }
+ f.left--
+ return true
+}
+
+// giveBack returns a look that asked nobody (an answer from the fresh
+// resolver's failure backoff).
+func (f *freshCount) giveBack() { f.left++ }
+
+// gateOpen is the DNS gate for a new order of h. With a fresh gate, h may
+// spend a fresh look from fresh only while it is young as of asOf.
+func (c *certWarm) gateOpen(h string, fresh *freshCount, asOf time.Time) bool {
+ if c.freshGate == nil {
+ return c.pointsHere(h)
+ }
+ if !c.freshYoung(h, asOf) {
+ fresh = nil
+ }
+ return c.freshGate(h, fresh)
+}
+
+// freshYoung reports whether h may take a fresh look: it has never missed
+// the DNS gate, or its first miss was less than warmDNSSlowFor before asOf.
+// Past that, the name falls back to the cached gate, so one that never
+// points here stops costing fresh lookups. The schedule lives in memory: an
+// issuer restart gives each unwarmed name one more young hour, within the
+// per-pass count.
+func (c *certWarm) freshYoung(h string, asOf time.Time) bool {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ m, ok := c.dnsMisses[h]
+ return !ok || asOf.Sub(m.first) < warmDNSSlowFor
}
// orderBudget is how many new orders the window still allows, after
@@ -437,8 +537,23 @@ func (c *certWarm) orderBudget(now time.Time) int {
}
// pass warms every host in the set once and returns how long to wait.
-func (c *certWarm) pass() time.Duration {
+func (c *certWarm) pass() time.Duration { return c.passAfter(probed{}) }
+
+// passAfter is pass, triggered by probe p (or by nothing, the zero p). The
+// hosts p saw pointing here are gated first, and fresh-look age is judged as
+// of p's start, so the fresh look that found a name pointing here in the
+// probe is repeated here rather than lost to the count or the clock. The
+// pass takes at most warmFreshPerPass fresh looks.
+func (c *certWarm) passAfter(p probed) time.Duration {
hosts := c.hosts()
+ if len(p.pointed) > 0 {
+ hosts = pointedFirst(hosts, p.pointed)
+ }
+ asOf := p.at
+ if asOf.IsZero() {
+ asOf = c.now()
+ }
+ fresh := &freshCount{left: warmFreshPerPass}
// Stand down before ANY work: unlike the wildcard manager there is no
// cache to load first, so a non-issuer has nothing useful to do here.
// The gate comes BEFORE the empty-set check, not after, because the
@@ -499,7 +614,7 @@ func (c *certWarm) pass() time.Duration {
c.discharge(h)
continue
}
- if !c.pointsHere(h) {
+ if !c.gateOpen(h, fresh, asOf) {
// Handed to the DNS-miss schedule.
c.discharge(h)
c.dnsMissed(h, now)
@@ -580,6 +695,28 @@ func (c *certWarm) pass() time.Duration {
return delay
}
+// pointedFirst is hosts with the ones in pointed moved to the front, each
+// group in its original order. Names in pointed but not in hosts have left
+// the set and are not added back.
+func pointedFirst(hosts, pointed []string) []string {
+ want := make(map[string]bool, len(pointed))
+ for _, h := range pointed {
+ want[h] = true
+ }
+ out := make([]string, 0, len(hosts))
+ for _, h := range hosts {
+ if want[h] {
+ out = append(out, h)
+ }
+ }
+ for _, h := range hosts {
+ if !want[h] {
+ out = append(out, h)
+ }
+ }
+ return out
+}
+
// settling puts a held name back for when its hold ends. nextAttempt is the
// floor pass() and hasNewHost already honour, so the name is neither retried
// early nor counted as new by a wake in the meantime; settleUntil is what
@@ -774,6 +911,25 @@ func dnsGate(resolve func(string) ([]string, error), edgeIPs func() []string) fu
}
func dnsGateAt(resolve func(string) ([]string, error), edgeIPs func() []string, now func() time.Time) func(string) bool {
+ gate := issuerDNSGateAt(resolve, nil, edgeIPs, now)
+ return func(host string) bool { return gate(host, nil) }
+}
+
+// issuerDNSGate is dnsGate with a fresh look (#724). The issuer's cached
+// resolver can hold a "no such name" for a new customer hostname for the
+// zone's SOA minimum, and so can the upstream resolver behind it, so the
+// cert would wait that long after the customer's record exists. When the
+// cached lookup fails or returns no addresses, and the caller's count
+// allows, the gate asks fresh (the zone's own nameservers, every address of
+// both families) and applies the SAME rule to that answer. A positive cached
+// answer decides alone, even one pointing elsewhere. The empty edge set
+// still refuses first, before any lookup. Nothing is kept between calls:
+// each look stands alone.
+func issuerDNSGate(resolve, fresh func(string) ([]string, error), edgeIPs func() []string) func(string, *freshCount) bool {
+ return issuerDNSGateAt(resolve, fresh, edgeIPs, time.Now)
+}
+
+func issuerDNSGateAt(resolve, fresh func(string) ([]string, error), edgeIPs func() []string, now func() time.Time) func(string, *freshCount) bool {
var (
mu sync.Mutex
cached map[string]bool
@@ -796,24 +952,69 @@ func dnsGateAt(resolve func(string) ([]string, error), edgeIPs func() []string,
readAt = now()
return cached
}
- return func(host string) bool {
+ allOurs := func(set map[string]bool, ips []string) bool {
+ for _, ip := range ips {
+ if !set[ip] {
+ return false
+ }
+ }
+ return len(ips) > 0
+ }
+ return func(host string, count *freshCount) bool {
set := edgeSet()
if len(set) == 0 {
return false
}
ips, err := resolve(host)
- if err != nil || len(ips) == 0 {
+ if err == nil && len(ips) > 0 {
+ return allOurs(set, ips)
+ }
+ if fresh == nil || !count.take() {
return false
}
- for _, ip := range ips {
- if !set[ip] {
- return false
- }
+ ips, err = fresh(host)
+ if errors.Is(err, domains.ErrBackingOff) {
+ count.giveBack()
+ return false
+ }
+ if err != nil || !allOurs(set, ips) {
+ return false
}
+ log.Printf("edge: cert warm: fresh DNS look says %s points here (cache had no answer)", host)
return true
}
}
+// warmCachedLookup and warmFreshResolver are the issuer warm's two DNS
+// sources. Variables so a test can drive newIssuerWarm, the production
+// construction, with fakes under it.
+var (
+ warmCachedLookup = warmResolver
+ warmFreshResolver freshAllResolver = &domains.FreshResolver{
+ LookupNS: net.DefaultResolver.LookupNS,
+ LookupHost: net.DefaultResolver.LookupHost,
+ LookupCNAME: net.DefaultResolver.LookupCNAME,
+ Backoff: warmFreshBackoff,
+ }
+)
+
+// freshAllResolver is the fresh look the warm needs: every address of both
+// families (domains.FreshResolver.ResolveAll), because a CA validating over
+// IPv6 against a foreign AAAA fails even when every A record is ours.
+type freshAllResolver interface {
+ ResolveAll(host string) ([]string, error)
+}
+
+// newIssuerWarm is the cert warm as the edge runs it with a deployment
+// store: the DNS gate over the cached resolver, with the fresh look (#724)
+// when the cache has no answer.
+func newIssuerWarm(hosts func() []string, getCert func(*tls.ClientHelloInfo) (*tls.Certificate, error), edgeIPs func() []string, issuer IssuerGate, store release.Store) *certWarm {
+ gate := issuerDNSGate(warmCachedLookup, warmFreshResolver.ResolveAll, edgeIPs)
+ w := newCertWarm(hosts, getCert, func(h string) bool { return gate(h, nil) }, issuer, store)
+ w.freshGate = gate
+ return w
+}
+
// warmResolver is the DNS gate's lookup: the system resolver, bounded so a
// hung one costs this host its turn in the pass and not the pass itself.
func warmResolver(host string) ([]string, error) {
diff --git a/internal/edge/certwarm_fresh_test.go b/internal/edge/certwarm_fresh_test.go
new file mode 100644
index 000000000..0186288ad
--- /dev/null
+++ b/internal/edge/certwarm_fresh_test.go
@@ -0,0 +1,497 @@
+package edge
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "net"
+ "sort"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/domains"
+)
+
+// #724: the issuer's own resolver can hold a "no such name" for a new
+// customer hostname for the zone's SOA minimum (the app is homed on the
+// issuer, a single-box deployment, or something looked the name up before
+// the record existed). The gate then takes one fresh look, bounded by a hard
+// count per pass and per probe and only for young names. These tests build
+// the warm with newIssuerWarm, the production construction, with fakes under
+// its two DNS sources.
+
+const (
+ edgeV4 = "203.0.113.7"
+ edgeV6 = "2001:db8::7"
+)
+
+// dnsFake is one DNS source: a name's addresses, or "no such host" when it
+// has none, or a set error. tick runs inside every lookup (a slow resolver,
+// or a clock that jumps).
+type dnsFake struct {
+ mu sync.Mutex
+ answers map[string][]string
+ errs map[string]error
+ calls map[string]int
+ total int
+ tick func()
+}
+
+func newDNSFake() *dnsFake {
+ return &dnsFake{answers: map[string][]string{}, errs: map[string]error{}, calls: map[string]int{}}
+}
+
+func (f *dnsFake) lookup(h string) ([]string, error) {
+ f.mu.Lock()
+ f.calls[h]++
+ f.total++
+ ips, ok := f.answers[h]
+ err := f.errs[h]
+ tick := f.tick
+ f.mu.Unlock()
+ if tick != nil {
+ tick()
+ }
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, &net.DNSError{Err: "no such host", Name: h, IsNotFound: true}
+ }
+ return ips, nil
+}
+
+func (f *dnsFake) ResolveAll(h string) ([]string, error) { return f.lookup(h) }
+
+func (f *dnsFake) set(h string, ips ...string) {
+ f.mu.Lock()
+ f.answers[h] = ips
+ f.mu.Unlock()
+}
+
+func (f *dnsFake) fail(h string, err error) {
+ f.mu.Lock()
+ f.errs[h] = err
+ f.mu.Unlock()
+}
+
+func (f *dnsFake) setTick(tick func()) {
+ f.mu.Lock()
+ f.tick = tick
+ f.mu.Unlock()
+}
+
+func (f *dnsFake) count(h string) int {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ return f.calls[h]
+}
+
+func (f *dnsFake) sum() int {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ return f.total
+}
+
+func (f *fakeClock) set(t time.Time) {
+ f.mu.Lock()
+ f.t = t
+ f.mu.Unlock()
+}
+
+type issuerHarness struct {
+ w *certWarm
+ clock *fakeClock
+ cached, fresh *dnsFake
+ mu sync.Mutex
+ hosts []string
+ edges []string
+ ordered []string
+}
+
+func (h *issuerHarness) addHost(name string) {
+ h.mu.Lock()
+ h.hosts = append(h.hosts, name)
+ h.mu.Unlock()
+}
+
+func (h *issuerHarness) setEdges(ips ...string) {
+ h.mu.Lock()
+ h.edges = ips
+ h.mu.Unlock()
+}
+
+func (h *issuerHarness) orders() []string {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ return append([]string(nil), h.ordered...)
+}
+
+func (h *issuerHarness) orderedHost(name string) bool {
+ for _, o := range h.orders() {
+ if o == name {
+ return true
+ }
+ }
+ return false
+}
+
+func (h *issuerHarness) firstMiss(name string) (dnsMiss, bool) {
+ h.w.mu.Lock()
+ defer h.w.mu.Unlock()
+ m, ok := h.w.dnsMisses[name]
+ return m, ok
+}
+
+// newIssuerHarness builds the production issuer warm over hosts, with the
+// cached and fresh sources swapped for fakes (both "no such host" for every
+// name until set), an edge set of edgeV4 and edgeV6, and a fake clock.
+func newIssuerHarness(t *testing.T, hosts ...string) *issuerHarness {
+ t.Helper()
+ fresh := newDNSFake()
+ h := buildIssuerHarness(t, fresh, hosts)
+ h.fresh = fresh
+ return h
+}
+
+// buildIssuerHarness is newIssuerHarness over any fresh source.
+func buildIssuerHarness(t *testing.T, fresh freshAllResolver, hosts []string) *issuerHarness {
+ t.Helper()
+ h := &issuerHarness{
+ clock: &fakeClock{t: time.Unix(1_800_000_000, 0)},
+ cached: newDNSFake(),
+ hosts: hosts,
+ edges: []string{edgeV4, edgeV6},
+ }
+ oldC, oldF := warmCachedLookup, warmFreshResolver
+ t.Cleanup(func() { warmCachedLookup, warmFreshResolver = oldC, oldF })
+ warmCachedLookup = h.cached.lookup
+ warmFreshResolver = fresh
+ h.w = newIssuerWarm(
+ // Sorted, as Edge.warmSet returns it.
+ func() []string {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ out := append([]string(nil), h.hosts...)
+ sort.Strings(out)
+ return out
+ },
+ func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
+ h.mu.Lock()
+ h.ordered = append(h.ordered, hello.ServerName)
+ h.mu.Unlock()
+ return &tls.Certificate{}, nil
+ },
+ func() []string {
+ h.mu.Lock()
+ defer h.mu.Unlock()
+ return append([]string(nil), h.edges...)
+ },
+ nil,
+ nil,
+ )
+ h.w.now = h.clock.now
+ return h
+}
+
+func hostNames(prefix string, n int) []string {
+ var out []string
+ for i := 0; i < n; i++ {
+ out = append(out, fmt.Sprintf("%s%02d.customer.test", prefix, i))
+ }
+ return out
+}
+
+// The fix itself: the cache says "no such host", the zone says the name
+// points here, and the first pass orders it.
+func TestIssuerWarmFreshLookOrdersANameTheCacheMissed(t *testing.T) {
+ h := newIssuerHarness(t, "shop.example.com")
+ h.fresh.set("shop.example.com", edgeV4)
+ h.w.pass()
+ if !h.orderedHost("shop.example.com") {
+ t.Fatalf("ordered %v; want shop.example.com on the fresh look", h.orders())
+ }
+ if n := h.fresh.count("shop.example.com"); n != 1 {
+ t.Fatalf("fresh looks = %d, want 1", n)
+ }
+}
+
+// A positive cached answer decides on its own, even one pointing elsewhere:
+// the fresh look is only for a cache that has no answer at all.
+func TestIssuerWarmTakesNoFreshLookOnACachedPositive(t *testing.T) {
+ h := newIssuerHarness(t, "elsewhere.example.com", "here.example.com")
+ h.cached.set("elsewhere.example.com", "192.0.2.1")
+ h.cached.set("here.example.com", edgeV4)
+ h.fresh.set("elsewhere.example.com", edgeV4)
+ h.fresh.set("here.example.com", edgeV4)
+ h.w.pass()
+ for i := 0; i < 15; i++ {
+ h.clock.advance(warmDNSRetryFast)
+ h.w.probeDNS()
+ }
+ h.w.pass()
+ // here.example.com is asked for again by the second pass (its renewal
+ // re-arm); elsewhere.example.com never is.
+ if h.orderedHost("elsewhere.example.com") || !h.orderedHost("here.example.com") {
+ t.Fatalf("ordered %v, want only here.example.com", h.orders())
+ }
+ if n := h.fresh.sum(); n != 0 {
+ t.Fatalf("fresh looks = %d with a positive cached answer, want 0", n)
+ }
+}
+
+// An empty edge set refuses before either source is asked, even when the
+// fresh look would say the name points here.
+func TestIssuerWarmEmptyEdgeSetRefusesBeforeAnyLookup(t *testing.T) {
+ h := newIssuerHarness(t, "shop.example.com")
+ h.setEdges()
+ h.fresh.set("shop.example.com", edgeV4)
+ h.w.pass()
+ h.clock.advance(warmDNSRetryFast)
+ if h.w.probeDNS() {
+ t.Fatal("probe reported the name pointing here with an empty edge set")
+ }
+ h.w.pass()
+ if len(h.orders()) != 0 || h.cached.sum() != 0 || h.fresh.sum() != 0 {
+ t.Fatalf("ordered %v, cached lookups %d, fresh looks %d; want none of each",
+ h.orders(), h.cached.sum(), h.fresh.sum())
+ }
+}
+
+// The fresh answer meets the same rule as the cached one: every address, of
+// both families, in the edge set. (ResolveAll itself refuses an IPv6-only
+// name and an unresolved CNAME-target AAAA; see freshdns_test.go.)
+func TestIssuerWarmFreshAnswerMustBeAllOurs(t *testing.T) {
+ for name, tc := range map[string]struct {
+ answer []string
+ err error
+ order bool
+ }{
+ "A ours": {answer: []string{edgeV4}, order: true},
+ "A and AAAA ours": {answer: []string{edgeV4, edgeV6}, order: true},
+ "A ours, AAAA elsewhere": {answer: []string{edgeV4, "2001:db8::66"}},
+ "A elsewhere": {answer: []string{"192.0.2.1"}},
+ "IPv6 only, elsewhere": {answer: []string{"2001:db8::66"}},
+ "A ours and A elsewhere": {answer: []string{edgeV4, "192.0.2.1"}},
+ "no addresses": {answer: []string{}},
+ "lookup failed": {err: fmt.Errorf("fresh dns: no authoritative answer")},
+ "AAAA of the target failed": {err: fmt.Errorf("fresh dns: in-zone CNAME target with no records of that type")},
+ } {
+ h := newIssuerHarness(t, "shop.example.com")
+ if tc.err != nil {
+ h.fresh.fail("shop.example.com", tc.err)
+ } else {
+ h.fresh.set("shop.example.com", tc.answer...)
+ }
+ h.w.pass()
+ if got := h.orderedHost("shop.example.com"); got != tc.order {
+ t.Errorf("%s: ordered = %v, want %v", name, got, tc.order)
+ }
+ }
+}
+
+// A pass takes at most warmFreshPerPass fresh looks, however long they take
+// and whatever the clock does meanwhile.
+func TestIssuerWarmPassTakesAtMostItsCountOfFreshLooks(t *testing.T) {
+ h := newIssuerHarness(t, hostNames("n", 30)...)
+ h.fresh.setTick(func() { h.clock.advance(10 * time.Minute) })
+ h.w.pass()
+ if n := h.fresh.sum(); n != warmFreshPerPass {
+ t.Fatalf("one pass took %d fresh looks, want exactly %d", n, warmFreshPerPass)
+ }
+}
+
+// A probe takes at most warmFreshPerProbe, however long they take.
+func TestIssuerWarmProbeTakesAtMostItsCountOfFreshLooks(t *testing.T) {
+ h := newIssuerHarness(t, hostNames("n", 30)...)
+ h.w.pass()
+ before := h.fresh.sum()
+ h.fresh.setTick(func() { h.clock.advance(time.Hour) })
+ h.clock.advance(warmDNSRetryFast)
+ h.w.probeDNS()
+ if n := h.fresh.sum() - before; n != warmFreshPerProbe {
+ t.Fatalf("one probe took %d fresh looks, want exactly %d", n, warmFreshPerProbe)
+ }
+}
+
+// An answer from inside a name's failure backoff asked nobody, so it does not
+// use up the count: the names behind it still get their looks.
+func TestIssuerWarmBackedOffAnswersSpendNoFreshLook(t *testing.T) {
+ hosts := hostNames("n", 20)
+ h := newIssuerHarness(t, hosts...)
+ for _, n := range hosts[:5] {
+ h.fresh.fail(n, fmt.Errorf("fresh dns: %w for %s", domains.ErrBackingOff, n))
+ }
+ h.w.pass()
+ real := 0
+ for _, n := range hosts[5:] {
+ real += h.fresh.count(n)
+ }
+ if real != warmFreshPerPass {
+ t.Fatalf("real fresh looks behind five backed-off names = %d, want %d", real, warmFreshPerPass)
+ }
+}
+
+// Fresh looks are for a name's first warmDNSSlowFor of misses. A name past
+// it, with or without probes in between, takes none, so a name that never
+// points here stops costing fresh lookups after an hour.
+func TestIssuerWarmNamesPastTheirFirstHourTakeNoFreshLook(t *testing.T) {
+ t.Run("hourly passes", func(t *testing.T) {
+ h := newIssuerHarness(t, "old.example.com")
+ h.w.pass()
+ if h.fresh.sum() != 1 {
+ t.Fatalf("first pass fresh looks = %d, want 1", h.fresh.sum())
+ }
+ h.clock.advance(warmDNSSlowFor)
+ h.w.pass()
+ h.clock.advance(warmInterval)
+ h.w.pass()
+ if n := h.fresh.sum(); n != 1 {
+ t.Fatalf("fresh looks after the first hour = %d, want none", n-1)
+ }
+ })
+ t.Run("probes through the hour", func(t *testing.T) {
+ h := newIssuerHarness(t, "old.example.com")
+ start := h.clock.now()
+ h.w.pass()
+ atHour := 0
+ for h.clock.now().Sub(start) < 3*time.Hour {
+ h.clock.advance(warmDNSRetryFast)
+ if h.clock.now().Sub(start) == warmDNSSlowFor {
+ atHour = h.fresh.sum()
+ }
+ h.w.probeDNS()
+ if h.clock.now().Sub(start) > warmDNSSlowFor {
+ h.w.pass()
+ }
+ }
+ if m, _ := h.firstMiss("old.example.com"); !m.first.Equal(start) {
+ t.Fatalf("first miss %v, want %v", m.first, start)
+ }
+ if atHour < 2 {
+ t.Fatalf("fresh looks in the first hour = %d, want the probes to have taken some", atHour)
+ }
+ if n := h.fresh.sum(); n != atHour {
+ t.Fatalf("an abandoned name took %d fresh looks after its first hour", n-atHour)
+ }
+ })
+}
+
+// The probe to pass handoff. A name whose cache keeps saying "no such host"
+// and whose fresh look in a probe says it points here is ordered by the pass
+// that probe triggers, even with:
+// - a dozen other young names ahead of it in the set, enough to spend the
+// pass's whole count of fresh looks before reaching it;
+// - cached lookups so slow that the name's first hour ends between the
+// probe's fresh look and the pass's.
+func TestIssuerWarmProbeFreshSuccessIsOrderedByThePassItTriggers(t *testing.T) {
+ h := newIssuerHarness(t, hostNames("a", 15)...)
+ h.w.pass()
+ // The customer's name arrives half an hour later; it misses everywhere.
+ h.clock.advance(30 * time.Minute)
+ h.addHost("z.customer.test")
+ h.w.pass()
+ if h.orderedHost("z.customer.test") {
+ t.Fatal("z ordered before its DNS existed")
+ }
+ zMiss, ok := h.firstMiss("z.customer.test")
+ if !ok {
+ t.Fatal("z is not on the DNS-miss schedule")
+ }
+ zFirst := zMiss.first
+ // More new names arrive, ahead of it in the set.
+ for _, n := range hostNames("b", 12) {
+ h.addHost(n)
+ }
+ // Walk the schedule to z's last re-check inside its first hour.
+ for {
+ m, _ := h.firstMiss("z.customer.test")
+ if m.next.IsZero() {
+ t.Fatal("z left the schedule before its record landed")
+ }
+ h.clock.set(m.next)
+ if m.next.Sub(zFirst) >= warmDNSSlowFor-warmDNSRetrySlow {
+ break
+ }
+ if _, ran := h.w.probeThenPass(); ran {
+ t.Fatal("a probe ran a pass before anything pointed here")
+ }
+ }
+ // The customer's record lands, and every cached lookup from here on
+ // takes six minutes of clock: z's own in the probe ends its first hour.
+ h.fresh.set("z.customer.test", edgeV4)
+ h.cached.setTick(func() { h.clock.advance(6 * time.Minute) })
+ freshBefore := h.fresh.count("z.customer.test")
+ if _, ran := h.w.probeThenPass(); !ran {
+ t.Fatal("the probe did not see z pointing here")
+ }
+ if !h.orderedHost("z.customer.test") {
+ t.Fatalf("ordered %v; want z.customer.test ordered by the pass its probe triggered", h.orders())
+ }
+ if n := h.fresh.count("z.customer.test") - freshBefore; n != 2 {
+ t.Fatalf("z's fresh looks in the probe and its pass = %d; want 2 (each takes its own)", n)
+ }
+}
+
+// Fresh lookups that fail (a silent nameserver) cost the pass at most their
+// timeout, never block the names behind them, and back off per name through
+// FreshResolver's own Backoff, set to warmFreshBackoff for the issuer.
+func TestIssuerWarmFreshFailuresBackOffAndDoNotStallThePass(t *testing.T) {
+ var nsAsks atomic.Int32
+ clock := &fakeClock{t: time.Unix(1_800_000_000, 0)}
+ fresh := &domains.FreshResolver{
+ LookupNS: func(ctx context.Context, name string) ([]*net.NS, error) {
+ nsAsks.Add(1)
+ <-ctx.Done()
+ return nil, ctx.Err()
+ },
+ LookupHost: func(ctx context.Context, host string) ([]string, error) { return nil, ctx.Err() },
+ Timeout: 50 * time.Millisecond,
+ Backoff: warmFreshBackoff,
+ Now: clock.now,
+ }
+ h := buildIssuerHarness(t, fresh, []string{"a.customer.test", "b.customer.test"})
+ h.clock = clock
+ h.w.now = clock.now
+ h.cached.set("b.customer.test", edgeV4)
+ began := time.Now()
+ h.w.pass()
+ if d := time.Since(began); d > 2*time.Second {
+ t.Fatalf("pass took %s behind one silent nameserver", d)
+ }
+ if !h.orderedHost("b.customer.test") {
+ t.Fatalf("ordered %v; want b.customer.test, behind the failing name", h.orders())
+ }
+ asked := nsAsks.Load()
+ if asked == 0 {
+ t.Fatal("the fresh look never asked for a.customer.test's zone")
+ }
+ h.clock.advance(warmDNSRetryFast)
+ h.w.probeDNS()
+ h.w.pass()
+ if nsAsks.Load() != asked {
+ t.Fatal("a.customer.test was asked again inside its backoff")
+ }
+ h.clock.advance(warmFreshBackoff)
+ h.w.probeDNS()
+ if nsAsks.Load() == asked {
+ t.Fatal("a.customer.test was not asked again after its backoff")
+ }
+}
+
+// The production fresh source: a zone-verified FreshResolver over the
+// system resolver, backing off a failed name for warmFreshBackoff.
+func TestIssuerWarmFreshResolverIsTheIssuerInstance(t *testing.T) {
+ r, ok := warmFreshResolver.(*domains.FreshResolver)
+ if !ok {
+ t.Fatalf("warmFreshResolver is %T, want *domains.FreshResolver", warmFreshResolver)
+ }
+ if r.LookupNS == nil || r.LookupHost == nil || r.LookupCNAME == nil {
+ t.Fatal("the issuer FreshResolver is missing a cached lookup")
+ }
+ if r.Backoff != warmFreshBackoff || warmFreshBackoff != 5*time.Minute {
+ t.Fatalf("Backoff = %s (warmFreshBackoff %s), want 5m", r.Backoff, warmFreshBackoff)
+ }
+}
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 0d012d33c..69d32bd28 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -4900,10 +4900,10 @@ func RunWithActivatorReady(args []string, act Activator, cache *Cache, ready fun
defer cancel()
return edgesfile.EdgeIPs(ctx, store, envIPs, time.Now())
}
- warm := newCertWarm(
+ warm := newIssuerWarm(
func() []string { return e.warmSet(appsDomain, parents) },
perHostCert,
- dnsGate(warmResolver, edgeIPs),
+ edgeIPs,
e.issuerGate,
store,
)