cert warm gate: per-family CNAME targets, fresh-backed names first
Astra round 1 on #724, two P1s. ResolveAll resolved an out-of-zone CNAME target with the cached LookupHost. Go's resolver (StrictErrors off) returns the A records with no error when the AAAA query fails, so an unknown IPv6 half read as "no IPv6" and could authorise an order. The target is now resolved one family at a time through a new LookupIP seam (production: net.DefaultResolver.LookupIP); a failure of either family refuses, and "not found" for ip6 after ip4 addresses is NODATA. Resolve (the stamp's, A only) is unchanged. A probe's names that pointed on a fresh look now lead probed.pointed, ahead of names that pointed on a cached answer, and pointedFirst keeps that order, so cached successes that turn into failing fresh looks in the pass cannot spend its count before the target. The gate's edge-set reuse runs on the warm's clock. Comment on what can still stop the order corrected. Tests: SERVFAIL AAAA on an out-of-zone target through a real Go resolver (domains and newIssuerWarm), per-family failures, ten cached-to-failing names ahead of the target, fresh success then fresh failure or an empty edge set across the edge-set TTL, and the loop ordering on a fresh look. Census envelopes re-pinned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+439
−51
internal/domains/freshdns.go+57 −21internal/domains/freshdns_test.go+114 −0internal/edge/certwarm.go+40 −26internal/edge/certwarm_fresh_test.go+226 −2internal/provision/testdata/plan14-root-reads-issuer-fresh-gate-724.tsv+2 −2
CI passed — run details
diff --git a/internal/domains/freshdns.go b/internal/domains/freshdns.go| index e43fbde3a..0e76f73b9 100644 |
| --- a/internal/domains/freshdns.go |
| +++ b/internal/domains/freshdns.go |
| @@ -24,7 +24,7 @@ import ( |
| // behind it may hold a "no such name" for the same SOA minimum. Asking the |
| // zone's own nameservers is the one answer no cache can hold back. |
| // |
| -// The cached resolver (LookupNS, LookupHost) is still used for the names |
| +// The cached resolver (LookupNS, LookupHost, LookupIP) is still used for the names |
| // around the answer that we are not waiting on: the zone's NS set, the |
| // nameservers' own addresses, and a CNAME target outside the zone (in |
| // practice the deployment's own edge name). |
| @@ -36,6 +36,13 @@ import ( |
| type FreshResolver struct { |
| LookupNS func(ctx context.Context, name string) ([]*net.NS, error) |
| LookupHost func(ctx context.Context, host string) ([]string, error) |
| + // LookupIP is the cached resolver for one address family ("ip4" or |
| + // "ip6"). ResolveAll uses it for an out-of-zone CNAME target, asking |
| + // each family on its own: LookupHost asks both at once and, with Go's |
| + // default StrictErrors off, returns the A records with no error when |
| + // the AAAA query fails. nil makes ResolveAll refuse such a target; |
| + // Resolve does not use it. |
| + LookupIP func(ctx context.Context, network, host string) ([]net.IP, error) |
| // LookupCNAME is the cached resolver's canonical name, used only to |
| // skip a level that is an alias; nil skips that shortcut (verifyZone |
| // still refuses a wrong zone). |
| @@ -415,21 +422,21 @@ var ( |
| // askType sends one non-recursive IN query of qtype (A or AAAA) for name to |
| // server, which verifyZone has shown holds zone, and reads the answer. A |
| // CNAME chain in the reply is followed; if it ends outside zone with no |
| -// address in the reply, the target goes to the cached LookupHost, which |
| -// answers for both families, and cached reports that. A chain that loops or |
| -// runs past freshMaxCNAMEs is not an answer. |
| -func (r *FreshResolver) askType(ctx context.Context, server netip.AddrPort, name, zone string, qtype dnsmessage.Type) (ips []string, cached bool, err error) { |
| +// address in the reply, askType returns that target, unresolved, for the |
| +// caller to resolve through the cache. A chain that loops or runs past |
| +// freshMaxCNAMEs is not an answer. |
| +func (r *FreshResolver) askType(ctx context.Context, server netip.AddrPort, name, zone string, qtype dnsmessage.Type) (ips []string, target string, err error) { |
| reply, err := r.exchange(ctx, server, name, qtype) |
| if err != nil { |
| - return nil, false, err |
| + return nil, "", err |
| } |
| fqdn := name + "." |
| switch reply.RCode { |
| case dnsmessage.RCodeSuccess: |
| case dnsmessage.RCodeNameError: |
| - return nil, false, &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true} |
| + return nil, "", &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true} |
| default: |
| - return nil, false, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name) |
| + return nil, "", fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name) |
| } |
| current := fqdn |
| @@ -447,7 +454,7 @@ func (r *FreshResolver) askType(ctx context.Context, server netip.AddrPort, name |
| break |
| } |
| if seen[next] || hops >= freshMaxCNAMEs { |
| - return nil, false, fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name) |
| + return nil, "", fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name) |
| } |
| seen[next] = true |
| current = next |
| @@ -468,31 +475,32 @@ func (r *FreshResolver) askType(ctx context.Context, server netip.AddrPort, name |
| } |
| } |
| if len(ips) > 0 { |
| - return ips, false, nil |
| + return ips, "", nil |
| } |
| - target := strings.TrimSuffix(current, ".") |
| + target = strings.TrimSuffix(current, ".") |
| if target == name { |
| - return nil, false, errNoData |
| + return nil, "", errNoData |
| } |
| if target == zone || strings.HasSuffix(target, "."+zone) { |
| // In-zone target this server gave no address for: no fallback to |
| // a cache that may hold an old one. |
| - return nil, false, errInZoneNoData |
| + return nil, "", errInZoneNoData |
| } |
| - ips, err = r.LookupHost(ctx, target) |
| - return ips, true, err |
| + return nil, target, nil |
| } |
| // ask is askType for A, with Resolve's long-standing errors: no A record at |
| // the name is "not found", and an in-zone target with no address is not an |
| // answer at all. |
| func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| - ips, _, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| + ips, target, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| switch { |
| case errors.Is(err, errNoData): |
| return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true} |
| case errors.Is(err, errInZoneNoData): |
| return nil, fmt.Errorf("%w: %s ends at in-zone target with no address", errNoAuthority, name) |
| + case err == nil && target != "": |
| + return r.LookupHost(ctx, target) |
| } |
| return ips, err |
| } |
| @@ -504,10 +512,10 @@ func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zo |
| // only at the name itself; a CNAME target the reply gives no AAAA for may |
| // sit in a delegated child this server does not answer for, so that, and |
| // any other AAAA failure, fails the whole answer. When the A half ended at an |
| -// out-of-zone target, the cached LookupHost already answered for both |
| -// families and nothing more is asked. |
| +// out-of-zone target, the target is resolved per family (targetAll) and |
| +// nothing more is asked here. |
| func (r *FreshResolver) askAll(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| - v4, cached, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| + v4, target, err := r.askType(ctx, server, name, zone, dnsmessage.TypeA) |
| switch { |
| case errors.Is(err, errNoData): |
| return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true} |
| @@ -516,8 +524,8 @@ func (r *FreshResolver) askAll(ctx context.Context, server netip.AddrPort, name, |
| case err != nil: |
| return nil, err |
| } |
| - if cached { |
| - return v4, nil |
| + if target != "" { |
| + return r.targetAll(ctx, target) |
| } |
| v6, _, err := r.askType(ctx, server, name, zone, dnsmessage.TypeAAAA) |
| switch { |
| @@ -532,3 +540,31 @@ func (r *FreshResolver) askAll(ctx context.Context, server netip.AddrPort, name, |
| } |
| return nil, err |
| } |
| + |
| +// targetAll resolves an out-of-zone CNAME target through the cache, one |
| +// family at a time, for askAll. Any failure of either family fails the |
| +// answer, and a target with no A is not one. "Not found" for ip6 after |
| +// addresses for ip4 is NODATA for a name that exists: no IPv6. |
| +func (r *FreshResolver) targetAll(ctx context.Context, target string) ([]string, error) { |
| + if r.LookupIP == nil { |
| + return nil, fmt.Errorf("%w: no per-family lookup for CNAME target %s", errNoAuthority, target) |
| + } |
| + v4, err := r.LookupIP(ctx, "ip4", target) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if len(v4) == 0 { |
| + return nil, fmt.Errorf("%w: CNAME target %s gave no IPv4 addresses", errNoAuthority, target) |
| + } |
| + v6, err := r.LookupIP(ctx, "ip6", target) |
| + if err != nil && !isNotFound(err) { |
| + return nil, err |
| + } |
| + var out []string |
| + for _, ip := range append(v4, v6...) { |
| + if a, ok := netip.AddrFromSlice(ip); ok { |
| + out = append(out, a.Unmap().String()) |
| + } |
| + } |
| + return out, nil |
| +} |
diff --git a/internal/domains/freshdns_test.go b/internal/domains/freshdns_test.go| index 40512e422..1a08f4d0e 100644 |
| --- a/internal/domains/freshdns_test.go |
| +++ b/internal/domains/freshdns_test.go |
| @@ -123,6 +123,7 @@ type world struct { |
| hosts map[string][]string // name -> addresses (the cache) |
| routes map[string]*fakeAuthority // "ip:53" -> server |
| cnames map[string]string // name -> cached canonical name |
| + ipErrs map[string]error // "ip4 name" or "ip6 name" -> LookupIP error |
| mu sync.Mutex |
| dialed []string |
| } |
| @@ -146,6 +147,23 @@ func (w *world) resolver(now *time.Time) *domains.FreshResolver { |
| } |
| return nil, errNotFound |
| }, |
| + LookupIP: func(_ context.Context, network, host string) ([]net.IP, error) { |
| + host = strings.TrimSuffix(host, ".") |
| + if err, ok := w.ipErrs[network+" "+host]; ok { |
| + return nil, err |
| + } |
| + var out []net.IP |
| + for _, a := range w.hosts[host] { |
| + ip := net.ParseIP(a) |
| + if (ip.To4() != nil) == (network == "ip4") { |
| + out = append(out, ip) |
| + } |
| + } |
| + if len(out) == 0 { |
| + return nil, errNotFound |
| + } |
| + return out, nil |
| + }, |
| LookupCNAME: func(_ context.Context, host string) (string, error) { |
| if c, ok := w.cnames[host]; ok { |
| return c, nil |
| @@ -668,3 +686,99 @@ func TestFreshResolverResolveAllBacksOffPerName(t *testing.T) { |
| t.Fatal("ResolveAll after Backoff did not query again") |
| } |
| } |
| + |
| +// recursive is a stand-in for the box's recursive resolver, answering |
| +// every A and AAAA query through answer, for driving Go's own resolver. |
| +func recursive(t *testing.T, answer func(q dnsmessage.Question, r *dnsmessage.Message) bool) *net.Resolver { |
| + rec := startAuthority(t, "", func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.RecursionAvailable = true |
| + return answer(q, r) |
| + }) |
| + return &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| + var d net.Dialer |
| + return d.DialContext(ctx, "udp", rec.addr) |
| + }} |
| +} |
| + |
| +// Astra #724 R1 P1-1: an out-of-zone CNAME target is resolved one family |
| +// at a time. Go's resolver, asked for both at once, returns the A records |
| +// with no error when the AAAA query fails (StrictErrors is off), so an |
| +// unknown IPv6 half would read as "no IPv6". With A ours and AAAA SERVFAIL |
| +// for the target, ResolveAll refuses. |
| +func TestFreshResolverResolveAllRefusesATargetWhoseAAAAFailed(t *testing.T) { |
| + fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "target.other.test."))) |
| + res := recursive(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + if q.Type == dnsmessage.TypeAAAA { |
| + r.RCode = dnsmessage.RCodeServerFailure |
| + return true |
| + } |
| + r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| + return true |
| + }) |
| + // The premise: the both-families lookup hides the failure. |
| + if ips, err := res.LookupHost(context.Background(), "target.other.test"); err != nil || len(ips) != 1 { |
| + t.Fatalf("premise: LookupHost = %v, %v; want the A record and no error", ips, err) |
| + } |
| + r := oneZone(fa).resolver(clock()) |
| + r.LookupIP = res.LookupIP |
| + if ips, err := r.ResolveAll("shop.example.com"); err == nil { |
| + t.Fatalf("ResolveAll = %v, want an error: the target's AAAA is unknown", ips) |
| + } |
| + // Resolve (A only, the stamp's) still answers. |
| + r = oneZone(fa).resolver(clock()) |
| + cachedHost := r.LookupHost |
| + r.LookupHost = func(ctx context.Context, host string) ([]string, error) { |
| + if strings.TrimSuffix(host, ".") == "target.other.test" { |
| + return res.LookupHost(ctx, host) |
| + } |
| + return cachedHost(ctx, host) |
| + } |
| + if ips, err := r.Resolve("shop.example.com"); err != nil || len(ips) != 1 { |
| + t.Fatalf("Resolve = %v, %v; want the A record", ips, err) |
| + } |
| +} |
| + |
| +// A target that exists and has no AAAA (NODATA, "not found" for ip6 after |
| +// addresses for ip4) is complete: A only. |
| +func TestFreshResolverResolveAllAcceptsATargetWithNoAAAA(t *testing.T) { |
| + fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "target.other.test."))) |
| + res := recursive(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + if q.Type == dnsmessage.TypeA { |
| + r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| + } |
| + return true |
| + }) |
| + r := oneZone(fa).resolver(clock()) |
| + r.LookupIP = res.LookupIP |
| + ips, err := r.ResolveAll("shop.example.com") |
| + if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| + t.Fatalf("ResolveAll = %v, %v; want [%s]", ips, err, edgeIP) |
| + } |
| +} |
| + |
| +// Either family failing at the target fails the answer; a target with no A |
| +// at all is not an answer either. |
| +func TestFreshResolverResolveAllNeedsBothFamiliesOfTheTarget(t *testing.T) { |
| + fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "target.other.test."))) |
| + temp := &net.DNSError{Err: "server misbehaving", IsTemporary: true} |
| + for name, errs := range map[string]map[string]error{ |
| + "ip6 failed": {"ip6 target.other.test": temp}, |
| + "ip4 failed": {"ip4 target.other.test": temp}, |
| + "no A": {"ip4 target.other.test": errNotFound}, |
| + } { |
| + w := oneZone(fa) |
| + w.hosts["target.other.test"] = []string{edgeIP, "2001:db8::7"} |
| + w.ipErrs = errs |
| + if ips, err := w.resolver(clock()).ResolveAll("shop.example.com"); err == nil { |
| + t.Errorf("%s: ResolveAll = %v, want an error", name, ips) |
| + } |
| + } |
| + // No LookupIP at all: ResolveAll cannot ask per family and refuses. |
| + w := oneZone(fa) |
| + w.hosts["target.other.test"] = []string{edgeIP} |
| + r := w.resolver(clock()) |
| + r.LookupIP = nil |
| + if ips, err := r.ResolveAll("shop.example.com"); err == nil { |
| + t.Errorf("ResolveAll without LookupIP = %v, want an error", ips) |
| + } |
| +} |
diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go| index bb98aaccc..68cc61837 100644 |
| --- a/internal/edge/certwarm.go |
| +++ b/internal/edge/certwarm.go |
| @@ -79,7 +79,7 @@ const ( |
| warmDNSFastFor = 10 * time.Minute |
| warmDNSRetrySlow = 5 * time.Minute |
| warmDNSSlowFor = time.Hour |
| - // The fresh look's bounds (#724, issuerDNSGate). Hard counts, not |
| + // The fresh look's bounds (#724, issuerDNSGateAt). 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 |
| @@ -144,7 +144,7 @@ type certWarm struct { |
| 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 |
| + // taken from the caller's count (issuerDNSGateAt, #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. |
| @@ -422,7 +422,8 @@ func (c *certWarm) probeDNS() bool { |
| } |
| // probed is what a probe hands the pass it triggers: the names that pointed |
| -// here, and when the probe started. |
| +// here, those that needed a fresh look first, and when the probe started. |
| +// Scheduling only: the pass repeats every DNS check. |
| type probed struct { |
| pointed []string |
| at time.Time |
| @@ -431,7 +432,10 @@ type probed struct { |
| // 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. |
| +// to leave the schedule anyway. In pointed, the names that pointed on a |
| +// fresh look come first (at most warmFreshPerProbe of them, fewer than the |
| +// pass's warmFreshPerPass): a name that pointed on a cached answer may need |
| +// a fresh look of its own in the pass, and must not spend the count first. |
| func (c *certWarm) probe() probed { |
| if !c.isIssuer() { |
| return probed{} |
| @@ -454,14 +458,19 @@ func (c *certWarm) probe() probed { |
| return due[i] < due[j] |
| }) |
| fresh := &freshCount{left: warmFreshPerProbe} |
| - out := probed{at: now} |
| + var onFresh, onCache []string |
| for _, h := range due { |
| + before := fresh.left |
| if c.gateOpen(h, fresh, now) { |
| - out.pointed = append(out.pointed, h) |
| + if fresh.left < before { |
| + onFresh = append(onFresh, h) |
| + } else { |
| + onCache = append(onCache, h) |
| + } |
| } |
| c.dnsMissed(h, now) |
| } |
| - return out |
| + return probed{pointed: append(onFresh, onCache...), at: now} |
| } |
| // probeThenPass is the loop's DNS re-check: a probe, and when a pending host |
| @@ -469,7 +478,10 @@ func (c *certWarm) probe() probed { |
| // 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. |
| +// slow the lookups in between, as long as nothing has changed: the order |
| +// cap, a lost lease, the name leaving the set, its DNS changing, the edge |
| +// set reading empty, or the pass's own fresh look failing all still stop |
| +// the order. |
| func (c *certWarm) probeThenPass() (time.Duration, bool) { |
| p := c.probe() |
| if len(p.pointed) == 0 { |
| @@ -695,22 +707,25 @@ func (c *certWarm) passAfter(p probed) 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. |
| +// pointedFirst is hosts with the ones in pointed moved to the front, in |
| +// pointed's order (fresh-backed first, see probe), then the rest in their |
| +// own 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 |
| + inSet := make(map[string]bool, len(hosts)) |
| + for _, h := range hosts { |
| + inSet[h] = true |
| } |
| + moved := make(map[string]bool, len(pointed)) |
| out := make([]string, 0, len(hosts)) |
| - for _, h := range hosts { |
| - if want[h] { |
| + for _, h := range pointed { |
| + if inSet[h] && !moved[h] { |
| + moved[h] = true |
| out = append(out, h) |
| } |
| } |
| for _, h := range hosts { |
| - if !want[h] { |
| + if !moved[h] { |
| out = append(out, h) |
| } |
| } |
| @@ -915,7 +930,7 @@ func dnsGateAt(resolve func(string) ([]string, error), edgeIPs func() []string, |
| return func(host string) bool { return gate(host, nil) } |
| } |
| -// issuerDNSGate is dnsGate with a fresh look (#724). The issuer's cached |
| +// issuerDNSGateAt 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 |
| @@ -924,11 +939,7 @@ func dnsGateAt(resolve func(string) ([]string, error), edgeIPs func() []string, |
| // 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) |
| -} |
| - |
| +// each look stands alone. now is the clock for the edge set's reuse. |
| func issuerDNSGateAt(resolve, fresh func(string) ([]string, error), edgeIPs func() []string, now func() time.Time) func(string, *freshCount) bool { |
| var ( |
| mu sync.Mutex |
| @@ -993,6 +1004,7 @@ var ( |
| warmFreshResolver freshAllResolver = &domains.FreshResolver{ |
| LookupNS: net.DefaultResolver.LookupNS, |
| LookupHost: net.DefaultResolver.LookupHost, |
| + LookupIP: net.DefaultResolver.LookupIP, |
| LookupCNAME: net.DefaultResolver.LookupCNAME, |
| Backoff: warmFreshBackoff, |
| } |
| @@ -1007,10 +1019,12 @@ type freshAllResolver interface { |
| // 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. |
| +// when the cache has no answer. The gate's edge-set reuse runs on the |
| +// warm's own clock. |
| 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) |
| + var w *certWarm |
| + gate := issuerDNSGateAt(warmCachedLookup, warmFreshResolver.ResolveAll, edgeIPs, func() time.Time { return w.now() }) |
| + w = newCertWarm(hosts, getCert, func(h string) bool { return gate(h, nil) }, issuer, store) |
| w.freshGate = gate |
| return w |
| } |
diff --git a/internal/edge/certwarm_fresh_test.go b/internal/edge/certwarm_fresh_test.go| index 0186288ad..0f4a43278 100644 |
| --- a/internal/edge/certwarm_fresh_test.go |
| +++ b/internal/edge/certwarm_fresh_test.go |
| @@ -5,12 +5,16 @@ import ( |
| "crypto/tls" |
| "fmt" |
| "net" |
| + "net/netip" |
| "sort" |
| + "strings" |
| "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| + "golang.org/x/net/dns/dnsmessage" |
| + |
| "github.com/carlosframework/platform/internal/domains" |
| ) |
| @@ -37,10 +41,13 @@ type dnsFake struct { |
| calls map[string]int |
| total int |
| tick func() |
| + // once holds names whose answer is given once and then forgotten |
| + // (an expiring cache entry, a record that changes). |
| + once map[string]bool |
| } |
| func newDNSFake() *dnsFake { |
| - return &dnsFake{answers: map[string][]string{}, errs: map[string]error{}, calls: map[string]int{}} |
| + return &dnsFake{answers: map[string][]string{}, errs: map[string]error{}, calls: map[string]int{}, once: map[string]bool{}} |
| } |
| func (f *dnsFake) lookup(h string) ([]string, error) { |
| @@ -49,11 +56,18 @@ func (f *dnsFake) lookup(h string) ([]string, error) { |
| f.total++ |
| ips, ok := f.answers[h] |
| err := f.errs[h] |
| + if f.once[h] { |
| + delete(f.answers, h) |
| + delete(f.once, h) |
| + } |
| tick := f.tick |
| f.mu.Unlock() |
| if tick != nil { |
| tick() |
| } |
| + if ok && len(ips) > 0 { |
| + return ips, nil |
| + } |
| if err != nil { |
| return nil, err |
| } |
| @@ -71,6 +85,15 @@ func (f *dnsFake) set(h string, ips ...string) { |
| f.mu.Unlock() |
| } |
| +// setOnce answers h with ips once; after that it is "no such host", or the |
| +// error set with fail. |
| +func (f *dnsFake) setOnce(h string, ips ...string) { |
| + f.mu.Lock() |
| + f.answers[h] = ips |
| + f.once[h] = true |
| + f.mu.Unlock() |
| +} |
| + |
| func (f *dnsFake) fail(h string, err error) { |
| f.mu.Lock() |
| f.errs[h] = err |
| @@ -419,6 +442,17 @@ func TestIssuerWarmProbeFreshSuccessIsOrderedByThePassItTriggers(t *testing.T) { |
| t.Fatal("a probe ran a pass before anything pointed here") |
| } |
| } |
| + // Ten more young names, due with z, point here on cached answers in |
| + // the probe; in the pass those entries have expired and each takes a |
| + // fresh look that fails. They sit ahead of z in the set. |
| + for _, m := range hostNames("m", 10) { |
| + h.addHost(m) |
| + h.cached.setOnce(m, edgeV4) |
| + h.fresh.fail(m, fmt.Errorf("fresh dns: no authoritative answer")) |
| + h.w.mu.Lock() |
| + h.w.dnsMisses[m] = dnsMiss{first: h.clock.now(), next: h.clock.now()} |
| + h.w.mu.Unlock() |
| + } |
| // 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) |
| @@ -488,10 +522,200 @@ func TestIssuerWarmFreshResolverIsTheIssuerInstance(t *testing.T) { |
| if !ok { |
| t.Fatalf("warmFreshResolver is %T, want *domains.FreshResolver", warmFreshResolver) |
| } |
| - if r.LookupNS == nil || r.LookupHost == nil || r.LookupCNAME == nil { |
| + if r.LookupNS == nil || r.LookupHost == nil || r.LookupIP == 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) |
| } |
| } |
| + |
| +// A probe's fresh success is not evidence for the pass: when the pass's own |
| +// fresh look fails, after the edge set's reuse window has run out, the name |
| +// is not ordered. |
| +func TestIssuerWarmProbeFreshSuccessThenPassFreshFailureOrdersNothing(t *testing.T) { |
| + h := newIssuerHarness(t, "shop.example.com") |
| + h.fresh.fail("shop.example.com", fmt.Errorf("fresh dns: no authoritative answer")) |
| + h.w.pass() |
| + h.fresh.setOnce("shop.example.com", edgeV4) |
| + h.fresh.setTick(func() { h.clock.advance(warmEdgeSetTTL + time.Second) }) |
| + h.clock.advance(warmDNSRetryFast) |
| + if _, ran := h.w.probeThenPass(); !ran { |
| + t.Fatal("the probe's fresh look did not see the name pointing here") |
| + } |
| + if h.orderedHost("shop.example.com") { |
| + t.Fatal("ordered on the probe's fresh look after the pass's own failed") |
| + } |
| + if n := h.fresh.count("shop.example.com"); n != 3 { |
| + t.Fatalf("fresh looks = %d, want 3 (first pass, probe, triggered pass)", n) |
| + } |
| +} |
| + |
| +// Likewise when the edge set, read again after its reuse window, is empty: |
| +// the pass refuses before any lookup. |
| +func TestIssuerWarmProbeFreshSuccessThenEmptyEdgeSetOrdersNothing(t *testing.T) { |
| + h := newIssuerHarness(t, "shop.example.com") |
| + h.w.pass() |
| + h.fresh.set("shop.example.com", edgeV4) |
| + h.fresh.setTick(func() { |
| + h.setEdges() |
| + h.clock.advance(warmEdgeSetTTL + time.Second) |
| + }) |
| + h.clock.advance(warmDNSRetryFast) |
| + if _, ran := h.w.probeThenPass(); !ran { |
| + t.Fatal("the probe's fresh look did not see the name pointing here") |
| + } |
| + if h.orderedHost("shop.example.com") { |
| + t.Fatal("ordered with an empty edge set") |
| + } |
| + if n := h.fresh.count("shop.example.com"); n != 2 { |
| + t.Fatalf("fresh looks = %d, want 2 (first pass, probe): none with the set empty", n) |
| + } |
| +} |
| + |
| +// Through the loop: the timer's DNS re-check runs probeThenPass, the probe's |
| +// fresh look sees the record that landed, and the pass it runs orders. |
| +func TestIssuerWarmLoopOrdersOnAFreshLook(t *testing.T) { |
| + h := newIssuerHarness(t, "shop.example.com") |
| + passes := loopWarm(t, h.w) |
| + if h.fresh.count("shop.example.com") != 1 || h.orderedHost("shop.example.com") { |
| + t.Fatalf("boot pass: fresh looks %d, ordered %v; want one look, no order", h.fresh.count("shop.example.com"), h.orders()) |
| + } |
| + h.fresh.set("shop.example.com", edgeV4) |
| + h.clock.advance(warmDNSRetryFast) |
| + deadline := time.Now().Add(2 * time.Second) |
| + for !h.orderedHost("shop.example.com") { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("not ordered through the loop (passes %d, fresh looks %d)", passes.Load(), h.fresh.count("shop.example.com")) |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| + if passes.Load() < 2 { |
| + t.Fatalf("passes = %d, want the probe's pass", passes.Load()) |
| + } |
| +} |
| + |
| +// fakeDNSServer answers every query on a loopback UDP socket through |
| +// answer, a stand-in for both the zone's nameserver and the box's |
| +// recursive resolver. |
| +func fakeDNSServer(t *testing.T, answer func(q dnsmessage.Question, r *dnsmessage.Message)) string { |
| + t.Helper() |
| + pc, err := net.ListenPacket("udp", "127.0.0.1:0") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { pc.Close() }) |
| + go func() { |
| + buf := make([]byte, 1500) |
| + for { |
| + n, from, err := pc.ReadFrom(buf) |
| + if err != nil { |
| + return |
| + } |
| + var q dnsmessage.Message |
| + if q.Unpack(buf[:n]) != nil || len(q.Questions) != 1 { |
| + continue |
| + } |
| + r := dnsmessage.Message{Header: dnsmessage.Header{ID: q.ID, Response: true}, Questions: q.Questions} |
| + answer(q.Questions[0], &r) |
| + if out, err := r.Pack(); err == nil { |
| + pc.WriteTo(out, from) |
| + } |
| + } |
| + }() |
| + return pc.LocalAddr().String() |
| +} |
| + |
| +// Astra #724 R1 P1-1, end to end through newIssuerWarm and a real |
| +// FreshResolver. The zone's own nameserver answers shop.example.com with a |
| +// CNAME to target.other.test, out of zone; the box's recursive resolver |
| +// gives that target A = ours and SERVFAIL for AAAA. Go's resolver, asked for |
| +// both families at once, would return the A record with no error; the gate |
| +// must refuse, since the target's IPv6 is unknown. |
| +func TestIssuerWarmRefusesACNAMETargetWhoseAAAAFailed(t *testing.T) { |
| + name := func(s string) dnsmessage.Name { return dnsmessage.MustNewName(s) } |
| + hdr := func(n string, typ dnsmessage.Type) dnsmessage.ResourceHeader { |
| + return dnsmessage.ResourceHeader{Name: name(n), Type: typ, Class: dnsmessage.ClassINET, TTL: 60} |
| + } |
| + authority := fakeDNSServer(t, func(q dnsmessage.Question, r *dnsmessage.Message) { |
| + r.Authoritative = true |
| + switch qn := strings.ToLower(q.Name.String()); { |
| + case q.Type == dnsmessage.TypeSOA && qn == "example.com.": |
| + r.Answers = []dnsmessage.Resource{{Header: hdr("example.com.", dnsmessage.TypeSOA), Body: &dnsmessage.SOAResource{ |
| + NS: name("ns1.example.com."), MBox: name("hostmaster.example.com."), Serial: 1, MinTTL: 1800}}} |
| + case qn == "shop.example.com.": |
| + r.Answers = []dnsmessage.Resource{{Header: hdr("shop.example.com.", dnsmessage.TypeCNAME), Body: &dnsmessage.CNAMEResource{CNAME: name("target.other.test.")}}} |
| + default: |
| + r.RCode = dnsmessage.RCodeRefused |
| + } |
| + }) |
| + recursive := fakeDNSServer(t, func(q dnsmessage.Question, r *dnsmessage.Message) { |
| + r.RecursionAvailable = true |
| + if q.Type == dnsmessage.TypeAAAA { |
| + r.RCode = dnsmessage.RCodeServerFailure |
| + return |
| + } |
| + r.Answers = []dnsmessage.Resource{{Header: hdr(q.Name.String(), dnsmessage.TypeA), Body: &dnsmessage.AResource{A: netip.MustParseAddr(edgeV4).As4()}}} |
| + }) |
| + res := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| + var d net.Dialer |
| + return d.DialContext(ctx, "udp", recursive) |
| + }} |
| + if ips, err := res.LookupHost(context.Background(), "target.other.test"); err != nil || len(ips) != 1 { |
| + t.Fatalf("premise: LookupHost = %v, %v; want the A record and no error", ips, err) |
| + } |
| + clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + newFresh := func(lookupIP func(context.Context, string, string) ([]net.IP, error)) *domains.FreshResolver { |
| + return &domains.FreshResolver{ |
| + LookupNS: func(_ context.Context, zone string) ([]*net.NS, error) { |
| + if zone == "example.com" { |
| + return []*net.NS{{Host: "ns1.example.com."}}, nil |
| + } |
| + return nil, &net.DNSError{Err: "no such host", Name: zone, IsNotFound: true} |
| + }, |
| + LookupHost: func(_ context.Context, host string) ([]string, error) { |
| + if strings.TrimSuffix(host, ".") == "ns1.example.com" { |
| + return []string{"192.0.2.53"}, nil |
| + } |
| + return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true} |
| + }, |
| + LookupIP: lookupIP, |
| + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { |
| + if address != "192.0.2.53:53" { |
| + return nil, fmt.Errorf("no route to %s", address) |
| + } |
| + var d net.Dialer |
| + return d.DialContext(ctx, network, authority) |
| + }, |
| + Backoff: warmFreshBackoff, |
| + Now: clock.now, |
| + } |
| + } |
| + h := buildIssuerHarness(t, newFresh(res.LookupIP), []string{"shop.example.com"}) |
| + h.clock = clock |
| + h.w.now = clock.now |
| + h.w.pass() |
| + if h.orderedHost("shop.example.com") { |
| + t.Fatal("ordered with the CNAME target's AAAA unresolved") |
| + } |
| + |
| + // Control: the same fixture with the AAAA query answered empty (NODATA) |
| + // orders, so the refusal above is the failed family and nothing else. |
| + recursive2 := fakeDNSServer(t, func(q dnsmessage.Question, r *dnsmessage.Message) { |
| + r.RecursionAvailable = true |
| + if q.Type == dnsmessage.TypeA { |
| + r.Answers = []dnsmessage.Resource{{Header: hdr(q.Name.String(), dnsmessage.TypeA), Body: &dnsmessage.AResource{A: netip.MustParseAddr(edgeV4).As4()}}} |
| + } |
| + }) |
| + res2 := &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { |
| + var d net.Dialer |
| + return d.DialContext(ctx, "udp", recursive2) |
| + }} |
| + h2 := buildIssuerHarness(t, newFresh(res2.LookupIP), []string{"shop.example.com"}) |
| + h2.clock = clock |
| + h2.w.now = clock.now |
| + h2.w.pass() |
| + if !h2.orderedHost("shop.example.com") { |
| + t.Fatal("control: not ordered with the target's AAAA answered NODATA") |
| + } |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-issuer-fresh-gate-724.tsv b/internal/provision/testdata/plan14-root-reads-issuer-fresh-gate-724.tsv| index 9397ff2c3..c3b830231 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-issuer-fresh-gate-724.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-issuer-fresh-gate-724.tsv |
| @@ -2,5 +2,5 @@ |
| # 1 body, 0 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| @legacy be159b9c8b2bda15a531ca118003a76e19c7156cc4fd4ca038625ca2da391f29 internal/edge/edge.go RunWithActivatorReady legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-certwarm-wake;basis=registers the cert warm's nudge on OnReload and on the home map's new served-hosts wake (#386); nudge only sends on a buffered channel; no read added, same 5 sites;rev3=census-ruling-edge-roll-blip;basis=the :80/:443 (and dev) listeners are bound on the calling goroutine with net.Listen via listen()/listenAddr (same empty-address defaults as ListenAndServe) before the serve goroutines, which now call Serve/ServeTLS on them, and one log line names the bound addresses; no read added, same 5 sites;rev3=census-ruling-edge-held-streams;basis=the edge in the agent (carlos-edge.service, user carlos): the inline home-map arming (newHomeMap, one boot Refresh, Store, run) moves unchanged into the new Edge.ArmHomeMap and is called here as e.ArmHomeMap(); same refresh, same timeout, same order. The reads are moved, none added.;rev3=census-ruling-agent-start-demo-1;basis=Logs the combined explicit wildcard parents through demoWildcardDomains; no new watcher or read loop.;rev3=census-ruling-region-ha-p3a;basis=P3a Task 3: invoke the local admission Close before HTTP/HTTPS draining and door shutdown in both dev and normal listeners. Preserve merged main's startup behavior and its census basis; the same five read sites, no new reads.;rev3=census-ruling-stateless-replicas-3;basis=Task 1b: defer stopping and joining the edge's replica jobs at shutdown. Existing five direct read sites unchanged; no new reader or authority.;rev3=census-ruling-region-ha-p3b;basis=Wrap dev, HTTP and HTTPS listeners through ConfigureGroupHairpin before serving. Indirectly read protected root boot identity and group-ingress.json; kernel local/peer addresses mark local ingress, never HTTP headers. Agent edge uses verified public source for accounting; direct read sites unchanged and absent marker keeps original listeners. CI fix removes interface enumeration: local peer must match snapshot primary IPv4, an ingress IPv4 installed locally by root, or loopback; kernel destination must still be an ingress EIP on port 80/443. Merge 45cebde4 retains deferred stopReplicaProbes alongside group listener wrapping and shutdown admission closure.;rev3=census-ruling-demo-api-site-route;basis=Change the startup failure context from load routes to initialize because origin validation now runs during construction. Existing five read sites unchanged; no new read or runtime authority.;rev3=census-ruling-region-ha-graceful-terminate-t2;basis=Invoke the agent shutdown timestamp hook synchronously at SIGTERM/SIGINT receipt before closing admission in dev and TLS paths. No read added.;rev3=census-ruling-region-ha-held-build;basis=region HA held-requests Task 5: registers the https and http servers with the connection drain (conns.serve) so a drain can close idle keep-alive connections; no read added;rev3=census-ruling-issuer-fresh-gate-724;basis=builds the cert warm through newIssuerWarm (certwarm.go) instead of newCertWarm with dnsGate(warmResolver, edgeIPs): the same cached DNS gate plus, when the cached lookup has no answer, one fresh look at the name's zone nameservers via domains.FreshResolver.ResolveAll (public nameserver addresses only, existing destination policy), bounded per pass and probe; the answer only decides whether to place an ACME order. No file or root-owned read added, same 5 sites. data-literal@129,data-literal@148,data-literal@284,data-literal@417,file:Open@1013 2cade75b3861ac808925bb7b3cef86bfe5e3a58323f53344776b719bff3f1cd4 |
| internal/edge/edge.go RunWithActivatorReady legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-certwarm-wake;basis=registers the cert warm's nudge on OnReload and on the home map's new served-hosts wake (#386); nudge only sends on a buffered channel; no read added, same 5 sites;rev3=census-ruling-edge-roll-blip;basis=the :80/:443 (and dev) listeners are bound on the calling goroutine with net.Listen via listen()/listenAddr (same empty-address defaults as ListenAndServe) before the serve goroutines, which now call Serve/ServeTLS on them, and one log line names the bound addresses; no read added, same 5 sites;rev3=census-ruling-edge-held-streams;basis=the edge in the agent (carlos-edge.service, user carlos): the inline home-map arming (newHomeMap, one boot Refresh, Store, run) moves unchanged into the new Edge.ArmHomeMap and is called here as e.ArmHomeMap(); same refresh, same timeout, same order. The reads are moved, none added.;rev3=census-ruling-agent-start-demo-1;basis=Logs the combined explicit wildcard parents through demoWildcardDomains; no new watcher or read loop.;rev3=census-ruling-region-ha-p3a;basis=P3a Task 3: invoke the local admission Close before HTTP/HTTPS draining and door shutdown in both dev and normal listeners. Preserve merged main's startup behavior and its census basis; the same five read sites, no new reads.;rev3=census-ruling-stateless-replicas-3;basis=Task 1b: defer stopping and joining the edge's replica jobs at shutdown. Existing five direct read sites unchanged; no new reader or authority.;rev3=census-ruling-region-ha-p3b;basis=Wrap dev, HTTP and HTTPS listeners through ConfigureGroupHairpin before serving. Indirectly read protected root boot identity and group-ingress.json; kernel local/peer addresses mark local ingress, never HTTP headers. Agent edge uses verified public source for accounting; direct read sites unchanged and absent marker keeps original listeners. CI fix removes interface enumeration: local peer must match snapshot primary IPv4, an ingress IPv4 installed locally by root, or loopback; kernel destination must still be an ingress EIP on port 80/443. Merge 45cebde4 retains deferred stopReplicaProbes alongside group listener wrapping and shutdown admission closure.;rev3=census-ruling-demo-api-site-route;basis=Change the startup failure context from load routes to initialize because origin validation now runs during construction. Existing five read sites unchanged; no new read or runtime authority.;rev3=census-ruling-region-ha-graceful-terminate-t2;basis=Invoke the agent shutdown timestamp hook synchronously at SIGTERM/SIGINT receipt before closing admission in dev and TLS paths. No read added.;rev3=census-ruling-region-ha-held-build;basis=region HA held-requests Task 5: registers the https and http servers with the connection drain (conns.serve) so a drain can close idle keep-alive connections; no read added;rev3=census-ruling-issuer-fresh-gate-724;basis=builds the cert warm through newIssuerWarm (certwarm.go) instead of newCertWarm with dnsGate(warmResolver, edgeIPs): the same cached DNS gate plus, when the cached lookup has no answer, one fresh look at the name's zone nameservers via domains.FreshResolver.ResolveAll (public nameserver addresses only, existing destination policy), bounded per pass and probe; the answer only decides whether to place an ACME order. No file or root-owned read added, same 5 sites. data-literal@129,data-literal@148,data-literal@284,data-literal@417,file:Open@1013 2cade75b3861ac808925bb7b3cef86bfe5e3a58323f53344776b719bff3f1cd4 |
| -@coverage internal/domains 62e854d948c9eee77bf0d9d77a617f21881bd6fb333ef47a04554f8d90a924ff |
| -@coverage internal/edge 8598cce8434ff1868ecb702e472599c2aca11847a4829510e7340e82d0a0c936 |
| +@coverage internal/domains 08d31cbe267c3b3f5d491ffe3a01b3df790f0d68301d015963670db206876371 |
| +@coverage internal/edge 7d6979c574fecde9b26c6f6054b763fdaad111e576a8d0c4ee014f3b3d69cdb6 |