| index bd2898ca7..b275e06b7 100644 |
| --- a/internal/console/submission_sim_test.go |
| +++ b/internal/console/submission_sim_test.go |
| @@ -13,6 +13,7 @@ import ( |
| "strconv" |
| "strings" |
| "sync" |
| + "sync/atomic" |
| "testing" |
| "time" |
| |
| @@ -260,17 +261,33 @@ func (o *simOverlay) ListDirs(ctx context.Context, prefix string) ([]string, err |
| // makes, one of which an armed step fails. |
| var simProvisionerFaults = []string{"EnsureUser", "MintKey", "ReapKeys", "DeleteUser", "SetKeyActive", "DeleteKey"} |
| |
| +// simHookedProvisioner is the in-memory AWS with a hook run before each |
| +// ReapKeys, so a step can pause a door inside its own work. |
| +type simHookedProvisioner struct { |
| + *mail.MemProvisioner |
| + beforeReapKeys atomic.Pointer[func(user string)] |
| +} |
| + |
| +func (p *simHookedProvisioner) ReapKeys(ctx context.Context, user string, keep []string) error { |
| + if h := p.beforeReapKeys.Load(); h != nil { |
| + (*h)(user) |
| + } |
| + return p.MemProvisioner.ReapKeys(ctx, user, keep) |
| +} |
| + |
| type channelSim struct { |
| t *testing.T |
| w *submissionWorld |
| rng *rand.Rand |
| faults *simFaultStore |
| prov *mail.MemProvisioner |
| + hooked *simHookedProvisioner |
| team string // a team credential of scope alta |
| hosts []string |
| envs []string // owned environments: "team" and the made ones |
| reported map[string]string |
| dry bool // a dry pass: no bookkeeping, no honesty checks |
| + liveKeys bool // check also checks that a switch-off's SMTP keys work |
| nextID int |
| steps []string |
| } |
| @@ -300,6 +317,8 @@ func newChannelSim(t *testing.T, seed int64) *channelSim { |
| hosts: []string{submissionHost}, envs: []string{"team"}, reported: map[string]string{}} |
| sim.faults = &simFaultStore{base: w.fx.srv.cfg.Releases, prefix: "apps/bab/titogo/"} |
| w.fx.srv.cfg.Releases = sim.faults |
| + sim.hooked = &simHookedProvisioner{MemProvisioner: sim.prov} |
| + w.fx.srv.cfg.MailProvisioner = sim.hooked |
| sim.boxTick(true, false) |
| return sim |
| } |
| @@ -433,6 +452,11 @@ func (sim *channelSim) declared(version string) bool { |
| |
| // check asserts the invariant after a step. wasSwitched is the switch |
| // before it: a step that turned it off must leave every instance on SMTP. |
| +// With liveKeys set, each of those SMTP keys must also be one AWS still |
| +// holds, active, for a user it still has (P2-3 of Fable's final review: a |
| +// reap that ran around a switch-off deleted the user the switch-off had |
| +// just delivered). liveKeys is set for that step only: the other steps' |
| +// faults can leave a key that is not live (reported with P2-3). |
| // While switched, every instance resolves to a declared build (and a box |
| // that has not adopted yet serves a declared one too), and its bundle |
| // holds a From the box's mailer sends. |
| @@ -469,6 +493,9 @@ func (sim *channelSim) check(wasSwitched bool) { |
| bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off without %s", rec.Host, rec.Environment, k)) |
| } |
| } |
| + if key := vars[user].Value; sim.liveKeys && key != "" && !sim.keyLive(key) { |
| + bad = append(bad, fmt.Sprintf("%s (environment %q) was switched off onto key %s, which no user holds active", rec.Host, rec.Environment, key)) |
| + } |
| } |
| } |
| if len(bad) > 0 { |
| @@ -476,6 +503,17 @@ func (sim *channelSim) check(wasSwitched bool) { |
| } |
| } |
| |
| +// keyLive reports whether the in-memory AWS holds key, active, for a user |
| +// it still has. |
| +func (sim *channelSim) keyLive(key string) bool { |
| + for _, u := range sim.prov.Users { |
| + if k, ok := u.Keys[key]; ok && k.Active && !u.Deleted { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| // provisionerState is the in-memory AWS's state, for a dry pass to put |
| // back. |
| type provisionerState struct { |
| @@ -711,6 +749,43 @@ func (sim *channelSim) reap() string { |
| return "ok" |
| } |
| |
| +// reapAroundSwitchOff is the hourly reap paused inside its own work (Fable's |
| +// final review, P2-3): it has read the app sender and is about to delete |
| +// the app-level user's keys when a whole switch-off is started. The |
| +// switch-off is given a moment to land. With the reap holding the account |
| +// and app locks, as the switch door does, it cannot: it runs once the reap |
| +// is done, and mints the user again. A reap that held neither went on to |
| +// delete the user the switch-off had just delivered. reached says whether |
| +// the reap got that far. |
| +func (sim *channelSim) reapAroundSwitchOff() (out string, reached bool) { |
| + user := mail.UserName("bab", "titogo") |
| + var off string |
| + var wg sync.WaitGroup |
| + hook := func(u string) { |
| + if u != user { |
| + return |
| + } |
| + sim.hooked.beforeReapKeys.Store(nil) |
| + reached = true |
| + done := make(chan struct{}) |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + defer close(done) |
| + off = sim.switchOff() |
| + }() |
| + select { |
| + case <-done: |
| + case <-time.After(simRaceWait): |
| + } |
| + } |
| + sim.hooked.beforeReapKeys.Store(&hook) |
| + defer sim.hooked.beforeReapKeys.Store(nil) |
| + out = sim.reap() |
| + wg.Wait() |
| + return out + " | switch-off in between: " + off, reached |
| +} |
| + |
| // raceSwitchOn is the switch-on door with another door started between |
| // its preconditions and its switch (P2-1 and its kin). The other door is |
| // given a moment to land; with the app lock held it cannot, and it runs, |
| @@ -895,6 +970,20 @@ func (sim *channelSim) scripted() { |
| sim.do(fmt.Sprintf("switch on, team's write #%d failing", n), false, sim.switchOn) |
| } |
| sim.do("switch on", false, sim.switchOn) |
| + // P2-3 of Fable's final review: a switch-off started inside a reap |
| + // paused before its deletes. |
| + reached := false |
| + sim.liveKeys = true |
| + sim.do("the hourly reap, a switch-off started inside it", false, func() string { |
| + out, r := sim.reapAroundSwitchOff() |
| + reached = r |
| + return out |
| + }) |
| + sim.liveKeys = false |
| + if !reached { |
| + sim.t.Fatalf("the reap never reached the app user's keys, so the step proved nothing\nsteps:\n %s", strings.Join(sim.steps, "\n ")) |
| + } |
| + sim.do("switch on", false, sim.switchOn) |
| for _, env := range slices.Clone(sim.envs) { |
| if env != "team" { |
| sim.do("release "+env, false, func() string { return sim.release(env) }) |