Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix lint
Antonio Ojea2f22bd827ea6
diff --git a/cmd/sam-hub/hooks.go b/cmd/sam-hub/hooks.goindex fa5d5e7a..9b6aa157 100644--- a/cmd/sam-hub/hooks.go+++ b/cmd/sam-hub/hooks.go@@ -85,7 +85,10 @@ func (n *notifier) Disconnected(_ network.Network, c network.Conn) {return}// Notify the mesh about the peer disconnection- n.hub.EventTopic.Publish(context.Background(), data)+ if err := n.hub.EventTopic.Publish(context.Background(), data); err != nil {+ fmt.Printf("Failed to publish mesh event: %v\n", err)+ return+ }fmt.Printf("[Mesh] Peer %s left. Broadcasted departure to agents.\n", p)}}diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.goindex 08e64e57..c19f8209 100644--- a/cmd/sam-hub/main.go+++ b/cmd/sam-hub/main.go@@ -154,7 +154,7 @@ func (h *Hub) handleCallback(w http.ResponseWriter, r *http.Request) {rawID, _ := token.Extra("id_token").(string)idToken, err := h.Verifier.Verify(ctx, rawID)if err != nil {- http.Error(w, "ID Token verification failed", 401)+ http.Error(w, "ID Token verification failed", http.StatusUnauthorized)return}// standard claims mapping@@ -188,50 +188,62 @@ func (h *Hub) handleCallback(w http.ResponseWriter, r *http.Request) {h.gater.mu.Unlock()w.Header().Set("Content-Type", "text/html")- fmt.Fprintf(w, `<html><body>+ if _, err := fmt.Fprintf(w, `<html><body><h3>Sovereign Identity Verified!</h3><p>Peer <code>%s</code> is now part of mesh <code>%s</code></p><p>Identity: <code>%s</code></p><hr/><p>Standard Identity Biscuit:</p><textarea rows="5" cols="60">%s</textarea>- </body></html>`, p, h.MeshID, claims.Email, biscuitToken)+ </body></html>`, p, h.MeshID, claims.Email, biscuitToken); err != nil {+ log.Printf("writing callback response: %v", err)+ }}func (h *Hub) issueStandardBiscuit(p peer.ID, sub string, email string, groups []string) (string, error) {builder := biscuit.NewBuilder(h.BiscuitKey)// user_id mapping (sub)- builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "user_id",IDs: []biscuit.Term{biscuit.String(sub)},- }})+ }}); err != nil {+ return "", err+ }// user_email mapping (email)- builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "user_email",IDs: []biscuit.Term{biscuit.String(email)},- }})+ }}); err != nil {+ return "", err+ }// group mapping (groups)for _, g := range groups {- builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "group",IDs: []biscuit.Term{biscuit.String(g)},- }})+ }}); err != nil {+ return "", err+ }}// peer_id mapping (hardware binding)- builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "peer_id",IDs: []biscuit.Term{biscuit.String(p.String())},- }})+ }}); err != nil {+ return "", err+ }// mesh_id mapping (namespace)- builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: "mesh_id",IDs: []biscuit.Term{biscuit.String(h.MeshID)},- }})+ }}); err != nil {+ return "", err+ }t, err := builder.Build()if err != nil {@@ -253,7 +265,9 @@ func (h *Hub) startWatchdog() {for pID, connectedAt := range h.gater.pending {if now.Sub(connectedAt) > GracePeriod {fmt.Printf("[Security] Evicting unauthenticated peer: %s\n", pID)- h.Host.Network().ClosePeer(pID)+ if err := h.Host.Network().ClosePeer(pID); err != nil {+ log.Printf("closing unauthenticated peer %s: %v", pID, err)+ }delete(h.gater.pending, pID)}}@@ -286,12 +300,18 @@ func main() {if err != nil {log.Fatal(err)}- defer listener.Close()+ defer func() {+ if err := listener.Close(); err != nil {+ log.Printf("closing p2p listener: %v", err)+ }+ }()go func() {server := &http.Server{Handler: mux,}- server.Serve(listener)+ if err := server.Serve(listener); err != nil {+ log.Printf("p2p http server exited: %v", err)+ }}()fmt.Printf("SAM Hub Online (QUIC + TCP)\n")diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 00bd3271..7676c136 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -38,7 +38,11 @@ func main() {if err != nil {log.Fatalf("Critical: %v", err)}- defer store.Close()+ defer func() {+ if err := store.Close(); err != nil {+ log.Printf("closing store: %v", err)+ }+ }()priv := getOrGenerateKey(store)// Temporary host to determine PeerID@@ -84,7 +88,11 @@ func main() {if err != nil {log.Fatalf("Failed to open store: %v", err)}- defer store.Close()+ defer func() {+ if err := store.Close(); err != nil {+ log.Printf("closing store: %v", err)+ }+ }()token := tokenFlagif token == "" {@@ -135,7 +143,9 @@ func getOrGenerateKey(s *Store) crypto.PrivKey {log.Fatalf("Failed to generate key: %v", err)}raw, _ := crypto.MarshalPrivateKey(priv)- s.SaveKey(raw)+ if err := s.SaveKey(raw); err != nil {+ log.Fatalf("Failed to save key: %v", err)+ }return priv}priv, err := crypto.UnmarshalPrivateKey(kb)diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 89b0784d..9044c513 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -59,7 +59,9 @@ func (n *SamNode) SecureStreamHandler(pid protocol.ID, handler network.StreamHanif !isTrusted {// DROP: Peer has not completed the /sam/auth/1.0.0 handshakefmt.Printf("Blocked unauthorized stream request for %s from %s\n", pid, s.Conn().RemotePeer())- s.Reset()+ if err := s.Reset(); err != nil {+ fmt.Printf("Failed to reset unauthorized stream: %v\n", err)+ }return}@@ -107,7 +109,11 @@ func (n *SamNode) ListenForMeshEvents(ctx context.Context) error {// HandleAuthHandshake handles incoming Identity Biscuitsfunc (n *SamNode) HandleAuthHandshake(s network.Stream) {- defer s.Close()+ defer func() {+ if err := s.Close(); err != nil {+ fmt.Printf("Failed to close auth stream: %v\n", err)+ }+ }()// MVP: Logic to read Biscuit from stream, verify against Hub Public Key,// and ensure it is bound to the connecting PeerID.