Skip to content

Agent Mesh

security: fail closed and aggregate all policy fact errors during biscuit token generation

Antonio Ojeaa1cc52ee1d5a
diff --git a/cmd/sam-hub/biscuit.go b/cmd/sam-hub/biscuit.go
index 3f638294..9223f50a 100644
--- a/cmd/sam-hub/biscuit.go
+++ b/cmd/sam-hub/biscuit.go
@@ -15,6 +15,7 @@
package main
import (
+ "errors"
"fmt"
"sort"
"strings"
@@ -103,12 +104,14 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
}
sort.Strings(roles)
+ var errs []error
for _, role := range roles {
if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactRole,
IDs: []biscuit.Term{biscuit.String(role)},
}}); err != nil {
- return nil, fmt.Errorf("failed to add role fact: %w", err)
+ errs = append(errs, fmt.Errorf("failed to add role fact for %s: %w", role, err))
+ continue
}
if h.Policy != nil {
@@ -118,7 +121,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
Name: api.FactMCPServer,
IDs: []biscuit.Term{biscuit.String(tool)},
}}); err != nil {
- logger.Errorw("Failed to add MCP tool fact to biscuit", "peer_id", remotePeer, "tool", tool, "error", err)
+ errs = append(errs, fmt.Errorf("failed to add MCP tool fact for %s: %w", tool, err))
}
}
for _, target := range rolePolicy.Network.AllowedTargets {
@@ -126,7 +129,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
Name: api.FactNetworkTarget,
IDs: []biscuit.Term{biscuit.String(target)},
}}); err != nil {
- logger.Errorw("Failed to add network target fact to biscuit", "peer_id", remotePeer, "target", target, "error", err)
+ errs = append(errs, fmt.Errorf("failed to add network target fact for %s: %w", target, err))
}
}
for _, customFact := range rolePolicy.CustomDatalog {
@@ -134,26 +137,34 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
if trimmed == "" {
continue
}
+ var factErr error
func() {
defer func() {
if r := recover(); r != nil {
- logger.Errorw("Panic parsing custom fact", "peer_id", remotePeer, "fact", trimmed, "recover", r)
+ factErr = fmt.Errorf("panic parsing custom fact %q: %v", trimmed, r)
}
}()
fact, err := parser.FromStringFact(trimmed)
if err != nil {
- logger.Errorw("Failed to parse custom fact", "peer_id", remotePeer, "fact", trimmed, "error", err)
+ factErr = fmt.Errorf("failed to parse custom fact %q: %w", trimmed, err)
return
}
if err := builder.AddAuthorityFact(fact); err != nil {
- logger.Errorw("Failed to add custom fact to biscuit", "peer_id", remotePeer, "fact", trimmed, "error", err)
+ factErr = fmt.Errorf("failed to add custom fact %q: %w", trimmed, err)
}
}()
+ if factErr != nil {
+ errs = append(errs, factErr)
+ }
}
}
}
}
+ if len(errs) > 0 {
+ return nil, fmt.Errorf("biscuit policy validation failed: %w", errors.Join(errs...))
+ }
+
t, err := builder.Build()
if err != nil {
return nil, fmt.Errorf("failed to build biscuit: %w", err)
diff --git a/cmd/sam-hub/biscuit_test.go b/cmd/sam-hub/biscuit_test.go
index df9e9b34..4ce1576a 100644
--- a/cmd/sam-hub/biscuit_test.go
+++ b/cmd/sam-hub/biscuit_test.go
@@ -16,6 +16,7 @@ package main
import (
"path/filepath"
+ "strings"
"sync"
"testing"
"time"
@@ -646,3 +647,60 @@ func TestVerifyBiscuit_Concurrent(t *testing.T) {
}
wg.Wait()
}
+
+func TestMintBiscuitToken_ErrorAggregation(t *testing.T) {
+ dir := t.TempDir()
+ dbPath := filepath.Join(dir, "test.db")
+
+ kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() { _ = kr.Close() }()
+
+ hub := &Hub{
+ KeyRing: kr,
+ Policy: &api.PolicyConfig{
+ Roles: map[string]api.RolePolicy{
+ "admin": {
+ CustomDatalog: []string{
+ "invalid-datalog-fact(123", // syntax error
+ "another-bad-fact);", // syntax error
+ },
+ },
+ },
+ },
+ }
+
+ priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ dummyPeer, err := peer.IDFromPrivateKey(priv)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ token := &oidc.IDToken{
+ Expiry: time.Now().Add(1 * time.Hour),
+ }
+ claims := jwt.MapClaims{
+ "sub": "user-123",
+ "roles": []any{"admin"},
+ }
+
+ _, err = hub.mintBiscuitToken(claims, token, dummyPeer)
+ if err == nil {
+ t.Fatal("Expected mintBiscuitToken to fail on custom datalog syntax errors, got nil error")
+ }
+
+ errStr := err.Error()
+ if !strings.Contains(errStr, "failed to parse custom fact") && !strings.Contains(errStr, "panic parsing custom fact") {
+ t.Errorf("Expected error message to contain parse failure info, got: %s", errStr)
+ }
+
+ // Verify that BOTH errors are aggregated in the error message
+ if !strings.Contains(errStr, "invalid-datalog-fact") || !strings.Contains(errStr, "another-bad-fact") {
+ t.Errorf("Expected error to aggregate both failures, got: %s", errStr)
+ }
+}