carlos / platform Public

domains: ResolveAll (A and AAAA) and ErrBackingOff for fresh lookups

ResolveAll is Resolve with the name's AAAA records too, for the issuer's
cert warm gate (#724), which must see every address a CA could validate
against. An unresolved AAAA on a CNAME target fails the answer. Reused
from the earlier attempt, where review accepted the AAAA handling.

ErrBackingOff marks the answer given inside a name's failure backoff, so
a caller counting its fresh looks can tell one that asked nobody.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1b459f7b6150a0c789bfcc43092b6e1eec74fd2f parent aed8933
2 files changed, +252 −25
  • internal/domains/freshdns.go +112 −25
  • internal/domains/freshdns_test.go +140 −0
diff --git a/internal/domains/freshdns.go b/internal/domains/freshdns.go
index b05641a30..e43fbde3a 100644
--- a/internal/domains/freshdns.go
+++ b/internal/domains/freshdns.go
@@ -64,6 +64,11 @@ type FreshResolver struct {
// errNoAuthority is a fresh lookup that got no usable authoritative answer.
var errNoAuthority = errors.New("fresh dns: no authoritative answer")
+// ErrBackingOff is the answer for a name whose last lookup failed less than
+// Backoff ago: the failure again, without asking anyone. A caller that
+// counts the lookups it takes can tell this one cost nothing.
+var ErrBackingOff = errors.New("fresh dns: backing off after a failure")
+
const (
// freshMaxServers bounds the nameserver addresses tried per zone, so a
// zone of silent servers leaves time for the next zone up.
@@ -100,6 +105,16 @@ func freshPublicName(name string) bool {
// 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) {
+ return r.lookup(host, false)
+}
+
+// ResolveAll is Resolve with the name's AAAA records too (see askAll). It
+// shares Resolve's timeout, backoff and zone verification.
+func (r *FreshResolver) ResolveAll(host string) ([]string, error) {
+ return r.lookup(host, true)
+}
+
+func (r *FreshResolver) lookup(host string, all bool) ([]string, error) {
name := strings.ToLower(strings.TrimSuffix(host, "."))
if !freshPublicName(name) {
return nil, fmt.Errorf("%w: %s has no public zone", errNoAuthority, name)
@@ -121,7 +136,7 @@ func (r *FreshResolver) Resolve(host string) ([]string, error) {
}
if at, ok := r.failedAt[name]; ok {
r.mu.Unlock()
- return nil, fmt.Errorf("%w for %s (backing off since %s)", errNoAuthority, name, at.UTC().Format(time.RFC3339))
+ return nil, fmt.Errorf("%w: %w for %s (since %s)", errNoAuthority, ErrBackingOff, name, at.UTC().Format(time.RFC3339))
}
r.mu.Unlock()
@@ -131,7 +146,7 @@ func (r *FreshResolver) Resolve(host string) ([]string, error) {
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
- ips, err := r.resolve(ctx, name)
+ ips, err := r.resolve(ctx, name, all)
if err != nil && !isNotFound(err) {
r.mu.Lock()
@@ -174,7 +189,7 @@ func isNotFound(err error) bool {
// target, tied to the zone that really holds the name. A level the cached
// resolver already knows to be an alias is skipped without asking anyone,
// since a CNAME cannot sit at a zone apex.
-func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, error) {
+func (r *FreshResolver) resolve(ctx context.Context, name string, all bool) ([]string, error) {
last := fmt.Errorf("%w: no zone found for %s", errNoAuthority, name)
zones := 0
for zone := name; strings.Contains(zone, "."); zone = zone[strings.Index(zone, ".")+1:] {
@@ -204,7 +219,11 @@ func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, err
}
continue
}
- ips, err := r.ask(ctx, s, name, zone)
+ ask := r.ask
+ if all {
+ ask = r.askAll
+ }
+ ips, err := ask(ctx, s, name, zone)
if err == nil || isNotFound(err) {
return ips, err
}
@@ -383,24 +402,34 @@ func (r *FreshResolver) verifyZone(ctx context.Context, server netip.AddrPort, z
return fmt.Errorf("%w: %s does not hold zone %s", errNoAuthority, server, zone)
}
-// ask sends one non-recursive IN A query 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. A chain that loops, runs
-// past freshMaxCNAMEs, or ends inside zone with no address is not an
-// answer.
-func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) {
- reply, err := r.exchange(ctx, server, name, dnsmessage.TypeA)
+var (
+ // errNoData: the server answered with authority that name (or the end
+ // of its in-reply CNAME chain, if that is name itself) has no records
+ // of the asked type.
+ errNoData = errors.New("fresh dns: no records of that type")
+ // errInZoneNoData: the chain ends at another name inside the zone and
+ // the reply carries no records of the asked type for it.
+ errInZoneNoData = errors.New("fresh dns: in-zone CNAME target with no records of that type")
+)
+
+// 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) {
+ reply, err := r.exchange(ctx, server, name, qtype)
if err != nil {
- return nil, err
+ return nil, false, err
}
fqdn := name + "."
switch reply.RCode {
case dnsmessage.RCodeSuccess:
case dnsmessage.RCodeNameError:
- return nil, &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true}
+ return nil, false, &net.DNSError{Err: "no such host", Name: name, Server: server.String(), IsNotFound: true}
default:
- return nil, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name)
+ return nil, false, fmt.Errorf("%w: %s answered %s for %s", errNoAuthority, server, reply.RCode, name)
}
current := fqdn
@@ -418,30 +447,88 @@ func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zo
break
}
if seen[next] || hops >= freshMaxCNAMEs {
- return nil, fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name)
+ return nil, false, fmt.Errorf("%w: CNAME chain from %s loops or runs too long", errNoAuthority, name)
}
seen[next] = true
current = next
}
- var ips []string
for _, rr := range reply.Answers {
- if a, ok := rr.Body.(*dnsmessage.AResource); ok && rr.Header.Class == dnsmessage.ClassINET &&
- strings.EqualFold(rr.Header.Name.String(), current) {
- ips = append(ips, netip.AddrFrom4(a.A).String())
+ if rr.Header.Class != dnsmessage.ClassINET || !strings.EqualFold(rr.Header.Name.String(), current) {
+ continue
+ }
+ switch b := rr.Body.(type) {
+ case *dnsmessage.AResource:
+ if qtype == dnsmessage.TypeA {
+ ips = append(ips, netip.AddrFrom4(b.A).String())
+ }
+ case *dnsmessage.AAAAResource:
+ if qtype == dnsmessage.TypeAAAA {
+ ips = append(ips, netip.AddrFrom16(b.AAAA).String())
+ }
}
}
if len(ips) > 0 {
- return ips, nil
+ return ips, false, nil
}
target := strings.TrimSuffix(current, ".")
if target == name {
- // Authoritative, the name exists, and it has no A record.
- return nil, &net.DNSError{Err: "no A record", Name: name, Server: server.String(), IsNotFound: true}
+ return nil, false, 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, fmt.Errorf("%w: %s ends at in-zone %s with no address", errNoAuthority, name, target)
+ return nil, false, errInZoneNoData
+ }
+ ips, err = r.LookupHost(ctx, target)
+ return ips, true, err
+}
+
+// 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)
+ 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)
+ }
+ return ips, err
+}
+
+// askAll is ask plus the name's AAAA records from the same server, for a
+// caller that must see every address a client could be sent to (the cert
+// warm's gate: a CA validating over IPv6 against a foreign AAAA fails even
+// when every A record is ours). The AAAA half may be authoritatively empty
+// 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.
+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)
+ 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:
+ return nil, err
}
- return r.LookupHost(ctx, target)
+ if cached {
+ return v4, nil
+ }
+ v6, _, err := r.askType(ctx, server, name, zone, dnsmessage.TypeAAAA)
+ switch {
+ case err == nil:
+ return append(v4, v6...), nil
+ case errors.Is(err, errNoData):
+ // The name itself, with no CNAME, has no AAAA: complete evidence.
+ return v4, nil
+ case isNotFound(err):
+ // NXDOMAIN for AAAA right after addresses for A: inconsistent.
+ return nil, fmt.Errorf("%w: %s gave A records but NXDOMAIN for AAAA", errNoAuthority, name)
+ }
+ return nil, err
}
diff --git a/internal/domains/freshdns_test.go b/internal/domains/freshdns_test.go
index 7d6fd2cd9..40512e422 100644
--- a/internal/domains/freshdns_test.go
+++ b/internal/domains/freshdns_test.go
@@ -528,3 +528,143 @@ func TestFreshResolverVerifiesTheSOAOwner(t *testing.T) {
t.Fatalf("Resolve = %v, want an error", ips)
}
}
+
+func aaaaRecord(name, ip string) dnsmessage.Resource {
+ return dnsmessage.Resource{
+ Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName(name), Type: dnsmessage.TypeAAAA, Class: dnsmessage.ClassINET, TTL: 300},
+ Body: &dnsmessage.AAAAResource{AAAA: netip.MustParseAddr(ip).As16()},
+ }
+}
+
+// ResolveAll reports every address a client could be sent to: a foreign
+// AAAA next to our A must show up, or a gate that requires all addresses to
+// be ours would pass a name the CA validates over IPv6 elsewhere.
+func TestFreshResolverResolveAllIncludesAAAA(t *testing.T) {
+ fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool {
+ r.Authoritative = true
+ switch q.Type {
+ case dnsmessage.TypeA:
+ r.Answers = []dnsmessage.Resource{aRecord("shop.example.com.", edgeIP)}
+ case dnsmessage.TypeAAAA:
+ r.Answers = []dnsmessage.Resource{aaaaRecord("shop.example.com.", "2001:db8::66")}
+ }
+ return true
+ })
+ ips, err := oneZone(fa).resolver(clock()).ResolveAll("shop.example.com")
+ if err != nil || len(ips) != 2 || ips[0] != edgeIP || ips[1] != "2001:db8::66" {
+ t.Fatalf("ResolveAll = %v, %v; want [%s 2001:db8::66]", ips, err, edgeIP)
+ }
+ // Resolve (the stamp's) is unchanged: A only.
+ if ips, err := oneZone(fa).resolver(clock()).Resolve("shop.example.com"); err != nil || len(ips) != 1 {
+ t.Fatalf("Resolve = %v, %v; want A only", ips, err)
+ }
+}
+
+func TestFreshResolverResolveAllAcceptsAnEmptyAAAA(t *testing.T) {
+ fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool {
+ r.Authoritative = true
+ if q.Type == dnsmessage.TypeA {
+ r.Answers = []dnsmessage.Resource{aRecord("shop.example.com.", edgeIP)}
+ }
+ return true // AAAA: authoritative NODATA
+ })
+ ips, err := oneZone(fa).resolver(clock()).ResolveAll("shop.example.com")
+ if err != nil || len(ips) != 1 || ips[0] != edgeIP {
+ t.Fatalf("ResolveAll = %v, %v; want [%s]", ips, err, edgeIP)
+ }
+}
+
+func TestFreshResolverResolveAllFailsOnABrokenAAAA(t *testing.T) {
+ fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool {
+ if q.Type == dnsmessage.TypeAAAA {
+ r.RCode = dnsmessage.RCodeServerFailure
+ return true
+ }
+ r.Authoritative = true
+ r.Answers = []dnsmessage.Resource{aRecord("shop.example.com.", edgeIP)}
+ return true
+ })
+ if ips, err := oneZone(fa).resolver(clock()).ResolveAll("shop.example.com"); err == nil {
+ t.Fatalf("ResolveAll = %v, want an error: the AAAA half is unknown", ips)
+ }
+}
+
+// An out-of-zone CNAME target goes to the cached LookupHost, which answers
+// for both families; its IPv6 addresses come through.
+func TestFreshResolverResolveAllTakesBothFamiliesFromTheCNAMETarget(t *testing.T) {
+ fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "edge.oncarlos.test.")))
+ w := oneZone(fa)
+ w.hosts["edge.oncarlos.test"] = []string{edgeIP, "2001:db8::7"}
+ ips, err := w.resolver(clock()).ResolveAll("shop.example.com")
+ if err != nil || len(ips) != 2 || ips[1] != "2001:db8::7" {
+ t.Fatalf("ResolveAll = %v, %v; want both families from the target", ips, err)
+ }
+}
+
+// Astra R2 (follow-up) P1: a CNAME target the AAAA reply gives nothing for
+// may sit in a delegated child this server does not answer for, so its
+// IPv6 addresses are unknown: ResolveAll fails rather than reporting A only.
+func TestFreshResolverResolveAllRefusesAnUnresolvedAAAATarget(t *testing.T) {
+ fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool {
+ r.Authoritative = true
+ r.Answers = []dnsmessage.Resource{cnameRecord("shop.example.com.", "app.sub.example.com.")}
+ if q.Type == dnsmessage.TypeA {
+ r.Answers = append(r.Answers, aRecord("app.sub.example.com.", edgeIP))
+ }
+ return true
+ })
+ if ips, err := oneZone(fa).resolver(clock()).ResolveAll("shop.example.com"); err == nil {
+ t.Fatalf("ResolveAll = %v, want an error: the target's AAAA is unknown", ips)
+ }
+}
+
+// An IPv6-only name (AAAA, no A) is not reported as pointing anywhere the
+// gate could accept: the A half is authoritatively empty and ResolveAll
+// says "not found", so a caller requiring every address to be ours refuses.
+func TestFreshResolverResolveAllRefusesAnIPv6OnlyName(t *testing.T) {
+ fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool {
+ r.Authoritative = true
+ if q.Type == dnsmessage.TypeAAAA {
+ r.Answers = []dnsmessage.Resource{aaaaRecord("shop.example.com.", "2001:db8::66")}
+ }
+ return true
+ })
+ ips, err := oneZone(fa).resolver(clock()).ResolveAll("shop.example.com")
+ if len(ips) != 0 || err == nil {
+ t.Fatalf("ResolveAll = %v, %v; want no addresses and an error", ips, err)
+ }
+}
+
+// A failed ResolveAll backs off per name like Resolve, the answer inside the
+// backoff says so (ErrBackingOff, so a caller counting real lookups can tell
+// it cost nothing), and Backoff sets how long.
+func TestFreshResolverResolveAllBacksOffPerName(t *testing.T) {
+ fa := startAuthority(t, "example.com", nil)
+ now := clock()
+ r := oneZone(fa).resolver(now)
+ r.Backoff = 5 * time.Minute
+ _, err := r.ResolveAll("shop.example.com")
+ if err == nil || errors.Is(err, domains.ErrBackingOff) {
+ t.Fatalf("first ResolveAll = %v; want a real failure", err)
+ }
+ sent := fa.queries.Load()
+ *now = now.Add(4 * time.Minute)
+ if _, err := r.ResolveAll("shop.example.com"); !errors.Is(err, domains.ErrBackingOff) {
+ t.Fatalf("ResolveAll inside the backoff = %v, want ErrBackingOff", err)
+ }
+ if fa.queries.Load() != sent {
+ t.Fatal("ResolveAll inside the backoff queried again")
+ }
+ // Another name is not held back by this one's failure.
+ if _, err := r.ResolveAll("www.example.com"); errors.Is(err, domains.ErrBackingOff) {
+ t.Fatal("a different name was backed off")
+ }
+ *now = now.Add(time.Minute)
+ sent = fa.queries.Load()
+ if _, err := r.ResolveAll("shop.example.com"); errors.Is(err, domains.ErrBackingOff) {
+ t.Fatalf("ResolveAll after Backoff still backing off: %v", err)
+ }
+ if fa.queries.Load() == sent {
+ t.Fatal("ResolveAll after Backoff did not query again")
+ }
+}