carlos / platform Public

mail: replace first: keep a bundle's key until its replacement lands

Ruled by Paul, 2026-11-14 (replace first, a standing rule for the
one-working-channel family). Enable and EnableEnvironment reaped every
key but a pending rotation's before they minted, so when delivery was
incomplete for another reason (seed 5063: a stale _FROM) and the mint
then failed, the bundle was left naming a deleted key. They now keep
the key the bundle names through the reap, mint, deliver and record
the new one, and only then delete the old. A failed mint leaves the old
working key in place; with a rotation also pending, IAM's two-key
ceiling refuses the mint and both keys stay.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev db3846ecacfd1b123f783eb3c605fcd1ddab5941 parent 093fd34
2 files changed, +68 −12
  • internal/mail/walk.go +58 −8
  • internal/mail/walk_environment.go +10 −4
diff --git a/internal/mail/walk.go b/internal/mail/walk.go
index 2c803c350..b59bac665 100644
--- a/internal/mail/walk.go
+++ b/internal/mail/walk.go
@@ -904,15 +904,21 @@ func claimUnserializedEnable(ctx context.Context, s release.Store, p Provisioner
active, pauseKind = false, pz.Kind
}
- // Reap before minting: reaching here means delivery is missing, so any
- // current key is either undelivered (its secret died with a failed
- // config write) or stranded by a crash between mint and record. No
- // record names either; only list-and-delete finds them. A pending-finish
- // key from an unfinished rotation is kept for its explicit finish.
- var keep []string
- if prev != "" {
- keep = append(keep, prev)
+ // Reap before minting, every key no bundle names: reaching here means
+ // delivery is missing, so such a key is either undelivered (its secret
+ // died with a failed config write) or stranded by a crash between mint
+ // and record. Only list-and-delete finds them. Kept: a pending-finish
+ // key from an unfinished rotation, for its explicit finish; and the key
+ // the bundle still names, until its replacement is delivered and
+ // recorded (ruled by Paul, 2026-11-14: replace first). Reaping that one
+ // first left a bundle naming a deleted key whenever the mint after it
+ // failed (seed 5063). With both kept, IAM's two-key ceiling refuses the
+ // mint, and the old working key stays.
+ named, err := deliveredKeyIn(ctx, s, acct, app, "", rec.Declared.EnvPrefix)
+ if err != nil {
+ return nil, err
}
+ keep := keepBeforeMint(prev, named)
if err := p.ReapKeys(ctx, user, keep); err != nil {
return nil, fmt.Errorf("mail: reap keys for %s: %w", user, err)
}
@@ -938,9 +944,53 @@ func claimUnserializedEnable(ctx context.Context, s release.Store, p Provisioner
if _, err := Put(ctx, s, acct, app, rec, etag); err != nil {
return nil, err
}
+ if err := retireReplacedKey(ctx, p, user, named, prev, keyID); err != nil {
+ return nil, err
+ }
return rec, nil
}
+// deliveredKeyIn is the access key id a bundle delivers (its <P>_USER):
+// the default bundle (env == "") or an owned environment's. "" when it
+// names none.
+func deliveredKeyIn(ctx context.Context, s release.Store, acct, app, env, prefix string) (string, error) {
+ m, err := bundleFor(ctx, s, acct, app, env)
+ if err != nil {
+ return "", fmt.Errorf("mail: read the key %s/%s%s delivers: %w", acct, app, envLabel(env), err)
+ }
+ _, _, userVar, _, _ := EnvNames(prefix)
+ return m.Vars[userVar].Value, nil
+}
+
+// keepBeforeMint is the keys a mint walk's reap must leave: a pending
+// rotation's (prev) and the one the bundle still names (named), until its
+// replacement is in place (ruled by Paul, 2026-11-14: replace first).
+func keepBeforeMint(prev, named string) []string {
+ var keep []string
+ if prev != "" {
+ keep = append(keep, prev)
+ }
+ if named != "" && named != prev {
+ keep = append(keep, named)
+ }
+ return keep
+}
+
+// retireReplacedKey deletes the key a bundle named before a mint walk,
+// once the walk has delivered and recorded its replacement (replace
+// first). A pending rotation's key is left for its explicit finish. A
+// failure is returned: the replacement is already in place, and the old
+// key is left as a stray, which the next walk that mints reaps.
+func retireReplacedKey(ctx context.Context, p Provisioner, user, named, prev, minted string) error {
+ if named == "" || named == prev || named == minted {
+ return nil
+ }
+ if err := p.DeleteKey(ctx, user, named); err != nil {
+ return fmt.Errorf("mail: delete the replaced key %s of %s: %w", named, user, err)
+ }
+ return nil
+}
+
func containsString(xs []string, want string) bool {
for _, x := range xs {
if x == want {
diff --git a/internal/mail/walk_environment.go b/internal/mail/walk_environment.go
index fb3643576..f0df58ade 100644
--- a/internal/mail/walk_environment.go
+++ b/internal/mail/walk_environment.go
@@ -119,11 +119,14 @@ func claimUnserializedEnableEnvironment(ctx context.Context, s release.Store, p
if cur != nil {
prev, active = cur.PrevKeyID, cur.Active
}
- var keep []string
- if prev != "" {
- keep = append(keep, prev)
+ // Replace first (ruled by Paul, 2026-11-14), as Enable: the key the
+ // environment's bundle still names is kept until its replacement is
+ // delivered and recorded, then deleted.
+ named, err := deliveredKeyIn(ctx, s, acct, app, env, rec.Declared.EnvPrefix)
+ if err != nil {
+ return nil, err
}
- if err := p.ReapKeys(ctx, user, keep); err != nil {
+ if err := p.ReapKeys(ctx, user, keepBeforeMint(prev, named)); err != nil {
return nil, fmt.Errorf("mail: reap keys for %s: %w", user, err)
}
keyID, secret, err := p.MintKey(ctx, user)
@@ -141,6 +144,9 @@ func claimUnserializedEnableEnvironment(ctx context.Context, s release.Store, p
if _, err := Put(ctx, s, acct, app, rec, etag); err != nil {
return nil, err
}
+ if err := retireReplacedKey(ctx, p, user, named, prev, keyID); err != nil {
+ return nil, err
+ }
return rec, nil
}