Skip to content

Agent Mesh

switch to proto

Antonio Ojeaed823800c910
diff --git a/api/sam.pb.go b/api/sam.pb.go
index 10f6d266..15545a48 100644
--- a/api/sam.pb.go
+++ b/api/sam.pb.go
@@ -139,6 +139,58 @@ 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"`
@@ -777,6 +829,82 @@ 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"`
+ 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
+}
+
var File_api_sam_proto protoreflect.FileDescriptor
const file_api_sam_proto_rawDesc = "" +
@@ -837,7 +965,19 @@ 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*s\n" +
+ "\baudience\x18\x03 \x01(\tR\baudience\"\xf7\x01\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\"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" +
"\vServiceType\x12\x1c\n" +
"\x18SERVICE_TYPE_UNSPECIFIED\x10\x00\x12\x14\n" +
"\x10SERVICE_TYPE_MCP\x10\x01\x12\x1a\n" +
@@ -856,34 +996,37 @@ func file_api_sam_proto_rawDescGZIP() []byte {
return file_api_sam_proto_rawDescData
}
-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_enumTypes = make([]protoimpl.EnumInfo, 3)
+var file_api_sam_proto_msgTypes = make([]protoimpl.MessageInfo, 12)
var file_api_sam_proto_goTypes = []any{
(ServiceType)(0), // 0: sam.v1.ServiceType
(MeshEvent_Type)(0), // 1: sam.v1.MeshEvent.Type
- (*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
+ (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
}
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
- 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
+ 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
+ 6, // [6:6] is the sub-list for method output_type
+ 6, // [6:6] is the sub-list for method input_type
+ 6, // [6:6] is the sub-list for extension type_name
+ 6, // [6:6] is the sub-list for extension extendee
+ 0, // [0:6] is the sub-list for field type_name
}
func init() { file_api_sam_proto_init() }
@@ -900,8 +1043,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: 2,
- NumMessages: 11,
+ NumEnums: 3,
+ NumMessages: 12,
NumExtensions: 0,
NumServices: 0,
},
diff --git a/api/sam.proto b/api/sam.proto
index d08f6115..c963f9ce 100644
--- a/api/sam.proto
+++ b/api/sam.proto
@@ -95,3 +95,17 @@ message HubInfoResponse {
string client_id = 2;
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;
+}
diff --git a/cmd/sam-hub/hooks.go b/cmd/sam-hub/hooks.go
index b7e76444..bc7a8a84 100644
--- a/cmd/sam-hub/hooks.go
+++ b/cmd/sam-hub/hooks.go
@@ -79,9 +79,9 @@ func (n *notifier) Disconnected(_ network.Network, c network.Conn) {
samHubActiveNodes.Dec() // Decrement active nodes
// Notify other hubs to clean up this peer
- n.hub.publishSyncMessage(context.Background(), HubSyncMessage{
- Action: "REMOVE",
- PeerID: p.String(),
+ n.hub.publishSyncMessage(context.Background(), &api.HubSyncMessage{
+ Action: api.HubSyncMessage_REMOVE,
+ PeerId: p.String(),
Timestamp: time.Now().Unix(),
})
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index cc70ce05..45108e93 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -309,10 +309,10 @@ func (h *Hub) handleAuthHandshake(s network.Stream) {
h.gater.authenticated[remotePeer] = true
h.gater.mu.Unlock()
- // NEW: Notify other hubs of this peer
- h.publishSyncMessage(context.Background(), HubSyncMessage{
- Action: "ADD",
- PeerID: remotePeer.String(),
+ // Notify other hubs of this peer
+ h.publishSyncMessage(context.Background(), &api.HubSyncMessage{
+ Action: api.HubSyncMessage_ADD,
+ PeerId: remotePeer.String(),
Timestamp: time.Now().Unix(),
})
diff --git a/cmd/sam-hub/main_test.go b/cmd/sam-hub/main_test.go
index 1ca3611c..dbcfbcea 100644
--- a/cmd/sam-hub/main_test.go
+++ b/cmd/sam-hub/main_test.go
@@ -337,9 +337,9 @@ func TestHubReplicaSynchronization(t *testing.T) {
hubA.gater.mu.Unlock()
// 7. Broadcast ADD event from Hub A
- hubA.publishSyncMessage(ctx, HubSyncMessage{
- Action: "ADD",
- PeerID: peerC.String(),
+ hubA.publishSyncMessage(ctx, &api.HubSyncMessage{
+ Action: api.HubSyncMessage_ADD,
+ PeerId: peerC.String(),
Timestamp: time.Now().Unix(),
})
@@ -363,9 +363,9 @@ func TestHubReplicaSynchronization(t *testing.T) {
delete(hubA.gater.authenticated, peerC)
hubA.gater.mu.Unlock()
- hubA.publishSyncMessage(ctx, HubSyncMessage{
- Action: "REMOVE",
- PeerID: peerC.String(),
+ hubA.publishSyncMessage(ctx, &api.HubSyncMessage{
+ Action: api.HubSyncMessage_REMOVE,
+ PeerId: peerC.String(),
Timestamp: time.Now().Unix(),
})
diff --git a/cmd/sam-hub/server.go b/cmd/sam-hub/server.go
index d6a3d00e..70245dd6 100644
--- a/cmd/sam-hub/server.go
+++ b/cmd/sam-hub/server.go
@@ -160,9 +160,9 @@ func handleRegisterHTTP(h *Hub) http.HandlerFunc {
h.gater.mu.Unlock()
// NEW: Propagate addition to other hubs
- h.publishSyncMessage(context.Background(), HubSyncMessage{
- Action: "ADD",
- PeerID: pID.String(),
+ h.publishSyncMessage(context.Background(), &api.HubSyncMessage{
+ Action: api.HubSyncMessage_ADD,
+ PeerId: pID.String(),
Timestamp: time.Now().Unix(),
})
diff --git a/cmd/sam-hub/sync.go b/cmd/sam-hub/sync.go
index 4faba250..d6325182 100644
--- a/cmd/sam-hub/sync.go
+++ b/cmd/sam-hub/sync.go
@@ -20,23 +20,16 @@ import (
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
- "encoding/json"
"fmt"
"io"
"strings"
"time"
+ "github.com/google/sam/api"
"github.com/libp2p/go-libp2p/core/peer"
+ "google.golang.org/protobuf/proto"
)
-type HubSyncMessage struct {
- Action string `json:"action"` // ADD, REMOVE, FULL_SYNC
- PeerID string `json:"peer_id,omitempty"`
- Peers []string `json:"peers,omitempty"`
- HubAddrs []string `json:"hub_addrs,omitempty"`
- Timestamp int64 `json:"timestamp"`
-}
-
func (h *Hub) encryptMsg(plaintext []byte) ([]byte, error) {
key := sha256.Sum256(h.KeyRing.GetCurrentKey())
block, err := aes.NewCipher(key[:])
@@ -101,26 +94,26 @@ func (h *Hub) startSyncListener(ctx context.Context) {
if err != nil {
continue // Invalid encryption/key, ignore
}
- var syncMsg HubSyncMessage
- if err := json.Unmarshal(plaintext, &syncMsg); err != nil {
+ var syncMsg api.HubSyncMessage
+ if err := proto.Unmarshal(plaintext, &syncMsg); err != nil {
continue
}
h.gater.mu.Lock()
switch syncMsg.Action {
- case "ADD":
- p, err := peer.Decode(syncMsg.PeerID)
+ case api.HubSyncMessage_ADD:
+ p, err := peer.Decode(syncMsg.PeerId)
if err == nil && !h.gater.authenticated[p] {
samHubActiveNodes.Inc()
h.gater.authenticated[p] = true
}
- case "REMOVE":
- p, err := peer.Decode(syncMsg.PeerID)
+ case api.HubSyncMessage_REMOVE:
+ p, err := peer.Decode(syncMsg.PeerId)
if err == nil && h.gater.authenticated[p] {
samHubActiveNodes.Dec()
delete(h.gater.authenticated, p)
}
- case "FULL_SYNC":
+ case api.HubSyncMessage_FULL_SYNC:
for _, pStr := range syncMsg.Peers {
p, err := peer.Decode(pStr)
if err == nil && !h.gater.authenticated[p] {
@@ -128,8 +121,8 @@ func (h *Hub) startSyncListener(ctx context.Context) {
h.gater.authenticated[p] = true
}
}
- if syncMsg.PeerID != "" && len(syncMsg.HubAddrs) > 0 {
- p, err := peer.Decode(syncMsg.PeerID)
+ if syncMsg.PeerId != "" && len(syncMsg.HubAddrs) > 0 {
+ p, err := peer.Decode(syncMsg.PeerId)
if err == nil {
h.otherHubAddrs[p] = syncMsg.HubAddrs
}
@@ -165,18 +158,22 @@ func (h *Hub) publishFullSync(ctx context.Context) {
}
h.gater.mu.RUnlock()
- msg := HubSyncMessage{
- Action: "FULL_SYNC",
- PeerID: h.Host.ID().String(),
+ msg := api.HubSyncMessage{
+ Action: api.HubSyncMessage_FULL_SYNC,
+ PeerId: h.Host.ID().String(),
Peers: peers,
HubAddrs: h.getMyHubAddrs(),
Timestamp: time.Now().Unix(),
}
- h.publishSyncMessage(ctx, msg)
+ h.publishSyncMessage(ctx, &msg)
}
-func (h *Hub) publishSyncMessage(ctx context.Context, msg HubSyncMessage) {
- data, _ := json.Marshal(msg)
+func (h *Hub) publishSyncMessage(ctx context.Context, msg *api.HubSyncMessage) {
+ 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)