provision: an empty reconcile pass forgets departed apps' cached policies (#328, #329)
Final review finding 1: the policy cache returned on an empty app set before pruning, so purge -> empty pass -> recreate before the backstop gave the recreated app its predecessor's egress allow and size on a disarmed or fleet box. Prune first; a lifecycle test keeps one cache and one registry throughout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+47
−4
internal/provision/apppolicy_cache.go+6 −4internal/provision/apppolicy_cache_test.go+41 −0
diff --git a/internal/provision/apppolicy_cache.go b/internal/provision/apppolicy_cache.go| index 149075840..9bb891b26 100644 |
| --- a/internal/provision/apppolicy_cache.go |
| +++ b/internal/provision/apppolicy_cache.go |
| @@ -136,11 +136,10 @@ func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.Pub |
| clear(c.entries) |
| c.pub = bytes.Clone(pub) |
| } |
| - if len(apps) == 0 { |
| - return out |
| - } |
| // An app that left the pass is forgotten: its entry would otherwise go |
| - // unrefreshed by the backstop and be trusted, stale, if it came back. |
| + // unrefreshed by the backstop and be trusted, stale, if it came back |
| + // (a purge and a recreation under the same name). This runs before the |
| + // empty-pass return, so a pass with no app at all forgets every entry. |
| inPass := make(map[[2]string]bool, len(apps)) |
| for _, a := range apps { |
| inPass[a] = true |
| @@ -150,6 +149,9 @@ func (c *PolicyCache) read(ctx context.Context, s release.Store, pub ed25519.Pub |
| delete(c.entries, a) |
| } |
| } |
| + if len(apps) == 0 { |
| + return out |
| + } |
| due := map[[2]string]uint64{} // app -> generation taken before its read |
| listedThisPass := false |
diff --git a/internal/provision/apppolicy_cache_test.go b/internal/provision/apppolicy_cache_test.go| index 119de4234..92ecf613b 100644 |
| --- a/internal/provision/apppolicy_cache_test.go |
| +++ b/internal/provision/apppolicy_cache_test.go |
| @@ -382,3 +382,44 @@ func TestPolicyCacheExpiresWithoutAReading(t *testing.T) { |
| t.Fatalf("PolicyReadStates = %q, want policy expired", got) |
| } |
| } |
| + |
| +// Final review finding 1: an app purged and recreated under its name |
| +// between backstops, with an empty pass between, must not inherit its |
| +// predecessor's cached policy. One cache and one registry throughout, as |
| +// the agent keeps them across passes. Fails if the empty pass returns |
| +// before forgetting the departed app. |
| +func TestARecreatedAppDoesNotInheritItsPredecessorsCachedPolicy(t *testing.T) { |
| + for _, armed := range []bool{false, true} { |
| + t.Run(fmt.Sprintf("armed=%v", armed), func(t *testing.T) { |
| + b := newBudgetFixture(t, armed, "shop") |
| + cache := b.cfg.Policies |
| + b.putPolicy("shop", policyGrant) |
| + b.pass() |
| + wantCols(t, b.row("shop"), "10.0.5.20", "large", b.fx.now.Add(apppolicy.Lifetime).Unix()) |
| + |
| + // Purge: the record and the policy go; the next pass has no |
| + // eligible app at all. |
| + if err := DeleteRecord(ctx, b.cs, b.sqid, "shop", b.host("shop")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := b.cs.DirStore.Delete(ctx, b.key("shop")); err != nil { |
| + t.Fatal(err) |
| + } |
| + b.fx.now = b.fx.now.Add(5 * time.Minute) |
| + b.pass() |
| + if rt, err := b.reg.Get(b.host("shop")); err != nil || rt != nil { |
| + t.Fatalf("after the empty pass: row %v, err %v; want the purged app's row gone", rt, err) |
| + } |
| + |
| + // Recreated under the same name, with no policy, before the |
| + // next backstop. |
| + b.fx.now = b.fx.now.Add(5 * time.Minute) |
| + b.addApp("shop", "") |
| + b.pass() |
| + if b.cfg.Policies != cache { |
| + t.Fatal("the fixture swapped the cache") |
| + } |
| + wantCols(t, b.row("shop"), "", "", 0) |
| + }) |
| + } |
| +} |