Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
identity: one role gate and one admission verifier, audit follow-ups
Antonio Ojeab8cd90c18637
diff --git a/api/policy.go b/api/policy.goindex 57edc59f..f9918d49 100644--- a/api/policy.go+++ b/api/policy.go@@ -19,17 +19,6 @@ const (SystemAuthenticated = "sam:system:authenticated")-var (- // ValidMemberPrefixes defines the allowed identity prefixes in policy configuration.- ValidMemberPrefixes = map[string]struct{}{- FactUser: {},- FactGroup: {},- FactEmail: {},- FactNode: {},- FactAgent: {},- }-)-type ServiceConfig struct {Type string `yaml:"type"` // e.g., "mcp", "inference"Name string `yaml:"name"`diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex bf1c737e..fdd09b04 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -35,7 +35,6 @@ import ("sync""time"- "github.com/biscuit-auth/biscuit-go/v2""github.com/biscuit-auth/biscuit-go/v2/parser""github.com/coreos/go-oidc/v3/oidc"jwt "github.com/golang-jwt/jwt/v5"@@ -996,24 +995,9 @@ func (s *Server) HandleRouterLease(w http.ResponseWriter, r *http.Request) {return}- // Enforce role("router") or role("bootstrap") inside the biscuit- authorizer, err := b.Authorizer(verifyingKey, identity.AuthorizerOptions(s.config.BiscuitTimeout)...)- if err != nil {- http.Error(w, "Internal authorizer error", http.StatusInternalServerError)- return- }-- authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{- {- Body: []biscuit.Predicate{- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}},- },- },- }})- authorizer.AddPolicy(api.AllowIfTruePolicy)-- if err := authorizer.Authorize(); err != nil {- logger.Warnf("Router %s lacks router role in its biscuit: %v\nWorld state:\n%s", canonical, err, authorizer.PrintWorld())+ // Enforce role("router") inside the biscuit+ if err := identity.RequireRole(b, verifyingKey, api.RoleRouter, s.config.BiscuitTimeout); err != nil {+ logger.Warnf("Router %s lacks router role in its biscuit: %v", canonical, err)http.Error(w, "Unauthorized: entity is not a router", http.StatusForbidden)return}diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.goindex 7971d857..11384100 100644--- a/internal/controlplane/server_test.go+++ b/internal/controlplane/server_test.go@@ -1332,6 +1332,83 @@ func TestRouterLeaseRevocation(t *testing.T) {}}+// TestRouterLeaseUnderRotatedKey covers the rotation grace window on+// /routers/lease: both keys are valid, the retiring one is listed first, and+// the biscuit is signed by the new one, so the role check has to run under the+// key that verified it rather than the first key in the ring.+func TestRouterLeaseUnderRotatedKey(t *testing.T) {+ issuer, _ := startCustomMockOIDC(t)+ srv, store, baseURL := setupTestServer(t, issuer)+ defer func() {+ _ = srv.Close()+ _ = store.Close()+ }()+ ctx := context.Background()++ newPub, newPriv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ if err := store.RotateKeys(ctx, newPriv, newPub, time.Hour); err != nil {+ t.Fatal(err)+ }+ valid, err := store.GetAllValidKeys(ctx)+ if err != nil {+ t.Fatal(err)+ }+ if len(valid) != 2 || bytes.Equal(valid[0].Public, newPub) {+ t.Fatalf("keyring is not [retiring, new]: %d keys", len(valid))+ }++ priv, pub, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ routerPeer, err := peer.IDFromPrivateKey(priv)+ if err != nil {+ t.Fatal(err)+ }+ pubBytes, err := crypto.MarshalPublicKey(pub)+ if err != nil {+ t.Fatal(err)+ }+ routerBiscuit, err := identity.MintBootstrapBiscuitToken(newPriv, routerPeer, api.RoleRouter, time.Now().Add(time.Hour), nil, nil)+ if err != nil {+ t.Fatal(err)+ }+ if err := store.EnrollNode(ctx, &storage.EnrolledNode{+ PeerID: routerPeer.String(),+ PublicKey: pubBytes,+ Biscuit: routerBiscuit,+ Role: api.RoleRouter,+ EnrollmentType: "BOOTSTRAP",+ EnrolledAt: time.Now(),+ }); err != nil {+ t.Fatal(err)+ }++ leaseData, err := proto.Marshal(&api.RouterLeaseRequest{+ PeerId: routerPeer.String(),+ Addresses: []string{"/ip4/127.0.0.1/tcp/4001/p2p/" + routerPeer.String()},+ Biscuit: routerBiscuit,+ })+ if err != nil {+ t.Fatal(err)+ }+ resp, err := (&http.Client{Timeout: 5 * time.Second}).Post(baseURL+"/routers/lease", "application/x-protobuf", bytes.NewReader(leaseData))+ if err != nil {+ t.Fatal(err)+ }+ body, err := io.ReadAll(resp.Body)+ _ = resp.Body.Close()+ if err != nil {+ t.Fatal(err)+ }+ if resp.StatusCode != http.StatusOK {+ t.Fatalf("lease with biscuit signed by the new key: got %d (%s), want 200", resp.StatusCode, body)+ }+}+// enrollRefreshTestNode enrolls a bootstrap node directly in the store and// returns its key and currently issued biscuit, ready to drive /refresh.func enrollRefreshTestNode(t *testing.T, ctx context.Context, store storage.Store) (crypto.PrivKey, []byte) {diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex 02067c5e..5ea57d39 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -91,9 +91,8 @@ func RequireAuthorityBinding(b *biscuit.Biscuit, expectedPeer peer.ID) error {return nil}-// ExpirationOf reports the expiration() fact of an already-authorized token, for-// callers that cache an admission decision and must later know when it lapses.-func ExpirationOf(authorizer biscuit.Authorizer) (time.Time, error) {+// expirationOf reports the expiration() fact of an already-authorized token.+func expirationOf(authorizer biscuit.Authorizer) (time.Time, error) {facts, err := authorizer.Query(biscuit.Rule{Head: biscuit.Predicate{Name: "get_exp", IDs: []biscuit.Term{biscuit.Variable("e")}},Body: []biscuit.Predicate{{Name: api.FactExpiration, IDs: []biscuit.Term{biscuit.Variable("e")}}},@@ -325,33 +324,47 @@ func mintBiscuit(signingKey ed25519.PrivateKey, remotePeer peer.ID, roles []stri// 2. The token is not expired.// 3. The token is securely bound to the expected remotePeer.func VerifyBiscuit(biscuitData []byte, expectedPeer peer.ID, trustedPublicKeys []ed25519.PublicKey, timeout time.Duration) (*biscuit.Biscuit, error) {- b, _, err := VerifyBiscuitAndGetKey(biscuitData, expectedPeer, trustedPublicKeys, timeout)+ b, _, _, err := verifyBiscuit(biscuitData, expectedPeer, trustedPublicKeys, timeout)return b, err}+// VerifyBiscuitAndGetKey is VerifyBiscuit that also reports which trusted key+// verified the token, for callers that go on to evaluate it under that key.func VerifyBiscuitAndGetKey(biscuitData []byte, expectedPeer peer.ID, trustedPublicKeys []ed25519.PublicKey, timeout time.Duration) (*biscuit.Biscuit, ed25519.PublicKey, error) {+ b, key, _, err := verifyBiscuit(biscuitData, expectedPeer, trustedPublicKeys, timeout)+ return b, key, err+}++// VerifyBiscuitAndGetExpiry is VerifyBiscuit that also reports when the token+// lapses, for callers that cache the admission and must drop it on time.+func VerifyBiscuitAndGetExpiry(biscuitData []byte, expectedPeer peer.ID, trustedPublicKeys []ed25519.PublicKey, timeout time.Duration) (time.Time, error) {+ _, _, expiry, err := verifyBiscuit(biscuitData, expectedPeer, trustedPublicKeys, timeout)+ return expiry, err+}++func verifyBiscuit(biscuitData []byte, expectedPeer peer.ID, trustedPublicKeys []ed25519.PublicKey, timeout time.Duration) (*biscuit.Biscuit, ed25519.PublicKey, time.Time, error) {b, err := biscuit.Unmarshal(biscuitData)if err != nil {- return nil, nil, fmt.Errorf("malformed biscuit: %w", err)+ return nil, nil, time.Time{}, fmt.Errorf("malformed biscuit: %w", err)}authOpts := AuthorizerOptions(timeout)var lastErr error- var authorized bool+ var authorizer biscuit.Authorizervar verifyingKey ed25519.PublicKeyfor _, pubKey := range trustedPublicKeys {- authorizer, err := b.Authorizer(pubKey, authOpts...)+ candidate, err := b.Authorizer(pubKey, authOpts...)if err != nil {lastErr = errcontinue}- EnforceExpiration(authorizer)- authorizer.AddPolicy(api.AllowIfTruePolicy)+ EnforceExpiration(candidate)+ candidate.AddPolicy(api.AllowIfTruePolicy)- if err := authorizer.Authorize(); err == nil {- authorized = true+ if err := candidate.Authorize(); err == nil {+ authorizer = candidateverifyingKey = pubKeybreak} else {@@ -359,15 +372,22 @@ func VerifyBiscuitAndGetKey(biscuitData []byte, expectedPeer peer.ID, trustedPub}}- if !authorized {- return nil, nil, fmt.Errorf("no valid key found for verification: %v", lastErr)+ if authorizer == nil {+ return nil, nil, time.Time{}, fmt.Errorf("no valid key found for verification: %v", lastErr)}if err := RequireAuthorityBinding(b, expectedPeer); err != nil {- return nil, nil, err+ return nil, nil, time.Time{}, err}- return b, verifyingKey, nil+ // EnforceExpiration already passed, so the fact is there; a failure here is+ // a query error, and fails closed rather than caching an admission forever.+ expiry, err := expirationOf(authorizer)+ if err != nil {+ return nil, nil, time.Time{}, err+ }++ return b, verifyingKey, expiry, nil}func translateClaimsToFacts(addFact func(biscuit.Fact) error, claims map[string]any) error {@@ -487,18 +507,16 @@ func extractPeerID(trustedPublicKeys []ed25519.PublicKey, biscuitData []byte, ti}// Extract the peer ID using Datalog query- peerRule, err := parser.FromStringRule(`get_peer($p) <- node($p)`)- if err != nil {- return "", fmt.Errorf("failed to parse query rule: %w", err)- }-- facts, err := authorizer.Query(peerRule)+ facts, err := authorizer.Query(biscuit.Rule{+ Head: biscuit.Predicate{Name: "get_peer", IDs: []biscuit.Term{biscuit.Variable("p")}},+ Body: []biscuit.Predicate{{Name: api.FactNode, IDs: []biscuit.Term{biscuit.Variable("p")}}},+ })if err != nil {return "", fmt.Errorf("query failed: %w", err)}if len(facts) == 0 {- return "", fmt.Errorf("no node fact found in biscuit. Authorizer state: %s", authorizer.PrintWorld())+ return "", fmt.Errorf("no %s fact found in biscuit. Authorizer state: %s", api.FactNode, authorizer.PrintWorld())}// Extract value from fact@@ -531,8 +549,15 @@ func VerifyBiscuitRole(biscuitData []byte, controlPlanePubKey ed25519.PublicKey,if err != nil {return fmt.Errorf("malformed biscuit: %w", err)}+ return RequireRole(b, controlPlanePubKey, expectedRole, timeout)+}- authorizer, err := b.Authorizer(controlPlanePubKey, AuthorizerOptions(timeout)...)+// RequireRole checks that the token carries role(expectedRole) under the given+// key. It checks nothing else: a token received from a peer must already have+// passed VerifyBiscuitAndGetKey, which is where expiry and the peer binding are+// enforced, and key must be the key that verified it.+func RequireRole(b *biscuit.Biscuit, key ed25519.PublicKey, expectedRole string, timeout time.Duration) error {+ authorizer, err := b.Authorizer(key, AuthorizerOptions(timeout)...)if err != nil {return fmt.Errorf("failed to create authorizer: %w", err)}diff --git a/internal/identity/biscuit_test.go b/internal/identity/biscuit_test.goindex a0e4d5e4..aef24b6b 100644--- a/internal/identity/biscuit_test.go+++ b/internal/identity/biscuit_test.go@@ -656,3 +656,111 @@ func TestVerifyBiscuitRole_TimeoutIsHonored(t *testing.T) {t.Errorf("expected datalog timeout error, got %v", err)}}++// TestRequireRole covers the role gate every router-only path shares. The+// appended-block case is the one that matters: appending needs no root key, so+// a node could otherwise promote itself to router by attenuating its own token.+func TestRequireRole(t *testing.T) {+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ otherPub, _, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }++ data, err := MintBootstrapBiscuitToken(priv, newTestPeer(t), api.RoleNode, time.Now().Add(time.Hour), nil, nil)+ if err != nil {+ t.Fatal(err)+ }+ token, err := biscuit.Unmarshal(data)+ if err != nil {+ t.Fatal(err)+ }++ if err := RequireRole(token, pub, api.RoleNode, testTimeout); err != nil {+ t.Errorf("granted role rejected: %v", err)+ }+ if err := RequireRole(token, pub, api.RoleRouter, testTimeout); err == nil {+ t.Error("missing role accepted")+ }+ if err := RequireRole(token, otherPub, api.RoleNode, testTimeout); err == nil {+ t.Error("token accepted under a key that did not sign it")+ }++ block := token.CreateBlock()+ if err := block.AddFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: api.FactRole,+ IDs: []biscuit.Term{biscuit.String(api.RoleRouter)},+ }}); err != nil {+ t.Fatal(err)+ }+ promoted, err := token.Append(rand.Reader, block.Build())+ if err != nil {+ t.Fatal(err)+ }+ if err := RequireRole(promoted, pub, api.RoleRouter, testTimeout); err == nil {+ t.Error("role from an appended block satisfied the check")+ }+}++// TestVerifyBiscuitAndGetExpiry pins the instant the peer-admission cache is+// keyed on: the token's own expiration, and with several the earliest, since+// that is the one the expiry check starts failing on.+func TestVerifyBiscuitAndGetExpiry(t *testing.T) {+ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }+ peerID := newTestPeer(t)+ keys := []ed25519.PublicKey{pub}++ mint := func(expirations ...time.Time) []byte {+ builder := biscuit.NewBuilder(priv)+ mustAddAuthorityFact(t, builder, api.FactNode, biscuit.String(peerID.String()))+ for _, e := range expirations {+ mustAddAuthorityFact(t, builder, api.FactExpiration, biscuit.Date(e))+ }+ token, err := builder.Build()+ if err != nil {+ t.Fatal(err)+ }+ data, err := token.Serialize()+ if err != nil {+ t.Fatal(err)+ }+ return data+ }+ // Biscuit dates are whole seconds.+ sameInstant := func(got, want time.Time) bool { return got.Sub(want).Abs() <= time.Second }++ soon := time.Now().Add(time.Hour)+ later := soon.Add(time.Hour)++ got, err := VerifyBiscuitAndGetExpiry(mint(soon), peerID, keys, testTimeout)+ if err != nil {+ t.Fatalf("valid token rejected: %v", err)+ }+ if !sameInstant(got, soon) {+ t.Errorf("expiry = %v, want %v", got, soon)+ }++ got, err = VerifyBiscuitAndGetExpiry(mint(later, soon), peerID, keys, testTimeout)+ if err != nil {+ t.Fatalf("token with two expirations rejected: %v", err)+ }+ if !sameInstant(got, soon) {+ t.Errorf("expiry = %v, want the earlier %v", got, soon)+ }++ if _, err := VerifyBiscuitAndGetExpiry(mint(time.Now().Add(-time.Hour)), peerID, keys, testTimeout); err == nil {+ t.Error("expired token admitted")+ }+ if _, err := VerifyBiscuitAndGetExpiry(mint(), peerID, keys, testTimeout); err == nil {+ t.Error("token without an expiration admitted")+ }+ if _, err := VerifyBiscuitAndGetExpiry(mint(soon), newTestPeer(t), keys, testTimeout); err == nil {+ t.Error("token bound to another peer admitted")+ }+}diff --git a/internal/node/enroll_test.go b/internal/node/enroll_test.goindex 3f57f14f..d104d72a 100644--- a/internal/node/enroll_test.go+++ b/internal/node/enroll_test.go@@ -71,7 +71,16 @@ func startMockRouterWithKey(t *testing.T, cpPriv ed25519.PrivateKey, role stringh.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {defer func() { _ = s.Close() }()- data, _ := proto.Marshal(&api.AuthResponse{Success: true, Biscuit: routerBiscuit})+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ reader.ReleaseMsg(msg)+ data, err := proto.Marshal(&api.AuthResponse{Success: true, Biscuit: routerBiscuit})+ if err != nil {+ return+ }_ = msgio.NewVarintWriter(s).WriteMsg(data)})diff --git a/internal/node/node.go b/internal/node/node.goindex 6ca1b088..f90bb569 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -939,21 +939,7 @@ func (n *SamNode) performRouterAuthHandshake(s network.Stream, biscuitBytes []by// Enforce role("router") inside the biscuit, under the key that verified:// with several valid keys loaded (rotation grace) the first is not// necessarily the signer.- authorizer, err := b.Authorizer(verifyingKey, identity.AuthorizerOptions(n.BiscuitTimeout)...)- if err != nil {- return false, fmt.Errorf("authorizer instantiation failed: %w", err)- }-- authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{- {- Body: []biscuit.Predicate{- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}},- },- },- }})- authorizer.AddPolicy(api.AllowIfTruePolicy)-- if err := authorizer.Authorize(); err != nil {+ if err := identity.RequireRole(b, verifyingKey, api.RoleRouter, n.BiscuitTimeout); err != nil {return false, fmt.Errorf("%w: remote peer lacks router authorization role: %w", ErrFatalAuth, err)}@@ -1592,29 +1578,14 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {return}- b, verifyingKey, err := identity.VerifyBiscuitAndGetKey(exchange.Biscuit, remotePeer, n.getTrustedPublicKeys(), n.BiscuitTimeout)+ // One verifier call enforces signature, expiry and the authority-block peer+ // binding, and reports when the admission lapses.+ expiry, err := identity.VerifyBiscuitAndGetExpiry(exchange.Biscuit, remotePeer, n.getTrustedPublicKeys(), n.BiscuitTimeout)if err != nil {logger.Warnf("[AuthN] Authorization failed for %s: %v", remotePeer, err)return}- // 4. Enforce hardware binding: token must include node(<remotePeerID>)- if err := identity.RequireAuthorityBinding(b, remotePeer); err != nil {- logger.Warnf("[AuthN] %v", err)- return- }-- expiry := time.Now().Add(n.BiscuitTimeout)- if authorizer, authErr := b.Authorizer(verifyingKey, identity.AuthorizerOptions(n.BiscuitTimeout)...); authErr == nil {- identity.EnforceExpiration(authorizer)- authorizer.AddPolicy(api.AllowIfTruePolicy)- if authErr := authorizer.Authorize(); authErr == nil {- if e, expErr := identity.ExpirationOf(authorizer); expErr == nil {- expiry = e- }- }- }-n.authPeers.Store(remotePeer, expiry)logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)diff --git a/internal/node/node_test.go b/internal/node/node_test.goindex 0877fed9..aa6c0430 100644--- a/internal/node/node_test.go+++ b/internal/node/node_test.go@@ -17,6 +17,7 @@ package nodeimport ("context""crypto/ed25519"+ "errors""os""os/exec""testing"@@ -26,9 +27,12 @@ import ("github.com/google/sam/api""github.com/google/sam/internal/identity"lru "github.com/hashicorp/golang-lru/v2"+ "github.com/libp2p/go-libp2p""github.com/libp2p/go-libp2p/core/crypto""github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-msgio""github.com/multiformats/go-multiaddr"+ "google.golang.org/protobuf/proto")func TestAnnounceFilter(t *testing.T) {@@ -307,20 +311,40 @@ func TestPruneTrustedKeys(t *testing.T) {}}-// TestVerifyBiscuitRejectsExpiredToken covers the peer-admission half of #296:-// HandleAuthHandshake admits a peer into authPeers, which the relay ACL then-// trusts, so it must reject an expired token exactly like the dataplane does.-func TestVerifyBiscuitRejectsExpiredToken(t *testing.T) {+// TestHandleAuthHandshake covers the admission office: the relay ACL trusts+// authPeers, so the cached instant must be the token's own expiry, and a token+// that is expired or bound to another peer must leave no entry at all (#296).+func TestHandleAuthHandshake(t *testing.T) {pub, priv, err := ed25519.GenerateKey(nil)if err != nil {t.Fatal(err)}- remotePeer := peer.ID("dummy-peer")+ ctx := context.Background()- mint := func(expiration time.Time) []byte {+ serverHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = serverHost.Close() }()+ clientHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = clientHost.Close() }()++ node := &SamNode{+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},+ BiscuitTimeout: time.Second,+ }+ serverHost.SetStreamHandler(api.AuthProtocolID, node.HandleAuthHandshake)+ if err := clientHost.Connect(ctx, peer.AddrInfo{ID: serverHost.ID(), Addrs: serverHost.Addrs()}); err != nil {+ t.Fatal(err)+ }++ mint := func(boundTo peer.ID, expiration time.Time) []byte {builder := biscuit.NewBuilder(priv)for _, f := range []biscuit.Fact{- {Predicate: biscuit.Predicate{Name: api.FactNode, IDs: []biscuit.Term{biscuit.String(remotePeer.String())}}},+ {Predicate: biscuit.Predicate{Name: api.FactNode, IDs: []biscuit.Term{biscuit.String(boundTo.String())}}},{Predicate: biscuit.Predicate{Name: api.FactExpiration, IDs: []biscuit.Term{biscuit.Date(expiration)}}},} {if err := builder.AddAuthorityFact(f); err != nil {@@ -338,34 +362,131 @@ func TestVerifyBiscuitRejectsExpiredToken(t *testing.T) {return data}- node := &SamNode{- trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},- BiscuitTimeout: 500 * time.Millisecond,+ // handshake presents token and returns the node's answer; a rejected peer+ // gets the stream closed on it with no answer at all.+ handshake := func(token []byte) (*api.AuthResponse, error) {+ s, err := clientHost.NewStream(ctx, serverHost.ID(), api.AuthProtocolID)+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = s.Close() }()+ if err := s.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {+ t.Fatal(err)+ }+ frame, err := proto.Marshal(&api.AuthFrame{Biscuit: token})+ if err != nil {+ t.Fatal(err)+ }+ if err := msgio.NewVarintWriter(s).WriteMsg(frame); err != nil {+ t.Fatal(err)+ }+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return nil, err+ }+ defer reader.ReleaseMsg(msg)+ var resp api.AuthResponse+ if err := proto.Unmarshal(msg, &resp); err != nil {+ t.Fatal(err)+ }+ return &resp, nil}want := time.Now().Add(time.Hour)- trustedKeys := node.getTrustedPublicKeys()- b, verifyingKey, err := identity.VerifyBiscuitAndGetKey(mint(want), remotePeer, trustedKeys, node.BiscuitTimeout)+ resp, err := handshake(mint(clientHost.ID(), want))if err != nil {- t.Fatalf("valid token rejected: %v", err)- }- expiry := time.Now().Add(node.BiscuitTimeout)- if authorizer, authErr := b.Authorizer(verifyingKey, identity.AuthorizerOptions(node.BiscuitTimeout)...); authErr == nil {- identity.EnforceExpiration(authorizer)- authorizer.AddPolicy(api.AllowIfTruePolicy)- if authErr := authorizer.Authorize(); authErr == nil {- if e, expErr := identity.ExpirationOf(authorizer); expErr == nil {- expiry = e- }- }+ t.Fatalf("valid token got no answer: %v", err)+ }+ if !resp.Success {+ t.Fatalf("valid token rejected: %s", resp.Error)+ }+ v, ok := node.authPeers.Load(clientHost.ID())+ if !ok {+ t.Fatal("admitted peer not recorded in authPeers")+ }+ expiry, ok := v.(time.Time)+ if !ok {+ t.Fatalf("authPeers holds %T, want time.Time", v)}// The admission is cached against this instant, so it has to be the token's.- if skew := expiry.Sub(want); skew < -time.Second || skew > time.Second {- t.Errorf("reported expiry %v, want ~%v", expiry, want)+ if skew := expiry.Sub(want).Abs(); skew > time.Second {+ t.Errorf("cached expiry %v, want ~%v", expiry, want)+ }++ for name, token := range map[string][]byte{+ "expired": mint(clientHost.ID(), time.Now().Add(-time.Hour)),+ "bound to another peer": mint(serverHost.ID(), want),+ } {+ node.authPeers.Delete(clientHost.ID())+ if resp, err := handshake(token); err == nil {+ t.Errorf("%s: token answered with %+v, want the stream closed", name, resp)+ }+ if _, admitted := node.authPeers.Load(clientHost.ID()); admitted {+ t.Errorf("%s: peer admitted", name)+ }+ }+}++// TestPerformRouterAuthHandshakeRequiresRouterRole: a router whose biscuit+// verifies but carries no role("router") is a fatal auth failure, so the node+// gives up on it instead of retrying.+func TestPerformRouterAuthHandshakeRequiresRouterRole(t *testing.T) {+ cpPub, cpPriv, err := ed25519.GenerateKey(nil)+ if err != nil {+ t.Fatal(err)+ }+ ctx := context.Background()++ clientHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = clientHost.Close() }()++ node := &SamNode{+ trustedKeys: []TrustedKey{{Key: cpPub, ReceivedAt: time.Now()}},+ BiscuitTimeout: time.Second,}- if _, _, err := identity.VerifyBiscuitAndGetKey(mint(time.Now().Add(-time.Hour)), remotePeer, trustedKeys, node.BiscuitTimeout); err == nil {- t.Fatal("expired token admitted on the peer-authentication path")+ for _, tt := range []struct {+ role string+ wantOK bool+ }{+ {api.RoleRouter, true},+ {api.RoleNode, false},+ } {+ t.Run(tt.role, func(t *testing.T) {+ info, err := peer.AddrInfoFromString(startMockRouterWithKey(t, cpPriv, tt.role))+ if err != nil {+ t.Fatal(err)+ }+ if err := clientHost.Connect(ctx, *info); err != nil {+ t.Fatal(err)+ }+ s, err := clientHost.NewStream(ctx, info.ID, api.AuthProtocolID)+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = s.Close() }()+ if err := s.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {+ t.Fatal(err)+ }++ ok, err := node.performRouterAuthHandshake(s, []byte("node-biscuit"), info.ID)+ if tt.wantOK {+ if err != nil || !ok {+ t.Fatalf("router with role %q rejected: ok=%v err=%v", tt.role, ok, err)+ }+ return+ }+ if err == nil || ok {+ t.Fatalf("router with role %q accepted", tt.role)+ }+ if !errors.Is(err, ErrFatalAuth) {+ t.Errorf("got %v, want ErrFatalAuth", err)+ }+ })}}diff --git a/internal/router/router.go b/internal/router/router.goindex 8e7797e2..781f8f4e 100644--- a/internal/router/router.go+++ b/internal/router/router.go@@ -35,7 +35,6 @@ import ("sync/atomic""time"- "github.com/biscuit-auth/biscuit-go/v2""github.com/google/sam/api""github.com/google/sam/internal/identity"golog "github.com/ipfs/go-log/v2"@@ -1142,21 +1141,7 @@ func (r *Router) performMutualAuth(s network.Stream) error {}// Enforce required role inside the biscuit- authorizer, err := b.Authorizer(verifyingKey, identity.AuthorizerOptions(r.config.BiscuitTimeout)...)- if err != nil {- return fmt.Errorf("authorizer instantiation failed: %w", err)- }-- authorizer.AddCheck(biscuit.Check{Queries: []biscuit.Rule{- {- Body: []biscuit.Predicate{- {Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(r.config.RequiredRole)}},- },- },- }})- authorizer.AddPolicy(api.AllowIfTruePolicy)-- if err := authorizer.Authorize(); err != nil {+ if err := identity.RequireRole(b, verifyingKey, r.config.RequiredRole, r.config.BiscuitTimeout); err != nil {return fmt.Errorf("remote peer lacks router authorization role: %w", err)}diff --git a/internal/router/router_test.go b/internal/router/router_test.goindex 71fe0628..6ce11b50 100644--- a/internal/router/router_test.go+++ b/internal/router/router_test.go@@ -805,10 +805,12 @@ func TestRouterDefaultBiscuitTimeout(t *testing.T) {}}-// TestPerformMutualAuthAcceptsRotatedKey covers the key rotation window: the-// remote biscuit is signed by the second trusted key, so the role authorizer-// must be built from the key that actually verified, not trustedKeys[0].-func TestPerformMutualAuthAcceptsRotatedKey(t *testing.T) {+// TestPerformMutualAuth covers the two decisions the client side makes about+// the remote biscuit. Rotated key: the biscuit is signed by the second trusted+// key, so the role check must run under the key that verified, not+// trustedKeys[0]. Missing role: a valid biscuit that lacks the required role+// is refused and the peer is not recorded.+func TestPerformMutualAuth(t *testing.T) {oldPub, _, err := ed25519.GenerateKey(nil)if err != nil {t.Fatal(err)@@ -818,59 +820,84 @@ func TestPerformMutualAuthAcceptsRotatedKey(t *testing.T) {t.Fatal(err)}- serverHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))- if err != nil {- t.Fatal(err)- }- defer func() { _ = serverHost.Close() }()-- clientHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))- if err != nil {- t.Fatal(err)- }- defer func() { _ = clientHost.Close() }()-- serverBiscuit, err := identity.MintBootstrapBiscuitToken(newPriv, serverHost.ID(), api.RoleRouter, time.Now().Add(time.Hour), nil, nil)- if err != nil {- t.Fatal(err)- }-- serverHost.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {- defer func() { _ = s.Close() }()- reader := msgio.NewVarintReaderSize(s, 1024*64)- msg, err := reader.ReadMsg()- if err != nil {- return- }- reader.ReleaseMsg(msg)- respBytes, _ := proto.Marshal(&api.AuthResponse{Success: true, Biscuit: serverBiscuit})- _ = msgio.NewVarintWriter(s).WriteMsg(respBytes)- })-- r := &Router{- Host: clientHost,- biscuitToken: []byte("client-biscuit"),- trustedPublicKeys: []ed25519.PublicKey{oldPub, newPub},- config: Options{- BiscuitTimeout: time.Second,- RequiredRole: api.RoleRouter,- },- }-- if err := clientHost.Connect(context.Background(), peer.AddrInfo{ID: serverHost.ID(), Addrs: serverHost.Addrs()}); err != nil {- t.Fatal(err)- }- s, err := clientHost.NewStream(context.Background(), serverHost.ID(), api.AuthProtocolID)- if err != nil {- t.Fatal(err)- }- defer func() { _ = s.Close() }()-- if err := r.performMutualAuth(s); err != nil {- t.Fatalf("mutual auth failed with biscuit signed by rotated key: %v", err)- }- if _, ok := r.authenticatedPeers.Load(serverHost.ID()); !ok {- t.Fatal("peer not recorded as authenticated")+ for _, tt := range []struct {+ name string+ serverRole string+ wantOK bool+ }{+ {"rotated key", api.RoleRouter, true},+ {"missing role", api.RoleNode, false},+ } {+ t.Run(tt.name, func(t *testing.T) {+ serverHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = serverHost.Close() }()++ clientHost, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = clientHost.Close() }()++ serverBiscuit, err := identity.MintBootstrapBiscuitToken(newPriv, serverHost.ID(), tt.serverRole, time.Now().Add(time.Hour), nil, nil)+ if err != nil {+ t.Fatal(err)+ }++ serverHost.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer func() { _ = s.Close() }()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ reader.ReleaseMsg(msg)+ respBytes, err := proto.Marshal(&api.AuthResponse{Success: true, Biscuit: serverBiscuit})+ if err != nil {+ return+ }+ _ = msgio.NewVarintWriter(s).WriteMsg(respBytes)+ })++ r := &Router{+ Host: clientHost,+ biscuitToken: []byte("client-biscuit"),+ trustedPublicKeys: []ed25519.PublicKey{oldPub, newPub},+ config: Options{+ BiscuitTimeout: time.Second,+ RequiredRole: api.RoleRouter,+ },+ }++ if err := clientHost.Connect(context.Background(), peer.AddrInfo{ID: serverHost.ID(), Addrs: serverHost.Addrs()}); err != nil {+ t.Fatal(err)+ }+ s, err := clientHost.NewStream(context.Background(), serverHost.ID(), api.AuthProtocolID)+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = s.Close() }()++ err = r.performMutualAuth(s)+ _, recorded := r.authenticatedPeers.Load(serverHost.ID())+ if tt.wantOK {+ if err != nil {+ t.Fatalf("mutual auth failed: %v", err)+ }+ if !recorded {+ t.Fatal("peer not recorded as authenticated")+ }+ return+ }+ if err == nil {+ t.Fatal("peer without the required role accepted")+ }+ if recorded {+ t.Fatal("rejected peer recorded as authenticated")+ }+ })}}diff --git a/site/content/docs/agent-architecture.md b/site/content/docs/agent-architecture.mdindex da595643..be383924 100644--- a/site/content/docs/agent-architecture.md+++ b/site/content/docs/agent-architecture.md@@ -618,7 +618,6 @@ machinery, because the existing vocabulary is already shaped for it:`agent` a first-class principal is:* `FactAgent = "agent"` in `api/datalog.go`;-* `FactAgent` added to `ValidMemberPrefixes` in `api/policy.go`;* the agent block's `agent(...)` fact injected as a `target_fact` atauthorization time.@@ -716,7 +715,7 @@ blocks in `biscuit-go` to be attributable. It is a clean upgrade, not a rewrite.### 8.7 Consequences for the plan-* `api/` — `FactAgent`, `ValidMemberPrefixes`, the §8.8 translation helpers, and+* `api/` — `FactAgent`, the §8.8 translation helpers, andthe §10 bundle schema.* `internal/node` — **nothing** for authorization: appended blocks are alreadyverified and evaluated. Only the namespace-binding policy is new.