mail: SenderOfCaller parses first and reads the index only for a slug (plan 2, task 6)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 file changed,
+37
−0
internal/mail/sender.go+37 −0
diff --git a/internal/mail/sender.go b/internal/mail/sender.go| index cc186bcc1..e08ba79bc 100644 |
| --- a/internal/mail/sender.go |
| +++ b/internal/mail/sender.go |
| @@ -373,3 +373,40 @@ func ResolveSenderNames(ctx context.Context, s release.Store, acct, app string, |
| } |
| return SenderNameSet{}, salted, fmt.Errorf("mail: could not find a free IAM name for sender %s/%s/%s after four attempts", acct, app, sn.ID) |
| } |
| + |
| + |
| +// SenderOfCaller resolves the IAM identity SES stamps on an event |
| +// (mail.tags["ses:caller-identity"]) to the sender that owns it. |
| +// |
| +// PARSE FIRST, in two steps, and the order is load-bearing. AppOfCaller |
| +// answers for carlos-mail-<acct>-<app> and its +s+ standalones, which is |
| +// every sender that exists today, and it costs nothing. Only a slugged +i+ |
| +// name, which is a hash and unparseable by construction, reaches the index |
| +// Get. Every app enabled today short-circuits Enable before MintKey |
| +// (walk.go:677), so it has no index entry until it next rotates: a Get-only |
| +// resolver would silently stop writing the by_sender log rows that |
| +// sesevents.go writes now, for every app in the fleet, on the roll. |
| +// |
| +// An unreadable index entry is logged and answers "unknown", which puts the |
| +// event back on the domain-wide fallback rather than dropping it. |
| +func SenderOfCaller(ctx context.Context, s release.Store, caller string) (string, string, string, bool) { |
| + if acct, app, ok := AppOfCaller(caller); ok { |
| + return acct, app, SenderAppID, true |
| + } |
| + if !strings.Contains(caller, "+i+") { |
| + return "", "", "", false |
| + } |
| + name := caller |
| + if i := strings.LastIndex(name, "/"); i >= 0 { |
| + name = name[i+1:] |
| + } |
| + idx, err := GetCallerIndex(ctx, s, name) |
| + if err != nil { |
| + log.Printf("mail: caller index %s -> %v", name, err) |
| + return "", "", "", false |
| + } |
| + if idx == nil || !release.ValidAccount(idx.Account) || !release.ValidApp(idx.App) || !ValidSenderID(idx.Sender) { |
| + return "", "", "", false |
| + } |
| + return idx.Account, idx.App, idx.Sender, true |
| +} |