Skip to content

Agent Mesh

node: remove the unused on-disk peer ban store

HosniBelfekic3c488947bfd
diff --git a/internal/node/debug_handlers.go b/internal/node/debug_handlers.go
index 118345cb..ff0cf323 100644
--- a/internal/node/debug_handlers.go
+++ b/internal/node/debug_handlers.go
@@ -265,9 +265,6 @@ func (n *SamNode) connectPeer(ctx context.Context, peerAddr string) error {
if n.revokedPeers != nil && n.revokedPeers.Contains(addrInfo.ID.String()) {
return fmt.Errorf("failed to dial: failed to dial %s: gater disallows connection to peer", addrInfo.ID)
}
- if n.Store.IsBanned(addrInfo.ID) {
- return fmt.Errorf("failed to dial: failed to dial %s: gater disallows connection to peer", addrInfo.ID)
- }
conns := n.Host.Network().ConnsToPeer(addrInfo.ID)
connectedness := n.Host.Network().Connectedness(addrInfo.ID)
logger.Debugf("[connect-peer] Target peer %s, connectedness: %v, active conns: %d", addrInfo.ID, connectedness, len(conns))
diff --git a/internal/node/gate.go b/internal/node/gate.go
index de56f049..f83c38ac 100644
--- a/internal/node/gate.go
+++ b/internal/node/gate.go
@@ -44,11 +44,6 @@ func (g *nodeConnGate) InterceptPeerDial(p peer.ID) (allow bool) {
logger.Warnf("[Gater] InterceptPeerDial denying %s: in revoked cache", p)
return false
}
- banned := g.node.Store.IsBanned(p)
- if banned {
- logger.Warnf("[Gater] InterceptPeerDial denying %s: banned in store", p)
- return false
- }
logger.Debugf("[Gater] InterceptPeerDial allowing %s", p)
return true
}
@@ -70,10 +65,6 @@ func (g *nodeConnGate) InterceptSecured(dir network.Direction, p peer.ID, n netw
logger.Warnf("[Gater] InterceptSecured denying %s: in revoked cache", p)
return false
}
- if g.node.Store.IsBanned(p) {
- logger.Warnf("[Gater] InterceptSecured denying %s: banned in store", p)
- return false
- }
logger.Debugf("[Gater] InterceptSecured allowing %s", p)
return true
}
diff --git a/internal/node/gate_test.go b/internal/node/gate_test.go
index c10593bc..c187bbf6 100644
--- a/internal/node/gate_test.go
+++ b/internal/node/gate_test.go
@@ -27,7 +27,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/modelcontextprotocol/go-sdk/mcp"
- "go.etcd.io/bbolt"
)
func TestConnectionGater(t *testing.T) {
@@ -82,20 +81,49 @@ func TestConnectionGater(t *testing.T) {
t.Errorf("expected InterceptSecured to deny peer2 (in revoked cache)")
}
- // Case 3: Peer is in persistent store (banned)
- err = store.db.Update(func(tx *bbolt.Tx) error {
- b := tx.Bucket([]byte(bucketBannedPeers))
- return b.Put([]byte(peer3.String()), []byte("true"))
- })
+ // Case 3: a peer the control plane does not ban stays reachable, even
+ // after another peer has been banned.
+ if !gater.InterceptPeerDial(peer3) {
+ t.Errorf("expected InterceptPeerDial to allow peer3")
+ }
+ if !gater.InterceptSecured(network.DirInbound, peer3, nil) {
+ t.Errorf("expected InterceptSecured to allow peer3")
+ }
+}
+
+// The revocation cache is seeded from the control plane's ban set at startup
+// (Options.BannedPeerIDs, filled by SyncMeshConfig). Without that a restarted
+// node would enforce no ban at all until the next MeshEvent_BANNED, which for
+// a ban published while it was down never arrives.
+func TestGaterEnforcesSeededBans(t *testing.T) {
+ priv, _, _ := crypto.GenerateEd25519Key(nil)
+ banned, err := peer.IDFromPrivateKey(priv)
+ if err != nil {
+ t.Fatal(err)
+ }
+ otherPriv, _, _ := crypto.GenerateEd25519Key(nil)
+ allowed, err := peer.IDFromPrivateKey(otherPriv)
if err != nil {
t.Fatal(err)
}
- if gater.InterceptPeerDial(peer3) {
- t.Errorf("expected InterceptPeerDial to deny peer3 (in store)")
+ cache, err := lru.New[string, int64](100)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := &SamNode{revokedPeers: cache}
+ // Mirrors what NewSamNode does with Options.BannedPeerIDs.
+ node.revokedPeers.Add(banned.String(), time.Now().Unix())
+
+ gater := &nodeConnGate{node: node}
+ if gater.InterceptPeerDial(banned) {
+ t.Error("a peer in the control plane's ban set must not be dialled")
+ }
+ if gater.InterceptSecured(network.DirInbound, banned, nil) {
+ t.Error("a peer in the control plane's ban set must not be accepted")
}
- if gater.InterceptSecured(network.DirInbound, peer3, nil) {
- t.Errorf("expected InterceptSecured to deny peer3 (in store)")
+ if !gater.InterceptPeerDial(allowed) {
+ t.Error("seeding a ban must not deny unrelated peers")
}
}
diff --git a/internal/node/identity_evidence.go b/internal/node/identity_evidence.go
index fe5f331e..cf47474a 100644
--- a/internal/node/identity_evidence.go
+++ b/internal/node/identity_evidence.go
@@ -295,11 +295,11 @@ func (n *SamNode) buildPeerEvidence(requested peer.ID, observation peerBiscuitOb
}, nil
}
+// peerIsRevoked reports whether the peer is in the revocation cache, which is
+// seeded from the control plane's ban set at startup (see SyncMeshConfig) and
+// updated by MeshEvent_BANNED.
func (n *SamNode) peerIsRevoked(peerID peer.ID) bool {
- if n.revokedPeers != nil && n.revokedPeers.Contains(peerID.String()) {
- return true
- }
- return n.Store != nil && n.Store.IsBanned(peerID)
+ return n.revokedPeers != nil && n.revokedPeers.Contains(peerID.String())
}
type biscuitClaims struct {
diff --git a/internal/node/store.go b/internal/node/store.go
index c0d0dfda..0356ea1a 100644
--- a/internal/node/store.go
+++ b/internal/node/store.go
@@ -22,7 +22,6 @@ import (
"path/filepath"
"time"
- "github.com/libp2p/go-libp2p/core/peer"
"go.etcd.io/bbolt"
bbolterrors "go.etcd.io/bbolt/errors"
)
@@ -79,9 +78,6 @@ func NewStore(dir string) (*Store, error) {
if _, err := tx.CreateBucketIfNotExists([]byte(bucketIdentity)); err != nil {
return err
}
- if _, err := tx.CreateBucketIfNotExists([]byte(bucketBannedPeers)); err != nil {
- return err
- }
return nil
})
@@ -308,22 +304,8 @@ func (s *Store) Close() error {
return s.db.Close()
}
-const (
- bucketBannedPeers = "banned_peers"
-)
-
-// IsBanned checks local store to see if this peer is banned.
-func (s *Store) IsBanned(p peer.ID) bool {
- var banned bool
- _ = s.db.View(func(tx *bbolt.Tx) error {
- b := tx.Bucket([]byte(bucketBannedPeers))
- if b == nil {
- return nil
- }
- if b.Get([]byte(p.String())) != nil {
- banned = true
- }
- return nil
- })
- return banned
-}
+// Peer bans are deliberately not kept here. A ban on disk cannot be undone by
+// the control plane -- there is no unban event -- and it says nothing about a
+// node that was offline when the ban was published. Both are handled instead by
+// reconciling against the ban set in /info on every start (see SyncMeshConfig),
+// with MeshEvent_BANNED as the sub-second path for nodes that are already up.
diff --git a/internal/node/store_test.go b/internal/node/store_test.go
index d2a1f385..0546fac7 100644
--- a/internal/node/store_test.go
+++ b/internal/node/store_test.go
@@ -18,15 +18,11 @@ import (
"bytes"
"crypto/ed25519"
"errors"
- "fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"
-
- "github.com/libp2p/go-libp2p/core/peer"
- "go.etcd.io/bbolt"
)
func TestStore_NewStore_And_Close(t *testing.T) {
@@ -416,46 +412,6 @@ func TestStore_ResetMeshIdentity(t *testing.T) {
t.Errorf("expected private key to survive reset, got %v want %v", loadedKey, key)
}
}
-
-func TestStore_IsBanned(t *testing.T) {
- store, err := NewStore(t.TempDir())
- if err != nil {
- t.Fatalf("Failed to create store: %v", err)
- }
- defer func() {
- if err := store.Close(); err != nil {
- t.Errorf("failed to close store: %v", err)
- }
- }()
-
- pID, err := peer.Decode("12D3KooWBysiyDVxxj7Lq8KvhFnZVhqKdZHtwRaJu7hvGwSZFMNg")
- if err != nil {
- t.Fatalf("Failed to decode peer ID: %v", err)
- }
-
- // Verify not banned initially
- if store.IsBanned(pID) {
- t.Errorf("Expected peer %s to not be banned", pID)
- }
-
- // Manually add peer to banned bucket to test IsBanned
- err = store.db.Update(func(tx *bbolt.Tx) error {
- b := tx.Bucket([]byte("banned_peers"))
- if b == nil {
- return fmt.Errorf("banned_peers bucket not found")
- }
- return b.Put([]byte(pID.String()), []byte("banned"))
- })
- if err != nil {
- t.Fatalf("Failed to manually ban peer: %v", err)
- }
-
- // Verify banned now
- if !store.IsBanned(pID) {
- t.Errorf("Expected peer %s to be banned", pID)
- }
-}
-
func TestGetDefaultDataDir(t *testing.T) {
dir, err := GetDefaultDataDir()
if err != nil {