Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
identity: centralize biscuit expiry enforcement and apply it on peer admission
Antonio Ojeaaeea47155bec
diff --git a/api/datalog.go b/api/datalog.goindex a105a954..d10134cb 100644--- a/api/datalog.go+++ b/api/datalog.go@@ -253,7 +253,9 @@ var (// TargetFactRules maps node and OIDC claims to target_fact datalog facts.TargetFactRules []biscuit.Rule- // ControlPlaneStaticTimeCheck is the standard check for verifying OIDC token expiration.+ // ControlPlaneStaticTimeCheck is the standard check for verifying a Biscuit's+ // own expiration() fact. Every path that admits a token must add it together+ // with a FactTime fact; see identity.EnforceExpiration.ControlPlaneStaticTimeCheck biscuit.Check// AllowIfTruePolicy is the static policy "allow if true" used during token verification.diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex 2b2de055..49455d0b 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -47,6 +47,21 @@ func AuthorizerOptions(timeout time.Duration) []biscuit.AuthorizerOption {return []biscuit.AuthorizerOption{biscuit.WithWorldOptions(datalog.WithMaxDuration(timeout))}}+// EnforceExpiration injects the current time and the expiration check into an+// authorizer. Every path that admits a biscuit must call this: expiry is a+// Datalog check over a time fact, so an authorizer built without the fact+// silently accepts expired tokens (see #296). A token carrying no expiration()+// fact fails the check, so this is fail-closed.+func EnforceExpiration(authorizer biscuit.Authorizer) {+ authorizer.AddFact(biscuit.Fact{+ Predicate: biscuit.Predicate{+ Name: api.FactTime,+ IDs: []biscuit.Term{biscuit.Date(time.Now())},+ },+ })+ authorizer.AddCheck(api.ControlPlaneStaticTimeCheck)+}+// MintBiscuitToken generates a signed Biscuit token for a peer with policy rules based on JWT claims.// labels are control-plane-attested key=value claims (canonical, pre-validated); empty means no claims.func MintBiscuitToken(signingKey ed25519.PrivateKey, claims jwt.MapClaims, token *oidc.IDToken, remotePeer peer.ID, biscuitExpiry time.Time, roles []string, policyRoles []*api.PolicyRole, labels map[string]string) ([]byte, []string, error) {@@ -253,14 +268,7 @@ func VerifyBiscuitAndGetKey(biscuitData []byte, expectedPeer peer.ID, trustedPubcontinue}- authorizer.AddFact(biscuit.Fact{- Predicate: biscuit.Predicate{- Name: api.FactTime,- IDs: []biscuit.Term{biscuit.Date(time.Now())},- },- })-- authorizer.AddCheck(api.ControlPlaneStaticTimeCheck)+ EnforceExpiration(authorizer)authorizer.AddPolicy(api.AllowIfTruePolicy)if err := authorizer.Authorize(); err == nil {diff --git a/internal/node/middleware.go b/internal/node/middleware.goindex add58684..538ea485 100644--- a/internal/node/middleware.go+++ b/internal/node/middleware.go@@ -19,7 +19,6 @@ import ("fmt""runtime/debug""sync/atomic"- "time""github.com/biscuit-auth/biscuit-go/v2""github.com/google/sam/api"@@ -263,14 +262,7 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.// Enforce client_peer_id matches connection_peer_idauthorizer.AddCheck(api.BaselineReplayCheck)- // Inject the current time and enforce expiration, matching identity.VerifyBiscuit.- authorizer.AddFact(biscuit.Fact{- Predicate: biscuit.Predicate{- Name: api.FactTime,- IDs: []biscuit.Term{biscuit.Date(time.Now())},- },- })- authorizer.AddCheck(api.ControlPlaneStaticTimeCheck)+ identity.EnforceExpiration(authorizer)// Inject facts from our own identity token to support target matchingif err := n.injectIdentityFacts(authorizer, pubKey); err != nil {diff --git a/internal/node/node.go b/internal/node/node.goindex c079da7a..67a88bc6 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -1452,6 +1452,7 @@ func (n *SamNode) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuicontinue}+ identity.EnforceExpiration(authorizer)authorizer.AddPolicy(api.AllowIfTruePolicy)if err := authorizer.Authorize(); err == nil {diff --git a/internal/node/node_test.go b/internal/node/node_test.goindex b3fa0123..67cf0666 100644--- a/internal/node/node_test.go+++ b/internal/node/node_test.go@@ -22,9 +22,11 @@ import ("testing""time"+ "github.com/biscuit-auth/biscuit-go/v2""github.com/google/sam/api"lru "github.com/hashicorp/golang-lru/v2""github.com/libp2p/go-libp2p/core/crypto"+ "github.com/libp2p/go-libp2p/core/peer""github.com/multiformats/go-multiaddr")@@ -142,6 +144,50 @@ func TestHandleKeyRotationEvent(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) {+ pub, priv, err := ed25519.GenerateKey(nil)+ if err != nil {+ t.Fatal(err)+ }+ remotePeer := peer.ID("dummy-peer")++ mint := func(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.FactExpiration, IDs: []biscuit.Term{biscuit.Date(expiration)}}},+ } {+ if err := builder.AddAuthorityFact(f); err != nil {+ t.Fatal(err)+ }+ }+ b, err := builder.Build()+ if err != nil {+ t.Fatal(err)+ }+ data, err := b.Serialize()+ if err != nil {+ t.Fatal(err)+ }+ return data+ }++ node := &SamNode{+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},+ BiscuitTimeout: 500 * time.Millisecond,+ }++ if _, err := node.verifyBiscuit(mint(time.Now().Add(time.Hour)), remotePeer); err != nil {+ t.Fatalf("valid token rejected: %v", err)+ }+ if _, err := node.verifyBiscuit(mint(time.Now().Add(-time.Hour)), remotePeer); err == nil {+ t.Fatal("expired token admitted on the peer-authentication path")+ }+}+func TestStartRenewalLoop_ExpiredAndFails(t *testing.T) {if os.Getenv("BE_CRASHER") == "1" {store, _ := NewStore(t.TempDir())