carlos / platform Public

mail: a delivery walk checks the key before it skips a mint

Ruled by Paul, 2026-11-14: switch-off checks keys. A part-failed walk
can leave a bundle naming a key whose user it already deleted while the
record still names that user, and Enable and EnableEnvironment (switch-
off's first step) read such a bundle as delivered and minted nothing.

Provisioner gains a read, KeyState (IAM ListAccessKeys; the in-memory
fake and every boundary wrapper pass it through, listed as a read beside
IdentityStatus). Before either walk skips a mint, it re-mints unless IAM
holds the recorded key under that user, active, or inactive for a
paused sender. An unreadable key state refuses the walk: a switch-off
then stops before it writes the switch, so the app keeps submitting;
reading it as missing would reap a key running instances may still use.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev bf7b3423fc226f364a08a6a994bb19ff1bd61936 parent 76f4f58
12 files changed, +187 −5
  • internal/console/claim_backend_recorders_test.go +3 −0
  • internal/console/claim_backends.go +3 −0
  • internal/console/claim_invariant_test.go +1 −1
  • internal/console/claim_purge_backend.go +3 −0
  • internal/console/claim_record_backend_boundary_test.go +1 −1
  • internal/mail/aws.go +28 −0
  • internal/mail/aws_iam_test.go +53 −1
  • internal/mail/claim_gate_effects.go +3 −0
  • internal/mail/memprovisioner.go +20 −0
  • internal/mail/provisioner.go +19 −0
  • internal/mail/walk.go +43 −1
  • internal/mail/walk_environment.go +10 −1
diff --git a/internal/console/claim_backend_recorders_test.go b/internal/console/claim_backend_recorders_test.go
index a611a25c6..5f5ac7627 100644
--- a/internal/console/claim_backend_recorders_test.go
+++ b/internal/console/claim_backend_recorders_test.go
@@ -259,6 +259,9 @@ func (w *MailProvisionerBoundary) EnsureIdentity(ctx context.Context, region str
func (w *MailProvisionerBoundary) IdentityStatus(ctx context.Context, region string, domain string) (mail.Status, error) {
return w.raw.IdentityStatus(ctx, region, domain)
}
+func (w *MailProvisionerBoundary) KeyState(ctx context.Context, user, keyID string) (mail.KeyState, error) {
+ return w.raw.KeyState(ctx, user, keyID)
+}
func (w *MailProvisionerBoundary) DeleteIdentity(ctx context.Context, region string, domain string) error {
body, err := json.Marshal(map[string]any{"region": region, "domain": domain})
if err != nil {
diff --git a/internal/console/claim_backends.go b/internal/console/claim_backends.go
index bbcae9421..9882e7462 100644
--- a/internal/console/claim_backends.go
+++ b/internal/console/claim_backends.go
@@ -647,6 +647,9 @@ func (p claimMailProvisioner) EnsureIdentity(ctx context.Context, region, domain
func (p claimMailProvisioner) IdentityStatus(ctx context.Context, region, domain string) (mail.Status, error) {
return p.raw.IdentityStatus(ctx, region, domain)
}
+func (p claimMailProvisioner) KeyState(ctx context.Context, user, keyID string) (mail.KeyState, error) {
+ return p.raw.KeyState(ctx, user, keyID)
+}
func (p claimMailProvisioner) DeleteIdentity(ctx context.Context, region, domain string) error {
if err := p.domain(ctx, "DeleteIdentity", domain, domainDestroy, region); err != nil {
return err
diff --git a/internal/console/claim_invariant_test.go b/internal/console/claim_invariant_test.go
index ce9d70fc1..cfea42910 100644
--- a/internal/console/claim_invariant_test.go
+++ b/internal/console/claim_invariant_test.go
@@ -742,7 +742,7 @@ func claimWriterInventory(s *Server) map[string]bool {
}{
{"objstore.Provisioner", reflect.TypeFor[objstore.Provisioner](), nil},
{"objstore.Scanner", reflect.TypeFor[objstore.Scanner](), []string{"Capacity", "Enforcement", "Provenance", "Status", "Verdict"}},
- {"mail.Provisioner", reflect.TypeFor[mail.Provisioner](), []string{"IdentityStatus"}},
+ {"mail.Provisioner", reflect.TypeFor[mail.Provisioner](), []string{"IdentityStatus", "KeyState"}},
{"dnsrec.Writer", reflect.TypeFor[dnsrec.Writer](), nil},
} {
for i := 0; i < b.iface.NumMethod(); i++ {
diff --git a/internal/console/claim_purge_backend.go b/internal/console/claim_purge_backend.go
index b2c76474d..926e5b1e9 100644
--- a/internal/console/claim_purge_backend.go
+++ b/internal/console/claim_purge_backend.go
@@ -124,6 +124,9 @@ func (p claimPurgeMail) EnsureIdentity(context.Context, string, string, string,
func (p claimPurgeMail) IdentityStatus(ctx context.Context, region, domain string) (mail.Status, error) {
return p.raw.IdentityStatus(ctx, region, domain)
}
+func (p claimPurgeMail) KeyState(ctx context.Context, user, keyID string) (mail.KeyState, error) {
+ return p.raw.KeyState(ctx, user, keyID)
+}
func (p claimPurgeMail) DeleteIdentity(ctx context.Context, region, domain string) error {
return p.e.run(ctx, "email-domain:"+region+":"+domain, "delete-identity", map[string]string{"region": region, "domain": domain}, func(ctx context.Context) error {
return p.raw.DeleteIdentity(ctx, region, domain)
diff --git a/internal/console/claim_record_backend_boundary_test.go b/internal/console/claim_record_backend_boundary_test.go
index 8480a9e71..d4037139e 100644
--- a/internal/console/claim_record_backend_boundary_test.go
+++ b/internal/console/claim_record_backend_boundary_test.go
@@ -27,7 +27,7 @@ func TestEveryBackendMutationTraversesBoundary(t *testing.T) {
}{
{provisioner, reflect.TypeFor[objstore.Provisioner](), nil},
{scanner, reflect.TypeFor[objstore.Scanner](), []string{"Capacity", "Enforcement", "Provenance", "Status", "Verdict"}},
- {email, reflect.TypeFor[mail.Provisioner](), []string{"IdentityStatus"}},
+ {email, reflect.TypeFor[mail.Provisioner](), []string{"IdentityStatus", "KeyState"}},
{dns, reflect.TypeFor[dnsrec.Writer](), nil},
}
for _, c := range cases {
diff --git a/internal/mail/aws.go b/internal/mail/aws.go
index 153e0a515..c83681fcd 100644
--- a/internal/mail/aws.go
+++ b/internal/mail/aws.go
@@ -618,6 +618,34 @@ func (p *awsProvisioner) DeleteKey(ctx context.Context, user, keyID string) erro
return nil
}
+func (p *awsProvisioner) KeyState(ctx context.Context, user, keyID string) (KeyState, error) {
+ var marker *string
+ for {
+ out, err := p.iam.ListAccessKeys(ctx, &iam.ListAccessKeysInput{
+ UserName: aws.String(user), Marker: marker,
+ })
+ if err != nil {
+ if isNotFound(err) {
+ return KeyMissing, nil
+ }
+ return KeyMissing, fmt.Errorf("mail: list access keys for %q: %w", user, err)
+ }
+ for _, k := range out.AccessKeyMetadata {
+ if aws.ToString(k.AccessKeyId) != keyID {
+ continue
+ }
+ if k.Status == iamtypes.StatusTypeActive {
+ return KeyActive, nil
+ }
+ return KeyInactive, nil
+ }
+ if !out.IsTruncated || out.Marker == nil {
+ return KeyMissing, nil
+ }
+ marker = out.Marker
+ }
+}
+
func (p *awsProvisioner) ReapKeys(ctx context.Context, user string, keep []string) error {
keepSet := make(map[string]bool, len(keep))
for _, id := range keep {
diff --git a/internal/mail/aws_iam_test.go b/internal/mail/aws_iam_test.go
index ac431ae6a..5b9369937 100644
--- a/internal/mail/aws_iam_test.go
+++ b/internal/mail/aws_iam_test.go
@@ -20,6 +20,7 @@ type stubIAM struct {
createUser func(*iam.CreateUserInput) (*iam.CreateUserOutput, error)
getUser func(*iam.GetUserInput) (*iam.GetUserOutput, error)
listUsers func(*iam.ListUsersInput) (*iam.ListUsersOutput, error)
+ listKeys func(*iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error)
putBoundary func(*iam.PutUserPermissionsBoundaryInput) error
boundariesOn []string
@@ -83,11 +84,62 @@ func (s *stubIAM) DeleteAccessKey(context.Context, *iam.DeleteAccessKeyInput, ..
return &iam.DeleteAccessKeyOutput{}, nil
}
-func (s *stubIAM) ListAccessKeys(context.Context, *iam.ListAccessKeysInput, ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
+func (s *stubIAM) ListAccessKeys(_ context.Context, in *iam.ListAccessKeysInput, _ ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
s.calls = append(s.calls, "ListAccessKeys")
+ if s.listKeys != nil {
+ return s.listKeys(in)
+ }
return &iam.ListAccessKeysOutput{}, nil
}
+// KeyState reads IAM's key listing and writes nothing: an absent user or
+// key is KeyMissing, a listed key its status, across pages; any other
+// error is an error, never missing (ruled by Paul, 2026-11-14: switch-off
+// checks keys).
+func TestKeyStateReadsIAM(t *testing.T) {
+ key := func(id string, st iamtypes.StatusType) iamtypes.AccessKeyMetadata {
+ return iamtypes.AccessKeyMetadata{AccessKeyId: aws.String(id), Status: st}
+ }
+ pages := func(in *iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error) {
+ if in.Marker == nil {
+ return &iam.ListAccessKeysOutput{AccessKeyMetadata: []iamtypes.AccessKeyMetadata{key("AKIA1", iamtypes.StatusTypeInactive)},
+ IsTruncated: true, Marker: aws.String("p2")}, nil
+ }
+ return &iam.ListAccessKeysOutput{AccessKeyMetadata: []iamtypes.AccessKeyMetadata{key("AKIA2", iamtypes.StatusTypeActive)}}, nil
+ }
+ for _, c := range []struct {
+ name string
+ list func(*iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error)
+ key string
+ want KeyState
+ wantErr bool
+ }{
+ {"active, on the second page", pages, "AKIA2", KeyActive, false},
+ {"inactive", pages, "AKIA1", KeyInactive, false},
+ {"not listed", pages, "AKIA3", KeyMissing, false},
+ {"no such user", func(*iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error) {
+ return nil, &iamtypes.NoSuchEntityException{Message: aws.String("gone")}
+ }, "AKIA1", KeyMissing, false},
+ {"unreadable", func(*iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error) {
+ return nil, errors.New("throttled")
+ }, "AKIA1", KeyMissing, true},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ stub := &stubIAM{listKeys: c.list}
+ p := &awsProvisioner{iam: stub, account: "123456789012", region: "eu-west-1"}
+ got, err := p.KeyState(context.Background(), "carlos-mail-bag-hm", c.key)
+ if (err != nil) != c.wantErr || got != c.want {
+ t.Fatalf("KeyState = %v, %v; want %v, error %v", got, err, c.want, c.wantErr)
+ }
+ for _, call := range stub.calls {
+ if call != "ListAccessKeys" {
+ t.Fatalf("KeyState called %s", call)
+ }
+ }
+ })
+ }
+}
+
func (s *stubIAM) ListUsers(_ context.Context, in *iam.ListUsersInput, _ ...func(*iam.Options)) (*iam.ListUsersOutput, error) {
s.calls = append(s.calls, "ListUsers")
if s.listUsers != nil {
diff --git a/internal/mail/claim_gate_effects.go b/internal/mail/claim_gate_effects.go
index 514cce2a3..e118a65c8 100644
--- a/internal/mail/claim_gate_effects.go
+++ b/internal/mail/claim_gate_effects.go
@@ -48,6 +48,9 @@ func (p claimMarkedProvisioner) EnsureIdentity(ctx context.Context, region, doma
func (p claimMarkedProvisioner) IdentityStatus(ctx context.Context, region, domain string) (Status, error) {
return p.raw.IdentityStatus(ctx, region, domain)
}
+func (p claimMarkedProvisioner) KeyState(ctx context.Context, user, keyID string) (KeyState, error) {
+ return p.raw.KeyState(ctx, user, keyID)
+}
func (p claimMarkedProvisioner) DeleteIdentity(ctx context.Context, region, domain string) error {
err := p.raw.DeleteIdentity(ctx, region, domain)
markClaimMailEffect(ctx, hostops.MailAttempt{Surface: "ses-identity", Method: "DeleteIdentity", Region: region, Domain: domain}, err)
diff --git a/internal/mail/memprovisioner.go b/internal/mail/memprovisioner.go
index e71a12d99..8b83e8d16 100644
--- a/internal/mail/memprovisioner.go
+++ b/internal/mail/memprovisioner.go
@@ -357,6 +357,26 @@ func (p *MemProvisioner) ReapKeys(_ context.Context, user string, keep []string)
return nil
}
+func (p *MemProvisioner) KeyState(_ context.Context, user, keyID string) (KeyState, error) {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if err := p.takeFailure("KeyState"); err != nil {
+ return KeyMissing, err
+ }
+ u, ok := p.Users[user]
+ if !ok || u.Deleted {
+ return KeyMissing, nil
+ }
+ k, ok := u.Keys[keyID]
+ switch {
+ case !ok:
+ return KeyMissing, nil
+ case k.Active:
+ return KeyActive, nil
+ }
+ return KeyInactive, nil
+}
+
func (p *MemProvisioner) DeleteUser(_ context.Context, user string) error {
p.mu.Lock()
defer p.mu.Unlock()
diff --git a/internal/mail/provisioner.go b/internal/mail/provisioner.go
index 3df1f2440..2c6016d36 100644
--- a/internal/mail/provisioner.go
+++ b/internal/mail/provisioner.go
@@ -115,4 +115,23 @@ type Provisioner interface {
// list-and-delete can find it.
ReapKeys(ctx context.Context, user string, keep []string) error
DeleteUser(ctx context.Context, user string) error
+ // KeyState reads one access key of user, and writes nothing: KeyMissing
+ // when the user or the key does not exist, else KeyActive or
+ // KeyInactive. An error means the state could not be read, never that
+ // the key is gone. The delivery walks ask it before they skip a mint
+ // because a bundle already holds a key (ruled by Paul, 2026-11-14:
+ // switch-off checks keys).
+ KeyState(ctx context.Context, user, keyID string) (KeyState, error)
}
+
+// KeyState is what IAM holds for one access key.
+type KeyState int
+
+const (
+ // KeyMissing: the user, or the key under it, does not exist.
+ KeyMissing KeyState = iota
+ // KeyActive: the key exists and IAM accepts it.
+ KeyActive
+ // KeyInactive: the key exists and is deactivated (a pause).
+ KeyInactive
+)
diff --git a/internal/mail/walk.go b/internal/mail/walk.go
index 33637f5c8..2c803c350 100644
--- a/internal/mail/walk.go
+++ b/internal/mail/walk.go
@@ -865,7 +865,20 @@ func claimUnserializedEnable(ctx context.Context, s release.Store, p Provisioner
return nil, err
}
if len(missing) == 0 {
- return rec, nil
+ // The bundle holds the recorded key: skip the mint only if IAM
+ // still holds that key, in the state the sender's pause says
+ // (ruled by Paul, 2026-11-14: switch-off checks keys).
+ owner := rec.Provisioned.IAMUser
+ if owner == "" {
+ owner = user
+ }
+ live, err := deliveredKeyLive(ctx, p, owner, rec.Provisioned.KeyID, rec.EffectivePause() != nil)
+ if err != nil {
+ return nil, err
+ }
+ if live {
+ return rec, nil
+ }
}
}
@@ -1049,6 +1062,35 @@ type StandaloneCreds struct {
// `if !active`: unconditional self-corrects in both directions (an active
// record's freshly minted key is confirmed active, not just left alone) and
// cannot drift out of sync with the record.
+// deliveredKeyLive is the check a delivery walk makes before it skips a
+// mint because a bundle already holds a key (ruled by Paul, 2026-11-14:
+// switch-off checks keys). A part-failed walk can leave a bundle naming a
+// key whose user it already deleted, while the record still names that
+// user; matching the bundle against the record alone then read as
+// delivered, and a switch-off left the app's instances on a dead key.
+//
+// The key is live when IAM holds it under user and it is active, or, for
+// a paused sender (paused), inactive: a pause deactivates keys on purpose,
+// and a mint would only deactivate the new one again. A key state that
+// cannot be read is an error, never live and never missing: the walk
+// stops, and for a switch-off that is before the switch is written, so
+// the app stays switched and keeps its working channel. Treating it as
+// missing would reap and re-mint, deleting a key that running instances
+// may still be using.
+func deliveredKeyLive(ctx context.Context, p Provisioner, user, keyID string, paused bool) (bool, error) {
+ st, err := p.KeyState(ctx, user, keyID)
+ if err != nil {
+ return false, fmt.Errorf("mail: read the state of key %s of %s: %w", keyID, user, err)
+ }
+ switch st {
+ case KeyActive:
+ return true, nil
+ case KeyInactive:
+ return paused, nil
+ }
+ return false, nil
+}
+
func enforceKeyActive(ctx context.Context, p Provisioner, user, keyID string, active bool) error {
if err := p.SetKeyActive(ctx, user, keyID, active); err != nil {
return fmt.Errorf("mail: set key active for %s/%s: %w", user, keyID, err)
diff --git a/internal/mail/walk_environment.go b/internal/mail/walk_environment.go
index 75b3c7395..9a73108ed 100644
--- a/internal/mail/walk_environment.go
+++ b/internal/mail/walk_environment.go
@@ -99,7 +99,16 @@ func claimUnserializedEnableEnvironment(ctx context.Context, s release.Store, p
return nil, err
}
if len(missing) == 0 && cur.IAMUser == user {
- return rec, nil
+ // The bundle holds the recorded key: skip the mint only if IAM
+ // still holds that key, in the state the record says (ruled by
+ // Paul, 2026-11-14: switch-off checks keys).
+ live, err := deliveredKeyLive(ctx, p, user, cur.KeyID, !cur.Active)
+ if err != nil {
+ return nil, err
+ }
+ if live {
+ return rec, nil
+ }
}
}
// The pause state follows the app's: a paused app's environments are