domains: retire account namespace claims instead of deleting them
Spec rev 4 (§4.3): remove retires a name for an hour before another account can have it, fixing the remove-then-reassign race (Astra final review P1). - Claim.RetiredAt (retired_at, RFC 3339 UTC), declared with its first writer per the round-trip rule. - AccountNamespaces skips retired claims, so boxes derive nothing from one and account_namespaces (and the edge's wildcard parent) drops it. RetiredAccountNamespaces lists them; a retired_at that does not parse is neither in force nor retired. - New etag-conditional writers, each answering the ETag of its own Put: RetireAccountNamespace (the holder's active claim), Unretire (the same account, stamping a fresh created_at/by so the bytes never repeat an earlier revision), ReplaceRetiredAccountNamespace (another account, only from NamespaceCooldown after retired_at, else ErrNamespaceCooling) and RestoreRetiredAccountNamespace (the undo of an unretire or replace, only over the undone write's ETag). - AttachAccountNamespace now answers its Put's ETag too (F3: rollback owns only its own write). DetachAccountNamespaceAt stays for the rollback of a fresh create.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 files changed,
+497
−43
internal/console/apisubdomain.go+1 −1internal/console/domainproof_test.go+1 −1internal/console/subdomains_test.go+1 −1internal/domains/claims.go+8 −0internal/domains/namespace.go+237 −28internal/domains/namespace_retire_test.go+237 −0internal/domains/namespace_test.go+7 −7internal/domains/namespacealiases_ha_test.go+1 −1internal/domains/namespacealiases_test.go+1 −1internal/edge/storedoor_test.go+1 −1internal/host/edgesfile_test.go+2 −2
diff --git a/internal/console/apisubdomain.go b/internal/console/apisubdomain.go| index 349c24757..c80b0cdeb 100644 |
| --- a/internal/console/apisubdomain.go |
| +++ b/internal/console/apisubdomain.go |
| @@ -343,7 +343,7 @@ func (s *Server) handleAPISubdomainSet(w http.ResponseWriter, r *http.Request) { |
| } |
| domain := s.appsDomain() |
| host := domains.NamespaceHost(name, domain) |
| - created, err := domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| + _, created, err := domains.AttachAccountNamespace(ctx, s.cfg.Releases, name, domain, acct.Sqid, |
| time.Now().UTC().Format(time.RFC3339), addr) |
| if err != nil { |
| log.Printf("console: subdomain set %s %s: write: %v", acct.Sqid, name, err) |
diff --git a/internal/console/domainproof_test.go b/internal/console/domainproof_test.go| index 0833c1517..8071c126c 100644 |
| --- a/internal/console/domainproof_test.go |
| +++ b/internal/console/domainproof_test.go |
| @@ -434,7 +434,7 @@ func TestOwnSubdomainNeedsNoProof(t *testing.T) { |
| seedChannel(t, fx, "story", "canary/live", "v1") |
| lookups := 0 |
| fx.srv.lookupTXT = noTXT(&lookups) |
| - if _, err := domains.AttachAccountNamespace(context.Background(), fx.rel, "harbour", "oncarlos.com", fx.sqid, "2026-11-11T00:00:00Z", "ops@example.com"); err != nil { |
| + if _, _, err := domains.AttachAccountNamespace(context.Background(), fx.rel, "harbour", "oncarlos.com", fx.sqid, "2026-11-11T00:00:00Z", "ops@example.com"); err != nil { |
| t.Fatal(err) |
| } |
| v, err := fx.srv.loadNamespaces(context.Background()) |
diff --git a/internal/console/subdomains_test.go b/internal/console/subdomains_test.go| index adbbb768e..ec015685c 100644 |
| --- a/internal/console/subdomains_test.go |
| +++ b/internal/console/subdomains_test.go |
| @@ -97,7 +97,7 @@ func nsAccount(t *testing.T, fx *consoleFixture, id int64, sqid string) { |
| // nsHold gives account the subdomain name, as the Task 6 door will. |
| func nsHold(t *testing.T, fx *consoleFixture, name, account string) { |
| t.Helper() |
| - created, err := domains.AttachAccountNamespace(context.Background(), fx.rel, name, nsApps, account, |
| + _, created, err := domains.AttachAccountNamespace(context.Background(), fx.rel, name, nsApps, account, |
| time.Now().UTC().Format(time.RFC3339), "ops@example.com") |
| if err != nil || !created { |
| t.Fatalf("hold %s for %s: created %v, %v", name, account, created, err) |
diff --git a/internal/domains/claims.go b/internal/domains/claims.go| index 883472390..df7d9fb7d 100644 |
| --- a/internal/domains/claims.go |
| +++ b/internal/domains/claims.go |
| @@ -107,6 +107,14 @@ type Claim struct { |
| // so no ordinary door can write, rewrite or detach one. Declared here for |
| // the round-trip reason above. |
| AccountNamespace bool `json:"account_namespace,omitempty"` |
| + // RetiredAt marks a retired account namespace claim (spec 2026-11-11 |
| + // §4.3, rev 4): when its account removed the name (RFC 3339, UTC). The |
| + // claim stays at its key so the name cannot pass to another account |
| + // before NamespaceCooldown has run; no box derives anything from it |
| + // (AccountNamespaces skips it). Only the namespace writers in |
| + // namespace.go set or clear it, and only on an AccountNamespace claim. |
| + // Declared here for the round-trip reason above. |
| + RetiredAt string `json:"retired_at,omitempty"` |
| // Placements makes this a regional claim (#447): the hostname is served |
| // in each listed region by that region's bound host, never backhauled. |
| // Only SetClaimPlacements writes it, and only on a schema-2 claim, which |
diff --git a/internal/domains/namespace.go b/internal/domains/namespace.go| index 6ba176999..1930566bc 100644 |
| --- a/internal/domains/namespace.go |
| +++ b/internal/domains/namespace.go |
| @@ -15,6 +15,7 @@ import ( |
| "fmt" |
| "os" |
| "strings" |
| + "time" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -120,10 +121,12 @@ func ParseNamespaceHost(host, appsDomain string) (name string, ok bool) { |
| } |
| // AttachAccountNamespace writes name's namespace claim for account, |
| -// create-only by key. created is false with a nil error when the key already |
| -// holds a claim of any kind (this account's namespace, another account's, or |
| -// an instance catch-all on the same parent): the caller reads the key to |
| -// tell those apart. A lost create race answers the same way. |
| +// create-only by key, and answers the ETag of its write, which a caller that |
| +// may have to undo the write conditions the undo on (DetachAccountNamespaceAt). |
| +// created is false with a nil error (and no ETag) when the key already holds |
| +// a claim of any kind (this account's namespace, active or retired, another |
| +// account's, or an instance catch-all on the same parent): the caller reads |
| +// the key to tell those apart. A lost create race answers the same way. |
| // |
| // Only the name's shape is checked here. The rules of spec §4.2 are the |
| // console door's (SubdomainRuleRefusal and the store reads it adds). |
| @@ -132,35 +135,37 @@ func ParseNamespaceHost(host, appsDomain string) (name string, ok bool) { |
| // claims.go: the record is account-level and names no app, the host-claim |
| // mutation boundary refuses an empty app (hostops app_mutation_runner, |
| // mutation_classifier), and the console door refuses while host claims are |
| -// on. |
| -func AttachAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, createdAt, createdBy string) (created bool, err error) { |
| +// on. The same holds for every namespace writer below. |
| +func AttachAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, createdAt, createdBy string) (etag string, created bool, err error) { |
| host, err := namespaceHostChecked(name, appsDomain, account) |
| if err != nil { |
| - return false, err |
| + return "", false, err |
| } |
| body, err := json.Marshal(Claim{ |
| Schema: 1, Host: host, Account: account, |
| CreatedAt: createdAt, CreatedBy: createdBy, AccountNamespace: true, |
| }) |
| if err != nil { |
| - return false, err |
| + return "", false, err |
| } |
| if _, _, err := getClaimETag(ctx, s, host); err == nil { |
| - return false, nil |
| + return "", false, nil |
| } else if !errors.Is(err, os.ErrNotExist) { |
| - return false, err |
| + return "", false, err |
| } |
| - if _, err := s.Put(ctx, ClaimKey(host), body, ""); errors.Is(err, release.ErrCASConflict) { |
| - return false, nil // lost a create race: the key is someone's now |
| + etag, err = s.Put(ctx, ClaimKey(host), body, "") |
| + if errors.Is(err, release.ErrCASConflict) { |
| + return "", false, nil // lost a create race: the key is someone's now |
| } else if err != nil { |
| - return false, err |
| + return "", false, err |
| } |
| - return true, nil |
| + return etag, true, nil |
| } |
| // DetachAccountNamespaceAt deletes name's namespace claim only when it is |
| -// still account's namespace claim and its ETag is etag, the revision the |
| -// caller read and judged. The delete is release.DeleteIfMatch, so a claim |
| +// still account's namespace claim and its ETag is etag: the set door's |
| +// rollback of its own fresh create, conditioned on the ETag that create |
| +// answered. Remove never deletes (it retires, RetireAccountNamespace). The delete is release.DeleteIfMatch, so a claim |
| // replaced between the read here and the delete is not removed either. Any |
| // other claim at the key (an instance catch-all), a changed or an absent one |
| // answers removed=false. A store that cannot delete conditionally refuses |
| @@ -209,18 +214,170 @@ func namespaceHostChecked(name, appsDomain, account string) (string, error) { |
| return NamespaceHost(name, appsDomain), nil |
| } |
| +// NamespaceCooldown is how long a retired name stays out of every other |
| +// account's reach (spec §4.3, rev 4): a create its old holder started before |
| +// the removal lands within one request (the console reads a body within 5 |
| +// minutes), so after the hour the occupancy walk sees it, and boxes have |
| +// dropped the old rows, before the name can change hands. |
| +const NamespaceCooldown = time.Hour |
| + |
| +// ErrNamespaceCooling is ReplaceRetiredAccountNamespace's refusal of a |
| +// retired claim whose cooldown has not run. |
| +var ErrNamespaceCooling = errors.New("domains: account namespace: retired less than NamespaceCooldown ago") |
| + |
| +// RetiredNamespace is a retired namespace claim: the account that held the |
| +// name and when it removed it. |
| +type RetiredNamespace struct { |
| + Account string |
| + RetiredAt time.Time |
| +} |
| + |
| +// AvailableAt is when another account may take the name. |
| +func (r RetiredNamespace) AvailableAt() time.Time { return r.RetiredAt.Add(NamespaceCooldown) } |
| + |
| +// RetireAccountNamespace stamps name's namespace claim retired at retiredAt |
| +// and answers the ETag of that write. It writes only when the claim is still |
| +// account's active namespace claim, one AccountNamespaces trusts, at etag, |
| +// the revision the caller read and judged; the write is conditional on that |
| +// revision too. Anything else at the key, a changed or an absent claim |
| +// answers retired=false. The claim stays at its key: nothing deletes a |
| +// retired claim but an operator's direct bucket write. |
| +func RetireAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, etag string, retiredAt time.Time) (newETag string, retired bool, err error) { |
| + host, err := namespaceHostChecked(name, appsDomain, account) |
| + if err != nil { |
| + return "", false, err |
| + } |
| + cur, ok, err := namespaceAt(ctx, s, host, etag) |
| + if err != nil || !ok || cur.Account != account || cur.RetiredAt != "" || !trustedNamespace(*cur, name, appsDomain) { |
| + return "", false, err |
| + } |
| + next := *cur |
| + next.RetiredAt = retiredAt.UTC().Format(time.RFC3339) |
| + return putNamespace(ctx, s, host, next, etag) |
| +} |
| + |
| +// UnretireAccountNamespace clears the retirement of name's namespace claim |
| +// for the account that retired it, and answers the ETag of that write: the |
| +// same account may set its name again at once (spec §4.3). It writes only |
| +// when the claim at etag is account's retired namespace claim |
| +// (RetiredAccountNamespaces trusts it), conditional on that revision; |
| +// anything else answers unretired=false. |
| +// |
| +// The claim is stamped with now and createdBy as a fresh set is. Besides |
| +// saying who set the name last, that keeps a retire-then-unretire from |
| +// writing the bytes of an earlier revision back: a store's ETag is a hash |
| +// of the body, so identical bytes would let an undo conditioned on the |
| +// earlier write's ETag land on the later one. |
| +func UnretireAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, etag string, now time.Time, createdBy string) (newETag string, unretired bool, err error) { |
| + host, err := namespaceHostChecked(name, appsDomain, account) |
| + if err != nil { |
| + return "", false, err |
| + } |
| + cur, ok, err := namespaceAt(ctx, s, host, etag) |
| + if err != nil || !ok || cur.Account != account { |
| + return "", false, err |
| + } |
| + if _, ok := retiredNamespace(*cur, name, appsDomain); !ok { |
| + return "", false, nil |
| + } |
| + next := *cur |
| + next.RetiredAt = "" |
| + next.CreatedAt, next.CreatedBy = now.UTC().Format(time.RFC3339), createdBy |
| + return putNamespace(ctx, s, host, next, etag) |
| +} |
| + |
| +// ReplaceRetiredAccountNamespace gives name to account in place of another |
| +// account's retired namespace claim, and answers the ETag of that write. It |
| +// writes only when the claim at etag is a retired namespace claim |
| +// (RetiredAccountNamespaces trusts it) of another account, conditional on |
| +// that revision, and only from NamespaceCooldown after its RetiredAt: |
| +// earlier is ErrNamespaceCooling, nothing written. Anything else at the key |
| +// answers replaced=false. The new claim is a fresh one, as |
| +// AttachAccountNamespace writes it; nothing of the old holder's survives. |
| +func ReplaceRetiredAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, etag string, now time.Time, createdBy string) (newETag string, replaced bool, err error) { |
| + host, err := namespaceHostChecked(name, appsDomain, account) |
| + if err != nil { |
| + return "", false, err |
| + } |
| + cur, ok, err := namespaceAt(ctx, s, host, etag) |
| + if err != nil || !ok || cur.Account == account { |
| + return "", false, err |
| + } |
| + r, ok := retiredNamespace(*cur, name, appsDomain) |
| + if !ok { |
| + return "", false, nil |
| + } |
| + if now.Before(r.AvailableAt()) { |
| + return "", false, ErrNamespaceCooling |
| + } |
| + return putNamespace(ctx, s, host, Claim{ |
| + Schema: 1, Host: host, Account: account, |
| + CreatedAt: now.UTC().Format(time.RFC3339), CreatedBy: createdBy, AccountNamespace: true, |
| + }, etag) |
| +} |
| + |
| +// RestoreRetiredAccountNamespace undoes an unretire or a replace: it writes |
| +// prev, the retired claim the undone write replaced, back at its key, only |
| +// when the claim there is still account's active namespace claim at etag, |
| +// the ETag the undone write answered, and conditional on that revision. So |
| +// it never overwrites a later write. prev must be a retired namespace claim |
| +// for name (RetiredAccountNamespaces trusts it). Anything else answers |
| +// restored=false. |
| +func RestoreRetiredAccountNamespace(ctx context.Context, s release.Store, name, appsDomain, account, etag string, prev Claim) (restored bool, err error) { |
| + host, err := namespaceHostChecked(name, appsDomain, account) |
| + if err != nil { |
| + return false, err |
| + } |
| + if _, ok := retiredNamespace(prev, name, appsDomain); !ok || prev.Host != host { |
| + return false, fmt.Errorf("domains: account namespace %q: the claim to restore is not a retired namespace claim", name) |
| + } |
| + cur, ok, err := namespaceAt(ctx, s, host, etag) |
| + if err != nil || !ok || cur.Account != account || cur.RetiredAt != "" { |
| + return false, err |
| + } |
| + _, restored, err = putNamespace(ctx, s, host, prev, etag) |
| + return restored, err |
| +} |
| + |
| +// namespaceAt reads the namespace claim at host when its ETag is etag: ok is |
| +// false with a nil error when the key is empty, holds another revision or a |
| +// claim that is not an AccountNamespace one. |
| +func namespaceAt(ctx context.Context, s release.Store, host, etag string) (*Claim, bool, error) { |
| + cur, curETag, err := getClaimETag(ctx, s, host) |
| + if errors.Is(err, os.ErrNotExist) { |
| + return nil, false, nil |
| + } |
| + if err != nil { |
| + return nil, false, err |
| + } |
| + if curETag != etag || !cur.AccountNamespace { |
| + return nil, false, nil |
| + } |
| + return cur, true, nil |
| +} |
| + |
| +// putNamespace writes c at host conditional on etag: a lost CAS answers |
| +// written=false with a nil error. |
| +func putNamespace(ctx context.Context, s release.Store, host string, c Claim, etag string) (string, bool, error) { |
| + body, err := json.Marshal(c) |
| + if err != nil { |
| + return "", false, err |
| + } |
| + newETag, err := s.Put(ctx, ClaimKey(host), body, etag) |
| + if errors.Is(err, release.ErrCASConflict) { |
| + return "", false, nil |
| + } |
| + if err != nil { |
| + return "", false, err |
| + } |
| + return newETag, true, nil |
| +} |
| + |
| // AccountNamespaces folds claims into the namespaces in force under |
| // appsDomain: byName maps a name to its account's sqid, byAccount a sqid to |
| -// its name. Only claims with AccountNamespace set count, and of those only |
| -// the ones that are verified (not Pending), sit at a valid namespace host |
| -// under appsDomain, name a valid account and carry nothing a namespace claim |
| -// never has (an App, Route, Wildcard, Catchall, Paths, Instances or |
| -// Placements: no door writes such a claim, so it is not trusted to be one). |
| -// A name SubdomainRuleRefusal refuses (a bad shape, a reserved label, a |
| -// sqid-shaped word shorter than the floor) is skipped too: the console door |
| -// never assigns one, so a claim at *.bdq.<apps> or *.www.<apps> is a direct |
| -// bucket write, and honouring it would serve another account's apps inside |
| -// bdq's own sqid namespace, or under a platform label. |
| +// its name. Only claims trustedNamespace accepts count, and of those only |
| +// the ones not retired (RetiredAt unset): a retired name serves nothing and |
| +// is nobody's (RetiredAccountNamespaces lists those). |
| // |
| // The door leaves an account at most one name. Should a store-mode write |
| // leave it two, byName keeps both and byAccount keeps the lexically first, |
| @@ -229,11 +386,11 @@ func AccountNamespaces(claims []Claim, appsDomain string) (byName, byAccount map |
| byName = map[string]string{} |
| byAccount = map[string]string{} |
| for _, c := range claims { |
| - if !c.AccountNamespace || c.Pending || !namespaceOnly(c) || !release.ValidAccount(c.Account) { |
| + if c.RetiredAt != "" { |
| continue |
| } |
| name, ok := ParseNamespaceHost(c.Host, appsDomain) |
| - if !ok || SubdomainRuleRefusal(name) != "" { |
| + if !ok || !trustedNamespace(c, name, appsDomain) { |
| continue |
| } |
| byName[name] = c.Account |
| @@ -244,6 +401,58 @@ func AccountNamespaces(claims []Claim, appsDomain string) (byName, byAccount map |
| return byName, byAccount |
| } |
| +// RetiredAccountNamespaces is the retired namespace claims among claims, by |
| +// name: the ones trustedNamespace accepts whose RetiredAt is set and is an |
| +// RFC 3339 time. A retired claim whose RetiredAt does not parse is neither |
| +// in force nor retired: a reader that must know (the console) counts it as |
| +// unread. |
| +func RetiredAccountNamespaces(claims []Claim, appsDomain string) map[string]RetiredNamespace { |
| + out := map[string]RetiredNamespace{} |
| + for _, c := range claims { |
| + name, ok := ParseNamespaceHost(c.Host, appsDomain) |
| + if !ok { |
| + continue |
| + } |
| + if r, ok := retiredNamespace(c, name, appsDomain); ok { |
| + out[name] = r |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// retiredNamespace is c as a retired namespace claim for name. |
| +func retiredNamespace(c Claim, name, appsDomain string) (RetiredNamespace, bool) { |
| + if c.RetiredAt == "" || !trustedNamespace(c, name, appsDomain) { |
| + return RetiredNamespace{}, false |
| + } |
| + at, err := time.Parse(time.RFC3339, c.RetiredAt) |
| + if err != nil { |
| + return RetiredNamespace{}, false |
| + } |
| + return RetiredNamespace{Account: c.Account, RetiredAt: at.UTC()}, true |
| +} |
| + |
| +// trustedNamespace reports whether c is a namespace claim for name every |
| +// reader may trust, retired or not: AccountNamespace set, verified (not |
| +// Pending), at name's namespace host under appsDomain, naming a valid |
| +// account and carrying nothing a namespace claim never has (an App, Route, |
| +// Wildcard, Catchall, Paths, Instances or Placements: no door writes such a |
| +// claim, so it is not trusted to be one). A name SubdomainRuleRefusal |
| +// refuses (a bad shape, a reserved label, a sqid-shaped word shorter than |
| +// the floor) is not trusted either: the console door never assigns one, so |
| +// a claim at *.bdq.<apps> or *.www.<apps> is a direct bucket write, and |
| +// honouring it would serve another account's apps inside bdq's own sqid |
| +// namespace, or under a platform label. |
| +func trustedNamespace(c Claim, name, appsDomain string) bool { |
| + if !c.AccountNamespace || c.Pending || !namespaceOnly(c) || !release.ValidAccount(c.Account) { |
| + return false |
| + } |
| + if got, ok := ParseNamespaceHost(c.Host, appsDomain); !ok || got != name { |
| + return false |
| + } |
| + return SubdomainRuleRefusal(name) == "" |
| +} |
| + |
| // namespaceOnly reports whether c carries nothing but the namespace. |
| func namespaceOnly(c Claim) bool { |
| return c.App == "" && c.Route == "" && !c.Wildcard && !c.Catchall && |
diff --git a/internal/domains/namespace_retire_test.go b/internal/domains/namespace_retire_test.go| new file mode 100644 |
| index 000000000..1cf01d53c |
| --- /dev/null |
| +++ b/internal/domains/namespace_retire_test.go |
| @@ -0,0 +1,237 @@ |
| +package domains |
| + |
| +// Account subdomains rev 4 (spec 2026-11-11-account-subdomains-design.md |
| +// §4.3): remove retires the namespace claim instead of deleting it. The same |
| +// account may unretire at once; another account replaces it only from |
| +// NamespaceCooldown after RetiredAt. Every writer is conditional on the |
| +// revision its caller judged, and answers the ETag of its own write. |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +var retireT0 = time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) |
| + |
| +// heldETag attaches harbour for account and answers the attach's ETag, |
| +// checking it is the ETag the key now reads at. |
| +func heldETag(t *testing.T, s release.Store, account string) string { |
| + t.Helper() |
| + etag, created, err := AttachAccountNamespace(context.Background(), s, "harbour", nsApps, account, "2026-11-11T00:00:00Z", "ops@example.com") |
| + if err != nil || !created || etag == "" { |
| + t.Fatalf("attach = (%q, %v, %v)", etag, created, err) |
| + } |
| + if _, got, err := GetClaimETag(context.Background(), s, NamespaceHost("harbour", nsApps)); err != nil || got != etag { |
| + t.Fatalf("attach answered ETag %q, the key reads %q (%v)", etag, got, err) |
| + } |
| + return etag |
| +} |
| + |
| +func TestRetireAccountNamespaceStampsAndKeepsTheClaim(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + etag := heldETag(t, s, "bdq") |
| + host := NamespaceHost("harbour", nsApps) |
| + for _, c := range []struct { |
| + account, etag string |
| + }{{"bcd", etag}, {"bdq", etag + "-stale"}} { |
| + if _, ok, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, c.account, c.etag, retireT0); err != nil || ok { |
| + t.Fatalf("retire as %s at %q = (%v, %v), want refused", c.account, c.etag, ok, err) |
| + } |
| + } |
| + retired, ok, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", etag, retireT0) |
| + if err != nil || !ok { |
| + t.Fatalf("retire = (%v, %v)", ok, err) |
| + } |
| + c, got, err := GetClaimETag(ctx, s, host) |
| + if err != nil || got != retired || c.RetiredAt != "2026-11-12T09:00:00Z" || c.Account != "bdq" || !c.AccountNamespace { |
| + t.Fatalf("after retire: %+v at %q (want %q), %v", c, got, retired, err) |
| + } |
| + if byName, _ := AccountNamespaces([]Claim{*c}, nsApps); len(byName) != 0 { |
| + t.Errorf("a retired claim is in force: %v", byName) |
| + } |
| + r := RetiredAccountNamespaces([]Claim{*c}, nsApps)["harbour"] |
| + if r.Account != "bdq" || !r.RetiredAt.Equal(retireT0) || !r.AvailableAt().Equal(retireT0.Add(time.Hour)) { |
| + t.Errorf("retired view = %+v", r) |
| + } |
| + // Retiring a retired claim is refused: it is not an active one. |
| + if _, ok, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0.Add(time.Minute)); err != nil || ok { |
| + t.Errorf("second retire = (%v, %v), want refused", ok, err) |
| + } |
| +} |
| + |
| +func TestUnretireAccountNamespaceSameAccountOnly(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + etag := heldETag(t, s, "bdq") |
| + retired, _, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", etag, retireT0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, ok, err := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bcd", retired, retireT0, ""); err != nil || ok { |
| + t.Fatalf("unretire by another account = (%v, %v), want refused", ok, err) |
| + } |
| + if _, ok, err := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired+"-stale", retireT0, ""); err != nil || ok { |
| + t.Fatalf("unretire at a stale ETag = (%v, %v), want refused", ok, err) |
| + } |
| + back, ok, err := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0.Add(time.Minute), "ops@example.com") |
| + if err != nil || !ok { |
| + t.Fatalf("unretire = (%v, %v)", ok, err) |
| + } |
| + c, got, _ := GetClaimETag(ctx, s, NamespaceHost("harbour", nsApps)) |
| + if got != back || c.RetiredAt != "" || c.CreatedAt != "2026-11-12T09:01:00Z" || c.CreatedBy != "ops@example.com" { |
| + t.Fatalf("after unretire: %+v at %q (want %q)", c, got, back) |
| + } |
| + if byName, _ := AccountNamespaces([]Claim{*c}, nsApps); byName["harbour"] != "bdq" { |
| + t.Errorf("unretired claim not in force: %v", byName) |
| + } |
| + // An active claim is not unretired again. |
| + if _, ok, err := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", back, retireT0, ""); err != nil || ok { |
| + t.Errorf("unretire of an active claim = (%v, %v), want refused", ok, err) |
| + } |
| +} |
| + |
| +func TestReplaceRetiredAccountNamespaceWaitsTheHour(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + etag := heldETag(t, s, "bdq") |
| + retired, _, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", etag, retireT0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + early := retireT0.Add(NamespaceCooldown - time.Second) |
| + if _, ok, err := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bcd", retired, early, "ops@example.com"); !errors.Is(err, ErrNamespaceCooling) || ok { |
| + t.Fatalf("replace before the hour = (%v, %v), want ErrNamespaceCooling", ok, err) |
| + } |
| + at := retireT0.Add(NamespaceCooldown) |
| + if _, ok, err := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, at, "ops@example.com"); err != nil || ok { |
| + t.Fatalf("replace by the same account = (%v, %v), want refused (unretire is its door)", ok, err) |
| + } |
| + if _, ok, err := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bcd", retired+"-stale", at, "ops@example.com"); err != nil || ok { |
| + t.Fatalf("replace at a stale ETag = (%v, %v), want refused", ok, err) |
| + } |
| + newETag, ok, err := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bcd", retired, at, "ops@example.com") |
| + if err != nil || !ok { |
| + t.Fatalf("replace at the hour = (%v, %v)", ok, err) |
| + } |
| + c, got, _ := GetClaimETag(ctx, s, NamespaceHost("harbour", nsApps)) |
| + if got != newETag || c.Account != "bcd" || c.RetiredAt != "" || c.CreatedAt != "2026-11-12T10:00:00Z" { |
| + t.Fatalf("after replace: %+v at %q", c, got) |
| + } |
| + // An active claim is never replaced. |
| + if _, ok, err := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bab", newETag, at.Add(time.Hour), ""); err != nil || ok { |
| + t.Errorf("replace of an active claim = (%v, %v), want refused", ok, err) |
| + } |
| +} |
| + |
| +// TestRestoreRetiredAccountNamespaceOnlyOverItsOwnWrite: the undo of an |
| +// unretire or a replace writes the retired claim back, conditional on the |
| +// undone write's ETag; a later write at the key is never overwritten. |
| +func TestRestoreRetiredAccountNamespaceOnlyOverItsOwnWrite(t *testing.T) { |
| + ctx := context.Background() |
| + host := NamespaceHost("harbour", nsApps) |
| + setup := func(t *testing.T) (release.Store, Claim, string) { |
| + s := release.NewDirStore(t.TempDir()) |
| + etag := heldETag(t, s, "bdq") |
| + retired, _, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", etag, retireT0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + prev, _, _ := GetClaimETag(ctx, s, host) |
| + return s, *prev, retired |
| + } |
| + t.Run("unretire", func(t *testing.T) { |
| + s, prev, retired := setup(t) |
| + back, _, _ := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0.Add(time.Minute), "") |
| + if ok, err := RestoreRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bdq", back, prev); err != nil || !ok { |
| + t.Fatalf("restore = (%v, %v)", ok, err) |
| + } |
| + if c, _ := GetClaim(ctx, s, host); c.RetiredAt != prev.RetiredAt || c.Account != "bdq" { |
| + t.Errorf("after restore: %+v", c) |
| + } |
| + }) |
| + t.Run("replace", func(t *testing.T) { |
| + s, prev, retired := setup(t) |
| + newETag, _, _ := ReplaceRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bcd", retired, retireT0.Add(time.Hour), "") |
| + if ok, err := RestoreRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bcd", newETag, prev); err != nil || !ok { |
| + t.Fatalf("restore = (%v, %v)", ok, err) |
| + } |
| + if c, _ := GetClaim(ctx, s, host); c.RetiredAt != prev.RetiredAt || c.Account != "bdq" { |
| + t.Errorf("after restore: %+v", c) |
| + } |
| + }) |
| + t.Run("successor", func(t *testing.T) { |
| + s, prev, retired := setup(t) |
| + back, _, _ := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0.Add(time.Minute), "") |
| + // Another console retires and takes it back: a later revision. |
| + again, _, _ := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", back, retireT0.Add(time.Minute)) |
| + later, _, _ := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", again, retireT0.Add(2*time.Minute), "") |
| + if ok, err := RestoreRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bdq", back, prev); err != nil || ok { |
| + t.Fatalf("restore over a successor = (%v, %v), want refused", ok, err) |
| + } |
| + if c, got, _ := GetClaimETag(ctx, s, host); got != later || c.RetiredAt != "" { |
| + t.Errorf("the successor changed: %+v at %q", c, got) |
| + } |
| + }) |
| + t.Run("not retired", func(t *testing.T) { |
| + s, prev, retired := setup(t) |
| + back, _, _ := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0.Add(time.Minute), "") |
| + active := prev |
| + active.RetiredAt = "" |
| + if ok, err := RestoreRetiredAccountNamespace(ctx, s, "harbour", nsApps, "bdq", back, active); err == nil || ok { |
| + t.Fatalf("restore of an active claim = (%v, %v), want an error", ok, err) |
| + } |
| + }) |
| +} |
| + |
| +// TestRetiredAccountNamespacesNeedsATime: a retired_at that does not parse |
| +// is neither in force nor retired (the console counts it unread); a |
| +// retired claim must pass every check a claim in force passes. |
| +func TestRetiredAccountNamespacesNeedsATime(t *testing.T) { |
| + claims := []Claim{ |
| + {Schema: 1, Host: "*.harbour.apps.test", Account: "bdq", AccountNamespace: true, RetiredAt: "yesterday"}, |
| + {Schema: 1, Host: "*.marinax.apps.test", Account: "bdq", AccountNamespace: true, RetiredAt: "2026-11-12T09:00:00Z", App: "shop"}, |
| + {Schema: 1, Host: "*.quayside.apps.test", Account: "bcd", AccountNamespace: true, RetiredAt: "2026-11-12T09:00:00Z"}, |
| + } |
| + byName, _ := AccountNamespaces(claims, nsApps) |
| + if len(byName) != 0 { |
| + t.Errorf("in force = %v, want none", byName) |
| + } |
| + got := RetiredAccountNamespaces(claims, nsApps) |
| + if len(got) != 1 || got["quayside"].Account != "bcd" { |
| + t.Errorf("retired = %+v, want only quayside", got) |
| + } |
| +} |
| + |
| +// TestNamespaceRetiredDerivesNothing: a box derives nothing from a retired |
| +// claim and drops the name from account_namespaces (so the edge drops the |
| +// wildcard parent); a pass over it is complete. Unretired, it derives again. |
| +func TestNamespaceRetiredDerivesNothing(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + reg := aliasRegWith(t) |
| + etag := heldETag(t, s, "bdq") |
| + addPlatformRoute(t, reg, "shop.bdq.apps.test", "bdq", "shop") |
| + reconcileNS(t, s, reg, nsApps) |
| + wantAliases(t, reg, "bdq", map[string]string{"shop.harbour.apps.test": "shop.bdq.apps.test"}) |
| + retired, ok, err := RetireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", etag, retireT0) |
| + if err != nil || !ok { |
| + t.Fatalf("retire = (%v, %v)", ok, err) |
| + } |
| + if rep := reconcileNS(t, s, reg, nsApps); !rep.Complete { |
| + t.Fatal("pass over a retired claim incomplete") |
| + } |
| + wantAliases(t, reg, "bdq", map[string]string{}) |
| + if got := namespaces(t, reg); len(got) != 0 { |
| + t.Errorf("account_namespaces = %v; want empty while retired", got) |
| + } |
| + if _, ok, err := UnretireAccountNamespace(ctx, s, "harbour", nsApps, "bdq", retired, retireT0, ""); err != nil || !ok { |
| + t.Fatalf("unretire = (%v, %v)", ok, err) |
| + } |
| + reconcileNS(t, s, reg, nsApps) |
| + wantAliases(t, reg, "bdq", map[string]string{"shop.harbour.apps.test": "shop.bdq.apps.test"}) |
| +} |
diff --git a/internal/domains/namespace_test.go b/internal/domains/namespace_test.go| index 93fd744d8..d5d7fb531 100644 |
| --- a/internal/domains/namespace_test.go |
| +++ b/internal/domains/namespace_test.go |
| @@ -55,15 +55,15 @@ func TestNormalizeSubdomain(t *testing.T) { |
| func TestAttachAccountNamespaceIsCreateOnly(t *testing.T) { |
| ctx := context.Background() |
| s := release.NewDirStore(t.TempDir()) |
| - created, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com") |
| + _, created, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com") |
| if err != nil || !created { |
| t.Fatalf("first attach = (%v, %v), want (true, nil)", created, err) |
| } |
| - created, err = AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bcd", "2026-11-11T00:00:01Z", "ops@example.com") |
| + _, created, err = AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bcd", "2026-11-11T00:00:01Z", "ops@example.com") |
| if err != nil || created { |
| t.Fatalf("second attach by another account = (%v, %v), want (false, nil)", created, err) |
| } |
| - created, err = AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:02Z", "ops@example.com") |
| + _, created, err = AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:02Z", "ops@example.com") |
| if err != nil || created { |
| t.Fatalf("repeat attach by holder = (%v, %v), want (false, nil)", created, err) |
| } |
| @@ -101,11 +101,11 @@ func TestAttachAccountNamespaceRefusesBadShape(t *testing.T) { |
| ctx := context.Background() |
| s := release.NewDirStore(t.TempDir()) |
| for _, name := range []string{"Harbour", "harbour.x", "ab--x", ""} { |
| - if created, err := AttachAccountNamespace(ctx, s, name, "apps.test", "bdq", "t", ""); err == nil || created { |
| + if _, created, err := AttachAccountNamespace(ctx, s, name, "apps.test", "bdq", "t", ""); err == nil || created { |
| t.Errorf("attach %q = (%v, %v), want an error", name, created, err) |
| } |
| } |
| - if created, err := AttachAccountNamespace(ctx, s, "harbour", "", "bdq", "t", ""); err == nil || created { |
| + if _, created, err := AttachAccountNamespace(ctx, s, "harbour", "", "bdq", "t", ""); err == nil || created { |
| t.Errorf("attach with no apps domain = (%v, %v), want an error", created, err) |
| } |
| keys, err := s.List(ctx, ClaimsPrefix) |
| @@ -126,7 +126,7 @@ func TestAttachAccountNamespaceLeavesCatchallAlone(t *testing.T) { |
| if _, err := AttachInstanceCatchall(ctx, s, "harbour.apps.test", "bcd", "shop", "t", ""); err != nil { |
| t.Fatal(err) |
| } |
| - created, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "t", "") |
| + _, created, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "t", "") |
| if err != nil || created { |
| t.Fatalf("attach over a catch-all = (%v, %v), want (false, nil)", created, err) |
| } |
| @@ -142,7 +142,7 @@ func TestAttachAccountNamespaceLeavesCatchallAlone(t *testing.T) { |
| func TestDetachAccountNamespaceNeedsEtagAndAccount(t *testing.T) { |
| ctx := context.Background() |
| s := release.NewDirStore(t.TempDir()) |
| - if _, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "t", ""); err != nil { |
| + if _, _, err := AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "t", ""); err != nil { |
| t.Fatal(err) |
| } |
| _, etag, err := GetClaimETag(ctx, s, NamespaceHost("harbour", "apps.test")) |
diff --git a/internal/domains/namespacealiases_ha_test.go b/internal/domains/namespacealiases_ha_test.go| index e560e0e3d..12841cc87 100644 |
| --- a/internal/domains/namespacealiases_ha_test.go |
| +++ b/internal/domains/namespacealiases_ha_test.go |
| @@ -20,7 +20,7 @@ import ( |
| func TestNamespaceHAIngress(t *testing.T) { |
| ctx := t.Context() |
| s := release.NewDirStore(t.TempDir()) |
| - if created, err := domains.AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| + if _, created, err := domains.AttachAccountNamespace(ctx, s, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| t.Fatalf("hold: %v %v", created, err) |
| } |
| if err := provision.PutManifest(ctx, s, "bdq", "shop", provision.Manifest{Schema: 1, App: "shop", Domain: "bdq.apps.test"}); err != nil { |
diff --git a/internal/domains/namespacealiases_test.go b/internal/domains/namespacealiases_test.go| index cc75aa4c1..a1a33e14a 100644 |
| --- a/internal/domains/namespacealiases_test.go |
| +++ b/internal/domains/namespacealiases_test.go |
| @@ -46,7 +46,7 @@ func reconcileNSWith(t *testing.T, s release.Store, reg *registry.Registry, apps |
| // writer, the one the console door uses. |
| func holdNamespace(t *testing.T, s release.Store, name, account string) { |
| t.Helper() |
| - created, err := AttachAccountNamespace(context.Background(), s, name, nsApps, account, "2026-11-11T00:00:00Z", "ops@example.com") |
| + _, created, err := AttachAccountNamespace(context.Background(), s, name, nsApps, account, "2026-11-11T00:00:00Z", "ops@example.com") |
| if err != nil || !created { |
| t.Fatalf("hold %s for %s: created=%v err=%v", name, account, created, err) |
| } |
diff --git a/internal/edge/storedoor_test.go b/internal/edge/storedoor_test.go| index 72ca12c7e..b1ac5cd99 100644 |
| --- a/internal/edge/storedoor_test.go |
| +++ b/internal/edge/storedoor_test.go |
| @@ -1455,7 +1455,7 @@ func TestStoreDoorReturnsOwnNamespaceClaim(t *testing.T) { |
| return d, store |
| } |
| own, store := doorFor("bdq") |
| - if created, err := domains.AttachAccountNamespace(context.Background(), store, "harbour", apps, "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| + if _, created, err := domains.AttachAccountNamespace(context.Background(), store, "harbour", apps, "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| t.Fatalf("hold: %v %v", created, err) |
| } |
| key := domains.ClaimKey(domains.NamespaceHost("harbour", apps)) |
diff --git a/internal/host/edgesfile_test.go b/internal/host/edgesfile_test.go| index 28b665264..cf890e732 100644 |
| --- a/internal/host/edgesfile_test.go |
| +++ b/internal/host/edgesfile_test.go |
| @@ -2646,7 +2646,7 @@ func TestEdgesFilePublishesDerivedRowAtOnce(t *testing.T) { |
| t.Fatal(err) |
| } |
| putStartupClaim(t, store, domains.Claim{Host: "blog.harbour.apps.test", Account: "bdq", App: "blog", Route: "blog.bdq.apps.test"}) |
| - if created, err := domains.AttachAccountNamespace(ctx, store, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| + if _, created, err := domains.AttachAccountNamespace(ctx, store, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| t.Fatalf("hold: %v %v", created, err) |
| } |
| if ok, err := aliasPassReportingComplete(ctx, store, reg, &aliasClaimCache, boxAssetAuthority, "", false); err != nil || !ok { |
| @@ -2718,7 +2718,7 @@ func TestEdgesFilePublishesExactClaimUnderKeptName(t *testing.T) { |
| } |
| } |
| route("shop") |
| - if created, err := domains.AttachAccountNamespace(ctx, store, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| + if _, created, err := domains.AttachAccountNamespace(ctx, store, "harbour", "apps.test", "bdq", "2026-11-11T00:00:00Z", "ops@example.com"); err != nil || !created { |
| t.Fatalf("hold: %v %v", created, err) |
| } |
| if ok, err := aliasPassReportingComplete(ctx, store, reg, &aliasClaimCache, boxAssetAuthority, "", false); err != nil || !ok { |