Skip to content

Agent Mesh

node: expire cached peer admissions instead of trusting them forever

Antonio Ojea61d2fc7a5dd7
diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.go
index 2a4e6bf2..71c851e2 100644
--- a/internal/identity/biscuit.go
+++ b/internal/identity/biscuit.go
@@ -62,6 +62,37 @@ func EnforceExpiration(authorizer biscuit.Authorizer) {
authorizer.AddCheck(api.ControlPlaneStaticTimeCheck)
}
+// 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) {
+ 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")}}},
+ })
+ if err != nil {
+ return time.Time{}, fmt.Errorf("expiration query failed: %w", err)
+ }
+
+ // A token may carry several; the earliest is the one that binds.
+ var earliest time.Time
+ for _, fact := range facts {
+ if len(fact.IDs) != 1 {
+ continue
+ }
+ date, ok := fact.IDs[0].(biscuit.Date)
+ if !ok {
+ continue
+ }
+ if t := time.Time(date); earliest.IsZero() || t.Before(earliest) {
+ earliest = t
+ }
+ }
+ if earliest.IsZero() {
+ return time.Time{}, fmt.Errorf("no %s fact in token", api.FactExpiration)
+ }
+ return earliest, nil
+}
+
// 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) {
diff --git a/internal/node/node.go b/internal/node/node.go
index a31e7358..f83fe45f 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -116,13 +116,27 @@ type nodeRelayACL struct {
}
func (a *nodeRelayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
- _, ok := a.node.authPeers.Load(p)
- return ok
+ return a.node.isAdmitted(p)
}
func (a *nodeRelayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
- _, ok := a.node.authPeers.Load(dest)
- return ok
+ return a.node.isAdmitted(dest)
+}
+
+// isAdmitted reports whether a peer completed the auth handshake and its token
+// has not lapsed since. The handshake only proves the token was valid at that
+// instant, so without this the relay ACL would honour an admission forever.
+func (n *SamNode) isAdmitted(p peer.ID) bool {
+ v, ok := n.authPeers.Load(p)
+ if !ok {
+ return false
+ }
+ expiry, ok := v.(time.Time)
+ if !ok || !time.Now().Before(expiry) {
+ n.authPeers.Delete(p)
+ return false
+ }
+ return true
}
type SamNode struct {
@@ -1412,7 +1426,7 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
return
}
- b, err := n.verifyBiscuit(exchange.Biscuit, remotePeer)
+ b, expiry, err := n.verifyBiscuit(exchange.Biscuit, remotePeer)
if err != nil {
logger.Warnf("[AuthN] Authorization failed for %s: %v", remotePeer, err)
return
@@ -1428,7 +1442,7 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
return
}
- n.authPeers.Store(remotePeer, true)
+ n.authPeers.Store(remotePeer, expiry)
logger.Infof("[AuthN] Successfully authenticated peer %s", remotePeer)
// Mutual response with our identity, mirroring the router handler, so
@@ -1440,10 +1454,10 @@ func (n *SamNode) HandleAuthHandshake(s network.Stream) {
}
}
-func (n *SamNode) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuit.Biscuit, error) {
+func (n *SamNode) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscuit.Biscuit, time.Time, error) {
b, err := biscuit.Unmarshal(biscuitData)
if err != nil {
- return nil, fmt.Errorf("malformed biscuit: %w", err)
+ return nil, time.Time{}, fmt.Errorf("malformed biscuit: %w", err)
}
n.keysMu.RLock()
@@ -1465,16 +1479,20 @@ func (n *SamNode) verifyBiscuit(biscuitData []byte, remotePeer peer.ID) (*biscui
authorizer.AddPolicy(api.AllowIfTruePolicy)
if err := authorizer.Authorize(); err == nil {
- return b, nil
+ expiry, err := identity.ExpirationOf(authorizer)
+ if err != nil {
+ return nil, time.Time{}, err
+ }
+ return b, expiry, nil
} else {
lastErr = fmt.Errorf("authorize error: %w", err)
}
}
if lastErr != nil {
- return nil, fmt.Errorf("no valid key found (last error: %v)", lastErr)
+ return nil, time.Time{}, fmt.Errorf("no valid key found (last error: %v)", lastErr)
}
- return nil, fmt.Errorf("no valid key found")
+ return nil, time.Time{}, fmt.Errorf("no valid key found")
}
func (n *SamNode) RegisterService(ctx context.Context, req *api.RegisterServiceRequest) error {
diff --git a/internal/node/node_test.go b/internal/node/node_test.go
index 67cf0666..e0a70af8 100644
--- a/internal/node/node_test.go
+++ b/internal/node/node_test.go
@@ -180,10 +180,17 @@ func TestVerifyBiscuitRejectsExpiredToken(t *testing.T) {
BiscuitTimeout: 500 * time.Millisecond,
}
- if _, err := node.verifyBiscuit(mint(time.Now().Add(time.Hour)), remotePeer); err != nil {
+ want := time.Now().Add(time.Hour)
+ _, expiry, err := node.verifyBiscuit(mint(want), remotePeer)
+ if err != nil {
t.Fatalf("valid token rejected: %v", err)
}
- if _, err := node.verifyBiscuit(mint(time.Now().Add(-time.Hour)), remotePeer); err == nil {
+ // 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 _, _, err := node.verifyBiscuit(mint(time.Now().Add(-time.Hour)), remotePeer); err == nil {
t.Fatal("expired token admitted on the peer-authentication path")
}
}
diff --git a/internal/node/relay_acl_test.go b/internal/node/relay_acl_test.go
index 7e799443..c0829300 100644
--- a/internal/node/relay_acl_test.go
+++ b/internal/node/relay_acl_test.go
@@ -38,14 +38,14 @@ func TestNodeRelayACL_AllowConnect(t *testing.T) {
}
// Src is authenticated, dest is not -> should fail
- node.authPeers.Store(srcPeer, struct{}{})
+ node.authPeers.Store(srcPeer, time.Now().Add(time.Hour))
if acl.AllowConnect(srcPeer, srcAddr, destPeer) {
t.Errorf("Expected AllowConnect to return false when dest is not authenticated, even if src is")
}
// Dest is authenticated, src is not -> should succeed
node.authPeers.Delete(srcPeer)
- node.authPeers.Store(destPeer, struct{}{})
+ node.authPeers.Store(destPeer, time.Now().Add(time.Hour))
if !acl.AllowConnect(srcPeer, srcAddr, destPeer) {
t.Errorf("Expected AllowConnect to return true when dest is authenticated")
}
@@ -62,12 +62,37 @@ func TestNodeRelayACL_AllowReserve(t *testing.T) {
t.Errorf("Expected AllowReserve to return false when peer is not authenticated")
}
- node.authPeers.Store(peerID, struct{}{})
+ node.authPeers.Store(peerID, time.Now().Add(time.Hour))
if !acl.AllowReserve(peerID, addr) {
t.Errorf("Expected AllowReserve to return true when peer is authenticated")
}
}
+// TestExpiredAdmissionLosesRelayRights covers a token lapsing after the peer was
+// admitted. The handshake only proves validity at that instant, so the ACL has
+// to re-check, otherwise one handshake buys relay rights forever.
+func TestExpiredAdmissionLosesRelayRights(t *testing.T) {
+ node := &SamNode{BiscuitTimeout: 500 * time.Millisecond}
+ acl := &nodeRelayACL{node: node}
+
+ peerID := peer.ID("lapsed-peer")
+ addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
+
+ node.authPeers.Store(peerID, time.Now().Add(-time.Second))
+ if acl.AllowReserve(peerID, addr) {
+ t.Error("expired admission still holds relay rights")
+ }
+ if _, still := node.authPeers.Load(peerID); still {
+ t.Error("expired admission was not evicted")
+ }
+
+ // A value of any other type is a bug, not an admission.
+ node.authPeers.Store(peerID, true)
+ if acl.AllowReserve(peerID, addr) {
+ t.Error("malformed admission entry granted relay rights")
+ }
+}
+
// TestBannedPeerLosesRelayRights covers a ban arriving after the peer was
// already admitted: the relay ACL reads authPeers, so leaving a stale entry
// there keeps granting reservations to a revoked peer.
@@ -85,7 +110,7 @@ func TestBannedPeerLosesRelayRights(t *testing.T) {
}
addr, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/1234")
- node.authPeers.Store(peerID, struct{}{})
+ node.authPeers.Store(peerID, time.Now().Add(time.Hour))
if !acl.AllowReserve(peerID, addr) {
t.Fatal("expected an admitted peer to hold relay rights")
}