| 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 { |