Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

refactor(egress): drop the binding-input policy update compatibility path

高然142372dbe26c
diff --git a/components/egress/fastsandbox_policy.go b/components/egress/fastsandbox_policy.go
index 2de0abe4e..163decb24 100644
--- a/components/egress/fastsandbox_policy.go
+++ b/components/egress/fastsandbox_policy.go
@@ -49,16 +49,14 @@ import (
// policyEntry records the per-subject policy bookkeeping: the revision the
// relay preconditions fence on, the runtime fence the record was
-// materialized under, and the two state flags. The policy itself lives in
-// the registry (active subjects) or pendingPolicies (denying subjects).
+// materialized under, and the sealed flag. The policy itself lives in the
+// registry (active subjects) or pendingPolicies (denying subjects). A
+// record is authoritative once it exists: the binding input only ever
+// seeds a fresh subject, and every later policy change goes through the
+// relay or the snapshot transfer — exactly like the vault.
type policyEntry struct {
revision int64
fence subject.Fencing
- // hydrated marks a record loaded from the persisted policy file: the
- // binding input no longer drives this subject's policy — the file (and
- // the relay writes on top of it) is authoritative, exactly like the
- // vault slice. Sticky for the record's lifetime.
- hydrated bool
// sealed marks a paused subject whose policy file was exported into the
// snapshot bundle: every relay op answers 409; enforcement is unchanged.
sealed bool
@@ -248,7 +246,7 @@ func (s *fastSandboxPolicyServer) handlePolicyRelayWrite(w http.ResponseWriter,
}
created := entry == nil
newRev := cur + 1
- s.putRelayPolicy(w, r, subj, fence, pol, newRev, created, entry, stateActive)
+ s.putRelayPolicy(w, r, subj, fence, pol, newRev, created, stateActive)
}
// relayPolicyPrecondition extracts the effective precondition. The revision
@@ -271,9 +269,8 @@ func relayPolicyPrecondition(doc *policyRelayDocument) (expected *int64, err err
// putRelayPolicy commits a full replace: persist → apply → ack, so the ack
// means durable AND applied. The caller holds policyCommitMu (the decision
-// and the commit are one serialized unit). The live entry keeps its
-// hydrated flag — a relay write on a file-backed record stays file-backed.
-func (s *fastSandboxPolicyServer) putRelayPolicy(w http.ResponseWriter, r *http.Request, subj subject.Subject, fence subject.Fencing, pol *policy.NetworkPolicy, newRev int64, created bool, entry *policyEntry, stateActive bool) {
+// and the commit are one serialized unit).
+func (s *fastSandboxPolicyServer) putRelayPolicy(w http.ResponseWriter, r *http.Request, subj subject.Subject, fence subject.Fencing, pol *policy.NetworkPolicy, newRev int64, created bool, stateActive bool) {
raw, err := json.Marshal(pol)
if err != nil {
log.Errorf("subject %s: policy marshal failed: %v", subj, err)
@@ -294,8 +291,7 @@ func (s *fastSandboxPolicyServer) putRelayPolicy(w http.ResponseWriter, r *http.
if !stateActive {
s.pendingPolicies[subj] = pol
}
- hydrated := entry != nil && entry.hydrated
- s.policies[subj] = &policyEntry{revision: newRev, fence: fence, hydrated: hydrated}
+ s.policies[subj] = &policyEntry{revision: newRev, fence: fence}
s.mu.Unlock()
return nil
}
diff --git a/components/egress/fastsandbox_policy_persist.go b/components/egress/fastsandbox_policy_persist.go
index 417c11c07..dbebee026 100644
--- a/components/egress/fastsandbox_policy_persist.go
+++ b/components/egress/fastsandbox_policy_persist.go
@@ -293,7 +293,7 @@ func (s *fastSandboxPolicyServer) clearPolicyRecord(subj subject.Subject) {
//
// - fence match → the file is authoritative — the replayed binding input
// carries only the original bootstrap policy, never the relay mutations
-// recorded since — and the entry materializes file-backed (hydrated)
+// recorded since — and the record materializes from the file
// - mismatch → stale, purged (a reset never inherits the predecessor's
// policy)
// - unreadable, a newer schema, or content that fails to parse → the file
@@ -365,7 +365,7 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe
log.Warnf("subject %s: policy file content rejected (%v); quarantined, the binding input seeds the subject", subj, parseErr)
return
}
- entry := &policyEntry{revision: pf.Revision, fence: fence, hydrated: true}
+ entry := &policyEntry{revision: pf.Revision, fence: fence}
// A durable seal marker (export acked, teardown not yet delivered)
// restores the sealed entry across a restart: relay ops stay 409 until
// REMOVE_BINDING purges or an import unseals.
@@ -378,41 +378,27 @@ func (s *fastSandboxPolicyServer) rehydratePolicyReload(subj subject.Subject, fe
log.Infof("subject %s: policy reloaded from file (revision %d, incarnation %s); binding input superseded", subj, pf.Revision, pf.Incarnation)
}
-// applyBindingPolicy installs the binding's policy for a registered subject.
-// It is the SET_BINDING tail counterpart of the relay writes and runs the
-// same commit protocol. The binding input keeps its authority only while the
-// subject's record is not file-backed: once the policy was rehydrated from
-// the persisted file, later binding replays (which carry only the bootstrap
-// input) never override the recorded state, and a sealed record never moves
-// (the bundle copy is taken).
+// applyBindingPolicy seeds the binding's policy for a freshly registered
+// subject (record absent) through the same commit protocol as the relay
+// writes. A subject with an existing record never reads its binding input
+// again: the recorded policy — seeded once, then owned by the relay writes
+// and the snapshot transfer — is authoritative, exactly like the vault
+// slice. Binding replays (at-least-once delivery, egress restarts, and the
+// superseded CRD-driven update path) are therefore policy no-ops.
func (s *fastSandboxPolicyServer) applyBindingPolicy(subj subject.Subject, fence subject.Fencing, pol *policy.NetworkPolicy, stateActive bool) error {
s.policyCommitMu.Lock()
defer s.policyCommitMu.Unlock()
s.mu.Lock()
entry := s.policies[subj]
- var (
- rev int64
- commit bool
- )
- switch {
- case entry != nil && entry.sealed:
- // Exported: the bundle copy is taken; the recorded state is frozen.
- case entry != nil && entry.hydrated:
- // File-backed: the binding input carries no authority.
- case entry != nil:
- rev, commit = entry.revision+1, true
- default:
- rev, commit = 1, true
- }
pp := s.policyFiles
s.mu.Unlock()
-
- if !commit {
+ if entry != nil {
log.Infof("subject %s: binding input superseded by the recorded policy (revision %d)", subj, entry.revision)
return nil
}
+ const rev = int64(1)
raw, err := json.Marshal(pol)
if err != nil {
return fmt.Errorf("policy marshal: %w", err)
diff --git a/components/egress/fastsandbox_policy_snapshot.go b/components/egress/fastsandbox_policy_snapshot.go
index cf2ea9ae6..9bb9e9f9d 100644
--- a/components/egress/fastsandbox_policy_snapshot.go
+++ b/components/egress/fastsandbox_policy_snapshot.go
@@ -18,7 +18,7 @@
// runtime teardown and seals the subject; POLICY_IMPORT lands the bundle
// file re-stamped with the restored fence — the only authorized incarnation
// transfer — through the same commit protocol as the relay writes, marking
-// the record file-backed so later binding replays never override it. The
+// the recorded policy so later binding replays never override it. The
// invocation side (driver/Fastlet hooks, snapshot manifest) is platform
// work in the fast-sandbox repository.
package main
@@ -187,7 +187,7 @@ func (s *fastSandboxPolicyServer) policyExportSourceImageLocked(subj subject.Sub
// applyPolicyImport lands the bundle file as the subject's policy file,
// re-stamped with the restored fence from the envelope (restore may land on
// any Pod, so the recorded incarnation is replaced, not compared). The file
-// goes through the commit protocol and the record materializes file-backed;
+// goes through the commit protocol and the record materializes from the bundle;
// the policy activates at the same data-plane-ready barrier as the vault.
//
// The bundle is read, decoded, and parsed before the commit serialization.
@@ -327,7 +327,7 @@ func (s *fastSandboxPolicyServer) installImportedPolicyLocked(subj subject.Subje
if !stateActive {
s.pendingPolicies[subj] = pol
}
- s.policies[subj] = &policyEntry{revision: revision, fence: fence, hydrated: true}
+ s.policies[subj] = &policyEntry{revision: revision, fence: fence}
s.mu.Unlock()
// Unseal only after the state accepted — a failed apply rolls the file
// back and keeps the seal.
diff --git a/components/egress/fastsandbox_server_test.go b/components/egress/fastsandbox_server_test.go
index cb9eea015..5cd13cbe9 100644
--- a/components/egress/fastsandbox_server_test.go
+++ b/components/egress/fastsandbox_server_test.go
@@ -298,20 +298,31 @@ func TestActionsSetBindingDenyFirstThenHookActivates(t *testing.T) {
assert.Equal(t, "allow", eff.Evaluate("example.com"))
}
-func TestActionsSetBindingUpdateWhileActiveDoesNotReplay(t *testing.T) {
+// TestActionsSetBindingReplayNeverMutatesRecordedPolicy: the binding input
+// only seeds a fresh subject's policy. A same-fence SET_BINDING carrying a
+// different input (the superseded CRD-driven update path, or an at-least-once
+// replay) must never mutate the recorded policy — runtime changes go through
+// the relay surface.
+func TestActionsSetBindingReplayNeverMutatesRecordedPolicy(t *testing.T) {
srv, reg, nft := fastSandboxTestServer(t)
input := `{"defaultAction":"deny","egress":[{"action":"allow","target":"example.com"}]}`
s := setBindingAndReady(t, srv, reg, testUID, testIP, &input)
+ nft.mu.Lock()
denyFirstInstalls := len(nft.denyFirst)
+ appliedAfterSeed := len(nft.policyApplied)
+ nft.mu.Unlock()
- // plain input update (new spec generation, same fence): applied in place
+ // same-fence binding with a different input: the recorded policy wins
newInput := `{"defaultAction":"deny","egress":[{"action":"allow","target":"updated.com"}]}`
rec := doAction(t, srv, bindBody(t, testUID, testIP, testFenceR, testFenceA, 2, &newInput))
require.Equal(t, http.StatusOK, rec.Code)
require.Equal(t, subject.StateActive, mustState(reg.Get(s)))
- require.Equal(t, 2, nft.appliedCount())
- require.Equal(t, denyFirstInstalls, len(nft.denyFirst), "an update must not re-install deny-first")
- assert.Equal(t, "allow", reg.EffectivePolicy(s).Evaluate("updated.com"))
+ nft.mu.Lock()
+ assert.Equal(t, appliedAfterSeed, len(nft.policyApplied), "a replayed binding must not re-apply a policy")
+ assert.Equal(t, denyFirstInstalls, len(nft.denyFirst), "a replayed binding must not re-install deny-first")
+ nft.mu.Unlock()
+ assert.Equal(t, "allow", reg.EffectivePolicy(s).Evaluate("example.com"), "the seeded policy survives the replay")
+ assert.Equal(t, "deny", reg.EffectivePolicy(s).Evaluate("updated.com"))
}
func TestActionsSetBindingNullRemovesPolicy(t *testing.T) {
@@ -364,14 +375,20 @@ func TestActionsRebindDiscardsPolicy(t *testing.T) {
require.Equal(t, subject.StateDenying, mustState(reg.Get(s)))
assert.Nil(t, reg.EffectivePolicy(s), "old policy must not survive a rebind")
- // the new instance's data-plane-ready activates with its own policy
- newInput := `{"defaultAction":"deny","egress":[{"action":"allow","target":"new.com"}]}`
- rec = doAction(t, srv, bindBody(t, testUID, testIP, "runtime-2", "attachment-2", 6, &newInput))
- require.Equal(t, http.StatusOK, rec.Code)
+ // the new instance's data-plane-ready activates with its own (re-seeded)
+ // policy
rec = doAction(t, srv, hookBody(t, testUID, "runtime-2", "attachment-2", constants.HookDataPlaneReady))
require.Equal(t, http.StatusOK, rec.Code)
require.Equal(t, subject.StateActive, mustState(reg.Get(s)))
+ assert.Equal(t, "allow", reg.EffectivePolicy(s).Evaluate("old.com"))
+
+ // a runtime policy change goes through the relay: the relayed policy is
+ // what the successor enforces
+ rec = doPolicy(t, srv, http.MethodPut, testUID, policyPutDoc(
+ `{"defaultAction":"deny","egress":[{"action":"allow","target":"new.com"}]}`, 1, false, true), "")
+ require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "allow", reg.EffectivePolicy(s).Evaluate("new.com"))
+ assert.Equal(t, "deny", reg.EffectivePolicy(s).Evaluate("old.com"))
require.Greater(t, len(nft.denyFirst), 0)
}
@@ -1016,25 +1033,43 @@ func TestFastSandboxServerAlwaysRulesReachNft(t *testing.T) {
// TestFastSandboxServerNftFailureKeepsRegistryState: a failed nft apply must leave
// the registry (DNS dispatch) on the PREVIOUS policy — nft commits before registry
-// state.
+// state, and the relay write NACKs with last-good kept on both sides.
func TestFastSandboxServerNftFailureKeepsRegistryState(t *testing.T) {
srv, reg, nft := fastSandboxTestServer(t)
input := `{"defaultAction":"deny"}`
s := setBindingAndReady(t, srv, reg, "u-1", testIP, &input)
require.Equal(t, subject.StateActive, mustState(reg.Get(s)))
- // in-place binding update fails at nft: registry must stay on the FIRST
- // policy
+ // relay write fails at nft: 500, the registry must stay on the FIRST
+ // policy (the record was seeded at revision 1; the write targeted
+ // revision 2)
nft.mu.Lock()
nft.policyErr = errors.New("nft busy")
nft.mu.Unlock()
- rec := doAction(t, srv, bindBody(t, "u-1", testIP, testFenceR, testFenceA, 2,
- strPtr(`{"defaultAction":"deny","egress":[{"action":"allow","target":"example.com"}]}`)))
+ rec := doPolicy(t, srv, http.MethodPut, "u-1", policyPutDoc(
+ `{"defaultAction":"deny","egress":[{"action":"allow","target":"example.com"}]}`, 1, false, true), "")
require.Equal(t, http.StatusInternalServerError, rec.Code)
eff := reg.EffectivePolicy(s)
require.NotNil(t, eff)
assert.Equal(t, "deny", eff.Evaluate("example.com"), "failed nft apply must not publish the new policy")
+
+ // a same-fence binding replay never carries the new policy either
+ rec = doAction(t, srv, bindBody(t, "u-1", testIP, testFenceR, testFenceA, 2,
+ strPtr(`{"defaultAction":"deny","egress":[{"action":"allow","target":"example.com"}]}`)))
+ require.Equal(t, http.StatusOK, rec.Code)
+ eff = reg.EffectivePolicy(s)
+ require.NotNil(t, eff)
+ assert.Equal(t, "deny", eff.Evaluate("example.com"), "the recorded policy is authoritative over binding replays")
+
+ // the retry with a working nft lands
+ nft.mu.Lock()
+ nft.policyErr = nil
+ nft.mu.Unlock()
+ rec = doPolicy(t, srv, http.MethodPut, "u-1", policyPutDoc(
+ `{"defaultAction":"deny","egress":[{"action":"allow","target":"example.com"}]}`, 1, false, true), "")
+ require.Equal(t, http.StatusOK, rec.Code)
+ assert.Equal(t, "allow", reg.EffectivePolicy(s).Evaluate("example.com"))
}
// TestFastSandboxServerGatewayRedirectRefcounted: the shared prerouting REDIRECT