Skip to content

Agent Mesh

controlplane: sweep the revocation set at a doubling threshold

Antonio Ojeaba7dbb860ab7
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index ab5dafb1..d275fbc4 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -102,7 +102,11 @@ type Server struct {
revokedBiscuitsMu sync.RWMutex
revokedBiscuits map[string]time.Time
- bannedNodeRevIDs map[string]string
+ // revokedPruneAt is the size at which RevokeBiscuitID next drops expired
+ // entries; it doubles after each pass, so a write costs O(1) amortized and
+ // the map holds at most twice its live entries.
+ revokedPruneAt int
+ bannedNodeRevIDs map[string]string
oauthCodesMu sync.Mutex
oauthCodes map[string]*oauthAuthCode
@@ -188,6 +192,7 @@ func NewServer(config Options, store storage.Store) (*Server, error) {
stsLimiter: stsLimiter,
oidcSigner: signer,
revokedBiscuits: make(map[string]time.Time),
+ revokedPruneAt: revokedPruneMin,
bannedNodeRevIDs: make(map[string]string),
oauthCodes: make(map[string]*oauthAuthCode),
providers: make(map[string]*oidc.Provider),
diff --git a/internal/controlplane/sts.go b/internal/controlplane/sts.go
index c9833da9..e27e72ee 100644
--- a/internal/controlplane/sts.go
+++ b/internal/controlplane/sts.go
@@ -420,6 +420,10 @@ func (s *Server) allowNodeSTSRequest(peerID string) bool {
return s.stsLimiter.Allow(peerID)
}
+// revokedPruneMin is the smallest revocation set RevokeBiscuitID bothers to
+// sweep for expired entries.
+const revokedPruneMin = 64
+
// RevokeBiscuitID records a root Biscuit revocation ID (base64url-encoded) as
// revoked until expiry.
func (s *Server) RevokeBiscuitID(revocationID string, expiry time.Time) {
@@ -433,10 +437,13 @@ func (s *Server) RevokeBiscuitID(revocationID string, expiry time.Time) {
s.revokedBiscuitsMu.Lock()
// Expired entries also go on read; dropping them here too keeps the map
// bounded when nothing polls /revocations.
- for id, exp := range s.revokedBiscuits {
- if !now.Before(exp) {
- delete(s.revokedBiscuits, id)
+ if len(s.revokedBiscuits) >= s.revokedPruneAt {
+ for id, exp := range s.revokedBiscuits {
+ if !now.Before(exp) {
+ delete(s.revokedBiscuits, id)
+ }
}
+ s.revokedPruneAt = max(revokedPruneMin, 2*len(s.revokedBiscuits))
}
s.revokedBiscuits[revocationID] = expiry
s.revokedBiscuitsMu.Unlock()
diff --git a/internal/controlplane/sts_test.go b/internal/controlplane/sts_test.go
index 840a8b8c..4f44ef5a 100644
--- a/internal/controlplane/sts_test.go
+++ b/internal/controlplane/sts_test.go
@@ -24,6 +24,7 @@ import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
+ "fmt"
"io"
"net/http"
"net/url"
@@ -933,8 +934,8 @@ func TestOAuthAndControlPlaneHardening(t *testing.T) {
}
}
-// A revocation that expired leaves the in-memory set on the next write, not
-// only when /revocations is read.
+// Expired revocations leave the in-memory set as writes accumulate, not only
+// when /revocations is read, and the set stays bounded by its live entries.
func TestRevokeBiscuitIDPrunesExpiredEntries(t *testing.T) {
srv, store, _ := setupTestServer(t, "")
defer func() {
@@ -942,13 +943,15 @@ func TestRevokeBiscuitIDPrunesExpiredEntries(t *testing.T) {
_ = store.Close()
}()
- srv.RevokeBiscuitID("expired", time.Now().Add(-time.Minute))
+ for i := 0; i < 4*revokedPruneMin; i++ {
+ srv.RevokeBiscuitID(fmt.Sprintf("expired-%d", i), time.Now().Add(-time.Minute))
+ }
srv.RevokeBiscuitID("live", time.Now().Add(time.Hour))
srv.revokedBiscuitsMu.RLock()
defer srv.revokedBiscuitsMu.RUnlock()
- if _, ok := srv.revokedBiscuits["expired"]; ok {
- t.Error("expired revocation still cached")
+ if n := len(srv.revokedBiscuits); n > revokedPruneMin+1 {
+ t.Errorf("revocation set holds %d entries after %d expired writes, want at most %d", n, 4*revokedPruneMin, revokedPruneMin+1)
}
if _, ok := srv.revokedBiscuits["live"]; !ok {
t.Error("live revocation missing")