Skip to content

Agent Mesh

api,controlplane: publish the ban set in /info

HosniBelfekia5bb58bb6531
diff --git a/api/sam.pb.go b/api/sam.pb.go
index 4c1280ef..65101f11 100644
--- a/api/sam.pb.go
+++ b/api/sam.pb.go
@@ -1127,8 +1127,15 @@ type ControlPlaneInfoResponse struct {
ClientId string `protobuf:"bytes,2,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"`
Audience string `protobuf:"bytes,3,opt,name=audience,proto3" json:"audience,omitempty"`
RouterAddresses []string `protobuf:"bytes,4,rep,name=router_addresses,json=routerAddresses,proto3" json:"router_addresses,omitempty"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
+ // The complete set of currently banned peer IDs. Consumers reconcile their
+ // local blocklist against this list rather than merging into it, so that a
+ // peer the control plane no longer bans is unbanned everywhere without
+ // needing an event of its own. MeshEvent_BANNED stays the fast path for
+ // sub-second eviction; this is how a node that restarted or was offline
+ // when the event was published catches up.
+ BannedPeerIds []string `protobuf:"bytes,5,rep,name=banned_peer_ids,json=bannedPeerIds,proto3" json:"banned_peer_ids,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
}
func (x *ControlPlaneInfoResponse) Reset() {
@@ -1189,6 +1196,13 @@ func (x *ControlPlaneInfoResponse) GetRouterAddresses() []string {
return nil
}
+func (x *ControlPlaneInfoResponse) GetBannedPeerIds() []string {
+ if x != nil {
+ return x.BannedPeerIds
+ }
+ return nil
+}
+
type RouterLeaseRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
PeerId string `protobuf:"bytes,1,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"`
@@ -2977,13 +2991,14 @@ const file_api_sam_proto_rawDesc = "" +
"\ttimestamp\x18\b \x01(\x03R\ttimestamp\x1a9\n" +
"\vLabelsEntry\x12\x10\n" +
"\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" +
- "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\x9f\x01\n" +
+ "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\xc7\x01\n" +
"\x18ControlPlaneInfoResponse\x12\x1f\n" +
"\voidc_issuer\x18\x01 \x01(\tR\n" +
"oidcIssuer\x12\x1b\n" +
"\tclient_id\x18\x02 \x01(\tR\bclientId\x12\x1a\n" +
"\baudience\x18\x03 \x01(\tR\baudience\x12)\n" +
- "\x10router_addresses\x18\x04 \x03(\tR\x0frouterAddresses\"\xa9\x01\n" +
+ "\x10router_addresses\x18\x04 \x03(\tR\x0frouterAddresses\x12&\n" +
+ "\x0fbanned_peer_ids\x18\x05 \x03(\tR\rbannedPeerIds\"\xa9\x01\n" +
"\x12RouterLeaseRequest\x12\x17\n" +
"\apeer_id\x18\x01 \x01(\tR\x06peerId\x12\x1c\n" +
"\taddresses\x18\x02 \x03(\tR\taddresses\x12\x18\n" +
diff --git a/api/sam.proto b/api/sam.proto
index 9ae07c1f..b56826ff 100644
--- a/api/sam.proto
+++ b/api/sam.proto
@@ -171,6 +171,13 @@ message ControlPlaneInfoResponse {
string client_id = 2;
string audience = 3;
repeated string router_addresses = 4;
+ // The complete set of currently banned peer IDs. Consumers reconcile their
+ // local blocklist against this list rather than merging into it, so that a
+ // peer the control plane no longer bans is unbanned everywhere without
+ // needing an event of its own. MeshEvent_BANNED stays the fast path for
+ // sub-second eviction; this is how a node that restarted or was offline
+ // when the event was published catches up.
+ repeated string banned_peer_ids = 5;
}
message RouterLeaseRequest {
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index 8a155cc9..5f398ba9 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -408,11 +408,25 @@ func (s *Server) HandleInfo(w http.ResponseWriter, r *http.Request) {
routerAddrs = append(routerAddrs, r.Addresses...)
}
+ // The ban set is published here, rather than only as a MeshEvent, because
+ // the event is broadcast once and gossip has no replay: a node or router
+ // that restarted or was offline at the time would otherwise never learn
+ // the ban. A failure to read it must not be served as an empty list --
+ // that would read as "nothing is banned" and unban everyone -- so it is
+ // treated like any other failure to build this response.
+ bannedPeerIDs, err := s.store.ListBannedPeerIDs(r.Context())
+ if err != nil {
+ logger.Errorf("Failed to retrieve banned peers: %v", err)
+ http.Error(w, "Internal server error", http.StatusInternalServerError)
+ return
+ }
+
resp := &api.ControlPlaneInfoResponse{
OidcIssuer: issuer,
ClientId: clientID,
Audience: aud,
RouterAddresses: routerAddrs, // Reused this field for back-compatibility with bootstrap routers list
+ BannedPeerIds: bannedPeerIDs,
}
respData, err := proto.Marshal(resp)
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index 48d58a15..a58fdd63 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -2611,3 +2611,66 @@ func TestAdminBootstrapTokensList(t *testing.T) {
t.Errorf("unauthenticated GET status = %s, want 401", resp.Status)
}
}
+
+// /info carries the whole ban set so a node or router that restarted, or was
+// offline when MeshEvent_BANNED was published, can reconcile against it. A
+// peer that is not banned must not appear, or consumers would blocklist it.
+func TestHandleInfoPublishesBanSet(t *testing.T) {
+ issuer, _ := startCustomMockOIDC(t)
+ srv, st, _ := setupTestServer(t, issuer)
+ defer func() {
+ _ = srv.Close()
+ _ = st.Close()
+ }()
+ ctx := context.Background()
+
+ const (
+ bannedPeer = "12D3KooWBanned000000000000000000000000000000000001"
+ allowedPeer = "12D3KooWAllowed00000000000000000000000000000000002"
+ )
+ for _, peerID := range []string{bannedPeer, allowedPeer} {
+ if err := st.EnrollNode(ctx, &storage.EnrolledNode{
+ PeerID: peerID,
+ PublicKey: []byte("test-public-key"),
+ Biscuit: []byte("test-biscuit"),
+ Role: "agent",
+ ExpiresAt: time.Now().Add(time.Hour),
+ }); err != nil {
+ t.Fatalf("EnrollNode(%s): %v", peerID, err)
+ }
+ }
+ // The path /admin/revoke takes: enrollment always starts unbanned.
+ if err := st.SetNodeBanned(ctx, bannedPeer, true); err != nil {
+ t.Fatalf("SetNodeBanned: %v", err)
+ }
+
+ rec := httptest.NewRecorder()
+ srv.HandleInfo(rec, httptest.NewRequest(http.MethodGet, "/info", nil))
+ if rec.Code != http.StatusOK {
+ t.Fatalf("unexpected /info status: %d", rec.Code)
+ }
+ var info api.ControlPlaneInfoResponse
+ if err := proto.Unmarshal(rec.Body.Bytes(), &info); err != nil {
+ t.Fatalf("unmarshal ControlPlaneInfoResponse: %v", err)
+ }
+
+ got := info.GetBannedPeerIds()
+ if len(got) != 1 || got[0] != bannedPeer {
+ t.Errorf("banned_peer_ids = %v, want exactly [%s]", got, bannedPeer)
+ }
+
+ // An unban must show up as absence, since that is the only signal
+ // consumers get: there is no unban event.
+ if err := st.SetNodeBanned(ctx, bannedPeer, false); err != nil {
+ t.Fatalf("SetNodeBanned(false): %v", err)
+ }
+ rec = httptest.NewRecorder()
+ srv.HandleInfo(rec, httptest.NewRequest(http.MethodGet, "/info", nil))
+ var after api.ControlPlaneInfoResponse
+ if err := proto.Unmarshal(rec.Body.Bytes(), &after); err != nil {
+ t.Fatalf("unmarshal ControlPlaneInfoResponse: %v", err)
+ }
+ if len(after.GetBannedPeerIds()) != 0 {
+ t.Errorf("after unban banned_peer_ids = %v, want empty", after.GetBannedPeerIds())
+ }
+}
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 86abd092..79291766 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -740,6 +740,26 @@ func (s *SQLStore) IsIdentityBanned(ctx context.Context, identity string) (bool,
return true, nil
}
+// ListBannedPeerIDs implements Store.
+func (s *SQLStore) ListBannedPeerIDs(ctx context.Context) ([]string, error) {
+ query := s.rebind(`SELECT peer_id FROM nodes WHERE banned = ?`)
+ rows, err := s.db.QueryContext(ctx, query, true)
+ if err != nil {
+ return nil, err
+ }
+ defer func() { _ = rows.Close() }()
+
+ var peerIDs []string
+ for rows.Next() {
+ var peerID string
+ if err := rows.Scan(&peerID); err != nil {
+ return nil, err
+ }
+ peerIDs = append(peerIDs, peerID)
+ }
+ return peerIDs, rows.Err()
+}
+
// UpsertRouterLease implements Store.
func (s *SQLStore) UpsertRouterLease(ctx context.Context, lease *RouterLease) error {
addrsBytes, err := json.Marshal(lease.Addresses)
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index bae522db..3158f4f6 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -177,6 +177,12 @@ type Store interface {
// IsIdentityBanned checks if an identity is currently banned.
IsIdentityBanned(ctx context.Context, identity string) (bool, error)
+ // ListBannedPeerIDs returns the peer IDs of every currently banned node.
+ // Callers publish this as the complete ban set (see
+ // ControlPlaneInfoResponse.banned_peer_ids), so it must be the whole list
+ // rather than a page of it.
+ ListBannedPeerIDs(ctx context.Context) ([]string, error)
+
// UpsertRouterLease updates or creates a lease for a sam-router.
UpsertRouterLease(ctx context.Context, lease *RouterLease) error