carlos / platform Public

route53zones: delete alias records before the sets they target

SetManaged and DeleteZone emit alias deletes first in each batch. The
empty-set test snapshots SOA/NS before SetManaged, and ManagedFor with no
apex is pinned as invalid.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 569c35bfefcf09c7366d7ee6e8794dde1bc892a4 parent f3f3892
3 files changed, +65 −1
  • internal/zonehost/route53zones/route53.go +16 −0
  • internal/zonehost/route53zones/route53_test.go +42 −1
  • internal/zonehost/zonehost_test.go +7 −0
diff --git a/internal/zonehost/route53zones/route53.go b/internal/zonehost/route53zones/route53.go
index ce7064161..86c428020 100644
--- a/internal/zonehost/route53zones/route53.go
+++ b/internal/zonehost/route53zones/route53.go
@@ -266,6 +266,7 @@ func (h *Host) SetManaged(ctx context.Context, z zonehost.Zone, m zonehost.Manag
changes = append(changes, r53types.Change{Action: r53types.ChangeActionDelete, ResourceRecordSet: &r})
}
}
+ aliasesFirst(changes)
for _, r := range desired {
changes = append(changes, r53types.Change{Action: r53types.ChangeActionUpsert, ResourceRecordSet: &r})
}
@@ -310,6 +311,20 @@ func managedRecords(z zonehost.Zone, m zonehost.ManagedSet) []r53types.ResourceR
return records
}
+// aliasesFirst orders deletes so every alias record goes before the
+// records it may target (the www alias before the apex A sets), keeping
+// the listing's order otherwise. Route 53 applies a batch as one change,
+// so this is hardening, not a known rejection.
+func aliasesFirst(deletes []r53types.Change) {
+ plain := func(c r53types.Change) int {
+ if c.ResourceRecordSet.AliasTarget != nil {
+ return 0
+ }
+ return 1
+ }
+ slices.SortStableFunc(deletes, func(a, b r53types.Change) int { return plain(a) - plain(b) })
+}
+
// DeleteZone destroys all contents, including customer and email records.
// Only the zone's own SOA and NS remain for DeleteHostedZone to remove.
func (h *Host) DeleteZone(ctx context.Context, z zonehost.Zone) error {
@@ -327,6 +342,7 @@ func (h *Host) DeleteZone(ctx context.Context, z zonehost.Zone) error {
}
changes = append(changes, r53types.Change{Action: r53types.ChangeActionDelete, ResourceRecordSet: &r})
}
+ aliasesFirst(changes)
// Keep teardown batches within the API's 1,000 resource-record limit.
for len(changes) > 0 {
end, count := 0, 0
diff --git a/internal/zonehost/route53zones/route53_test.go b/internal/zonehost/route53zones/route53_test.go
index 23344519d..81cbdffa6 100644
--- a/internal/zonehost/route53zones/route53_test.go
+++ b/internal/zonehost/route53zones/route53_test.go
@@ -296,6 +296,8 @@ func TestSetManagedEmptyDeletesApexWWWAndProofs(t *testing.T) {
for _, m := range []zonehost.ManagedSet{latency(), plain()} {
h, f, z := fixture()
ctx := context.Background()
+ // The zone's own SOA and NS, as they are before anything is set.
+ soa, ns := f.records[0], f.records[1]
mx := rr("shop.example.", r53types.RRTypeMx, "10 mail.example.")
other := rr("verify.shop.example.", r53types.RRTypeTxt, `"customer"`)
f.records = append(f.records, mx, other)
@@ -324,7 +326,8 @@ func TestSetManagedEmptyDeletesApexWWWAndProofs(t *testing.T) {
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}
+ aliasDeletesFirst(t, last)
+ want := []r53types.ResourceRecordSet{soa, ns, mx, other}
if len(f.records) != 4 {
t.Fatalf("records left = %#v", f.records)
}
@@ -386,6 +389,44 @@ func TestDeleteZoneRemovesEverythingButOwnSOANS(t *testing.T) {
t.Fatalf("teardown: records %v, deleted %v", f.records, f.deleted)
}
}
+// aliasDeletesFirst fails unless every alias delete in batch comes before
+// every other delete, and at least one alias is deleted: a www alias goes
+// before the apex sets it targets.
+func aliasDeletesFirst(t *testing.T, batch []r53types.Change) {
+ t.Helper()
+ aliases, plainSeen := 0, false
+ for i, c := range batch {
+ if c.Action != r53types.ChangeActionDelete {
+ continue
+ }
+ if c.ResourceRecordSet.AliasTarget == nil {
+ plainSeen = true
+ continue
+ }
+ aliases++
+ if plainSeen {
+ t.Fatalf("alias delete %d (%s) after a non-alias delete: %#v", i, aws.ToString(c.ResourceRecordSet.Name), batch)
+ }
+ }
+ if aliases == 0 {
+ t.Fatalf("no alias delete in %#v", batch)
+ }
+}
+func TestDeleteZoneDeletesAliasesFirst(t *testing.T) {
+ h, f, z := fixture()
+ ctx := context.Background()
+ if err := h.SetManaged(ctx, z, zonehost.ManagedFor(z.Domain, plain().Apex, nil)); err != nil {
+ t.Fatal(err)
+ }
+ f.records = append(f.records, rr("shop.example.", r53types.RRTypeMx, "10 mail.example."))
+ if err := h.DeleteZone(ctx, z); err != nil {
+ t.Fatal(err)
+ }
+ aliasDeletesFirst(t, f.batches[len(f.batches)-1])
+ if len(f.records) != 2 {
+ t.Fatalf("records left = %#v", f.records)
+ }
+}
func TestDeleteZoneNotFoundAndFailures(t *testing.T) {
for _, stage := range []string{"list", "change", "delete"} {
for _, missing := range []bool{false, true} {
diff --git a/internal/zonehost/zonehost_test.go b/internal/zonehost/zonehost_test.go
index a439f1734..e4b04216b 100644
--- a/internal/zonehost/zonehost_test.go
+++ b/internal/zonehost/zonehost_test.go
@@ -48,6 +48,13 @@ func TestManagedSetValidateEmpty(t *testing.T) {
if err := (zonehost.ManagedSet{}).ValidateForZone(zonehost.Zone{ID: "zone-1", Domain: "shop.example"}); err != nil {
t.Fatalf("empty set refused for zone: %v", err)
}
+ // The canonical pointed set with no apex is refused, proofs or not.
+ proof := func(host string) zonehost.TXT { return zonehost.TXT{Name: "_carlos-challenge." + host, Value: "proof"} }
+ for _, p := range []func(string) zonehost.TXT{proof, nil} {
+ if err := zonehost.ManagedFor("shop.example", nil, p).Validate(); err == nil {
+ t.Fatal("ManagedFor with no apex accepted")
+ }
+ }
}
func TestManagedForProofsWithoutExtraNames(t *testing.T) {