Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
Fail fast
Antonio Ojea8ef163878b60
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 371ec97f..c9671fa8 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -65,6 +65,8 @@ const (KeyPruningInterval = 1 * time.Hour)+var ErrFatalAuth = errors.New("fatal authentication error")+type TrustedKey struct {Key ed25519.PublicKeyReceivedAt time.Time@@ -183,14 +185,28 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.Pnode.services = NewServiceRegistry(node.DHT)// Bootstrap: Connect and Auth to the Hub+ var authenticated bool+ var fatalAuthErr error+for _, addr := range hubAddrs {if err := node.ConnectAndAuthWithHub(ctx, addr); err != nil {logger.Warnf("[AuthN] Failed to bootstrap and auth with hub %s: %v", addr, err)+ if errors.Is(err, ErrFatalAuth) {+ fatalAuthErr = err+ }} else {+ authenticated = truebreak}}+ if len(hubAddrs) > 0 && !authenticated {+ if fatalAuthErr != nil {+ return nil, fmt.Errorf("fatal auth failure: %w", fatalAuthErr)+ }+ }++// Initialize Gossipsub for Hub Eventsps, err := pubsub.NewGossipSub(ctx, h)if err != nil {@@ -276,6 +292,9 @@ func (n *SamNode) ConnectAndAuthWithHub(ctx context.Context, addr multiaddr.Mult}if err := n.Host.Connect(ctx, *addrInfo); err != nil {+ if strings.Contains(err.Error(), "peer id mismatch") {+ return fmt.Errorf("%w: %w", ErrFatalAuth, err)+ }return fmt.Errorf("failed to connect to hub %s: %w", addr, err)}@@ -285,16 +304,16 @@ func (n *SamNode) ConnectAndAuthWithHub(ctx context.Context, addr multiaddr.Mult// Load biscuit from storebiscuitBytes, err := n.Store.LoadIdentity()if err != nil {- return fmt.Errorf("failed to load identity from store: %w", err)+ return fmt.Errorf("%w: failed to load identity from store: %w", ErrFatalAuth, err)}if len(biscuitBytes) == 0 {- return fmt.Errorf("no identity biscuit found in store")+ return fmt.Errorf("%w: no identity biscuit found in store", ErrFatalAuth)}// Open auth streams, err := n.Host.NewStream(ctx, addrInfo.ID, api.AuthProtocolID)if err != nil {- return fmt.Errorf("failed to open auth stream to hub: %w", err)+ return fmt.Errorf("%w: failed to open auth stream to hub: %w", ErrFatalAuth, err)}defer func() {if err := s.Close(); err != nil {@@ -325,7 +344,7 @@ func (n *SamNode) ConnectAndAuthWithHub(ctx context.Context, addr multiaddr.Mult}if !resp.Success {- return fmt.Errorf("auth failed: %s", resp.Error)+ return fmt.Errorf("%w: auth failed: %s", ErrFatalAuth, resp.Error)}logger.Infof("[AuthN] Successfully authenticated with hub via libp2p")diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.goindex f498a7af..ec1e1519 100644--- a/tests/integration/fallback_test.go+++ b/tests/integration/fallback_test.go@@ -37,6 +37,7 @@ import (dht "github.com/libp2p/go-libp2p-kad-dht""github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr""google.golang.org/protobuf/proto"@@ -257,6 +258,21 @@ func startMockHubDynamic(t *testing.T, pubA, pubB ed25519.PublicKey) (peer.ID, st.Fatalf("failed to create mock libp2p host: %v", err)}+ h.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer s.Close()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ defer reader.ReleaseMsg(msg)++ writer := msgio.NewVarintWriter(s)+ resp := &api.AuthResponse{Success: true}+ respBytes, _ := proto.Marshal(resp)+ _ = writer.WriteMsg(respBytes)+ })+kdht, err := dht.New(context.Background(), h, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))if err != nil {t.Fatalf("failed to create DHT on mock hub: %v", err)diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.goindex 449a0ba0..36727159 100644--- a/tests/integration/minimal_helpers_test.go+++ b/tests/integration/minimal_helpers_test.go@@ -31,6 +31,8 @@ import ("github.com/libp2p/go-libp2p"dht "github.com/libp2p/go-libp2p-kad-dht""github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-msgio""google.golang.org/protobuf/proto")@@ -98,6 +100,21 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {t.Fatalf("failed to create mock libp2p host: %v", err)}+ h.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer s.Close()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ defer reader.ReleaseMsg(msg)++ writer := msgio.NewVarintWriter(s)+ resp := &api.AuthResponse{Success: true}+ respBytes, _ := proto.Marshal(resp)+ _ = writer.WriteMsg(respBytes)+ })+kdht, err := dht.New(context.Background(), h, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))if err != nil {t.Fatalf("failed to create DHT on mock hub: %v", err)@@ -156,6 +173,21 @@ func startMockLibp2pHubWithOIDC(t *testing.T, oidcIssuerURL string) (peer.ID, stt.Fatalf("failed to create mock libp2p host: %v", err)}+ h.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer s.Close()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ defer reader.ReleaseMsg(msg)++ writer := msgio.NewVarintWriter(s)+ resp := &api.AuthResponse{Success: true}+ respBytes, _ := proto.Marshal(resp)+ _ = writer.WriteMsg(respBytes)+ })+kdht, err := dht.New(context.Background(), h, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))if err != nil {t.Fatalf("failed to create DHT on mock hub: %v", err)