Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
consolidate constatns
Antonio Ojea07b58511dc6a
diff --git a/api/constants.go b/api/constants.goindex 02e71ebf..8ebb0a4e 100644--- a/api/constants.go+++ b/api/constants.go@@ -19,3 +19,14 @@ import "github.com/libp2p/go-libp2p/core/protocol"const EnrollProtocolID protocol.ID = "/sam/enroll/1.0.0"const MCPProtocolID protocol.ID = "/sam/mcp/1.0.0"const GossipEvents = "/sam/mesh/events/v1"+const AuthProtocolID protocol.ID = "/sam/auth/1.0.0"++// Biscuit fact names+const (+ FactExpiration = "expiration"+ FactNode = "node"+ FactClientPeerID = "client_peer_id"+ FactGroup = "group"+ FactMCPTool = "allow_mcp_tool"+ FactNetworkTarget = "allow_network_target"+)diff --git a/api/policy.go b/api/policy.goindex 4979acfa..2ba0c665 100644--- a/api/policy.go+++ b/api/policy.go@@ -1,13 +1,5 @@package api-const (- // FactMCPTool is the Datalog fact name injected by the Hub to authorize specific MCP tools.- FactMCPTool = "allow_mcp_tool"-- // FactNetworkTarget is the Datalog fact name injected by the Hub to authorize connections to specific agents.- FactNetworkTarget = "allow_network_target"-)-// PolicyConfig is the root authorization configuration for the SAM Hub.type PolicyConfig struct {Version string `yaml:"version"`diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.goindex 1b4af22c..06a6058d 100644--- a/cmd/sam-hub/main.go+++ b/cmd/sam-hub/main.go@@ -64,12 +64,6 @@ const (HighWaterMark = 400ConnGracePeriod = 1 * time.Minute- // Biscuit fact names- FactExpiration = "expiration"- FactNode = "node"- FactClientPeerID = "client_peer_id"- FactGroup = "group"-// DefaultsDefaultOIDCIssuer = "https://accounts.google.com"DefaultMeshName = "public-mesh"@@ -405,21 +399,21 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotebuilder := biscuit.NewBuilder(h.KeyRing.GetCurrentKey())if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: FactExpiration,+ Name: api.FactExpiration,IDs: []biscuit.Term{biscuit.Date(token.Expiry)},}}); err != nil {return nil, fmt.Errorf("failed to add expiration fact: %w", err)}if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: FactNode,+ Name: api.FactNode,IDs: []biscuit.Term{biscuit.String(remotePeer.String())},}}); err != nil {return nil, fmt.Errorf("failed to add node fact: %w", err)}if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: FactClientPeerID,+ Name: api.FactClientPeerID,IDs: []biscuit.Term{biscuit.String(remotePeer.String())},}}); err != nil {return nil, fmt.Errorf("failed to add client_peer_id fact: %w", err)@@ -427,7 +421,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotefor _, role := range roles {if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: FactGroup,+ Name: api.FactGroup,IDs: []biscuit.Term{biscuit.String(role)},}}); err != nil {return nil, fmt.Errorf("failed to add group fact: %w", err)diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 19386b4b..9a99a634 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -216,7 +216,7 @@ func main() {// Start renewal loopnode.StartRenewalLoop(ctx, tokenURLFlag, clientIDFlag, clientSecretFlag, jwtPathFlag)- node.Host.SetStreamHandler(AuthProtocolID, node.HandleAuthHandshake)+ node.Host.SetStreamHandler(api.AuthProtocolID, node.HandleAuthHandshake)// Start MCP ServerstartMCPServer(node, mcpSocketFlag, dataDir)diff --git a/cmd/sam-node/mcp_test.go b/cmd/sam-node/mcp_test.goindex 1143589d..68ecaeb0 100644--- a/cmd/sam-node/mcp_test.go+++ b/cmd/sam-node/mcp_test.go@@ -22,6 +22,7 @@ import (lru "github.com/hashicorp/golang-lru/v2""github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-libp2p/core/peer"+ "github.com/google/sam/api""github.com/libp2p/go-libp2p/core/protocol")@@ -99,8 +100,8 @@ func TestZeroTrustMCPServer(t *testing.T) {pr1, pw1 := io.Pipe()pr2, pw2 := io.Pipe()- serverStream := &mockStream{r: pr1, w: pw2, protocol: protocol.ID("/sam/mcp/1.0.0")}- clientStream := &mockStream{r: pr2, w: pw1, protocol: protocol.ID("/sam/mcp/1.0.0")}+ serverStream := &mockStream{r: pr1, w: pw2, protocol: api.MCPProtocolID}+ clientStream := &mockStream{r: pr2, w: pw1, protocol: api.MCPProtocolID}rl, _ := NewPeerRateLimiter(100)rp, _ := lru.New[string, int64](100)diff --git a/cmd/sam-node/middleware_test.go b/cmd/sam-node/middleware_test.goindex c5dce03d..09a427f6 100644--- a/cmd/sam-node/middleware_test.go+++ b/cmd/sam-node/middleware_test.go@@ -442,7 +442,7 @@ func TestHandleAuthHandshakeCache(t *testing.T) {pr1, pw1 := io.Pipe()- serverStream := &mockStream{r: pr1, w: io.Discard, protocol: protocol.ID("/sam/auth/1.0.0")}+ serverStream := &mockStream{r: pr1, w: io.Discard, protocol: api.AuthProtocolID}go func() {node.HandleAuthHandshake(serverStream)@@ -483,7 +483,7 @@ func TestHandleAuthHandshakeCache(t *testing.T) {node.Store = store2pr5, pw5 := io.Pipe()- serverStream3 := &mockStream{r: pr5, w: io.Discard, protocol: protocol.ID("/sam/auth/1.0.0")}+ serverStream3 := &mockStream{r: pr5, w: io.Discard, protocol: api.AuthProtocolID}go func() {node.HandleAuthHandshake(serverStream3)diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.goindex 4cdf77e8..a95c4051 100644--- a/cmd/sam-node/node.go+++ b/cmd/sam-node/node.go@@ -37,7 +37,6 @@ import ("github.com/libp2p/go-libp2p/core/host""github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-libp2p/core/peer"- "github.com/libp2p/go-libp2p/core/protocol""github.com/libp2p/go-libp2p/p2p/discovery/routing""github.com/libp2p/go-libp2p/p2p/discovery/util""github.com/libp2p/go-libp2p/p2p/net/connmgr"@@ -50,8 +49,6 @@ import ()const (- AuthProtocolID = protocol.ID("/sam/auth/1.0.0")-// Cache sizesRateLimiterSize = 1000RevocationCacheSize = 10000@@ -224,7 +221,7 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.Pgo node.startDiscovery(ctx, meshID, interval)// Layer 3: Open the Lobby Door (Auth Protocol is bypassed by Layer 4)- node.Host.SetStreamHandler(AuthProtocolID, node.HandleAuthHandshake)+ node.Host.SetStreamHandler(api.AuthProtocolID, node.HandleAuthHandshake)// Layer 3: Wire up MCP handler wrapped in middlewarenode.Host.SetStreamHandler(api.MCPProtocolID, node.WithBiscuitAuth(node.HandleMCPStream))diff --git a/tests/integration/fallback_test.go b/tests/integration/fallback_test.goindex 97849ccf..a0aea526 100644--- a/tests/integration/fallback_test.go+++ b/tests/integration/fallback_test.go@@ -35,7 +35,6 @@ import ("github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-libp2p/core/peer"- "github.com/libp2p/go-libp2p/core/protocol""github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr""google.golang.org/protobuf/proto"@@ -262,7 +261,7 @@ func startMockHubDynamic(t *testing.T, pubA, pubB ed25519.PublicKey) (peer.ID, s}var callCount int- h.SetStreamHandler(protocol.ID("/sam/enroll/1.0.0"), func(s network.Stream) {+ h.SetStreamHandler(api.EnrollProtocolID, func(s network.Stream) {defer func() { _ = s.Close() }()callCount++diff --git a/tests/integration/minimal_helpers_test.go b/tests/integration/minimal_helpers_test.goindex aee6dd47..a788ebf0 100644--- a/tests/integration/minimal_helpers_test.go+++ b/tests/integration/minimal_helpers_test.go@@ -30,7 +30,6 @@ import (dht "github.com/libp2p/go-libp2p-kad-dht""github.com/libp2p/go-libp2p/core/network""github.com/libp2p/go-libp2p/core/peer"- "github.com/libp2p/go-libp2p/core/protocol""github.com/libp2p/go-msgio""google.golang.org/protobuf/proto")@@ -104,7 +103,7 @@ func startMockLibp2pHub(t *testing.T) (peer.ID, string) {t.Fatalf("failed to create DHT on mock hub: %v", err)}- h.SetStreamHandler(protocol.ID("/sam/enroll/1.0.0"), func(s network.Stream) {+ h.SetStreamHandler(api.EnrollProtocolID, func(s network.Stream) {defer func() { _ = s.Close() }()// The following constants are mock values used for testing.