Skip to content

Agent Mesh

address comments from review

Antonio Ojea604ea70b12ec
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index 83c091c1..af7bca24 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -16,12 +16,12 @@ package main
import (
"context"
- "os"
- "strings"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"fmt"
+ "os"
+ "strings"
"sync"
"time"
@@ -30,6 +30,7 @@ import (
"github.com/biscuit-auth/biscuit-go/v2/parser"
"github.com/google/sam/api"
lru "github.com/hashicorp/golang-lru/v2"
+ "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -45,9 +46,8 @@ import (
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/libp2p/go-msgio"
"github.com/multiformats/go-multiaddr"
- "google.golang.org/protobuf/proto"
- "github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
+ "google.golang.org/protobuf/proto"
)
const (
@@ -619,20 +619,24 @@ func serviceNameToCID(name string) (cid.Cid, error) {
}
func (n *SamNode) RegisterService(ctx context.Context, serviceName string) error {
- n.servicesMu.Lock()
- n.services[serviceName] = true
- n.servicesMu.Unlock()
-
- // Announce to DHT
c, err := serviceNameToCID(serviceName)
if err != nil {
return err
}
- logger.Infof("[ServiceRegistry] Registering service %s (CID: %s)", serviceName, c)
- if n.DHT != nil {
- return n.DHT.Provide(ctx, c, true)
+ if n.DHT == nil {
+ return fmt.Errorf("DHT not initialized")
+ }
+
+ if err := n.DHT.Provide(ctx, c, true); err != nil {
+ return err
}
+
+ n.servicesMu.Lock()
+ n.services[serviceName] = true
+ n.servicesMu.Unlock()
+
+ logger.Infof("[ServiceRegistry] Registering service %s (CID: %s)", serviceName, c)
return nil
}
@@ -658,6 +662,10 @@ func (n *SamNode) FindProviders(ctx context.Context, serviceName string) ([]peer
return nil, err
}
+ if n.DHT == nil {
+ return nil, fmt.Errorf("DHT not initialized")
+ }
+
providers := n.DHT.FindProvidersAsync(ctx, c, 20)
var providerInfos []peer.AddrInfo
for p := range providers {
diff --git a/cmd/sam-node/sidecar.go b/cmd/sam-node/sidecar.go
index 042b8f5c..a5ebc257 100644
--- a/cmd/sam-node/sidecar.go
+++ b/cmd/sam-node/sidecar.go
@@ -59,8 +59,14 @@ func startSidecarServer(node *SamNode, addr, token, certFile, keyFile, caFile st
actualAddr := listener.Addr().String()
+ if (certFile != "") != (keyFile != "") {
+ logger.Errorf("Both --tls-cert and --tls-key must be provided to enable TLS")
+ return
+ }
+
if certFile != "" && keyFile != "" {
tlsConfig := &tls.Config{}
+ isMTLS := false
if caFile != "" {
caCert, err := os.ReadFile(caFile)
if err != nil {
@@ -71,6 +77,12 @@ func startSidecarServer(node *SamNode, addr, token, certFile, keyFile, caFile st
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
+ isMTLS = true
+ }
+
+ if !isMTLS && token == "" {
+ logger.Errorf("Token is mandatory when not using mTLS")
+ return
}
server.TLSConfig = tlsConfig
@@ -81,6 +93,10 @@ func startSidecarServer(node *SamNode, addr, token, certFile, keyFile, caFile st
}
}()
} else {
+ if token == "" {
+ logger.Errorf("Token is mandatory when not using mTLS")
+ return
+ }
logger.Infof("Starting MCP server on TCP address %s", actualAddr)
go func() {
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
@@ -107,6 +123,8 @@ func handleReadyz(w http.ResponseWriter, r *http.Request) {
func withAuth(token string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if token == "" {
+ // If token is empty, we assume mTLS is handling authentication.
+ // startSidecarServer enforces that token is present if mTLS is not used.
next.ServeHTTP(w, r)
return
}
diff --git a/cmd/sam-node/sidecar_test.go b/cmd/sam-node/sidecar_test.go
index efdf5bf1..d3f063ad 100644
--- a/cmd/sam-node/sidecar_test.go
+++ b/cmd/sam-node/sidecar_test.go
@@ -16,10 +16,16 @@ package main
import (
"bytes"
+ "context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
+ "time"
+
+ "github.com/libp2p/go-libp2p"
+ dht "github.com/libp2p/go-libp2p-kad-dht"
+ "github.com/libp2p/go-libp2p/core/peer"
)
func TestWithAuth(t *testing.T) {
@@ -69,6 +75,20 @@ func TestWithAuth(t *testing.T) {
}
})
}
+
+ t.Run("Empty token configured", func(t *testing.T) {
+ handlerEmpty := withAuth("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ }))
+ req := httptest.NewRequest("GET", "/any", nil)
+ req.Header.Set("Authorization", "Bearer anything")
+ rr := httptest.NewRecorder()
+ handlerEmpty.ServeHTTP(rr, req)
+
+ if rr.Code != http.StatusOK {
+ t.Errorf("expected status %d, got %d", http.StatusOK, rr.Code)
+ }
+ })
}
func TestPublicEndpoints(t *testing.T) {
@@ -90,8 +110,40 @@ func TestPublicEndpoints(t *testing.T) {
}
func TestHandleRegisterService(t *testing.T) {
+ h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer h.Close()
+ d, err := dht.New(context.Background(), h, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer d.Close()
+
+ // Create a second host to populate routing table
+ h2, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer h2.Close()
+ d2, err := dht.New(context.Background(), h2, dht.Mode(dht.ModeServer), dht.ProtocolPrefix("/sam"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer d2.Close()
+
+ err = h.Connect(context.Background(), peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Wait for DHT to recognize the peer
+ time.Sleep(100 * time.Millisecond)
+
node := &SamNode{
services: make(map[string]bool),
+ DHT: d,
}
reqBody := ServiceRequest{ServiceName: "test-service"}
@@ -103,7 +155,7 @@ func TestHandleRegisterService(t *testing.T) {
handleRegisterService(node, rr, req)
if rr.Code != http.StatusOK {
- t.Errorf("expected status OK, got %d", rr.Code)
+ t.Errorf("expected status OK, got %d, body: %s", rr.Code, rr.Body.String())
}
if !node.IsServiceRegistered("test-service") {
diff --git a/tests/e2e/lib/container_mesh.bash b/tests/e2e/lib/container_mesh.bash
index c42d6de6..2fd46bc8 100644
--- a/tests/e2e/lib/container_mesh.bash
+++ b/tests/e2e/lib/container_mesh.bash
@@ -242,6 +242,7 @@ if [[ -z "${MESH_HELPERS_LOADED:-}" ]]; then
--listen "/ip4/0.0.0.0/udp/5001/quic-v1" \
--listen "/ip4/0.0.0.0/tcp/5002" \
--bind-addr "0.0.0.0:8080" \
+ --api-token "secret-token" \
--mesh "e2e-mesh" >/dev/null
MESH_CONTAINERS+=("${name}")
diff --git a/tests/e2e/policy.bats b/tests/e2e/policy.bats
index 6b96579b..94d42b37 100644
--- a/tests/e2e/policy.bats
+++ b/tests/e2e/policy.bats
@@ -216,6 +216,7 @@ EOF"
--listen "/ip4/0.0.0.0/udp/5001/quic-v1" \
--listen "/ip4/0.0.0.0/tcp/5002" \
--bind-addr "0.0.0.0:8080" \
+ --api-token "secret-token" \
--mesh "e2e-mesh" \
--local-policy "/etc/sam/local_policy.yaml" >/dev/null
diff --git a/tests/integration/catalog_test.go b/tests/integration/catalog_test.go
index eb404109..54d284cc 100644
--- a/tests/integration/catalog_test.go
+++ b/tests/integration/catalog_test.go
@@ -19,7 +19,7 @@ func startBackgroundNode(t *testing.T, nodeBin string, hubAddr string, homeDir s
"HOME="+homeDir,
"XDG_CONFIG_HOME="+filepath.Join(homeDir, ".config"),
)
- allArgs := append([]string{"run", "--hub", hubAddr, "--jwt", "test-jwt", "--bind-addr", "127.0.0.1:0"}, args...)
+ allArgs := append([]string{"run", "--hub", hubAddr, "--jwt", "test-jwt", "--bind-addr", "127.0.0.1:0", "--api-token", "test-token"}, args...)
cmd := exec.Command(nodeBin, allArgs...)
cmd.Env = env
diff --git a/tests/integration/pubsub_test.go b/tests/integration/pubsub_test.go
index 14f51095..111bbb0b 100644
--- a/tests/integration/pubsub_test.go
+++ b/tests/integration/pubsub_test.go
@@ -46,7 +46,7 @@ func TestPubSubTools(t *testing.T) {
// Start Node 1
env1 := append(os.Environ(), "HOME="+tmpHome1, "XDG_CONFIG_HOME="+filepath.Join(tmpHome1, ".config"))
- cmd1 := exec.CommandContext(ctx, nodeBin, "run", "--hub", hubAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5003/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5004", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms")
+ cmd1 := exec.CommandContext(ctx, nodeBin, "run", "--hub", hubAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5003/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5004", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms", "--api-token", "dummy-token")
cmd1.Env = env1
logFile1, err := os.Create(filepath.Join(tmpHome1, "node1.log"))
if err != nil {
@@ -61,7 +61,7 @@ func TestPubSubTools(t *testing.T) {
// Start Node 2
env2 := append(os.Environ(), "HOME="+tmpHome2, "XDG_CONFIG_HOME="+filepath.Join(tmpHome2, ".config"))
- cmd2 := exec.CommandContext(ctx, nodeBin, "run", "--hub", hubAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5005/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5006", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms")
+ cmd2 := exec.CommandContext(ctx, nodeBin, "run", "--hub", hubAddr, "--bind-addr", "127.0.0.1:0", "--listen", "/ip4/127.0.0.1/udp/5005/quic-v1", "--listen", "/ip4/127.0.0.1/tcp/5006", "--jwt", "dummy-token", "--log-level", "debug", "--discovery-interval", "100ms", "--api-token", "dummy-token")
cmd2.Env = env2
logFile2, err := os.Create(filepath.Join(tmpHome2, "node2.log"))
if err != nil {
diff --git a/tests/integration/service_discovery_test.go b/tests/integration/service_discovery_test.go
index d096aa22..68fbb814 100644
--- a/tests/integration/service_discovery_test.go
+++ b/tests/integration/service_discovery_test.go
@@ -35,8 +35,6 @@ func TestServiceDiscovery(t *testing.T) {
homeB := t.TempDir()
apiToken := "secret-token"
- apiAddrA := "127.0.0.1:8091"
- apiAddrB := "127.0.0.1:8092"
// Start Node A
t.Log("Starting Node A...")
@@ -44,7 +42,7 @@ func TestServiceDiscovery(t *testing.T) {
"--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
"--listen", "/ip4/127.0.0.1/tcp/0",
"--discovery-interval", "100ms",
- "--bind-addr", apiAddrA,
+ "--bind-addr", "127.0.0.1:0",
"--api-token", apiToken,
)
@@ -54,26 +52,30 @@ func TestServiceDiscovery(t *testing.T) {
"--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
"--listen", "/ip4/127.0.0.1/tcp/0",
"--discovery-interval", "100ms",
- "--bind-addr", apiAddrB,
+ "--bind-addr", "127.0.0.1:0",
"--api-token", apiToken,
)
+ // Resolve actual addresses from logs
+ actualApiAddrA := waitForMCPAddr(t, filepath.Join(homeA, "node.log"))
+ actualApiAddrB := waitForMCPAddr(t, filepath.Join(homeB, "node.log"))
+
// Wait for nodes to start sidecar API
- waitForAPI(t, apiAddrA)
- waitForAPI(t, apiAddrB)
+ waitForAPI(t, actualApiAddrA)
+ waitForAPI(t, actualApiAddrB)
addrA := waitForPeerInfoInLog(t, filepath.Join(homeA, "node.log"))
// Connect Node B to Node A (to ensure they are in same network)
// We use the multiplexed HTTP address for MCP calls too!
- callMCP(t, apiAddrB, "connect_peer", map[string]any{"peer_addr": addrA})
+ callMCP(t, actualApiAddrB, "connect_peer", map[string]any{"peer_addr": addrA})
// Wait for DHT to have peers on Node A
t.Log("Waiting for DHT to have peers on Node A...")
deadline := time.Now().Add(10 * time.Second)
var dhtReady bool
for time.Now().Before(deadline) {
- respData := callMCP(t, apiAddrA, "get_mesh_info", map[string]any{})
+ respData := callMCP(t, actualApiAddrA, "get_mesh_info", map[string]any{})
var data map[string]any
if err := json.Unmarshal([]byte(respData), &data); err == nil {
dhtSize, _ := data["dht_size"].(float64)
@@ -90,7 +92,7 @@ func TestServiceDiscovery(t *testing.T) {
// Agent A registers a service
serviceName := "mcp:github-tools"
- registerService(t, apiAddrA, apiToken, serviceName)
+ registerService(t, actualApiAddrA, apiToken, serviceName)
// Wait for DHT propagation
t.Log("Waiting for DHT propagation...")
@@ -98,7 +100,7 @@ func TestServiceDiscovery(t *testing.T) {
// Agent B queries the DHT via Sidecar API
t.Log("Agent B discovering service...")
- providers := discoverService(t, apiAddrB, apiToken, serviceName)
+ providers := discoverService(t, actualApiAddrB, apiToken, serviceName)
if len(providers) == 0 {
t.Fatalf("Agent B failed to discover any providers for %s", serviceName)
@@ -119,7 +121,7 @@ func TestServiceDiscovery(t *testing.T) {
}
// Agent A unregisters the service
- unregisterService(t, apiAddrA, apiToken, serviceName)
+ unregisterService(t, actualApiAddrA, apiToken, serviceName)
t.Log("Service discovery test passed.")
}