Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix(policy): address PR 224 review comments and fix key rotation, logging, and compiler bugs
Antonio Ojeaeba7026d06b2
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex fb4d9440..f69025f0 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -529,7 +529,7 @@ func (s *Server) HandleRefresh(w http.ResponseWriter, r *http.Request) {// Bootstrap nodebBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, nodeRecord.Role, biscuitExpiry)if err != nil {- logger.Errorf("Failed to mint refreshed token for node %s: %v")+ logger.Errorf("Failed to mint refreshed token for node %s: %v", nodeRecord.PeerID, err)http.Error(w, "Internal server error", http.StatusInternalServerError)return}@@ -960,7 +960,7 @@ func (s *Server) HandleEnroll(w http.ResponseWriter, r *http.Request) {// Mode A: Auto-ApprovebiscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(api.BiscuitTokenTTL))if err != nil {- logger.Errorf("Failed to mint bootstrap biscuit: %v")+ logger.Errorf("Failed to mint bootstrap biscuit: %v", err)http.Error(w, "Internal server error", http.StatusInternalServerError)return}@@ -1302,7 +1302,7 @@ func (s *Server) HandleAdminEnrollmentAction(w http.ResponseWriter, r *http.RequbiscuitBytes, err := identity.MintBootstrapBiscuitToken(privKey, pID, tokenRecord.Role, time.Now().Add(api.BiscuitTokenTTL))if err != nil {- logger.Errorf("Failed to mint bootstrap biscuit: %v")+ logger.Errorf("Failed to mint bootstrap biscuit: %v", err)http.Error(w, "Internal server error", http.StatusInternalServerError)return}diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex f724f8e6..1ce8fa8b 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -195,7 +195,6 @@ func VerifyBiscuitAndGetKey(biscuitData []byte, expectedPeer peer.ID, trustedPubbreak} else {lastErr = err- break // Signature is valid, but authorization failed. No need to try other keys.}}diff --git a/internal/node/policy.go b/internal/node/policy.goindex a4ad6d5c..74d42f33 100644--- a/internal/node/policy.go+++ b/internal/node/policy.go@@ -4,6 +4,7 @@ import ("strings""github.com/biscuit-auth/biscuit-go/v2"+ "github.com/biscuit-auth/biscuit-go/v2/parser""github.com/google/sam/api")@@ -15,6 +16,16 @@ func BuildPolicyRules(roles []*api.PolicyRole, bindings []*api.PolicyBinding) []continue}for _, m := range b.Members {+ if m == api.SystemAuthenticated {+ rules = append(rules, biscuit.Rule{+ Head: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(b.Role)},+ },+ Body: []biscuit.Predicate{},+ })+ continue+ }parts := strings.SplitN(m, ":", 2)if len(parts) == 2 {memberType := parts[0]@@ -96,17 +107,42 @@ func BuildPolicyRules(roles []*api.PolicyRole, bindings []*api.PolicyBinding) []}}+ hasUnrestricted := false+ hasSpecificTargets := false+ for _, t := range role.AllowedTargets {+ if t == "*" {+ hasUnrestricted = true+ } else {+ hasSpecificTargets = true+ }+ }++ if hasUnrestricted {+ rules = append(rules, biscuit.Rule{+ Head: biscuit.Predicate{+ Name: api.FactTargetUnrestricted,+ IDs: []biscuit.Term{},+ },+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},+ },+ })+ }++ if hasSpecificTargets {+ rules = append(rules, biscuit.Rule{+ Head: biscuit.Predicate{+ Name: api.FactTargetRestricted,+ IDs: []biscuit.Term{},+ },+ Body: []biscuit.Predicate{+ {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},+ },+ })+ }+for _, t := range role.AllowedTargets {if t == "*" {- rules = append(rules, biscuit.Rule{- Head: biscuit.Predicate{- Name: api.FactTargetUnrestricted,- IDs: []biscuit.Term{},- },- Body: []biscuit.Predicate{- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(roleName)}},- },- })continue}@@ -140,6 +176,25 @@ func BuildPolicyRules(roles []*api.PolicyRole, bindings []*api.PolicyBinding) []})}}++ for _, dl := range role.CustomDatalog {+ trimmed := strings.TrimRight(strings.TrimSpace(dl), ";")+ if trimmed == "" {+ continue+ }+ r, err := parser.FromStringRule(trimmed)+ if err == nil {+ rules = append(rules, r)+ } else {+ f, err := parser.FromStringFact(trimmed)+ if err == nil {+ rules = append(rules, biscuit.Rule{+ Head: f.Predicate,+ Body: []biscuit.Predicate{},+ })+ }+ }+ }}return rulesdiff --git a/internal/node/policy_test.go b/internal/node/policy_test.gonew file mode 100644index 00000000..67d35a1f--- /dev/null+++ b/internal/node/policy_test.go@@ -0,0 +1,81 @@+package node++import (+ "strings"+ "testing"++ "github.com/biscuit-auth/biscuit-go/v2"+ "github.com/google/sam/api"+)++func ruleToString(r biscuit.Rule) string {+ head := r.Head.String()+ if len(r.Body) == 0 {+ return head+ }+ var bodyParts []string+ for _, b := range r.Body {+ bodyParts = append(bodyParts, b.String())+ }+ return head + " <- " + strings.Join(bodyParts, ", ")+}++func TestBuildPolicyRules(t *testing.T) {+ roles := []*api.PolicyRole{+ {+ Name: "test-role",+ AllowedTargets: []string{"*", "node:peer-abc", "custom-fact:custom-val", "legacy-peer"},+ AllowedServices: []string{"*:*", "mcp:*", "mcp:*.suffix", "mcp:prefix.*", "mcp:exact"},+ CustomDatalog: []string{+ "custom_rule($x) <- fact($x);",+ "custom_fact(\"hello\")",+ },+ },+ }+ bindings := []*api.PolicyBinding{+ {+ Role: "test-role",+ Members: []string{"sam:system:authenticated", "user:alice"},+ },+ }++ rules := BuildPolicyRules(roles, bindings)++ expectedStrings := map[string]bool{+ "role(\"test-role\")": false,+ "role(\"test-role\") <- user(\"alice\")": false,+ "granted_service_all_types() <- role(\"test-role\")": false,+ "granted_service_all(\"mcp\") <- role(\"test-role\")": false,+ "granted_service_suffix(\"mcp\", \".suffix\") <- role(\"test-role\")": false,+ "granted_service_prefix(\"mcp\", \"prefix.\") <- role(\"test-role\")": false,+ "granted_service_exact(\"mcp\", \"exact\") <- role(\"test-role\")": false,+ "target_unrestricted() <- role(\"test-role\")": false,+ "target_restricted() <- role(\"test-role\")": false,+ "granted_target_exact(\"node\", \"peer-abc\") <- role(\"test-role\")": false,+ "granted_target_exact(\"custom-fact\", \"custom-val\") <- role(\"test-role\")": false,+ "granted_target_exact(\"node\", \"legacy-peer\") <- role(\"test-role\")": false,+ "custom_rule($x) <- fact($x)": false,+ "custom_fact(\"hello\")": false,+ }++ for _, rule := range rules {+ rStr := ruleToString(rule)+ found := false+ for k := range expectedStrings {+ if rStr == k {+ expectedStrings[k] = true+ found = true+ break+ }+ }+ if !found {+ t.Errorf("Unexpected rule generated: %q", rStr)+ }+ }++ for k, v := range expectedStrings {+ if !v {+ t.Errorf("Expected rule was not generated: %q", k)+ }+ }+}