Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
close security gap
Antonio Ojea6fd46903602c
diff --git a/cmd/sam-agent/call.go b/cmd/sam-agent/call.goindex 62022a14..d0994480 100644--- a/cmd/sam-agent/call.go+++ b/cmd/sam-agent/call.go@@ -74,7 +74,7 @@ func runCall(parent context.Context, cfg *runConfig, targetArg string) error {return runCallDryRunClient(cfg, targetArg)}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/identity_login.go b/cmd/sam-agent/identity_login.goindex 66ef76ba..ad004bbd 100644--- a/cmd/sam-agent/identity_login.go+++ b/cmd/sam-agent/identity_login.go@@ -94,7 +94,7 @@ func runIdentityLogin(parent context.Context, cfg *runConfig, clientID string) efmt.Fprintf(os.Stderr, "Authorization successful. Issuing identity passport …\n")// Build the node to obtain our stable PeerID.- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/mesh_get_agents.go b/cmd/sam-agent/mesh_get_agents.goindex 0884dac5..bf058dc3 100644--- a/cmd/sam-agent/mesh_get_agents.go+++ b/cmd/sam-agent/mesh_get_agents.go@@ -69,7 +69,7 @@ func runMeshGetAgents(parent context.Context, cfg *runConfig) error {return err}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/node.go b/cmd/sam-agent/node.goindex 511e94b5..80495614 100644--- a/cmd/sam-agent/node.go+++ b/cmd/sam-agent/node.go@@ -29,6 +29,7 @@ import ("time""github.com/libp2p/go-libp2p/core/crypto"+ libp2ppeer "github.com/libp2p/go-libp2p/core/peer""github.com/multiformats/go-multiaddr"samnet "sam/pkg/net"@@ -37,20 +38,22 @@ import (// buildNode constructs a SAM mesh node from the shared runConfig flags.// --hub is mandatory: the hub provides OIDC-backed passport issuance and// acts as the mandatory P2P rendezvous/bootstrap point for the mesh.-func buildNode(cfg *runConfig) (samnet.Node, error) {+// It also returns the hub's libp2p peer ID so callers can register it as+// a trusted peer (exempt from passport auth) before joining the mesh.+func buildNode(cfg *runConfig) (samnet.Node, libp2ppeer.ID, error) {hubURL := strings.TrimSpace(resolveHubURL(cfg))if hubURL == "" {- return nil, fmt.Errorf("--hub (or SAM_HUB env) is required: the hub is the rendezvous point and passport issuer for the mesh")+ return nil, "", fmt.Errorf("--hub (or SAM_HUB env) is required: the hub is the rendezvous point and passport issuer for the mesh")}listen, err := parseMultiaddrs(cfg.listenAddrs)if err != nil {- return nil, fmt.Errorf("parsing --listen: %w", err)+ return nil, "", fmt.Errorf("parsing --listen: %w", err)}key, err := loadOrGenerateKey(cfg.identityPath)if err != nil {- return nil, fmt.Errorf("loading identity key: %w", err)+ return nil, "", fmt.Errorf("loading identity key: %w", err)}// Resolve hub hostname for rendezvous namespace and relay hint.@@ -59,7 +62,7 @@ func buildNode(cfg *runConfig) (samnet.Node, error) {// Fetch hub P2P bootstrap addresses from the hub's well-known endpoint.// This is the primary rendezvous mechanism: all agents bootstrap through// the hub, which acts as DHT server and relay.- hubBootstrap, err := fetchHubP2PAddrs(hubURL)+ hubBootstrap, hubPeerID, err := fetchHubP2PInfo(hubURL)if err != nil {slog.Default().Warn("could not fetch hub P2P addresses; hub may not be running in P2P mode", "hub", hubURL, "err", err)}@@ -79,26 +82,33 @@ func buildNode(cfg *runConfig) (samnet.Node, error) {opts = append(opts, samnet.WithRelayService())}- return samnet.New(opts...)+ node, err := samnet.New(opts...)+ if err != nil {+ return nil, "", err+ }+ return node, hubPeerID, nil}// fetchHubP2PAddrs contacts the hub's /.well-known/sam-hub-p2p endpoint and// returns the hub's libp2p multiaddrs for use as bootstrap/rendezvous peers.-func fetchHubP2PAddrs(hubURL string) ([]multiaddr.Multiaddr, error) {+// fetchHubP2PInfo contacts the hub's /.well-known/sam-hub-p2p endpoint and+// returns the hub's libp2p multiaddrs and peer ID.+func fetchHubP2PInfo(hubURL string) ([]multiaddr.Multiaddr, libp2ppeer.ID, error) {endpoint := strings.TrimRight(hubURL, "/") + "/.well-known/sam-hub-p2p"resp, err := http.Get(endpoint) //nolint:gosec // hubURL is user-supplied and validatedif err != nil {- return nil, fmt.Errorf("fetching hub P2P info: %w", err)+ return nil, "", fmt.Errorf("fetching hub P2P info: %w", err)}defer func() { _ = resp.Body.Close() }()if resp.StatusCode != http.StatusOK {- return nil, fmt.Errorf("hub P2P info returned %d", resp.StatusCode)+ return nil, "", fmt.Errorf("hub P2P info returned %d", resp.StatusCode)}var info struct {- Addrs []string `json:"addrs"`+ PeerID string `json:"peer_id"`+ Addrs []string `json:"addrs"`}if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {- return nil, fmt.Errorf("decoding hub P2P info: %w", err)+ return nil, "", fmt.Errorf("decoding hub P2P info: %w", err)}var out []multiaddr.Multiaddrfor _, s := range info.Addrs {@@ -108,7 +118,8 @@ func fetchHubP2PAddrs(hubURL string) ([]multiaddr.Multiaddr, error) {}out = append(out, ma)}- return out, nil+ hubPeer, _ := libp2ppeer.Decode(info.PeerID) // empty string if hub has no P2P mode+ return out, hubPeer, nil}func hubHostFromURL(hubURL string) string {diff --git a/cmd/sam-agent/proxy.go b/cmd/sam-agent/proxy.goindex 3af8f357..95b0af17 100644--- a/cmd/sam-agent/proxy.go+++ b/cmd/sam-agent/proxy.go@@ -73,7 +73,7 @@ func runProxy(parent context.Context, cfg *runConfig) error {cfg.proxyTimeout = 30 * time.Second}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/publish.go b/cmd/sam-agent/publish.goindex 87f6a6f4..bed0012b 100644--- a/cmd/sam-agent/publish.go+++ b/cmd/sam-agent/publish.go@@ -94,7 +94,7 @@ func runPublish(parent context.Context, cfg *runConfig) error {return runPublishDryRunClient(cfg)}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/publish_card.go b/cmd/sam-agent/publish_card.goindex 5c2e59e2..e00cd630 100644--- a/cmd/sam-agent/publish_card.go+++ b/cmd/sam-agent/publish_card.go@@ -45,7 +45,7 @@ func runPublishCard(parent context.Context, cfg *runConfig) error {return fmt.Errorf("at least one --capability is required")}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/publish_mcp.go b/cmd/sam-agent/publish_mcp.goindex 4bd407e8..068ec04d 100644--- a/cmd/sam-agent/publish_mcp.go+++ b/cmd/sam-agent/publish_mcp.go@@ -54,7 +54,7 @@ func runPublishMCP(parent context.Context, cfg *runConfig) error {return fmt.Errorf("--port must be a positive integer")}- node, err := buildNode(cfg)+ node, _, err := buildNode(cfg)if err != nil {return err}diff --git a/cmd/sam-agent/up.go b/cmd/sam-agent/up.goindex e0c1dc44..99b984ed 100644--- a/cmd/sam-agent/up.go+++ b/cmd/sam-agent/up.go@@ -51,10 +51,18 @@ func runUp(parent context.Context, cfg *runConfig) error {return err}- node, err := buildNode(cfg)+ node, hubPeerID, err := buildNode(cfg)if err != nil {return err}++ // Register the hub as a trusted peer before starting the node.+ // The hub is infrastructure — it doesn't run passport auth — so we+ // must exempt it from the connection-close-on-auth-failure gate.+ if hubPeerID != "" {+ identity.RegisterTrustedPeer(node.Host(), hubPeerID)+ }+if err := node.Start(parent); err != nil {return fmt.Errorf("starting node: %w", err)}diff --git a/pkg/identity/libp2p_passport.go b/pkg/identity/libp2p_passport.goindex 1a316bc4..9d0c3809 100644--- a/pkg/identity/libp2p_passport.go+++ b/pkg/identity/libp2p_passport.go@@ -30,6 +30,7 @@ type passportAuthManager struct {mu sync.RWMutexlocalPassport stringpeerClaims map[string]*PassportClaims+ trustedPeers map[peer.ID]struct{} // hub, relays — exempt from passport authinstalled bool}@@ -53,7 +54,18 @@ func EnsurePassportAuth(h host.Host, _ string) error {h.Network().Notify(&network.NotifyBundle{ConnectedF: func(_ network.Network, conn network.Conn) {go func() {- _ = AuthenticatePeerPassport(context.Background(), h, conn.RemotePeer())+ remote := conn.RemotePeer()+ mgr.mu.RLock()+ _, trusted := mgr.trustedPeers[remote]+ mgr.mu.RUnlock()+ if trusted {+ return+ }+ if err := AuthenticatePeerPassport(context.Background(), h, remote); err != nil {+ // Peer could not present a valid hub-issued passport.+ // Close the connection to prevent unauthenticated mesh participation.+ _ = conn.Close()+ }}()},DisconnectedF: func(_ network.Network, conn network.Conn) {@@ -65,6 +77,20 @@ func EnsurePassportAuth(h host.Host, _ string) error {return nil}+// RegisterTrustedPeer marks a peer as infrastructure (e.g. the hub) that is+// exempt from passport authentication. Connections to/from trusted peers are+// never closed due to a missing biscuit. Must be called before the node starts+// making connections.+func RegisterTrustedPeer(h host.Host, p peer.ID) {+ if h == nil || p == "" {+ return+ }+ mgr := getOrCreatePassportAuthManager(h)+ mgr.mu.Lock()+ mgr.trustedPeers[p] = struct{}{}+ mgr.mu.Unlock()+}+func SetLocalPassport(h host.Host, _ string, passport string) error {if err := EnsurePassportAuth(h, "default"); err != nil {return err@@ -158,7 +184,12 @@ func getOrCreatePassportAuthManager(h host.Host) *passportAuthManager {if mgr, ok := passportAuthManagers.Load(h); ok {return mgr.(*passportAuthManager)}- mgr := &passportAuthManager{host: h, federationID: "default", peerClaims: map[string]*PassportClaims{}}+ mgr := &passportAuthManager{+ host: h,+ federationID: "default",+ peerClaims: map[string]*PassportClaims{},+ trustedPeers: map[peer.ID]struct{}{},+ }actual, _ := passportAuthManagers.LoadOrStore(h, mgr)return actual.(*passportAuthManager)}