domains: uncached DNS look for un-pointing custom hostnames
A customer who attaches a hostname and then adds the record (the order attach prints) waited out their zone's negative-cache TTL before the alias was published and a cert ordered: the box's caching resolver had already answered NXDOMAIN. Seen at 38 minutes; zones with a day-long SOA minimum would wait a day. StampHealth now takes a second look through FreshResolver for a name outside the apps domain whose cached verdict is "" or "elsewhere". It finds the zone's nameservers, asks them directly with a non-recursive query, trusts only an authoritative reply, and follows an out-of-zone CNAME through the cached resolver. The fresh answer can only upgrade a verdict to "pointing"; anything else leaves the cached verdict as it was. A name whose fresh lookup fails is not asked again for 30 minutes. Census: StampHealth and domainsTickConfig bodies, new FreshResolver.ask (a UDP socket read). #724. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Human
Prompt
Yes, go ahead. (fix the DNS cache issue #724: ask the customer's zone nameservers directly for names not yet pointing)
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
FreshResolver + freshDNSStatus: uncached, authoritative second look that can only upgrade a custom hostname's DNS verdict to pointing.
9 files changed,
+751
−2
docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md+25 −0internal/domains/freshdns.go+248 −0internal/domains/freshdns_stamp_test.go+179 −0internal/domains/freshdns_test.go+230 −0internal/domains/reconcile.go+45 −2internal/host/agent.go+7 −0internal/host/host_test.go+5 −0internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv+11 −0
CI failed — run details
diff --git a/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md b/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md| new file mode 100644 |
| index 000000000..0373ebc0a |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md |
| @@ -0,0 +1,25 @@ |
| +# Census approval: dns-fresh-lookup-724 |
| + |
| +Ruling `census-ruling-dns-fresh-lookup-724`. The independent reviewer approves or rejects every change below, 1 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +| unit | change | read sites | what changed | |
| +|---|---|---|---| |
| +| `internal/domains/reconcile.go` `StampHealth` | body | unchanged (21) | DNS loops call freshDNSStatus: a second, uncached lookup for a non-apps-domain name whose cached verdict is "" or "elsewhere"; it may only upgrade to "pointing" (#724) | |
| +| `internal/host/agent.go` `domainsTickConfig` | body | unchanged (2) | wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) | |
| + |
| +Envelopes re-pinned: `internal/domains`, `internal/host`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Recorded dispositions, which each change must still satisfy: |
| + |
| +- `internal/domains/reconcile.go` `StampHealth`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J1;basis=root-route |
| +- `internal/host/agent.go` `domainsTickConfig`: legacy, task accepted-legacy. Basis: accepted-legacy;rev3=J4;basis=root-input |
| + |
| +New dispositions, which census-propose wrote and the reviewer checks against the unit's reads and the diff: |
| + |
| +- new `internal/domains/freshdns.go:FreshResolver.ask`: reads +file:Read; written legacy, accepted-legacy, frozen |
| + |
| +Seal: `2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216` → `6d029851d1a1ff285658bf3740def2fe651a8039e3f202bc694b0e973cadb74a`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/domains/freshdns.go b/internal/domains/freshdns.go| new file mode 100644 |
| index 000000000..b1d43dfd7 |
| --- /dev/null |
| +++ b/internal/domains/freshdns.go |
| @@ -0,0 +1,248 @@ |
| +package domains |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "math/rand/v2" |
| + "net" |
| + "strings" |
| + "sync" |
| + "time" |
| + |
| + "golang.org/x/net/dns/dnsmessage" |
| +) |
| + |
| +// FreshResolver answers "what does this hostname resolve to right now" |
| +// without any cache in the way (#724). It finds the zone that holds the |
| +// name, asks that zone's own nameservers, and trusts only an authoritative |
| +// reply. Config.ResolveFresh is its Resolve. |
| +// |
| +// Why not just skip the box's cache: systemd-resolved is not the only |
| +// cache between us and the customer's zone. The upstream (VPC) resolver |
| +// 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 |
| +// around the answer that we are not waiting on: the zone's NS set, the |
| +// nameservers' own addresses, and an out-of-zone CNAME target (in practice |
| +// the deployment's own edge name). |
| +type FreshResolver struct { |
| + LookupNS func(ctx context.Context, name string) ([]*net.NS, error) |
| + LookupHost func(ctx context.Context, host string) ([]string, error) |
| + // Port is the nameservers' port; "" is 53. A seam for tests. |
| + Port string |
| + // Timeout bounds one whole Resolve; 0 is 3 s, the cached seam's own |
| + // budget (internal/host resolveHost). |
| + Timeout time.Duration |
| + // Backoff is how long a name whose lookup FAILED is answered with the |
| + // failure again without asking anyone; 0 is 30 minutes. A dead or lame |
| + // nameserver would otherwise cost a full Timeout on every pass. |
| + Backoff time.Duration |
| + // Now is the clock seam; nil is time.Now. |
| + Now func() time.Time |
| + |
| + mu sync.Mutex |
| + failedAt map[string]time.Time |
| +} |
| + |
| +// errNoAuthority is a fresh lookup that got no usable authoritative answer. |
| +var errNoAuthority = errors.New("fresh dns: no authoritative answer") |
| + |
| +// freshMaxServers bounds the nameserver addresses one Resolve tries. |
| +const freshMaxServers = 3 |
| + |
| +// Resolve returns host's addresses as its zone's nameservers give them now. |
| +// An authoritative "no such name" is a *net.DNSError with IsNotFound, the |
| +// same shape the cached resolver gives, so callers classify both alike. |
| +func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| + now := time.Now |
| + if r.Now != nil { |
| + now = r.Now |
| + } |
| + backoff := r.Backoff |
| + if backoff == 0 { |
| + backoff = 30 * time.Minute |
| + } |
| + name := strings.ToLower(strings.TrimSuffix(host, ".")) |
| + r.mu.Lock() |
| + if at, ok := r.failedAt[name]; ok && now().Sub(at) < backoff { |
| + r.mu.Unlock() |
| + return nil, fmt.Errorf("%w for %s (backing off since %s)", errNoAuthority, name, at.UTC().Format(time.RFC3339)) |
| + } |
| + r.mu.Unlock() |
| + |
| + timeout := r.Timeout |
| + if timeout == 0 { |
| + timeout = 3 * time.Second |
| + } |
| + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| + defer cancel() |
| + ips, err := r.resolve(ctx, name) |
| + |
| + var dnsErr *net.DNSError |
| + failed := err != nil && !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) |
| + r.mu.Lock() |
| + if failed { |
| + if r.failedAt == nil { |
| + r.failedAt = map[string]time.Time{} |
| + } |
| + r.failedAt[name] = now() |
| + } else { |
| + delete(r.failedAt, name) |
| + } |
| + r.mu.Unlock() |
| + return ips, err |
| +} |
| + |
| +func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, error) { |
| + servers, err := r.zoneServers(ctx, name) |
| + if err != nil { |
| + return nil, err |
| + } |
| + port := r.Port |
| + if port == "" { |
| + port = "53" |
| + } |
| + last := errNoAuthority |
| + for _, ip := range servers { |
| + ips, err := r.ask(ctx, net.JoinHostPort(ip, port), name) |
| + if err == nil { |
| + return ips, nil |
| + } |
| + var dnsErr *net.DNSError |
| + if errors.As(err, &dnsErr) && dnsErr.IsNotFound { |
| + return nil, err |
| + } |
| + last = err |
| + if ctx.Err() != nil { |
| + break |
| + } |
| + } |
| + return nil, last |
| +} |
| + |
| +// zoneServers finds the closest enclosing zone of name that has an NS set |
| +// and returns up to freshMaxServers of its nameservers' addresses. It |
| +// stops below the top level: a TLD's servers only refer back down, and a |
| +// name with no zone under its TLD has nothing fresh to read. |
| +func (r *FreshResolver) zoneServers(ctx context.Context, name string) ([]string, error) { |
| + for zone := name; strings.Contains(zone, "."); zone = zone[strings.Index(zone, ".")+1:] { |
| + nss, err := r.LookupNS(ctx, zone) |
| + if err != nil || len(nss) == 0 { |
| + if ctx.Err() != nil { |
| + return nil, ctx.Err() |
| + } |
| + continue |
| + } |
| + var out []string |
| + for _, i := range rand.Perm(len(nss)) { |
| + addrs, err := r.LookupHost(ctx, nss[i].Host) |
| + if err != nil { |
| + continue |
| + } |
| + for _, a := range addrs { |
| + if len(out) < freshMaxServers { |
| + out = append(out, a) |
| + } |
| + } |
| + if len(out) >= freshMaxServers { |
| + break |
| + } |
| + } |
| + if len(out) == 0 { |
| + return nil, fmt.Errorf("%w: no address for any nameserver of %s", errNoAuthority, zone) |
| + } |
| + return out, nil |
| + } |
| + return nil, fmt.Errorf("%w: no zone found for %s", errNoAuthority, name) |
| +} |
| + |
| +// ask sends one non-recursive A query for name to server and reads the |
| +// authoritative reply. A CNAME chain in the reply is followed; if it ends |
| +// outside what the server answered, the chain's last name goes to the |
| +// cached LookupHost. |
| +func (r *FreshResolver) ask(ctx context.Context, server, name string) ([]string, error) { |
| + qname, err := dnsmessage.NewName(name + ".") |
| + if err != nil { |
| + return nil, err |
| + } |
| + id := uint16(rand.Uint32()) |
| + query := dnsmessage.Message{ |
| + Header: dnsmessage.Header{ID: id}, |
| + Questions: []dnsmessage.Question{{Name: qname, Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET}}, |
| + } |
| + packed, err := query.Pack() |
| + if err != nil { |
| + return nil, err |
| + } |
| + var d net.Dialer |
| + conn, err := d.DialContext(ctx, "udp", server) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer conn.Close() |
| + if deadline, ok := ctx.Deadline(); ok { |
| + _ = conn.SetDeadline(deadline) |
| + } |
| + if _, err := conn.Write(packed); err != nil { |
| + return nil, err |
| + } |
| + buf := make([]byte, 1500) |
| + var reply dnsmessage.Message |
| + for { |
| + n, err := conn.Read(buf) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if reply.Unpack(buf[:n]) != nil || !reply.Response || reply.ID != id || |
| + len(reply.Questions) != 1 || !strings.EqualFold(reply.Questions[0].Name.String(), name+".") { |
| + continue // not our reply; keep reading until the deadline |
| + } |
| + break |
| + } |
| + if !reply.Authoritative { |
| + return nil, fmt.Errorf("%w: %s is not authoritative for %s", errNoAuthority, server, name) |
| + } |
| + if reply.Truncated { |
| + return nil, fmt.Errorf("%w: truncated reply from %s", errNoAuthority, server) |
| + } |
| + switch reply.RCode { |
| + case dnsmessage.RCodeSuccess: |
| + case dnsmessage.RCodeNameError: |
| + return nil, &net.DNSError{Err: "no such host", Name: name, Server: server, IsNotFound: true} |
| + default: |
| + return nil, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name) |
| + } |
| + |
| + // Follow the CNAME chain the server gave, from name, then collect the |
| + // A records of the chain's last name. |
| + current := name + "." |
| + for hops := 0; hops < 8; hops++ { |
| + next := "" |
| + for _, rr := range reply.Answers { |
| + if c, ok := rr.Body.(*dnsmessage.CNAMEResource); ok && strings.EqualFold(rr.Header.Name.String(), current) { |
| + next = c.CNAME.String() |
| + break |
| + } |
| + } |
| + if next == "" { |
| + break |
| + } |
| + current = next |
| + } |
| + var ips []string |
| + for _, rr := range reply.Answers { |
| + if a, ok := rr.Body.(*dnsmessage.AResource); ok && strings.EqualFold(rr.Header.Name.String(), current) { |
| + ips = append(ips, net.IP(a.A[:]).String()) |
| + } |
| + } |
| + if len(ips) > 0 { |
| + return ips, nil |
| + } |
| + if !strings.EqualFold(current, name+".") { |
| + return r.LookupHost(ctx, strings.TrimSuffix(current, ".")) |
| + } |
| + // Authoritative, the name exists, and it has no A record here. |
| + return nil, &net.DNSError{Err: "no A record", Name: name, Server: server, IsNotFound: true} |
| +} |
diff --git a/internal/domains/freshdns_stamp_test.go b/internal/domains/freshdns_stamp_test.go| new file mode 100644 |
| index 000000000..c3313db42 |
| --- /dev/null |
| +++ b/internal/domains/freshdns_stamp_test.go |
| @@ -0,0 +1,179 @@ |
| +package domains_test |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// #724: a customer attaches a hostname and THEN adds the record, as the |
| +// attach output tells them to. The box's caching resolver has already |
| +// answered NXDOMAIN for the name and holds that for the zone's SOA minimum, |
| +// so every pass inside that window reads "" and the alias stays unpublished. |
| +// ResolveFresh asks without the cache and may only UPGRADE a verdict to |
| +// "pointing"; it never makes a verdict worse. |
| + |
| +const edgeIP = "203.0.113.7" |
| + |
| +func freshCfg(cached, fresh func(string) ([]string, error), calls *[]string) domains.Config { |
| + return domains.Config{ |
| + AppsDomain: "oncarlos.test", |
| + EdgeIPs: func() []string { return []string{edgeIP} }, |
| + Resolve: cached, |
| + ResolveFresh: func(host string) ([]string, error) { |
| + *calls = append(*calls, host) |
| + return fresh(host) |
| + }, |
| + Now: func() time.Time { return time.Unix(5000, 0) }, |
| + } |
| +} |
| + |
| +func nxdomain(string) ([]string, error) { |
| + return nil, &net.DNSError{Err: "no such host", IsNotFound: true} |
| +} |
| + |
| +func pointingAt(string) ([]string, error) { return []string{edgeIP}, nil } |
| + |
| +func aliasStatus(t *testing.T, reg *registry.Registry, alias string) string { |
| + t.Helper() |
| + aliases, err := reg.ListAliases() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, a := range aliases { |
| + if a.Alias == alias { |
| + return a.DNSStatus |
| + } |
| + } |
| + t.Fatalf("alias %s not found", alias) |
| + return "" |
| +} |
| + |
| +func regWithAlias(t *testing.T, alias string) *registry.Registry { |
| + t.Helper() |
| + reg := regWith(t, registry.Route{Host: "app.acct.oncarlos.test", Kind: "service", Addr: "10.0.0.1:1", Status: "ready"}) |
| + if err := reg.UpsertAlias(registry.RouteAlias{Alias: alias, Host: "app.acct.oncarlos.test", Account: "acct", App: "app"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + return reg |
| +} |
| + |
| +func TestStampHealthFreshLookupBeatsCachedNXDOMAIN(t *testing.T) { |
| + reg := regWithAlias(t, "shop.example.com") |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + if h == "shop.example.com" { |
| + return nxdomain(h) |
| + } |
| + return pointingAt(h) |
| + }, pointingAt, &calls) |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := aliasStatus(t, reg, "shop.example.com"); got != "pointing" { |
| + t.Fatalf("alias dns_status = %q, want pointing: the uncached answer says the record is live", got) |
| + } |
| +} |
| + |
| +func TestStampHealthFreshLookupBeatsCachedElsewhere(t *testing.T) { |
| + // The customer moved the name from another host: the cache still holds |
| + // the old record until its TTL runs out. |
| + reg := regWithAlias(t, "shop.example.com") |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + if h == "shop.example.com" { |
| + return []string{"198.51.100.9"}, nil |
| + } |
| + return pointingAt(h) |
| + }, pointingAt, &calls) |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := aliasStatus(t, reg, "shop.example.com"); got != "pointing" { |
| + t.Fatalf("alias dns_status = %q, want pointing", got) |
| + } |
| +} |
| + |
| +func TestStampHealthFreshLookupNeverDowngrades(t *testing.T) { |
| + cases := map[string]func(string) ([]string, error){ |
| + "fresh NXDOMAIN": nxdomain, |
| + "fresh failure": func(string) ([]string, error) { return nil, errors.New("timeout") }, |
| + "fresh elsewhere": func(string) ([]string, error) { return []string{"198.51.100.9"}, nil }, |
| + } |
| + for name, fresh := range cases { |
| + t.Run(name, func(t *testing.T) { |
| + reg := regWithAlias(t, "shop.example.com") |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + if h == "shop.example.com" { |
| + return nxdomain(h) |
| + } |
| + return pointingAt(h) |
| + }, fresh, &calls) |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got := aliasStatus(t, reg, "shop.example.com"); got != "" { |
| + t.Fatalf("alias dns_status = %q, want \"\" (the cached verdict stands)", got) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStampHealthFreshLookupOnlyWhereItCanHelp(t *testing.T) { |
| + reg := regWith(t, |
| + registry.Route{Host: "app.acct.oncarlos.test", Kind: "service", Addr: "10.0.0.1:1", Status: "ready"}, |
| + registry.Route{Host: "gone.acct.oncarlos.test", Kind: "service", Addr: "10.0.0.1:2", Status: "ready"}, |
| + ) |
| + for _, a := range []string{"live.example.com", "new.example.com"} { |
| + if err := reg.UpsertAlias(registry.RouteAlias{Alias: a, Host: "app.acct.oncarlos.test", Account: "acct", App: "app"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + switch h { |
| + case "new.example.com", "gone.acct.oncarlos.test": |
| + return nxdomain(h) |
| + } |
| + return pointingAt(h) |
| + }, pointingAt, &calls) |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Already pointing: no second lookup. Under the apps domain: the |
| + // platform's own zone, not a customer's, so nothing to bypass. |
| + if len(calls) != 1 || calls[0] != "new.example.com" { |
| + t.Fatalf("fresh lookups = %v, want only [new.example.com]", calls) |
| + } |
| + if got := mustGet(t, reg, "gone.acct.oncarlos.test"); got.DNSStatus != "" { |
| + t.Fatalf("apps-domain route = %q, want \"\"", got.DNSStatus) |
| + } |
| +} |
| + |
| +func TestStampHealthFreshLookupLeavesResolverFailureAlone(t *testing.T) { |
| + // Our resolver failing is not a customer verdict to second-guess: the |
| + // existing hold-last-verdict rule applies untouched. |
| + reg := regWithAlias(t, "shop.example.com") |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + if h == "shop.example.com" { |
| + return nil, errors.New("SERVFAIL") |
| + } |
| + return pointingAt(h) |
| + }, pointingAt, &calls) |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(calls) != 0 { |
| + t.Fatalf("fresh lookups = %v, want none on a resolver failure", calls) |
| + } |
| + if got := aliasStatus(t, reg, "shop.example.com"); got != "unresolved" { |
| + t.Fatalf("alias dns_status = %q, want unresolved", got) |
| + } |
| +} |
diff --git a/internal/domains/freshdns_test.go b/internal/domains/freshdns_test.go| new file mode 100644 |
| index 000000000..22663a06d |
| --- /dev/null |
| +++ b/internal/domains/freshdns_test.go |
| @@ -0,0 +1,230 @@ |
| +package domains_test |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + "golang.org/x/net/dns/dnsmessage" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| +) |
| + |
| +// fakeAuthority is a one-zone authoritative server on loopback. answer |
| +// builds the reply to each query; returning nil drops the query (a dead |
| +// nameserver). |
| +type fakeAuthority struct { |
| + port string |
| + queries atomic.Int32 |
| + asked atomic.Value // last question name |
| +} |
| + |
| +func startAuthority(t *testing.T, answer func(q dnsmessage.Question, reply *dnsmessage.Message) bool) *fakeAuthority { |
| + t.Helper() |
| + pc, err := net.ListenPacket("udp", "127.0.0.1:0") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { pc.Close() }) |
| + fa := &fakeAuthority{} |
| + _, fa.port, _ = net.SplitHostPort(pc.LocalAddr().String()) |
| + 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 |
| + } |
| + fa.queries.Add(1) |
| + fa.asked.Store(q.Questions[0].Name.String()) |
| + reply := dnsmessage.Message{Header: dnsmessage.Header{ID: q.ID, Response: true}, Questions: q.Questions} |
| + if !answer(q.Questions[0], &reply) { |
| + continue |
| + } |
| + out, err := reply.Pack() |
| + if err != nil { |
| + t.Errorf("pack reply: %v", err) |
| + return |
| + } |
| + pc.WriteTo(out, from) |
| + } |
| + }() |
| + return fa |
| +} |
| + |
| +func aRecord(name, ip string) dnsmessage.Resource { |
| + var a [4]byte |
| + copy(a[:], net.ParseIP(ip).To4()) |
| + return dnsmessage.Resource{ |
| + Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName(name), Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET, TTL: 300}, |
| + Body: &dnsmessage.AResource{A: a}, |
| + } |
| +} |
| + |
| +func cnameRecord(name, target string) dnsmessage.Resource { |
| + return dnsmessage.Resource{ |
| + Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName(name), Type: dnsmessage.TypeCNAME, Class: dnsmessage.ClassINET, TTL: 300}, |
| + Body: &dnsmessage.CNAMEResource{CNAME: dnsmessage.MustNewName(target)}, |
| + } |
| +} |
| + |
| +var errNotFound = &net.DNSError{Err: "no such host", IsNotFound: true} |
| + |
| +// resolverFor wires a FreshResolver whose only zone is example.com, served |
| +// by fa, with the CACHED half answering the nameserver's and the edge's |
| +// addresses. |
| +func resolverFor(fa *fakeAuthority, now *time.Time) *domains.FreshResolver { |
| + return &domains.FreshResolver{ |
| + LookupNS: func(_ context.Context, name string) ([]*net.NS, error) { |
| + if name == "example.com" { |
| + return []*net.NS{{Host: "ns1.example.com."}}, nil |
| + } |
| + return nil, errNotFound |
| + }, |
| + LookupHost: func(_ context.Context, host string) ([]string, error) { |
| + switch strings.TrimSuffix(host, ".") { |
| + case "ns1.example.com": |
| + return []string{"127.0.0.1"}, nil |
| + case "edge.oncarlos.test": |
| + return []string{edgeIP}, nil |
| + } |
| + return nil, errNotFound |
| + }, |
| + Port: fa.port, |
| + Timeout: 500 * time.Millisecond, |
| + Now: func() time.Time { return *now }, |
| + } |
| +} |
| + |
| +func TestFreshResolverReadsTheAuthoritativeA(t *testing.T) { |
| + fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| + return true |
| + }) |
| + now := time.Unix(1000, 0) |
| + ips, err := resolverFor(fa, &now).Resolve("shop.example.com") |
| + if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| + t.Fatalf("Resolve = %v, %v; want [%s]", ips, err, edgeIP) |
| + } |
| + // The zone was found by walking up from the host, and the host itself |
| + // is what the authority was asked. |
| + if got := fa.asked.Load(); got != "shop.example.com." { |
| + t.Fatalf("authority asked for %v, want shop.example.com.", got) |
| + } |
| +} |
| + |
| +func TestFreshResolverFollowsAnOutOfZoneCNAME(t *testing.T) { |
| + // The usual custom-domain setup: the customer's zone holds only the |
| + // CNAME, and the target is ours. |
| + fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + r.Answers = []dnsmessage.Resource{cnameRecord(q.Name.String(), "edge.oncarlos.test.")} |
| + return true |
| + }) |
| + now := time.Unix(1000, 0) |
| + ips, err := resolverFor(fa, &now).Resolve("shop.example.com") |
| + if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| + t.Fatalf("Resolve = %v, %v; want [%s] via the CNAME target", ips, err, edgeIP) |
| + } |
| +} |
| + |
| +func TestFreshResolverReportsAuthoritativeNXDOMAIN(t *testing.T) { |
| + fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + r.RCode = dnsmessage.RCodeNameError |
| + return true |
| + }) |
| + now := time.Unix(1000, 0) |
| + _, err := resolverFor(fa, &now).Resolve("shop.example.com") |
| + var dnsErr *net.DNSError |
| + if !errors.As(err, &dnsErr) || !dnsErr.IsNotFound { |
| + t.Fatalf("Resolve error = %v, want a not-found DNSError", err) |
| + } |
| +} |
| + |
| +func TestFreshResolverIgnoresNonAuthoritativeAnswers(t *testing.T) { |
| + // A server that is not authoritative for the name (a lame delegation, |
| + // or a resolver squatting on the address) is not the fresh answer we |
| + // went looking for. |
| + fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| + return true |
| + }) |
| + now := time.Unix(1000, 0) |
| + if ips, err := resolverFor(fa, &now).Resolve("shop.example.com"); err == nil { |
| + t.Fatalf("Resolve = %v, want an error for a non-authoritative answer", ips) |
| + } |
| +} |
| + |
| +func TestFreshResolverRejectsAnswersForOtherNames(t *testing.T) { |
| + fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + r.Answers = []dnsmessage.Resource{aRecord("other.example.com.", edgeIP)} |
| + return true |
| + }) |
| + now := time.Unix(1000, 0) |
| + if ips, err := resolverFor(fa, &now).Resolve("shop.example.com"); err == nil { |
| + t.Fatalf("Resolve = %v, want an error: the answer names another host", ips) |
| + } |
| +} |
| + |
| +func TestFreshResolverBacksOffAfterAFailure(t *testing.T) { |
| + // A dead nameserver costs one timeout per backoff window, not one per |
| + // pass: every pass would otherwise stall on it. |
| + fa := startAuthority(t, func(dnsmessage.Question, *dnsmessage.Message) bool { return false }) |
| + now := time.Unix(1000, 0) |
| + r := resolverFor(fa, &now) |
| + if _, err := r.Resolve("shop.example.com"); err == nil { |
| + t.Fatal("first Resolve against a dead server succeeded") |
| + } |
| + sent := fa.queries.Load() |
| + if sent == 0 { |
| + t.Fatal("no query reached the server") |
| + } |
| + now = now.Add(10 * time.Minute) |
| + if _, err := r.Resolve("shop.example.com"); err == nil { |
| + t.Fatal("Resolve inside the backoff succeeded") |
| + } |
| + if fa.queries.Load() != sent { |
| + t.Fatalf("Resolve inside the backoff queried again (%d -> %d)", sent, fa.queries.Load()) |
| + } |
| + now = now.Add(time.Hour) |
| + _, _ = r.Resolve("shop.example.com") |
| + if fa.queries.Load() == sent { |
| + t.Fatal("Resolve after the backoff did not query again") |
| + } |
| +} |
| + |
| +func TestFreshResolverStopsAtTheTopLevel(t *testing.T) { |
| + // No zone found below the TLD: an error, and the TLD's own servers are |
| + // never asked (they would only refer us back down). |
| + var asked []string |
| + r := &domains.FreshResolver{ |
| + LookupNS: func(_ context.Context, name string) ([]*net.NS, error) { |
| + asked = append(asked, name) |
| + if name == "test" { |
| + return []*net.NS{{Host: "tld.test."}}, nil |
| + } |
| + return nil, errNotFound |
| + }, |
| + LookupHost: func(context.Context, string) ([]string, error) { return nil, errNotFound }, |
| + Timeout: 500 * time.Millisecond, |
| + } |
| + if _, err := r.Resolve("a.b.nowhere.test"); err == nil { |
| + t.Fatal("Resolve with no zone succeeded") |
| + } |
| + for _, n := range asked { |
| + if n == "test" { |
| + t.Fatalf("asked the TLD for NS: %v", asked) |
| + } |
| + } |
| +} |
diff --git a/internal/domains/reconcile.go b/internal/domains/reconcile.go| index dc3a64c61..ad89fa2a8 100644 |
| --- a/internal/domains/reconcile.go |
| +++ b/internal/domains/reconcile.go |
| @@ -41,6 +41,9 @@ type Config struct { |
| // Resolve is the DNS seam (net resolver in production, a fake in |
| // tests); nil skips DNS stamping entirely. |
| Resolve func(host string) ([]string, error) |
| + // ResolveFresh asks without any cache (#724); see freshDNSStatus. nil |
| + // leaves every verdict to Resolve alone. |
| + ResolveFresh func(host string) ([]string, error) |
| // AcmeDir is the edge's autocert DirCache directory; "" skips TLS |
| // stamping entirely. |
| AcmeDir string |
| @@ -494,7 +497,7 @@ func StampHealth(ctx context.Context, reg *registry.Registry, cfg Config, routes |
| // "pointing" — refusing that news would be the same lie in the |
| // opposite direction. |
| for _, rt := range routes { |
| - status := dnsStatus(cfg.Resolve, rt.Host, edge) |
| + status := freshDNSStatus(cfg, rt.Host, edge) |
| if status == unresolvedDNS && rt.DNSStatus != "" { |
| continue |
| } |
| @@ -503,7 +506,7 @@ func StampHealth(ctx context.Context, reg *registry.Registry, cfg Config, routes |
| } |
| } |
| for _, a := range aliases { |
| - status := dnsStatus(cfg.Resolve, a.Alias, edge) |
| + status := freshDNSStatus(cfg, a.Alias, edge) |
| if status == unresolvedDNS && a.DNSStatus != "" { |
| continue |
| } |
| @@ -567,6 +570,46 @@ func dnsStatus(resolve func(host string) ([]string, error), host string, edge ma |
| return "elsewhere" |
| } |
| +// freshDNSStatus is dnsStatus with a second, uncached look for a customer's |
| +// hostname that does not point here yet (#724). |
| +// |
| +// The cached answer is the box's resolver's, and that resolver holds a |
| +// "no such name" for the zone's SOA minimum: a customer who attaches a |
| +// hostname and then adds the record (the order attach prints) is read as |
| +// "" for 30 minutes to a day after the record went live, and the alias stays |
| +// unpublished all that time. A customer moving a name from another host has |
| +// the same wait on the old record's TTL ("elsewhere"). |
| +// |
| +// The fresh answer may only UPGRADE the verdict to "pointing". Every other |
| +// fresh result (no record, another address, a failure) leaves the cached |
| +// verdict as it was, so this path can never unpublish anything or turn an |
| +// answer into "unresolved". A cached lookup that FAILED is left alone too: |
| +// that is our resolver's fault, handled by section 4's hold-last-verdict |
| +// rule. Names under the apps domain are the platform's own zone and are |
| +// never asked twice. |
| +func freshDNSStatus(cfg Config, host string, edge map[string]bool) string { |
| + status := dnsStatus(cfg.Resolve, host, edge) |
| + if cfg.ResolveFresh == nil || (status != "" && status != "elsewhere") { |
| + return status |
| + } |
| + if apps := strings.ToLower(cfg.AppsDomain); apps != "" { |
| + h := strings.ToLower(strings.TrimSuffix(host, ".")) |
| + if h == apps || strings.HasSuffix(h, "."+apps) { |
| + return status |
| + } |
| + } |
| + ips, err := cfg.ResolveFresh(host) |
| + if err != nil { |
| + return status |
| + } |
| + for _, ip := range ips { |
| + if edge[ip] { |
| + return "pointing" |
| + } |
| + } |
| + return status |
| +} |
| + |
| // OpsSqid is the built-in ops account's frozen sqid (registry id 1). Every |
| // deployment has it, console or not. |
| var OpsSqid = Sqid(1) |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index b020fac99..d0010704e 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -3788,6 +3788,13 @@ func domainsTickConfig(getenv func(string) string, publicIP string) (domains.Con |
| AcmeDir: filepath.Join(dd, "acme"), |
| Now: time.Now, |
| Resolve: resolveHost, |
| + // #724: the uncached second look, for a customer hostname the |
| + // cached answer says does not point here yet. The cached resolver |
| + // still finds the zone's nameservers and our own CNAME target. |
| + ResolveFresh: (&domains.FreshResolver{ |
| + LookupNS: net.DefaultResolver.LookupNS, |
| + LookupHost: net.DefaultResolver.LookupHost, |
| + }).Resolve, |
| } |
| // EdgeIPs is deliberately left nil here — a Config out of this function |
| // alone stamps nothing. domainsLoop installs the fold once it has a |
diff --git a/internal/host/host_test.go b/internal/host/host_test.go| index 733ad7c54..b92f06a36 100644 |
| --- a/internal/host/host_test.go |
| +++ b/internal/host/host_test.go |
| @@ -766,6 +766,11 @@ func TestDomainsTickConfig(t *testing.T) { |
| if got.Resolve == nil || got.Now == nil { |
| t.Error("Resolve/Now seams not populated on a configured deployment") |
| } |
| + // #724: the uncached second look for a customer's hostname is wired in |
| + // production, not only in the domains tests. |
| + if got.ResolveFresh == nil { |
| + t.Error("ResolveFresh not populated on a configured deployment") |
| + } |
| // No explicit edge IPs: the box's own public IP (IMDS) is the default. |
| got, envIPs, _ = domainsTickConfig(env(map[string]string{"PLATFORM_APPS_DOMAIN": "oncarlos.test"}), "9.9.9.9") |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index f99c90548..26e838691 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -134,3 +134,4 @@ plan14-root-reads-managed-fleets-2b1.tsv d467adec3d86ae220afe98269aa226e87adba1b |
| plan14-root-reads-design-console-secrets.tsv ba2d479f7176757f8c8a4e251c498854a2ae092509ff77d9786f79d10c503c19 |
| plan14-root-reads-edge-page-cache-1.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216 |
| plan14-root-reads-cli-default-console-carloku.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216 |
| +plan14-root-reads-dns-fresh-lookup-724.tsv 6d029851d1a1ff285658bf3740def2fe651a8039e3f202bc694b0e973cadb74a |
diff --git a/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv b/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv| new file mode 100644 |
| index 000000000..a0199fbd0 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv |
| @@ -0,0 +1,11 @@ |
| +# Census overlay dns-fresh-lookup-724, written by census-propose (#369). census-ruling-dns-fresh-lookup-724, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md. |
| +# 2 body, 0 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +# 1 new dispositions: 0 reads re-frozen at their disposition with the claim, 1 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other. |
| +@legacy a7e7f45a2e134e460e74edc122b538cdeba410be31ea02c5bed92533978f832e internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=DNS loops call freshDNSStatus: a second, uncached lookup for a non-apps-domain name whose cached verdict is "" or "elsewhere"; it may only upgrade to "pointing" (#724) registry:Alias@3194,registry:Alias@3319,registry:Alias@3409,registry:Alias@3843,registry:Alias@3907,registry:Alias@3995,registry:DNSStatus@2948,registry:DNSStatus@3243,registry:Host@2899,registry:Host@3020,registry:Host@3104,registry:Host@3612,registry:Host@3671,registry:Host@3753,whole:Route@199,whole:Route@236,whole:Route@2856,whole:Route@3566,whole:RouteAlias@3150,whole:RouteAlias@3796,whole:RouteAlias@780 2f3e5a42dce0c5006e98ff8c37ce7ba10bd304b9be9a38d5e039233715b63d10 |
| +@legacy 5d3238530e5a92db8e4c656224087ad2a623211170b712dc3a812f55d33f0c52 internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) data-literal@469,data-literal@510 b5843939ae5cfaf543c55c0806fef21bfb4008c7ebbec67baf22480b9084fc39 |
| +@legacy 88b363b774cbdc2fd722b687c9ceb7acb0e8284d48cf48d60d20cd54fbb53b98 internal/domains/freshdns.go FreshResolver.ask legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one UDP DNS query to a zone nameserver; the Read is the socket reply, not a file (#724) file:Read@792 d789fcf6379853ba1e36ebc6cadf896af14af479d53b2683c02dfd019fe9ab6f |
| +internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=DNS loops call freshDNSStatus: a second, uncached lookup for a non-apps-domain name whose cached verdict is "" or "elsewhere"; it may only upgrade to "pointing" (#724) registry:Alias@3194,registry:Alias@3319,registry:Alias@3409,registry:Alias@3843,registry:Alias@3907,registry:Alias@3995,registry:DNSStatus@2948,registry:DNSStatus@3243,registry:Host@2899,registry:Host@3020,registry:Host@3104,registry:Host@3612,registry:Host@3671,registry:Host@3753,whole:Route@199,whole:Route@236,whole:Route@2856,whole:Route@3566,whole:RouteAlias@3150,whole:RouteAlias@3796,whole:RouteAlias@780 2f3e5a42dce0c5006e98ff8c37ce7ba10bd304b9be9a38d5e039233715b63d10 |
| +internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) data-literal@469,data-literal@510 b5843939ae5cfaf543c55c0806fef21bfb4008c7ebbec67baf22480b9084fc39 |
| +internal/domains/freshdns.go FreshResolver.ask legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one UDP DNS query to a zone nameserver; the Read is the socket reply, not a file (#724) file:Read@792 d789fcf6379853ba1e36ebc6cadf896af14af479d53b2683c02dfd019fe9ab6f |
| +@coverage internal/domains 010df09d6fdcd1e83569ba2dcb0e74f4f92e9e38eeb32b8531a7187ecafcb6ec |
| +@coverage internal/host 2ebf59ee154f59349da7887e5675ab5bd0d732ae8748685d8d2f5ecb4e9d7cdf |