carlos / platform Public

host: the egress nudge also fires on an expiry-only change (#328)

Controller ruling on Task 7 concern 2: key the nudge on egress_allow and
policy_expires_at, so a re-sign that re-opens a host root closed as
expired lands in about 2 s rather than on root's 30 s loop. An expiry
change with no allow before or after still sends nothing: root's desired
list is empty either way.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev f6e965ac71572fd968f0d75f3154d63f8c69232f parent d1c4b1c
2 files changed, +67 −18
  • internal/host/egress_nudge.go +23 −15
  • internal/host/egress_nudge_test.go +44 −3
diff --git a/internal/host/egress_nudge.go b/internal/host/egress_nudge.go
index 31348ab3c..f92f96b22 100644
--- a/internal/host/egress_nudge.go
+++ b/internal/host/egress_nudge.go
@@ -11,7 +11,8 @@ import (
// egressNudgeLoop tells root about an egress allow change as soon as the
// registry holds it (#328): every edgesFileWatchInterval it reads the
// registry's SnapshotFingerprint and, when that moved, lists the rows and
-// sends POST /egress?host= for each host whose egress_allow changed.
+// sends POST /egress?host= for each host whose egress_allow or
+// policy_expires_at changed.
//
// It watches SnapshotFingerprint, not Fingerprint: the policy columns
// (egress_allow, policy_expires_at) are kept out of Fingerprint, so a grant
@@ -52,11 +53,11 @@ type egressNudger struct {
}
logf func(format string, args ...any)
- // nudged is each host's egress_allow as root last heard of it (or,
- // for a host first seen with none, as first seen). failing holds the
- // hosts whose last call failed: each is sent again on the next pass,
- // whatever its column holds by then.
- nudged map[string]string
+ // nudged is each host's policy columns as root last heard of them
+ // (or, for a host first seen with no allow, as first seen). failing
+ // holds the hosts whose last call failed: each is sent again on the
+ // next pass, whatever its columns hold by then.
+ nudged map[string]egressNudgeKey
failing map[string]bool
fp string
haveFP bool
@@ -67,7 +68,7 @@ type egressNudger struct {
func newEgressNudger(c interface {
Egress(context.Context, string) error
}) *egressNudger {
- return &egressNudger{c: c, logf: log.Printf, nudged: map[string]string{}, failing: map[string]bool{}}
+ return &egressNudger{c: c, logf: log.Printf, nudged: map[string]egressNudgeKey{}, failing: map[string]bool{}}
}
// tick is one beat of the loop: a pass when the snapshot fingerprint moved
@@ -93,10 +94,16 @@ func (n *egressNudger) tick(ctx context.Context, reg egressNudgeRegistry) {
n.pass(ctx, rows)
}
-// pass sends POST /egress for each row whose egress_allow differs from
-// what root last heard. A host first seen with an allow counts as changed;
-// one first seen with none does not. A failed call is logged (throttled)
-// and sent again on the next pass. Hosts no longer listed are forgotten.
+// egressNudgeKey is what a nudge is keyed on: the allow, and its expiry,
+// so a re-sign that re-opens a host root closed as expired lands at once.
+type egressNudgeKey struct{ allow, expires string }
+
+// pass sends POST /egress for each row whose egress_allow or
+// policy_expires_at differs from what root last heard. A host first seen
+// with an allow counts as changed; one first seen with none does not, and
+// nor does an expiry change with no allow before or after (root's desired
+// list is empty either way). A failed call is logged (throttled) and sent
+// again on the next pass. Hosts no longer listed are forgotten.
func (n *egressNudger) pass(ctx context.Context, rows []registry.Route) {
listed := make(map[string]bool, len(rows))
failed := false
@@ -108,13 +115,14 @@ func (n *egressNudger) pass(ctx context.Context, rows []registry.Route) {
continue
}
listed[rt.Host] = true
+ cur := egressNudgeKey{rt.EgressAllow, rt.PolicyExpiresAt}
prev, known := n.nudged[rt.Host]
switch {
case n.failing[rt.Host]:
- case !known && rt.EgressAllow == "":
- n.nudged[rt.Host] = ""
+ case known && prev == cur:
continue
- case known && prev == rt.EgressAllow:
+ case cur.allow == "" && (!known || prev.allow == ""):
+ n.nudged[rt.Host] = cur
continue
}
if err := n.c.Egress(ctx, rt.Host); err != nil {
@@ -127,7 +135,7 @@ func (n *egressNudger) pass(ctx context.Context, rows []registry.Route) {
continue
}
delete(n.failing, rt.Host)
- n.nudged[rt.Host] = rt.EgressAllow
+ n.nudged[rt.Host] = cur
}
for h := range n.nudged {
if !listed[h] {
diff --git a/internal/host/egress_nudge_test.go b/internal/host/egress_nudge_test.go
index 9260d397b..7e5e289e5 100644
--- a/internal/host/egress_nudge_test.go
+++ b/internal/host/egress_nudge_test.go
@@ -122,6 +122,45 @@ func TestEgressNudgeOnlyChangedHosts(t *testing.T) {
}
}
+func policyRow(host, allow, expires string) registry.Route {
+ rt := allowRow(host, allow)
+ rt.PolicyExpiresAt = expires
+ return rt
+}
+
+// TestEgressNudgeExpiryOnlyChange: a re-sign moves only policy_expires_at.
+// Root may have closed the host as expired, so the new expiry is sent,
+// once. With no allow before or after, an expiry change cannot move root's
+// desired list (empty either way), so nothing is sent.
+func TestEgressNudgeExpiryOnlyChange(t *testing.T) {
+ t.Parallel()
+ c := &fakeEgressNudge{}
+ var logs []string
+ n := newTestNudger(c, &logs)
+ ctx := context.Background()
+
+ n.pass(ctx, []registry.Route{
+ policyRow("shop-a.example.com", "10.0.5.20", "1900000000"),
+ policyRow("shop-b.example.com", "", "1900000000"),
+ })
+ wantNudged(t, "first pass", c.take(), "shop-a.example.com")
+
+ n.pass(ctx, []registry.Route{
+ policyRow("shop-a.example.com", "10.0.5.20", "1900043200"),
+ policyRow("shop-b.example.com", "", "1900043200"),
+ })
+ wantNudged(t, "re-sign pass", c.take(), "shop-a.example.com")
+
+ n.pass(ctx, []registry.Route{
+ policyRow("shop-a.example.com", "10.0.5.20", "1900043200"),
+ policyRow("shop-b.example.com", "", "1900043200"),
+ })
+ wantNudged(t, "after the re-sign", c.take())
+ if len(logs) != 0 {
+ t.Fatalf("logged %q with every nudge succeeding", logs)
+ }
+}
+
func TestEgressNudgeRevokeToEmpty(t *testing.T) {
t.Parallel()
c := &fakeEgressNudge{}
@@ -264,8 +303,8 @@ func TestEgressNudgeWatchesTheSnapshotFingerprint(t *testing.T) {
n.tick(ctx, reg)
wantNudged(t, "egress_allow tick", c.take(), "shop-a.example.com")
- // policy_expires_at alone (a re-sign): the snapshot moves, the allow
- // did not, so nothing is sent.
+ // policy_expires_at alone (a re-sign): the snapshot moves, and the
+ // new expiry reaches root, which may have closed the host as expired.
if err := reg.SetPolicy("shop-a.example.com", "10.0.5.21", "", "1900043200"); err != nil {
t.Fatal(err)
}
@@ -274,7 +313,9 @@ func TestEgressNudgeWatchesTheSnapshotFingerprint(t *testing.T) {
t.Fatalf("policy_expires_at alone: Fingerprint moved %v, SnapshotFingerprint moved %v; want false, true", fp3 != fp2, snap3 != snap2)
}
n.tick(ctx, reg)
- wantNudged(t, "re-sign tick", c.take())
+ wantNudged(t, "re-sign tick", c.take(), "shop-a.example.com")
+ n.tick(ctx, reg)
+ wantNudged(t, "quiet tick after the re-sign", c.take())
// The revoke reaches root on the next tick.
if err := reg.SetPolicy("shop-a.example.com", "", "", ""); err != nil {