Skip to content

Agent Mesh

api: canonicalize node: binding members to the peer ID form tokens carry

Antonio Ojea9ad7f97b1492
diff --git a/api/policy_rules.go b/api/policy_rules.go
index 092b3622..24ce1b31 100644
--- a/api/policy_rules.go
+++ b/api/policy_rules.go
@@ -20,6 +20,7 @@ import (
"github.com/biscuit-auth/biscuit-go/v2"
"github.com/biscuit-auth/biscuit-go/v2/parser"
+ "github.com/libp2p/go-libp2p/core/peer"
)
// PolicyRule is one mesh policy rule in both the form biscuit-go evaluates
@@ -60,9 +61,21 @@ func BuildPolicyRules(roles []*PolicyRole, bindings []*PolicyBinding) (rules []P
continue
}
parts := strings.SplitN(m, ":", 2)
- if len(parts) == 2 && allowedMemberPrefix[parts[0]] {
- add(roleHead, biscuit.Predicate{Name: parts[0], IDs: []biscuit.Term{biscuit.String(parts[1])}})
+ if len(parts) != 2 || !allowedMemberPrefix[parts[0]] {
+ continue
+ }
+ value := parts[1]
+ // A token carries node() in peer.ID.String() form; an operator may
+ // have written any encoding peer.Decode accepts.
+ if parts[0] == FactNode {
+ id, err := peer.Decode(value)
+ if err != nil {
+ warnings = append(warnings, fmt.Sprintf("Binding member %q is not a peer ID and grants role %s to nobody: %v", m, b.Role, err))
+ continue
+ }
+ value = id.String()
}
+ add(roleHead, biscuit.Predicate{Name: parts[0], IDs: []biscuit.Term{biscuit.String(value)}})
}
}
diff --git a/api/policy_rules_test.go b/api/policy_rules_test.go
index 6f44a550..cd7b82ab 100644
--- a/api/policy_rules_test.go
+++ b/api/policy_rules_test.go
@@ -15,12 +15,26 @@
package api
import (
+ "strings"
"testing"
"github.com/biscuit-auth/biscuit-go/v2/parser"
+ "github.com/libp2p/go-libp2p/core/peer"
)
func TestBuildPolicyRules(t *testing.T) {
+ // A node member written in the CIDv1 encoding peer.Decode also accepts;
+ // the rule must carry the base58 form a token's node() fact uses.
+ const nodeID = "12D3KooWA4Xop1JaT3MHxwYMkCepYsv4iPVopMXwCz5iHYdBfeSB"
+ decoded, err := peer.Decode(nodeID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ nodeCID := peer.ToCid(decoded).String()
+ if nodeCID == nodeID {
+ t.Fatal("test needs a non-canonical encoding")
+ }
+
roles := []*PolicyRole{
{
Name: "test-role",
@@ -37,7 +51,7 @@ func TestBuildPolicyRules(t *testing.T) {
bindings := []*PolicyBinding{
{
Role: "test-role",
- Members: []string{"sam:system:authenticated", "user:alice", "role:admin", "agent:spoofed"},
+ Members: []string{"sam:system:authenticated", "user:alice", "role:admin", "agent:spoofed", "node:" + nodeCID, "node:not-a-peer-id"},
},
}
@@ -46,6 +60,7 @@ func TestBuildPolicyRules(t *testing.T) {
expectedStrings := map[string]bool{
"role(\"test-role\") <- true": false,
"role(\"test-role\") <- user(\"alice\")": false,
+ "role(\"test-role\") <- node(\"" + nodeID + "\")": false,
"granted_service_all_types(true) <- role(\"test-role\")": false,
"granted_service_all(\"mcp\") <- role(\"test-role\")": false,
"granted_service_suffix(\"mcp\", \".suffix\") <- role(\"test-role\")": false,
@@ -78,8 +93,11 @@ func TestBuildPolicyRules(t *testing.T) {
}
}
- if len(warnings) != 2 {
- t.Fatalf("warnings = %q, want one for granted_agent_all and one for the unparseable entry", warnings)
+ if len(warnings) != 3 {
+ t.Fatalf("warnings = %q, want one for granted_agent_all, one for the unparseable entry and one for the bad peer ID", warnings)
+ }
+ if !strings.Contains(strings.Join(warnings, "\n"), `"node:not-a-peer-id"`) {
+ t.Errorf("warnings = %q, want one naming the member that is not a peer ID", warnings)
}
// The text is the contract every member evaluates; it must yield the same