Skip to content

Agent Mesh

decentralize hub

Antonio Ojeac6269de66573
diff --git a/api/sam.pb.go b/api/sam.pb.go
index 319ac929..c13076f4 100644
--- a/api/sam.pb.go
+++ b/api/sam.pb.go
@@ -90,25 +90,19 @@ func (ServiceType) EnumDescriptor() ([]byte, []int) {
type MeshEvent_Type int32
const (
- MeshEvent_JOIN MeshEvent_Type = 0
- MeshEvent_EXIT MeshEvent_Type = 1
- MeshEvent_BANNED MeshEvent_Type = 2
- MeshEvent_KEY_ROTATION MeshEvent_Type = 3
+ MeshEvent_BANNED MeshEvent_Type = 0
+ MeshEvent_KEY_ROTATION MeshEvent_Type = 1
)
// Enum value maps for MeshEvent_Type.
var (
MeshEvent_Type_name = map[int32]string{
- 0: "JOIN",
- 1: "EXIT",
- 2: "BANNED",
- 3: "KEY_ROTATION",
+ 0: "BANNED",
+ 1: "KEY_ROTATION",
}
MeshEvent_Type_value = map[string]int32{
- "JOIN": 0,
- "EXIT": 1,
- "BANNED": 2,
- "KEY_ROTATION": 3,
+ "BANNED": 0,
+ "KEY_ROTATION": 1,
}
)
@@ -139,58 +133,6 @@ func (MeshEvent_Type) EnumDescriptor() ([]byte, []int) {
return file_api_sam_proto_rawDescGZIP(), []int{2, 0}
}
-type HubSyncMessage_Action int32
-
-const (
- HubSyncMessage_ACTION_UNSPECIFIED HubSyncMessage_Action = 0
- HubSyncMessage_ADD HubSyncMessage_Action = 1
- HubSyncMessage_REMOVE HubSyncMessage_Action = 2
- HubSyncMessage_FULL_SYNC HubSyncMessage_Action = 3
-)
-
-// Enum value maps for HubSyncMessage_Action.
-var (
- HubSyncMessage_Action_name = map[int32]string{
- 0: "ACTION_UNSPECIFIED",
- 1: "ADD",
- 2: "REMOVE",
- 3: "FULL_SYNC",
- }
- HubSyncMessage_Action_value = map[string]int32{
- "ACTION_UNSPECIFIED": 0,
- "ADD": 1,
- "REMOVE": 2,
- "FULL_SYNC": 3,
- }
-)
-
-func (x HubSyncMessage_Action) Enum() *HubSyncMessage_Action {
- p := new(HubSyncMessage_Action)
- *p = x
- return p
-}
-
-func (x HubSyncMessage_Action) String() string {
- return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
-}
-
-func (HubSyncMessage_Action) Descriptor() protoreflect.EnumDescriptor {
- return file_api_sam_proto_enumTypes[2].Descriptor()
-}
-
-func (HubSyncMessage_Action) Type() protoreflect.EnumType {
- return &file_api_sam_proto_enumTypes[2]
-}
-
-func (x HubSyncMessage_Action) Number() protoreflect.EnumNumber {
- return protoreflect.EnumNumber(x)
-}
-
-// Deprecated: Use HubSyncMessage_Action.Descriptor instead.
-func (HubSyncMessage_Action) EnumDescriptor() ([]byte, []int) {
- return file_api_sam_proto_rawDescGZIP(), []int{10, 0}
-}
-
type AuthFrame struct {
state protoimpl.MessageState `protogen:"open.v1"`
Biscuit []byte `protobuf:"bytes,1,opt,name=biscuit,proto3" json:"biscuit,omitempty"`
@@ -332,7 +274,7 @@ func (x *MeshEvent) GetType() MeshEvent_Type {
if x != nil {
return x.Type
}
- return MeshEvent_JOIN
+ return MeshEvent_BANNED
}
func (x *MeshEvent) GetPeerId() string {
@@ -422,7 +364,6 @@ type EnrollResponse struct {
HubPublicKey []byte `protobuf:"bytes,3,opt,name=hub_public_key,json=hubPublicKey,proto3" json:"hub_public_key,omitempty"`
HubAddresses []string `protobuf:"bytes,4,rep,name=hub_addresses,json=hubAddresses,proto3" json:"hub_addresses,omitempty"`
Expiration int64 `protobuf:"varint,5,opt,name=expiration,proto3" json:"expiration,omitempty"`
- KnownPeers []string `protobuf:"bytes,6,rep,name=known_peers,json=knownPeers,proto3" json:"known_peers,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -492,13 +433,6 @@ func (x *EnrollResponse) GetExpiration() int64 {
return 0
}
-func (x *EnrollResponse) GetKnownPeers() []string {
- if x != nil {
- return x.KnownPeers
- }
- return nil
-}
-
type ServiceInfo struct {
state protoimpl.MessageState `protogen:"open.v1"`
Type ServiceType `protobuf:"varint,1,opt,name=type,proto3,enum=sam.v1.ServiceType" json:"type,omitempty"`
@@ -829,90 +763,6 @@ func (x *HubInfoResponse) GetAudience() string {
return ""
}
-type HubSyncMessage struct {
- state protoimpl.MessageState `protogen:"open.v1"`
- Action HubSyncMessage_Action `protobuf:"varint,1,opt,name=action,proto3,enum=sam.v1.HubSyncMessage_Action" json:"action,omitempty"`
- PeerId string `protobuf:"bytes,2,opt,name=peer_id,json=peerId,proto3" json:"peer_id,omitempty"`
- Peers []string `protobuf:"bytes,3,rep,name=peers,proto3" json:"peers,omitempty"`
- HubAddrs []string `protobuf:"bytes,4,rep,name=hub_addrs,json=hubAddrs,proto3" json:"hub_addrs,omitempty"`
- Timestamp int64 `protobuf:"varint,5,opt,name=timestamp,proto3" json:"timestamp,omitempty"`
- PeerTimestamps map[string]int64 `protobuf:"bytes,6,rep,name=peer_timestamps,json=peerTimestamps,proto3" json:"peer_timestamps,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
- unknownFields protoimpl.UnknownFields
- sizeCache protoimpl.SizeCache
-}
-
-func (x *HubSyncMessage) Reset() {
- *x = HubSyncMessage{}
- mi := &file_api_sam_proto_msgTypes[10]
- ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
- ms.StoreMessageInfo(mi)
-}
-
-func (x *HubSyncMessage) String() string {
- return protoimpl.X.MessageStringOf(x)
-}
-
-func (*HubSyncMessage) ProtoMessage() {}
-
-func (x *HubSyncMessage) ProtoReflect() protoreflect.Message {
- mi := &file_api_sam_proto_msgTypes[10]
- if x != nil {
- ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
- if ms.LoadMessageInfo() == nil {
- ms.StoreMessageInfo(mi)
- }
- return ms
- }
- return mi.MessageOf(x)
-}
-
-// Deprecated: Use HubSyncMessage.ProtoReflect.Descriptor instead.
-func (*HubSyncMessage) Descriptor() ([]byte, []int) {
- return file_api_sam_proto_rawDescGZIP(), []int{10}
-}
-
-func (x *HubSyncMessage) GetAction() HubSyncMessage_Action {
- if x != nil {
- return x.Action
- }
- return HubSyncMessage_ACTION_UNSPECIFIED
-}
-
-func (x *HubSyncMessage) GetPeerId() string {
- if x != nil {
- return x.PeerId
- }
- return ""
-}
-
-func (x *HubSyncMessage) GetPeers() []string {
- if x != nil {
- return x.Peers
- }
- return nil
-}
-
-func (x *HubSyncMessage) GetHubAddrs() []string {
- if x != nil {
- return x.HubAddrs
- }
- return nil
-}
-
-func (x *HubSyncMessage) GetTimestamp() int64 {
- if x != nil {
- return x.Timestamp
- }
- return 0
-}
-
-func (x *HubSyncMessage) GetPeerTimestamps() map[string]int64 {
- if x != nil {
- return x.PeerTimestamps
- }
- return nil
-}
-
var File_api_sam_proto protoreflect.FileDescriptor
const file_api_sam_proto_rawDesc = "" +
@@ -922,22 +772,20 @@ const file_api_sam_proto_rawDesc = "" +
"\abiscuit\x18\x01 \x01(\fR\abiscuit\">\n" +
"\fAuthResponse\x12\x18\n" +
"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x14\n" +
- "\x05error\x18\x02 \x01(\tR\x05error\"\xec\x01\n" +
+ "\x05error\x18\x02 \x01(\tR\x05error\"\xd8\x01\n" +
"\tMeshEvent\x12*\n" +
"\x04type\x18\x01 \x01(\x0e2\x16.sam.v1.MeshEvent.TypeR\x04type\x12\x17\n" +
"\apeer_id\x18\x02 \x01(\tR\x06peerId\x12\x1c\n" +
"\ttimestamp\x18\x03 \x01(\x03R\ttimestamp\x12$\n" +
"\x0enew_public_key\x18\x04 \x01(\fR\fnewPublicKey\x12\x1c\n" +
- "\tsignature\x18\x05 \x01(\fR\tsignature\"8\n" +
- "\x04Type\x12\b\n" +
- "\x04JOIN\x10\x00\x12\b\n" +
- "\x04EXIT\x10\x01\x12\n" +
+ "\tsignature\x18\x05 \x01(\fR\tsignature\"$\n" +
+ "\x04Type\x12\n" +
"\n" +
- "\x06BANNED\x10\x02\x12\x10\n" +
- "\fKEY_ROTATION\x10\x03\":\n" +
+ "\x06BANNED\x10\x00\x12\x10\n" +
+ "\fKEY_ROTATION\x10\x01\":\n" +
"\rEnrollRequest\x12\x10\n" +
"\x03jwt\x18\x01 \x01(\tR\x03jwt\x12\x17\n" +
- "\apeer_id\x18\x02 \x01(\tR\x06peerId\"\xe6\x01\n" +
+ "\apeer_id\x18\x02 \x01(\tR\x06peerId\"\xc5\x01\n" +
"\x0eEnrollResponse\x12#\n" +
"\rbiscuit_token\x18\x01 \x01(\fR\fbiscuitToken\x12#\n" +
"\rerror_message\x18\x02 \x01(\tR\ferrorMessage\x12$\n" +
@@ -945,9 +793,7 @@ const file_api_sam_proto_rawDesc = "" +
"\rhub_addresses\x18\x04 \x03(\tR\fhubAddresses\x12\x1e\n" +
"\n" +
"expiration\x18\x05 \x01(\x03R\n" +
- "expiration\x12\x1f\n" +
- "\vknown_peers\x18\x06 \x03(\tR\n" +
- "knownPeers\"l\n" +
+ "expiration\"l\n" +
"\vServiceInfo\x12'\n" +
"\x04type\x18\x01 \x01(\x0e2\x13.sam.v1.ServiceTypeR\x04type\x12\x12\n" +
"\x04name\x18\x02 \x01(\tR\x04name\x12 \n" +
@@ -973,23 +819,7 @@ const file_api_sam_proto_rawDesc = "" +
"\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\"\x8f\x03\n" +
- "\x0eHubSyncMessage\x125\n" +
- "\x06action\x18\x01 \x01(\x0e2\x1d.sam.v1.HubSyncMessage.ActionR\x06action\x12\x17\n" +
- "\apeer_id\x18\x02 \x01(\tR\x06peerId\x12\x14\n" +
- "\x05peers\x18\x03 \x03(\tR\x05peers\x12\x1b\n" +
- "\thub_addrs\x18\x04 \x03(\tR\bhubAddrs\x12\x1c\n" +
- "\ttimestamp\x18\x05 \x01(\x03R\ttimestamp\x12S\n" +
- "\x0fpeer_timestamps\x18\x06 \x03(\v2*.sam.v1.HubSyncMessage.PeerTimestampsEntryR\x0epeerTimestamps\x1aA\n" +
- "\x13PeerTimestampsEntry\x12\x10\n" +
- "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" +
- "\x05value\x18\x02 \x01(\x03R\x05value:\x028\x01\"D\n" +
- "\x06Action\x12\x16\n" +
- "\x12ACTION_UNSPECIFIED\x10\x00\x12\a\n" +
- "\x03ADD\x10\x01\x12\n" +
- "\n" +
- "\x06REMOVE\x10\x02\x12\r\n" +
- "\tFULL_SYNC\x10\x03*s\n" +
+ "\baudience\x18\x03 \x01(\tR\baudience*s\n" +
"\vServiceType\x12\x1c\n" +
"\x18SERVICE_TYPE_UNSPECIFIED\x10\x00\x12\x14\n" +
"\x10SERVICE_TYPE_MCP\x10\x01\x12\x1a\n" +
@@ -1008,39 +838,34 @@ func file_api_sam_proto_rawDescGZIP() []byte {
return file_api_sam_proto_rawDescData
}
-var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
-var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 13)
+var file_api_sam_proto_enumTypes = make([]protoimpl.EnumInfo, 2)
+var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 11)
var file_api_sam_proto_goTypes = []any{
(ServiceType)(0), // 0: sam.v1.ServiceType
(MeshEvent_Type)(0), // 1: sam.v1.MeshEvent.Type
- (HubSyncMessage_Action)(0), // 2: sam.v1.HubSyncMessage.Action
- (*AuthFrame)(nil), // 3: sam.v1.AuthFrame
- (*AuthResponse)(nil), // 4: sam.v1.AuthResponse
- (*MeshEvent)(nil), // 5: sam.v1.MeshEvent
- (*EnrollRequest)(nil), // 6: sam.v1.EnrollRequest
- (*EnrollResponse)(nil), // 7: sam.v1.EnrollResponse
- (*ServiceInfo)(nil), // 8: sam.v1.ServiceInfo
- (*CommandBackend)(nil), // 9: sam.v1.CommandBackend
- (*RegisterServiceRequest)(nil), // 10: sam.v1.RegisterServiceRequest
- (*DiscoveredProvider)(nil), // 11: sam.v1.DiscoveredProvider
- (*HubInfoResponse)(nil), // 12: sam.v1.HubInfoResponse
- (*HubSyncMessage)(nil), // 13: sam.v1.HubSyncMessage
- nil, // 14: sam.v1.CommandBackend.EnvEntry
- nil, // 15: sam.v1.HubSyncMessage.PeerTimestampsEntry
+ (*AuthFrame)(nil), // 2: sam.v1.AuthFrame
+ (*AuthResponse)(nil), // 3: sam.v1.AuthResponse
+ (*MeshEvent)(nil), // 4: sam.v1.MeshEvent
+ (*EnrollRequest)(nil), // 5: sam.v1.EnrollRequest
+ (*EnrollResponse)(nil), // 6: sam.v1.EnrollResponse
+ (*ServiceInfo)(nil), // 7: sam.v1.ServiceInfo
+ (*CommandBackend)(nil), // 8: sam.v1.CommandBackend
+ (*RegisterServiceRequest)(nil), // 9: sam.v1.RegisterServiceRequest
+ (*DiscoveredProvider)(nil), // 10: sam.v1.DiscoveredProvider
+ (*HubInfoResponse)(nil), // 11: sam.v1.HubInfoResponse
+ nil, // 12: sam.v1.CommandBackend.EnvEntry
}
var file_api_sam_proto_depIdxs = []int32{
1, // 0: sam.v1.MeshEvent.type:type_name -> sam.v1.MeshEvent.Type
0, // 1: sam.v1.ServiceInfo.type:type_name -> sam.v1.ServiceType
- 14, // 2: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry
- 8, // 3: sam.v1.RegisterServiceRequest.service:type_name -> sam.v1.ServiceInfo
- 9, // 4: sam.v1.RegisterServiceRequest.command:type_name -> sam.v1.CommandBackend
- 2, // 5: sam.v1.HubSyncMessage.action:type_name -> sam.v1.HubSyncMessage.Action
- 15, // 6: sam.v1.HubSyncMessage.peer_timestamps:type_name -> sam.v1.HubSyncMessage.PeerTimestampsEntry
- 7, // [7:7] is the sub-list for method output_type
- 7, // [7:7] is the sub-list for method input_type
- 7, // [7:7] is the sub-list for extension type_name
- 7, // [7:7] is the sub-list for extension extendee
- 0, // [0:7] is the sub-list for field type_name
+ 12, // 2: sam.v1.CommandBackend.env:type_name -> sam.v1.CommandBackend.EnvEntry
+ 7, // 3: sam.v1.RegisterServiceRequest.service:type_name -> sam.v1.ServiceInfo
+ 8, // 4: sam.v1.RegisterServiceRequest.command:type_name -> sam.v1.CommandBackend
+ 5, // [5:5] is the sub-list for method output_type
+ 5, // [5:5] is the sub-list for method input_type
+ 5, // [5:5] is the sub-list for extension type_name
+ 5, // [5:5] is the sub-list for extension extendee
+ 0, // [0:5] is the sub-list for field type_name
}
func init() { file_api_sam_proto_init() }
@@ -1057,8 +882,8 @@ func file_api_sam_proto_init() {
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_api_sam_proto_rawDesc), len(file_api_sam_proto_rawDesc)),
- NumEnums: 3,
- NumMessages: 13,
+ NumEnums: 2,
+ NumMessages: 11,
NumExtensions: 0,
NumServices: 0,
},
diff --git a/api/sam.proto b/api/sam.proto
index 9c206f81..ad077a6a 100644
--- a/api/sam.proto
+++ b/api/sam.proto
@@ -31,10 +31,8 @@ message AuthResponse {
message MeshEvent {
enum Type {
- JOIN = 0;
- EXIT = 1;
- BANNED = 2;
- KEY_ROTATION = 3;
+ BANNED = 0;
+ KEY_ROTATION = 1;
}
Type type = 1;
string peer_id = 2;
@@ -54,7 +52,6 @@ message EnrollResponse {
bytes hub_public_key = 3;
repeated string hub_addresses = 4;
int64 expiration = 5;
- repeated string known_peers = 6;
}
enum ServiceType {
@@ -96,17 +93,3 @@ message HubInfoResponse {
string audience = 3;
}
-message HubSyncMessage {
- enum Action {
- ACTION_UNSPECIFIED = 0;
- ADD = 1;
- REMOVE = 2;
- FULL_SYNC = 3;
- }
- Action action = 1;
- string peer_id = 2;
- repeated string peers = 3;
- repeated string hub_addrs = 4;
- int64 timestamp = 5;
- map<string, int64> peer_timestamps = 6;
-}
diff --git a/cmd/sam-hub/hooks.go b/cmd/sam-hub/hooks.go
deleted file mode 100644
index a95163d7..00000000
--- a/cmd/sam-hub/hooks.go
+++ /dev/null
@@ -1,125 +0,0 @@
-// Copyright 2026 Google LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package main
-
-import (
- "context"
- "sync"
- "time"
-
- "github.com/google/sam/api"
- "github.com/libp2p/go-libp2p/core/connmgr"
- "github.com/libp2p/go-libp2p/core/control"
- "github.com/libp2p/go-libp2p/core/network"
- "github.com/libp2p/go-libp2p/core/peer"
- "github.com/multiformats/go-multiaddr"
- "google.golang.org/protobuf/proto"
-)
-
-var _ connmgr.ConnectionGater = (*hubConnGate)(nil)
-
-// hubConnGate implements libp2p ConnectionGater to enforce "Auth-or-Drop"
-type hubConnGate struct {
- mu sync.RWMutex
- authenticated map[peer.ID]bool
- lastUpdated map[peer.ID]int64
-}
-
-func newHubConnGate() *hubConnGate {
- return &hubConnGate{
- authenticated: make(map[peer.ID]bool),
- lastUpdated: make(map[peer.ID]int64),
- }
-}
-
-// We allow all physical connections initially but track them for "Grace Period"
-func (g *hubConnGate) InterceptPeerDial(p peer.ID) bool { return true }
-func (g *hubConnGate) InterceptAddrDial(p peer.ID, m multiaddr.Multiaddr) bool { return true }
-func (g *hubConnGate) InterceptAccept(c network.ConnMultiaddrs) bool { return true }
-
-func (g *hubConnGate) InterceptSecured(dir network.Direction, p peer.ID, mas network.ConnMultiaddrs) bool {
- return true
-}
-
-func (g *hubConnGate) InterceptUpgraded(c network.Conn) (bool, control.DisconnectReason) {
- // We allow all connections to be upgraded. Protocol-level authorization
- // is handled at the stream level or by the watchdog.
- return true, 0
-}
-
-var _ network.Notifiee = (*notifier)(nil)
-
-type notifier struct {
- hub *Hub
-}
-
-func (n *notifier) Listen(network.Network, multiaddr.Multiaddr) {}
-func (n *notifier) ListenClose(network.Network, multiaddr.Multiaddr) {}
-func (n *notifier) Connected(network.Network, network.Conn) {}
-func (n *notifier) Disconnected(_ network.Network, c network.Conn) {
- p := c.RemotePeer()
-
- // If there are still active connections to this peer, don't remove them yet
- if len(n.hub.Host.Network().ConnsToPeer(p)) > 0 {
- return
- }
-
- n.hub.gater.mu.Lock()
- wasAuth := n.hub.gater.authenticated[p]
- now := time.Now().UnixMilli()
- n.hub.gater.lastUpdated[p] = now
- delete(n.hub.gater.authenticated, p)
- n.hub.gater.mu.Unlock()
-
- logger.Infow("Peer disconnected", "peer_id", p, "was_authenticated", wasAuth)
-
- if wasAuth {
- samHubActiveNodes.Dec() // Decrement active nodes
-
- // Notify other hubs to clean up this peer
- n.hub.publishSyncMessage(context.Background(), &api.HubSyncMessage{
- Action: api.HubSyncMessage_REMOVE,
- PeerId: p.String(),
- Timestamp: now,
- })
-
- event := &api.MeshEvent{
- Type: api.MeshEvent_EXIT,
- PeerId: p.String(),
- Timestamp: time.Now().UnixMilli(),
- }
- // Sign event
- if err := n.hub.signEvent(event); err != nil {
- logger.Errorw("Failed to sign mesh event", "peer_id", p, "error", err)
- } else {
- data, err := proto.Marshal(event)
- if err != nil {
- logger.Errorw("Failed to marshal mesh event", "peer_id", p, "error", err)
- return
- }
- // Notify the mesh about the peer disconnection
- if n.hub.EventTopic != nil {
- if err := n.hub.EventTopic.Publish(context.Background(), data); err != nil {
- logger.Errorw("Failed to publish mesh event", "peer_id", p, "error", err)
- return
- }
- logger.Infow("Published EXIT event", "peer_id", p)
- samHubMeshEventsTotal.WithLabelValues("EXIT").Inc()
- } else {
- logger.Warnw("EventTopic is nil, could not publish EXIT event", "peer_id", p)
- }
- }
- }
-}
diff --git a/cmd/sam-hub/hooks_test.go b/cmd/sam-hub/hooks_test.go
deleted file mode 100644
index 2cc2bd10..00000000
--- a/cmd/sam-hub/hooks_test.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package main
-
-import (
- "testing"
-
- "time"
- "path/filepath"
-
- "github.com/libp2p/go-libp2p/core/network"
- "github.com/libp2p/go-libp2p/core/peer"
- "github.com/libp2p/go-libp2p/core/host"
-)
-
-type mockConn struct {
- network.Conn
- peer peer.ID
-}
-
-func (m mockConn) RemotePeer() peer.ID { return m.peer }
-
-type mockNetwork struct {
- network.Network
- conns int
-}
-
-func (m *mockNetwork) ConnsToPeer(p peer.ID) []network.Conn {
- conns := make([]network.Conn, m.conns)
- return conns
-}
-
-type mockHost struct {
- host.Host
- net *mockNetwork
-}
-
-func (m *mockHost) Network() network.Network { return m.net }
-
-func TestNotifier_Disconnected(t *testing.T) {
- dir := t.TempDir()
- dbPath := filepath.Join(dir, "test.db")
- kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)
- if err != nil {
- t.Fatal(err)
- }
- defer func() { _ = kr.Close() }()
-
- remotePeer := peer.ID("peer1")
-
- mockNet := &mockNetwork{conns: 1} // Initial state: 1 connection remains after one drops (so 2 total originally)
- mockH := &mockHost{net: mockNet}
-
- hub := &Hub{
- Host: mockH,
- KeyRing: kr,
- gater: newHubConnGate(),
- }
-
- notifier := &notifier{
- hub: hub,
- }
-
- // Simulate authentication
- hub.gater.mu.Lock()
- hub.gater.authenticated[remotePeer] = true
- hub.gater.mu.Unlock()
-
- conn := mockConn{peer: remotePeer}
-
- // 1. One connection drops, but another remains active (mockNet.conns = 1)
- notifier.Disconnected(mockNet, conn)
-
- hub.gater.mu.RLock()
- authed := hub.gater.authenticated[remotePeer]
- hub.gater.mu.RUnlock()
-
- if !authed {
- t.Fatal("Expected peer to remain authenticated because another connection is active")
- }
-
- // 2. The last connection drops (mockNet.conns = 0)
- mockNet.conns = 0
- notifier.Disconnected(mockNet, conn)
-
- hub.gater.mu.RLock()
- authed = hub.gater.authenticated[remotePeer]
- hub.gater.mu.RUnlock()
-
- if authed {
- t.Fatal("Expected peer to be removed from authenticated map after last connection dropped")
- }
-}
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index d41b48ba..48ca3b03 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -38,13 +38,11 @@ import (
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/host"
- "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
libp2ptls "github.com/libp2p/go-libp2p/p2p/security/tls"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
- "github.com/libp2p/go-msgio"
"github.com/multiformats/go-multiaddr"
"golang.org/x/time/rate"
@@ -112,21 +110,15 @@ type Hub struct {
MeshID string
PubSub *pubsub.PubSub
EventTopic *pubsub.Topic
- SyncTopic *pubsub.Topic
- otherHubAddrs map[peer.ID][]string
- otherHubLastSeen map[peer.ID]time.Time
- gater *hubConnGate
Policy *api.PolicyConfig
limiter *rate.Limiter
ExternalAddrs []string
AllowedAudiences []string
AllowLoopback bool
- SyncKey []byte
}
// NewHub starts a host supporting both QUIC and TCP (with TLS 1.3)
func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (*Hub, error) {
- gater := newHubConnGate()
// Connection Manager for DoS protection
cm, err := connmgr.NewConnManager(LowWaterMark, HighWaterMark, connmgr.WithGracePeriod(ConnGracePeriod))
@@ -141,7 +133,6 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
// FIPS compliant Security
libp2p.Security(libp2ptls.ID, libp2ptls.New),
libp2p.EnableRelayService(),
- libp2p.ConnectionGater(gater),
libp2p.ConnectionManager(cm),
libp2p.EnableAutoNATv2(),
libp2p.EnableNATService(),
@@ -228,7 +219,6 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
hub := &Hub{
Host: h,
DHT: kadDHT,
- gater: gater,
Providers: providers,
KeyRing: kr,
MeshID: meshName,
@@ -237,20 +227,7 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
ExternalAddrs: externalMultiaddrs,
AllowedAudiences: auds,
AllowLoopback: allowLoopback,
- otherHubAddrs: make(map[peer.ID][]string),
- otherHubLastSeen: make(map[peer.ID]time.Time),
- SyncKey: func() []byte {
- if len(initialSeed) > 0 {
- return initialSeed
- }
- if kr != nil {
- return kr.GetCurrentKey()
- }
- return nil
- }(),
}
-
- h.Network().Notify(&notifier{hub: hub})
ps, err := pubsub.NewGossipSub(ctx, h)
if err != nil {
return nil, err
@@ -260,107 +237,12 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
return nil, err
}
- syncTopic, err := ps.Join(api.GossipHubSync)
- if err != nil {
- return nil, err
- }
-
hub.PubSub = ps
hub.EventTopic = topic
- hub.SyncTopic = syncTopic
- hub.startSyncListener(ctx)
return hub, nil
}
-func (h *Hub) handleAuthHandshake(s network.Stream) {
- defer func() {
- if err := s.Close(); err != nil {
- logger.Errorf("[AuthN] Failed to close auth stream: %v", err)
- }
- }()
- remotePeer := s.Conn().RemotePeer()
-
- reader := msgio.NewVarintReaderSize(s, 1024*64)
- msg, err := reader.ReadMsg()
- if err != nil {
- logger.Errorf("[AuthN] Failed to read handshake from %s: %v", remotePeer, err)
- return
- }
- defer reader.ReleaseMsg(msg)
-
- var exchange api.AuthFrame
- if err := proto.Unmarshal(msg, &exchange); err != nil {
- logger.Warnf("[AuthN] Invalid protobuf from %s", remotePeer)
- return
- }
-
- b, err := h.verifyBiscuit(exchange.Biscuit, remotePeer)
- if err != nil {
- logger.Warnf("[AuthN] Authorization failed for %s: %v", remotePeer, err)
- return
- }
-
- // Enforce hardware binding: token must include node(<remotePeerID>)
- boundFact := biscuit.Fact{Predicate: biscuit.Predicate{
- Name: "node",
- IDs: []biscuit.Term{biscuit.String(remotePeer.String())},
- }}
- if _, err := b.GetBlockID(boundFact); err != nil {
- logger.Warnf("[AuthN] Token is not bound to peer %s", remotePeer)
- return
- }
-
- logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)
-
- // Update active nodes gauge and gater state
- h.gater.mu.Lock()
- now := time.Now().UnixMilli()
- h.gater.lastUpdated[remotePeer] = now
- if !h.gater.authenticated[remotePeer] {
- samHubActiveNodes.Inc()
- }
- h.gater.authenticated[remotePeer] = true
- h.gater.mu.Unlock()
-
- // Notify other hubs of this peer
- h.publishSyncMessage(context.Background(), &api.HubSyncMessage{
- Action: api.HubSyncMessage_ADD,
- PeerId: remotePeer.String(),
- Timestamp: now,
- })
-
- // Publish JOIN event
- event := &api.MeshEvent{
- Type: api.MeshEvent_JOIN,
- PeerId: remotePeer.String(),
- Timestamp: time.Now().UnixMilli(),
- }
- if err := h.signEvent(event); err != nil {
- logger.Errorw("Failed to sign mesh event", "peer_id", remotePeer, "error", err)
- } else {
- eventData, err := proto.Marshal(event)
- if err != nil {
- logger.Errorw("Failed to marshal mesh event", "peer_id", remotePeer, "error", err)
- } else {
- if err := h.EventTopic.Publish(context.Background(), eventData); err != nil {
- logger.Errorw("Failed to publish mesh event", "peer_id", remotePeer, "error", err)
- } else {
- logger.Infow("Published JOIN event", "peer_id", remotePeer)
- samHubMeshEventsTotal.WithLabelValues("JOIN").Inc()
- }
- }
- }
-
- // Send ACK back to client
- writer := msgio.NewVarintWriter(s)
- resp := &api.AuthResponse{Success: true}
- respBytes, _ := proto.Marshal(resp)
- if err := writer.WriteMsg(respBytes); err != nil {
- logger.Errorf("[AuthN] Failed to write ACK to %s: %v", remotePeer, err)
- }
-}
-
func (h *Hub) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuit.Biscuit, error) {
b, err := biscuit.Unmarshal(biscuitData)
if err != nil {
@@ -729,7 +611,6 @@ func main() {
if err != nil {
logger.Fatal(err)
}
- h.Host.SetStreamHandler(api.AuthProtocolID, h.handleAuthHandshake)
// Start key rotation if enabled
h.startRotation(ctx)
diff --git a/cmd/sam-hub/main_test.go b/cmd/sam-hub/main_test.go
index 0c2d67b2..3d0daa3a 100644
--- a/cmd/sam-hub/main_test.go
+++ b/cmd/sam-hub/main_test.go
@@ -15,8 +15,6 @@
package main
import (
- "bytes"
- "context"
"io"
"net/http"
"net/http/httptest"
@@ -262,180 +260,3 @@ func TestHandleInfoHTTP(t *testing.T) {
t.Errorf("Expected audience 'test-audience-1', got %q", info.Audience)
}
}
-
-func TestHubReplicaSynchronization(t *testing.T) {
- // 1. Save and restore original global variables to avoid side effects
- origOidcIssuer := oidcIssuer
- origListenAddrs := listenAddrs
- origKeysDBPath := keysDBPath
- origBiscuitHex := biscuitHex
- origKeyGracePeriod := keyGracePeriod
- origAllowLoopbackFlag := allowLoopbackFlag
-
- defer func() {
- oidcIssuer = origOidcIssuer
- listenAddrs = origListenAddrs
- keysDBPath = origKeysDBPath
- biscuitHex = origBiscuitHex
- keyGracePeriod = origKeyGracePeriod
- allowLoopbackFlag = origAllowLoopbackFlag
- }()
-
- // Configure parameters for test
- oidcIssuer = "https://accounts.google.com"
- listenAddrs = []string{"/ip4/127.0.0.1/tcp/0"}
- keyGracePeriod = 24 * time.Hour
- allowLoopbackFlag = true
- // Use same hex key so they can decrypt each other's messages!
- biscuitHex = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
-
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
-
- tmpDir := t.TempDir()
-
- // 2. Spin up Hub Replica A
- keysDBPath = filepath.Join(tmpDir, "keysA.db")
- hubA, err := NewHub(ctx, nil, true)
- if err != nil {
- t.Fatalf("failed to create hub A: %v", err)
- }
- defer func() { _ = hubA.Host.Close() }()
-
- // 3. Spin up Hub Replica B
- keysDBPath = filepath.Join(tmpDir, "keysB.db")
- hubB, err := NewHub(ctx, nil, true)
- if err != nil {
- t.Fatalf("failed to create hub B: %v", err)
- }
- defer func() { _ = hubB.Host.Close() }()
-
- // 4. Connect Hub A and Hub B directly
- err = hubA.Host.Connect(ctx, peer.AddrInfo{
- ID: hubB.Host.ID(),
- Addrs: hubB.Host.Addrs(),
- })
- if err != nil {
- t.Fatalf("failed to connect Hub A to Hub B: %v", err)
- }
-
- // Wait a moment for GossipSub sub and mesh connection
- time.Sleep(200 * time.Millisecond)
-
- // 5. Create a dummy peer to authenticate
- priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
- if err != nil {
- t.Fatal(err)
- }
- peerC, err := peer.IDFromPrivateKey(priv)
- if err != nil {
- t.Fatal(err)
- }
-
- // 6. Authenticate peerC on Hub A
- hubA.gater.mu.Lock()
- hubA.gater.authenticated[peerC] = true
- hubA.gater.mu.Unlock()
-
- // 7. Broadcast ADD event from Hub A
- hubA.publishSyncMessage(ctx, &api.HubSyncMessage{
- Action: api.HubSyncMessage_ADD,
- PeerId: peerC.String(),
- Timestamp: time.Now().UnixMilli(),
- })
-
- // 8. Verify Hub B receives the ADD and authenticates peerC
- authedOnB := false
- for i := 0; i < 100; i++ {
- hubB.gater.mu.RLock()
- authedOnB = hubB.gater.authenticated[peerC]
- hubB.gater.mu.RUnlock()
- if authedOnB {
- break
- }
- time.Sleep(50 * time.Millisecond)
- }
- if !authedOnB {
- t.Fatal("Expected Hub B to authenticate peerC via sync ADD event, but it did not")
- }
-
- // 9. Remove peerC from Hub A and broadcast REMOVE
- hubA.gater.mu.Lock()
- delete(hubA.gater.authenticated, peerC)
- hubA.gater.mu.Unlock()
-
- hubA.publishSyncMessage(ctx, &api.HubSyncMessage{
- Action: api.HubSyncMessage_REMOVE,
- PeerId: peerC.String(),
- Timestamp: time.Now().UnixMilli(),
- })
-
- // 10. Verify Hub B receives the REMOVE and removes peerC
- removedOnB := false
- for i := 0; i < 100; i++ {
- hubB.gater.mu.RLock()
- authed := hubB.gater.authenticated[peerC]
- hubB.gater.mu.RUnlock()
- if !authed {
- removedOnB = true
- break
- }
- time.Sleep(50 * time.Millisecond)
- }
- if !removedOnB {
- t.Fatal("Expected Hub B to de-authenticate peerC via sync REMOVE event, but it remained authenticated")
- }
-
- // 11. Trigger a key rotation on Hub A to change its keyring keypair
- newPub, newPriv, _, err := hubA.KeyRing.PrepareRotation()
- if err != nil {
- t.Fatalf("failed to prepare rotation: %v", err)
- }
- if err := hubA.KeyRing.CommitRotation(newPub, newPriv, keyGracePeriod); err != nil {
- t.Fatalf("failed to commit rotation: %v", err)
- }
-
- // Verify that Hub A's current key is indeed rotated and different from Hub B's
- if bytes.Equal(hubA.KeyRing.GetCurrentKey(), hubB.KeyRing.GetCurrentKey()) {
- t.Fatal("Expected Hub A's keyring key to be rotated and different from Hub B's")
- }
-
- // 12. Authenticate another dummy peer (peerD) on Hub A
- privD, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
- if err != nil {
- t.Fatal(err)
- }
- peerD, err := peer.IDFromPrivateKey(privD)
- if err != nil {
- t.Fatal(err)
- }
-
- hubA.gater.mu.Lock()
- hubA.gater.authenticated[peerD] = true
- hubA.gater.mu.Unlock()
-
- // 13. Broadcast ADD event from Hub A (which now has a rotated KeyRing)
- hubA.publishSyncMessage(ctx, &api.HubSyncMessage{
- Action: api.HubSyncMessage_ADD,
- PeerId: peerD.String(),
- Timestamp: time.Now().UnixMilli(),
- })
-
- // 14. Verify Hub B can STILL decrypt and authenticate peerD
- authedOnBAfterRotation := false
- for i := 0; i < 100; i++ {
- hubB.gater.mu.RLock()
- authedOnBAfterRotation = hubB.gater.authenticated[peerD]
- hubB.gater.mu.RUnlock()
- if authedOnBAfterRotation {
- break
- }
- time.Sleep(50 * time.Millisecond)
- }
- if !authedOnBAfterRotation {
- t.Fatal("Expected Hub B to successfully decrypt and authenticate peerD after Hub A's key rotation, but it failed")
- }
-}
-
-
-
diff --git a/cmd/sam-hub/server.go b/cmd/sam-hub/server.go
index 65835452..62cd7037 100644
--- a/cmd/sam-hub/server.go
+++ b/cmd/sam-hub/server.go
@@ -24,7 +24,6 @@ import (
"sort"
"strings"
"sync/atomic"
- "time"
"github.com/google/sam/api"
"github.com/libp2p/go-libp2p/core/peer"
@@ -151,48 +150,13 @@ func handleRegisterHTTP(h *Hub) http.HandlerFunc {
http.Error(w, "Failed to mint biscuit", http.StatusInternalServerError)
return
}
-
- h.gater.mu.Lock()
- now := time.Now().UnixMilli()
- h.gater.lastUpdated[pID] = now
- if !h.gater.authenticated[pID] {
- samHubActiveNodes.Inc()
- }
- h.gater.authenticated[pID] = true
- h.gater.mu.Unlock()
-
- // NEW: Propagate addition to other hubs
- h.publishSyncMessage(context.Background(), &api.HubSyncMessage{
- Action: api.HubSyncMessage_ADD,
- PeerId: pID.String(),
- Timestamp: now,
- })
-
- // NEW: Compile addresses of ALL Hub Replicas + Known Peers
- var authPeers []peer.ID
- h.gater.mu.Lock()
- for p := range h.gater.authenticated {
- authPeers = append(authPeers, p)
- }
-
- var allHubAddrs []string
- allHubAddrs = append(allHubAddrs, h.getMyHubAddrs()...)
- for _, addrs := range h.otherHubAddrs {
- allHubAddrs = append(allHubAddrs, addrs...)
- }
- h.gater.mu.Unlock()
-
- var knownPeers []string
- for _, p := range authPeers {
- knownPeers = append(knownPeers, p.String())
- }
+ allHubAddrs := getMyHubAddrs(h)
resp := &api.EnrollResponse{
BiscuitToken: biscuitData,
HubPublicKey: h.KeyRing.GetCurrentPublicKey(),
HubAddresses: allHubAddrs,
Expiration: token.Expiry.Unix(),
- KnownPeers: knownPeers,
}
respData, err := proto.Marshal(resp)
@@ -263,3 +227,22 @@ func handleInfoHTTP(h *Hub) http.HandlerFunc {
_, _ = w.Write(respData)
}
}
+
+func getMyHubAddrs(h *Hub) []string {
+ var addrs []string
+ if len(h.ExternalAddrs) > 0 {
+ for _, addr := range h.ExternalAddrs {
+ addrs = append(addrs, addr+"/p2p/"+h.Host.ID().String())
+ }
+ } else {
+ for _, addr := range h.Host.Addrs() {
+ if h.AllowLoopback {
+ addrs = append(addrs, addr.String()+"/p2p/"+h.Host.ID().String())
+ } else {
+ // We don't have isLoopbackOrLinkLocal easily here so just include all or rely on AddrsFactory from main.go
+ addrs = append(addrs, addr.String()+"/p2p/"+h.Host.ID().String())
+ }
+ }
+ }
+ return addrs
+}
diff --git a/cmd/sam-hub/sync.go b/cmd/sam-hub/sync.go
deleted file mode 100644
index 9713482f..00000000
--- a/cmd/sam-hub/sync.go
+++ /dev/null
@@ -1,280 +0,0 @@
-// Copyright 2026 Google LLC
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package main
-
-import (
- "context"
- "crypto/aes"
- "crypto/cipher"
- "crypto/rand"
- "crypto/sha256"
- "fmt"
- "io"
- "strings"
- "time"
-
- "github.com/google/sam/api"
- "github.com/libp2p/go-libp2p/core/peer"
- "google.golang.org/protobuf/proto"
-)
-
-func (h *Hub) encryptMsg(plaintext []byte) ([]byte, error) {
- if len(h.SyncKey) == 0 {
- return nil, fmt.Errorf("no sync key available")
- }
- key := sha256.Sum256(h.SyncKey)
- block, err := aes.NewCipher(key[:])
- if err != nil {
- return nil, err
- }
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- return nil, err
- }
- nonce := make([]byte, gcm.NonceSize())
- if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
- return nil, err
- }
- return gcm.Seal(nonce, nonce, plaintext, nil), nil
-}
-
-func (h *Hub) decryptMsg(ciphertext []byte) ([]byte, error) {
- if len(h.SyncKey) == 0 {
- return nil, fmt.Errorf("no sync key available")
- }
- key := sha256.Sum256(h.SyncKey)
- block, err := aes.NewCipher(key[:])
- if err != nil {
- return nil, err
- }
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- return nil, err
- }
- nonceSize := gcm.NonceSize()
- if len(ciphertext) < nonceSize {
- return nil, fmt.Errorf("ciphertext too short")
- }
- nonce, actualCiphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
- return gcm.Open(nil, nonce, actualCiphertext, nil)
-}
-
-func (h *Hub) startSyncListener(ctx context.Context) {
- if h.SyncTopic == nil {
- logger.Warn("SyncTopic is nil, sync listener will not start")
- return
- }
- sub, err := h.SyncTopic.Subscribe()
- if err != nil {
- logger.Errorf("Failed to subscribe to sync topic: %v", err)
- return
- }
-
- // Message processor
- go func() {
- defer sub.Cancel()
- for {
- msg, err := sub.Next(ctx)
- if err != nil {
- return
- }
- fromPeer, err := peer.IDFromBytes(msg.From)
- if err == nil && fromPeer == h.Host.ID() {
- continue // Skip our own broadcast
- }
-
- plaintext, err := h.decryptMsg(msg.Data)
- if err != nil {
- continue // Invalid encryption/key, ignore
- }
- var syncMsg api.HubSyncMessage
- if err := proto.Unmarshal(plaintext, &syncMsg); err != nil {
- continue
- }
-
- h.gater.mu.Lock()
- switch syncMsg.Action {
- case api.HubSyncMessage_ADD:
- p, err := peer.Decode(syncMsg.PeerId)
- if err == nil {
- if syncMsg.Timestamp > h.gater.lastUpdated[p] {
- h.gater.lastUpdated[p] = syncMsg.Timestamp
- if !h.gater.authenticated[p] {
- samHubActiveNodes.Inc()
- h.gater.authenticated[p] = true
- }
- }
- }
- case api.HubSyncMessage_REMOVE:
- p, err := peer.Decode(syncMsg.PeerId)
- if err == nil {
- if syncMsg.Timestamp > h.gater.lastUpdated[p] {
- h.gater.lastUpdated[p] = syncMsg.Timestamp
- if h.gater.authenticated[p] {
- samHubActiveNodes.Dec()
- delete(h.gater.authenticated, p)
- }
- }
- }
- case api.HubSyncMessage_FULL_SYNC:
- activePeers := make(map[string]bool)
- for _, activeStr := range syncMsg.Peers {
- activePeers[activeStr] = true
- }
-
- for pStr, ts := range syncMsg.PeerTimestamps {
- p, err := peer.Decode(pStr)
- if err != nil {
- continue
- }
- if ts > h.gater.lastUpdated[p] {
- h.gater.lastUpdated[p] = ts
-
- // Determine if the peer is active in the incoming state
- if activePeers[pStr] {
- if !h.gater.authenticated[p] {
- samHubActiveNodes.Inc()
- h.gater.authenticated[p] = true
- }
- } else {
- if h.gater.authenticated[p] {
- samHubActiveNodes.Dec()
- delete(h.gater.authenticated, p)
- }
- }
- }
- }
- if syncMsg.PeerId != "" && len(syncMsg.HubAddrs) > 0 {
- p, err := peer.Decode(syncMsg.PeerId)
- if err == nil {
- h.otherHubAddrs[p] = syncMsg.HubAddrs
- h.otherHubLastSeen[p] = time.Now()
- }
- }
- }
- h.gater.mu.Unlock()
- }
- }()
-
- // Periodic synchronizer
- go func() {
- ticker := time.NewTicker(15 * time.Second)
- defer ticker.Stop()
- for {
- select {
- case <-ctx.Done():
- return
- case <-ticker.C:
- h.publishFullSync(ctx)
- h.pruneStaleData()
- }
- }
- }()
-
- // Immediate sync broadcast upon starting
- h.publishFullSync(ctx)
-}
-
-func (h *Hub) publishFullSync(ctx context.Context) {
- h.gater.mu.RLock()
- var peers []string
- for p := range h.gater.authenticated {
- peers = append(peers, p.String())
- }
- peerTimestamps := make(map[string]int64)
- for p, ts := range h.gater.lastUpdated {
- peerTimestamps[p.String()] = ts
- }
- h.gater.mu.RUnlock()
-
- msg := api.HubSyncMessage{
- Action: api.HubSyncMessage_FULL_SYNC,
- PeerId: h.Host.ID().String(),
- Peers: peers,
- HubAddrs: h.getMyHubAddrs(),
- Timestamp: time.Now().UnixMilli(),
- PeerTimestamps: peerTimestamps,
- }
- h.publishSyncMessage(ctx, &msg)
-}
-
-func (h *Hub) publishSyncMessage(ctx context.Context, msg *api.HubSyncMessage) {
- if h.SyncTopic == nil {
- logger.Warn("SyncTopic is nil, sync message will not be published")
- return
- }
- data, err := proto.Marshal(msg)
- if err != nil {
- logger.Errorf("Failed to marshal sync msg: %v", err)
- return
- }
- ciphertext, err := h.encryptMsg(data)
- if err != nil {
- logger.Errorf("Failed to encrypt sync msg: %v", err)
- return
- }
- if err := h.SyncTopic.Publish(ctx, ciphertext); err != nil {
- logger.Errorf("Failed to publish sync msg: %v", err)
- }
-}
-
-func (h *Hub) getMyHubAddrs() []string {
- var hubAddrs []string
- if len(h.ExternalAddrs) > 0 {
- for _, addr := range h.ExternalAddrs {
- fullAddr := addr
- if !strings.Contains(addr, "/p2p/") {
- fullAddr = addr + "/p2p/" + h.Host.ID().String()
- }
- hubAddrs = append(hubAddrs, fullAddr)
- }
- } else {
- for _, addr := range h.Host.Addrs() {
- if !h.AllowLoopback && isLoopbackOrLinkLocal(addr) {
- continue
- }
- hubAddrs = append(hubAddrs, addr.String()+"/p2p/"+h.Host.ID().String())
- }
- }
- return hubAddrs
-}
-
-func (h *Hub) pruneStaleData() {
- h.gater.mu.Lock()
- defer h.gater.mu.Unlock()
- now := time.Now()
- for p, lastSeen := range h.otherHubLastSeen {
- if now.Sub(lastSeen) > 45*time.Second {
- delete(h.otherHubAddrs, p)
- delete(h.otherHubLastSeen, p)
- logger.Infof("Pruned stale hub %s (last seen %s ago)", p.String(), now.Sub(lastSeen))
- }
- }
-
- nowMilli := now.UnixMilli()
- for p, ts := range h.gater.lastUpdated {
- if !h.gater.authenticated[p] {
- if nowMilli-ts > int64(time.Hour/time.Millisecond) {
- delete(h.gater.lastUpdated, p)
- logger.Debugf("Pruned transient peer %s from lastUpdated", p.String())
- }
- } else if nowMilli-ts > int64(2*time.Hour/time.Millisecond) {
- samHubActiveNodes.Dec()
- delete(h.gater.authenticated, p)
- delete(h.gater.lastUpdated, p)
- logger.Infof("Pruned expired peer %s from authenticated list", p.String())
- }
- }
-}
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.go
index 7d912ef5..c72b62ad 100644
--- a/cmd/sam-node/main.go
+++ b/cmd/sam-node/main.go
@@ -231,13 +231,6 @@ func main() {
logger.Fatalf("Enrollment failed: %v", err)
}
- node.mu.Lock()
- enrollKnownPeers := make(map[string]bool)
- for k, v := range node.knownPeers {
- enrollKnownPeers[k] = v
- }
- node.mu.Unlock()
-
if teardownErr := node.Teardown(); teardownErr != nil {
logger.Errorf("Failed to teardown enrollment node: %v", teardownErr)
}
@@ -259,15 +252,6 @@ func main() {
if err != nil {
logger.Fatalf("Failed to start mesh node after enrollment: %v", err)
}
-
- node.mu.Lock()
- for k, v := range enrollKnownPeers {
- if node.Host != nil && k == node.Host.ID().String() {
- continue
- }
- node.knownPeers[k] = v
- }
- node.mu.Unlock()
}
// Register static services from config
@@ -565,17 +549,6 @@ func (n *SamNode) Enroll(ctx context.Context, jwt string) error {
n.trustedKeys = append(n.trustedKeys, TrustedKey{Key: ed25519.PublicKey(enrollResp.HubPublicKey), ReceivedAt: time.Now()})
n.keysMu.Unlock()
- // Add known peers from response
- n.mu.Lock()
- for _, p := range enrollResp.KnownPeers {
- if n.Host != nil && p == n.Host.ID().String() {
- continue
- }
- n.knownPeers[p] = true
- fmt.Printf("[Enroll] Added known peer from hub: %s\n", p)
- }
- n.mu.Unlock()
-
// Connect and Auth to hub after enrollment to join the mesh
for _, addrStr := range enrollResp.HubAddresses {
addr, err := multiaddr.NewMultiaddr(addrStr)
diff --git a/cmd/sam-node/mcp_handlers.go b/cmd/sam-node/mcp_handlers.go
index d57434c9..ca4af7da 100644
--- a/cmd/sam-node/mcp_handlers.go
+++ b/cmd/sam-node/mcp_handlers.go
@@ -163,12 +163,6 @@ func (n *SamNode) handleGetMeshInfo(ctx context.Context, req *mcp.CallToolReques
if n == nil {
return nil, nil, fmt.Errorf("node not initialized")
}
- n.mu.Lock()
- var knownPeers []string
- for p := range n.knownPeers {
- knownPeers = append(knownPeers, p)
- }
- n.mu.Unlock()
peers := n.Host.Network().Peers()
var connectedPeers []string
@@ -178,7 +172,6 @@ func (n *SamNode) handleGetMeshInfo(ctx context.Context, req *mcp.CallToolReques
dhtSize := n.DHT.RoutingTable().Size()
resData := map[string]any{
- "known_peers": knownPeers,
"connected_peers": connectedPeers,
"dht_size": dhtSize,
"hub_peer_id": n.HubPeerID.String(),
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 1bece9a2..09234562 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -82,7 +82,6 @@ type SamNode struct {
PubSub *pubsub.PubSub
Store *Store
HubPeerID peer.ID
- knownPeers map[string]bool
peerLastEventTime map[string]int64
receivedMsgs map[string][]string
topics map[string]*pubsub.Topic
@@ -108,7 +107,6 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
node := &SamNode{
Store: store,
trustedKeys: trustedKeys,
- knownPeers: make(map[string]bool),
peerLastEventTime: make(map[string]int64),
receivedMsgs: make(map[string][]string),
topics: make(map[string]*pubsub.Topic),
@@ -479,10 +477,6 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {
}
switch event.Type {
- case api.MeshEvent_JOIN:
- n.handleJoinEvent(&event)
- case api.MeshEvent_EXIT:
- n.handleExitEvent(&event)
case api.MeshEvent_BANNED:
n.handleBannedEvent(&event)
case api.MeshEvent_KEY_ROTATION:
@@ -491,39 +485,7 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {
}
}
-func (n *SamNode) handleJoinEvent(event *api.MeshEvent) {
- n.mu.Lock()
- defer n.mu.Unlock()
- if n.peerLastEventTime == nil {
- n.peerLastEventTime = make(map[string]int64)
- }
- if event.Timestamp < n.peerLastEventTime[event.PeerId] {
- logger.Warnf("[Mesh Event] Dropping out-of-order JOIN event for peer %s (event timestamp: %d, last processed: %d)", event.PeerId, event.Timestamp, n.peerLastEventTime[event.PeerId])
- return
- }
- n.peerLastEventTime[event.PeerId] = event.Timestamp
-
- if n.Host == nil || event.PeerId != n.Host.ID().String() {
- n.knownPeers[event.PeerId] = true
- }
- logger.Infof("[Mesh Event] Peer joined: %s", event.PeerId)
-}
-func (n *SamNode) handleExitEvent(event *api.MeshEvent) {
- n.mu.Lock()
- defer n.mu.Unlock()
- if n.peerLastEventTime == nil {
- n.peerLastEventTime = make(map[string]int64)
- }
- if event.Timestamp < n.peerLastEventTime[event.PeerId] {
- logger.Warnf("[Mesh Event] Dropping out-of-order EXIT event for peer %s (event timestamp: %d, last processed: %d)", event.PeerId, event.Timestamp, n.peerLastEventTime[event.PeerId])
- return
- }
- n.peerLastEventTime[event.PeerId] = event.Timestamp
-
- delete(n.knownPeers, event.PeerId)
- logger.Infof("[Mesh Event] Peer left: %s", event.PeerId)
-}
func (n *SamNode) handleBannedEvent(event *api.MeshEvent) {
n.mu.Lock()
@@ -537,7 +499,6 @@ func (n *SamNode) handleBannedEvent(event *api.MeshEvent) {
}
n.peerLastEventTime[event.PeerId] = event.Timestamp
- delete(n.knownPeers, event.PeerId)
n.mu.Unlock()
logger.Infof("[Mesh Event] Peer banned: %s", event.PeerId)
@@ -675,9 +636,6 @@ func (n *SamNode) startDiscovery(ctx context.Context, meshID string, interval ti
if p.ID == n.Host.ID() {
continue
}
- n.mu.Lock()
- n.knownPeers[p.ID.String()] = true
- n.mu.Unlock()
if n.Host.Network().Connectedness(p.ID) != network.Connected {
logger.Infof("[Discovery] Found peer not connected via DHT: %s", p.ID)
diff --git a/cmd/sam-node/node_test.go b/cmd/sam-node/node_test.go
index 312e3a97..5f0c0cd7 100644
--- a/cmd/sam-node/node_test.go
+++ b/cmd/sam-node/node_test.go
@@ -23,50 +23,11 @@ import (
lru "github.com/hashicorp/golang-lru/v2"
)
-func TestHandleJoinEvent(t *testing.T) {
- node := &SamNode{
- knownPeers: make(map[string]bool),
- }
-
- event := &api.MeshEvent{
- Type: api.MeshEvent_JOIN,
- PeerId: "12D3KooWAFv4iJst5G6MjwXhZ66K5zS1tP7A9vSg4vK8f1T7X8t9",
- Timestamp: time.Now().UnixMilli(),
- }
-
- node.handleJoinEvent(event)
-
- if !node.knownPeers[event.PeerId] {
- t.Error("Expected peer to be added to knownPeers")
- }
-}
-
-func TestHandleExitEvent(t *testing.T) {
- node := &SamNode{
- knownPeers: map[string]bool{
- "12D3KooWAFv4iJst5G6MjwXhZ66K5zS1tP7A9vSg4vK8f1T7X8t9": true,
- },
- }
-
- event := &api.MeshEvent{
- Type: api.MeshEvent_EXIT,
- PeerId: "12D3KooWAFv4iJst5G6MjwXhZ66K5zS1tP7A9vSg4vK8f1T7X8t9",
- Timestamp: time.Now().UnixMilli(),
- }
-
- node.handleExitEvent(event)
- if node.knownPeers[event.PeerId] {
- t.Error("Expected peer to be removed from knownPeers")
- }
-}
func TestHandleBannedEvent(t *testing.T) {
revokedCache, _ := lru.New[string, int64](10)
node := &SamNode{
- knownPeers: map[string]bool{
- "12D3KooWAFv4iJst5G6MjwXhZ66K5zS1tP7A9vSg4vK8f1T7X8t9": true,
- },
revokedPeers: revokedCache,
}
@@ -78,10 +39,6 @@ func TestHandleBannedEvent(t *testing.T) {
node.handleBannedEvent(event)
- if node.knownPeers[event.PeerId] {
- t.Error("Expected peer to be removed from knownPeers")
- }
-
if !node.revokedPeers.Contains(event.PeerId) {
t.Error("Expected peer to be added to revokedPeers")
}
diff --git a/cmd/sam-node/static_service_test.go b/cmd/sam-node/static_service_test.go
index 5ba02c92..32b4aeae 100644
--- a/cmd/sam-node/static_service_test.go
+++ b/cmd/sam-node/static_service_test.go
@@ -54,7 +54,6 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {
BiscuitToken: []byte("mock-biscuit-token"),
HubPublicKey: []byte("mock-hub-pub-key"),
HubAddresses: []string{h.Addrs()[0].String() + "/p2p/" + h.ID().String()},
- KnownPeers: []string{},
}
data, err := proto.Marshal(resp)
if err != nil {
diff --git a/tests/e2e/lib/container_mesh.bash b/tests/e2e/lib/container_mesh.bash
index 132158df..f871c905 100644
--- a/tests/e2e/lib/container_mesh.bash
+++ b/tests/e2e/lib/container_mesh.bash
@@ -122,7 +122,7 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; then
local idx="$1"
local output
output="$(timeout 15s docker run --rm --network "${MESH_NETWORK}" "${MESH_RUNTIME_IMAGE}" mcp-client -url "http://sam-node-${idx}:8080/mcp/events" -tool "get_mesh_info" 2>/dev/null)"
- echo "${output}" | jq '.known_peers | length'
+ echo "${output}" | jq 'if .connected_peers then (.connected_peers | length) - 1 else 0 end'
}
mesh_wait_for_node_count() {
@@ -135,7 +135,7 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; then
output="$(timeout 15s docker run --rm --network "${MESH_NETWORK}" "${MESH_RUNTIME_IMAGE}" mcp-client -url "http://sam-node-${idx}:8080/mcp/events" -tool "get_mesh_info" 2>/dev/null)"
echo "Node ${idx} get_mesh_info raw output: ${output}"
local count
- count="$(echo "${output}" | jq '.known_peers | length')"
+ count="$(echo "${output}" | jq 'if .connected_peers then (.connected_peers | length) - 1 else 0 end')"
echo "Node ${idx} reported known peers count: ${count}"
if [[ "${count}" -eq "${expected}" ]]; then
return 0
diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.go
index 1e159f93..24d52259 100644
--- a/tests/integration/fallback_test.go
+++ b/tests/integration/fallback_test.go
@@ -308,7 +308,6 @@ func startMockHubDynamic(t *testing.T, pubA, pubB ed25519.PublicKey) (peer.ID, s
BiscuitToken: []byte("mock-biscuit-token"),
HubPublicKey: pub,
HubAddresses: []string{h.Addrs()[0].String() + "/p2p/" + h.ID().String()},
- KnownPeers: []string{},
}
data, err := proto.Marshal(resp)
if err != nil {
diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.go
index 2854c330..94da1773 100644
--- a/tests/integration/minimal_helpers_test.go
+++ b/tests/integration/minimal_helpers_test.go
@@ -142,7 +142,6 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {
BiscuitToken: []byte("mock-biscuit-token"),
HubPublicKey: []byte("mock-hub-pub-key"),
HubAddresses: []string{h.Addrs()[0].String() + "/p2p/" + h.ID().String()},
- KnownPeers: []string{},
}
data, err := proto.Marshal(resp)
if err != nil {
@@ -234,7 +233,6 @@ func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, st
BiscuitToken: []byte("mock-biscuit-token"),
HubPublicKey: []byte("mock-hub-pub-key"),
HubAddresses: []string{h.Addrs()[0].String() + "/p2p/" + h.ID().String()},
- KnownPeers: []string{},
}
data, err := proto.Marshal(resp)
if err != nil {