zonehost: unpointed zones and one canonical pointed set
Validate accepts the empty ManagedSet (an unpointed zone); www or TXT without an apex target is still refused. SetManaged with the empty set deletes the apex A sets, the www alias and the apex/www challenge TXT, nothing else. ManagedFor builds the one canonical pointed set: apex targets, www, and the apex and www proofs when a proof func is given. Route 53: managedRecords returns nothing for the empty set, and the steering source refuses an empty answer explicitly now that Validate no longer does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+166
−2
internal/zonehost/route53zones/route53.go+9 −0internal/zonehost/route53zones/route53_test.go+54 −0internal/zonehost/zonehost.go+24 −1internal/zonehost/zonehost_test.go+79 −1
CI passed — run details
diff --git a/internal/zonehost/route53zones/route53.go b/internal/zonehost/route53zones/route53.go| index 941d7ee60..ce7064161 100644 |
| --- a/internal/zonehost/route53zones/route53.go |
| +++ b/internal/zonehost/route53zones/route53.go |
| @@ -273,6 +273,11 @@ func (h *Host) SetManaged(ctx context.Context, z zonehost.Zone, m zonehost.Manag |
| } |
| func managedRecords(z zonehost.Zone, m zonehost.ManagedSet) []r53types.ResourceRecordSet { |
| var records []r53types.ResourceRecordSet |
| + if len(m.Apex) == 0 { |
| + // Unpointed: Validate allows no www or TXT here, so nothing is wanted |
| + // and SetManaged deletes every owned set. |
| + return nil |
| + } |
| if m.Apex[0].Region == "" { |
| r := r53types.ResourceRecordSet{Name: aws.String(fqdn(z.Domain)), Type: r53types.RRTypeA, TTL: aws.Int64(300)} |
| for _, a := range m.Apex { |
| @@ -381,6 +386,10 @@ func (s steered) ApexTargets(ctx context.Context) ([]zonehost.ApexTarget, error) |
| } |
| targets = append(targets, zonehost.ApexTarget{IP: aws.ToString(r.ResourceRecords[0].Value), Region: string(r.Region), SetID: aws.ToString(r.SetIdentifier), HealthCheckID: aws.ToString(r.HealthCheckId)}) |
| } |
| + // An empty set is valid (unpointed) but never a steering answer. |
| + if len(targets) == 0 { |
| + return nil, fmt.Errorf("route53zones: steering host has no A records") |
| + } |
| if err := (zonehost.ManagedSet{Apex: targets}).Validate(); err != nil { |
| return nil, fmt.Errorf("route53zones: invalid steering targets: %w", err) |
| } |
diff --git a/internal/zonehost/route53zones/route53_test.go b/internal/zonehost/route53zones/route53_test.go| index 4b92fbc4d..23344519d 100644 |
| --- a/internal/zonehost/route53zones/route53_test.go |
| +++ b/internal/zonehost/route53zones/route53_test.go |
| @@ -292,6 +292,60 @@ func TestSetManagedOwnership(t *testing.T) { |
| } |
| } |
| } |
| +func TestSetManagedEmptyDeletesApexWWWAndProofs(t *testing.T) { |
| + for _, m := range []zonehost.ManagedSet{latency(), plain()} { |
| + h, f, z := fixture() |
| + ctx := context.Background() |
| + mx := rr("shop.example.", r53types.RRTypeMx, "10 mail.example.") |
| + other := rr("verify.shop.example.", r53types.RRTypeTxt, `"customer"`) |
| + f.records = append(f.records, mx, other) |
| + f.pageSize = 1 |
| + proof := func(host string) zonehost.TXT { |
| + return zonehost.TXT{Name: "_carlos-challenge." + host, Value: "proof " + host} |
| + } |
| + if err := h.SetManaged(ctx, z, zonehost.ManagedFor(z.Domain, m.Apex, proof)); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := len(f.records) |
| + if err := h.SetManaged(ctx, z, zonehost.ManagedSet{}); err != nil { |
| + t.Fatalf("empty set: %v", err) |
| + } |
| + last := f.batches[len(f.batches)-1] |
| + for _, c := range last { |
| + if c.Action != r53types.ChangeActionDelete { |
| + t.Fatalf("empty set wrote %#v", c) |
| + } |
| + } |
| + // apex A sets (one plain or one per latency target), www alias, two proofs |
| + apexSets := len(m.Apex) |
| + if m.Apex[0].Region == "" { |
| + apexSets = 1 |
| + } |
| + if len(last) != apexSets+3 || len(f.records) != before-len(last) { |
| + t.Fatalf("deleted %d of %d records: %#v", len(last), before, last) |
| + } |
| + want := []r53types.ResourceRecordSet{f.records[0], f.records[1], mx, other} |
| + if len(f.records) != 4 { |
| + t.Fatalf("records left = %#v", f.records) |
| + } |
| + for _, r := range want { |
| + found := false |
| + for _, got := range f.records { |
| + if reflect.DeepEqual(r, got) { |
| + found = true |
| + } |
| + } |
| + if !found { |
| + t.Fatalf("unrelated record changed: %#v", r) |
| + } |
| + } |
| + // Unpointing an unpointed zone writes nothing. |
| + batches := len(f.batches) |
| + if err := h.SetManaged(ctx, z, zonehost.ManagedSet{}); err != nil || len(f.batches) != batches { |
| + t.Fatalf("second empty set: %v, batches %d -> %d", err, batches, len(f.batches)) |
| + } |
| + } |
| +} |
| func TestSetManagedSwitchesRouting(t *testing.T) { |
| h, f, z := fixture() |
| ctx := context.Background() |
diff --git a/internal/zonehost/zonehost.go b/internal/zonehost/zonehost.go| index 2fed2a66f..f1c0d1493 100644 |
| --- a/internal/zonehost/zonehost.go |
| +++ b/internal/zonehost/zonehost.go |
| @@ -5,6 +5,7 @@ import ( |
| "context" |
| "fmt" |
| "net/netip" |
| + "slices" |
| "strings" |
| "github.com/carlosframework/platform/internal/domains" |
| @@ -12,6 +13,10 @@ import ( |
| type ApexTarget struct{ IP, Region, SetID, HealthCheckID string } |
| type TXT struct{ Name, Value string } |
| + |
| +// ManagedSet is every record the platform owns in a bought domain's zone. |
| +// The empty set is an unpointed zone: it exists, but nothing in it points |
| +// anywhere. A pointed zone is always built by ManagedFor. |
| type ManagedSet struct { |
| Apex []ApexTarget |
| WWW bool |
| @@ -27,6 +32,7 @@ type Host interface { |
| DeleteZone(ctx context.Context, z Zone) error |
| // SetManaged replaces apex A, www A and the apex/www challenge TXT sets. |
| // All other name+type pairs belong to other writers and are left alone. |
| + // The empty ManagedSet deletes exactly those managed sets (unpointing). |
| SetManaged(ctx context.Context, z Zone, m ManagedSet) error |
| Nameservers() []string |
| } |
| @@ -47,9 +53,26 @@ func (s staticApex) ApexTargets(ctx context.Context) ([]ApexTarget, error) { |
| return append([]ApexTarget(nil), s...), ctx.Err() |
| } |
| +// ManagedFor is the one canonical set for a pointed zone: the apex targets, |
| +// www, and, when proof is non-nil, the proof TXT for the apex and for www. |
| +// Every writer of a pointed zone uses it, so every write is the same set and |
| +// rewriting it is a no-op. It names nothing beyond the apex and www. |
| +func ManagedFor(domain string, apex []ApexTarget, proof func(host string) TXT) ManagedSet { |
| + m := ManagedSet{Apex: slices.Clone(apex), WWW: true} |
| + if proof != nil { |
| + m.TXT = []TXT{proof(domain), proof("www." + domain)} |
| + } |
| + return m |
| +} |
| + |
| +// Validate accepts the empty set (an unpointed zone) or a set with at least |
| +// one apex target. www and proof records never stand without an apex. |
| func (m ManagedSet) Validate() error { |
| if len(m.Apex) == 0 { |
| - return fmt.Errorf("zonehost: at least one apex target is required") |
| + if m.WWW || len(m.TXT) > 0 { |
| + return fmt.Errorf("zonehost: www and TXT records require an apex target") |
| + } |
| + return nil |
| } |
| latency := m.Apex[0].Region != "" |
| ids := map[string]bool{} |
diff --git a/internal/zonehost/zonehost_test.go b/internal/zonehost/zonehost_test.go| index 0d8b7b627..a439f1734 100644 |
| --- a/internal/zonehost/zonehost_test.go |
| +++ b/internal/zonehost/zonehost_test.go |
| @@ -16,7 +16,6 @@ func TestManagedSetValidate(t *testing.T) { |
| apex []zonehost.ApexTarget |
| bad bool |
| }{ |
| - {"empty", nil, true}, |
| {"plain", []zonehost.ApexTarget{{IP: "192.0.2.1"}, {IP: "192.0.2.2"}}, false}, |
| {"latency", []zonehost.ApexTarget{{IP: "192.0.2.1", Region: "eu-west-1", SetID: "eu"}, {IP: "192.0.2.2", Region: "us-east-1", SetID: "us"}}, false}, |
| {"mixed", []zonehost.ApexTarget{{IP: "192.0.2.1", Region: "eu-west-1", SetID: "eu"}, {IP: "192.0.2.2"}}, true}, |
| @@ -33,6 +32,85 @@ func TestManagedSetValidate(t *testing.T) { |
| } |
| } |
| +// An empty set is an unpointed zone: the zone exists, nothing points |
| +// anywhere. www and proof records only make sense beside an apex. |
| +func TestManagedSetValidateEmpty(t *testing.T) { |
| + if err := (zonehost.ManagedSet{}).Validate(); err != nil { |
| + t.Fatalf("empty set refused: %v", err) |
| + } |
| + if err := (zonehost.ManagedSet{WWW: true}).Validate(); err == nil { |
| + t.Fatal("www without apex accepted") |
| + } |
| + txt := []zonehost.TXT{{Name: "_carlos-challenge.shop.example", Value: "proof"}} |
| + if err := (zonehost.ManagedSet{TXT: txt}).Validate(); err == nil { |
| + t.Fatal("TXT without apex accepted") |
| + } |
| + if err := (zonehost.ManagedSet{}).ValidateForZone(zonehost.Zone{ID: "zone-1", Domain: "shop.example"}); err != nil { |
| + t.Fatalf("empty set refused for zone: %v", err) |
| + } |
| +} |
| + |
| +func TestManagedForProofsWithoutExtraNames(t *testing.T) { |
| + apex := []zonehost.ApexTarget{{IP: "192.0.2.1"}, {IP: "192.0.2.2"}} |
| + var asked []string |
| + proof := func(host string) zonehost.TXT { |
| + asked = append(asked, host) |
| + return zonehost.TXT{Name: "_carlos-challenge." + host, Value: "token:" + host} |
| + } |
| + got := zonehost.ManagedFor("shop.example", apex, proof) |
| + want := zonehost.ManagedSet{Apex: []zonehost.ApexTarget{{IP: "192.0.2.1"}, {IP: "192.0.2.2"}}, WWW: true, TXT: []zonehost.TXT{ |
| + {Name: "_carlos-challenge.shop.example", Value: "token:shop.example"}, |
| + {Name: "_carlos-challenge.www.shop.example", Value: "token:www.shop.example"}, |
| + }} |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("ManagedFor = %#v, want %#v", got, want) |
| + } |
| + if !reflect.DeepEqual(asked, []string{"shop.example", "www.shop.example"}) { |
| + t.Fatalf("proof asked for %v", asked) |
| + } |
| + if err := got.ValidateForZone(zonehost.Zone{ID: "zone-1", Domain: "shop.example"}); err != nil { |
| + t.Fatalf("canonical set refused: %v", err) |
| + } |
| + apex[0].IP = "192.0.2.9" |
| + if got.Apex[0].IP != "192.0.2.1" { |
| + t.Fatal("ManagedFor retained caller slice") |
| + } |
| + if again := zonehost.ManagedFor("shop.example", []zonehost.ApexTarget{{IP: "192.0.2.1"}, {IP: "192.0.2.2"}}, proof); !reflect.DeepEqual(again, want) { |
| + t.Fatalf("not deterministic: %#v", again) |
| + } |
| + |
| + bare := zonehost.ManagedFor("shop.example", apex, nil) |
| + if !bare.WWW || len(bare.TXT) != 0 || !reflect.DeepEqual(bare.Apex, apex) { |
| + t.Fatalf("ManagedFor without proof = %#v", bare) |
| + } |
| +} |
| + |
| +func TestFakeSetManagedEmptyClearsApex(t *testing.T) { |
| + ctx := context.Background() |
| + f := zonehosttest.New("ns1.brand.example") |
| + z, err := f.CreateZone(ctx, "shop.example", "key") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + proof := func(host string) zonehost.TXT { return zonehost.TXT{Name: "_carlos-challenge." + host, Value: "p"} } |
| + if err := f.SetManaged(ctx, z, zonehost.ManagedFor(z.Domain, []zonehost.ApexTarget{{IP: "192.0.2.1"}}, proof)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := f.SetManaged(ctx, z, zonehost.ManagedSet{}); err != nil { |
| + t.Fatalf("empty set refused: %v", err) |
| + } |
| + got, ok := f.Managed[z.Domain] |
| + if !ok || len(got.Apex) != 0 || got.WWW || len(got.TXT) != 0 { |
| + t.Fatalf("managed after unpointing = %#v (present %v)", got, ok) |
| + } |
| + if f.Zones[z.Domain] != z { |
| + t.Fatal("unpointing removed the zone") |
| + } |
| + if err := f.SetManaged(ctx, z, zonehost.ManagedSet{WWW: true}); err == nil { |
| + t.Fatal("www without apex accepted") |
| + } |
| +} |
| + |
| func TestFakeSetManagedReplaces(t *testing.T) { |
| ctx := context.Background() |
| f := zonehosttest.New("ns1.brand.example.") |