| 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) |
| + } |
| +} |