Skip to content

Agent Mesh

node, storage: preserve policy denial across trusted keys and serialize initial key creation

Antonio Ojea613d55f76ec9
diff --git a/internal/node/middleware.go b/internal/node/middleware.go
index 1ac4d060..62445efd 100644
--- a/internal/node/middleware.go
+++ b/internal/node/middleware.go
@@ -16,6 +16,7 @@ package node
import (
"crypto/ed25519"
+ "errors"
"fmt"
"runtime/debug"
"sync/atomic"
@@ -234,6 +235,9 @@ func (n *AgentMeshNode) verifyBiscuitTokenWithRules(biscuitBytes []byte, reqCtx
break
} else {
lastErr = err
+ if !errors.Is(err, biscuit.ErrInvalidSignature) {
+ break
+ }
}
}
diff --git a/internal/node/middleware_test.go b/internal/node/middleware_test.go
index 839c5667..3e468365 100644
--- a/internal/node/middleware_test.go
+++ b/internal/node/middleware_test.go
@@ -21,7 +21,9 @@ import (
cryptorand "crypto/rand"
"encoding/base64"
"encoding/json"
+ "errors"
"fmt"
+ "strings"
"io"
"os"
@@ -1253,3 +1255,69 @@ func TestTARConformanceVectors(t *testing.T) {
})
}
}
+
+func TestVerifyBiscuitPreservesPolicyDenialAcrossMultipleTrustedKeys(t *testing.T) {
+ pub1, priv1, err := ed25519.GenerateKey(cryptorand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pub2, _, err := ed25519.GenerateKey(cryptorand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ callerPeer := peer.ID("caller-peer")
+ builder := biscuit.NewBuilder(priv1)
+ if err := builder.AddAuthorityFact(api.MarkerFact(api.FactTargetUnrestricted)); err != nil {
+ t.Fatal(err)
+ }
+ for _, pred := range []string{"node", "client_peer_id"} {
+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: pred,
+ IDs: []biscuit.Term{biscuit.String(callerPeer.String())},
+ }}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ // Grant only /allowed/proto so /denied/proto fails Datalog authorization.
+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactGrantedServiceExact,
+ IDs: []biscuit.Term{biscuit.String(api.SystemNamespace), biscuit.String("/allowed/proto")},
+ }}); err != nil {
+ t.Fatal(err)
+ }
+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactExpiration,
+ IDs: []biscuit.Term{biscuit.Date(time.Now().Add(time.Hour))},
+ }}); err != nil {
+ t.Fatal(err)
+ }
+ b, err := builder.Build()
+ if err != nil {
+ t.Fatal(err)
+ }
+ tokenBytes, err := b.Serialize()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ node := &AgentMeshNode{
+ // Place the signing key (pub1) before another trusted key (pub2).
+ trustedKeys: []TrustedKey{
+ {Key: pub1, ReceivedAt: time.Now()},
+ {Key: pub2, ReceivedAt: time.Now()},
+ },
+ BiscuitTimeout: 500 * time.Millisecond,
+ }
+
+ _, err = node.verifyBiscuitTokenWithRules(tokenBytes, RequestContext{
+ PeerID: callerPeer,
+ Protocol: "/denied/proto",
+ })
+ if err == nil {
+ t.Fatal("verifyBiscuitTokenWithRules succeeded for denied protocol, want policy denial error")
+ }
+ if errors.Is(err, biscuit.ErrInvalidSignature) || strings.Contains(err.Error(), "invalid signature") {
+ t.Fatalf("verifyBiscuitTokenWithRules overwrote policy denial with signature error from second key: %v", err)
+ }
+}
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 7b1abd42..b702b894 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -967,11 +967,24 @@ func (s *SQLStore) SetKeyring(ctx context.Context, current KeyPair, verifying []
return tx.Commit()
}
-// SaveInitialKey implements Store.
+// SaveInitialKey implements Store. It serializes on rotation_lock and only
+// inserts when no active (unexpired) key exists, so concurrent control-plane
+// replicas starting against an empty store converge on one initial key.
func (s *SQLStore) SaveInitialKey(ctx context.Context, priv ed25519.PrivateKey, pub ed25519.PublicKey) error {
- query := s.rebind(`INSERT INTO keyring (private_key, public_key, created_at) VALUES (?, ?, ?)`)
- _, err := s.db.ExecContext(ctx, query, []byte(priv), []byte(pub), time.Now().UnixMilli())
- return err
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer func() { _ = tx.Rollback() }()
+
+ if _, err := tx.ExecContext(ctx, `UPDATE rotation_lock SET next_rotation_at = next_rotation_at WHERE id = 1`); err != nil {
+ return err
+ }
+ query := s.rebind(`INSERT INTO keyring (private_key, public_key, created_at) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM keyring WHERE expiration IS NULL)`)
+ if _, err := tx.ExecContext(ctx, query, []byte(priv), []byte(pub), time.Now().UnixMilli()); err != nil {
+ return err
+ }
+ return tx.Commit()
}
// EnrollNode implements Store.
@@ -2226,16 +2239,24 @@ func (s *SQLStore) GetAllValidOIDCKeys(ctx context.Context) ([]OIDCKeyPair, erro
return keys, rows.Err()
}
-// SaveInitialOIDCKey implements Store.
+// SaveInitialOIDCKey implements Store. It serializes on rotation_lock and only
+// inserts when no active OIDC key exists, so concurrent control-plane replicas
+// starting against an empty store converge on one initial OIDC key.
func (s *SQLStore) SaveInitialOIDCKey(ctx context.Context, kid string, priv, pub []byte) error {
- var query string
- if s.isPostgres() {
- query = s.rebind(`INSERT INTO oidc_keyring (kid, private_key, public_key, created_at) VALUES (?, ?, ?, ?) ON CONFLICT (kid) DO NOTHING`)
- } else {
- query = s.rebind(`INSERT OR IGNORE INTO oidc_keyring (kid, private_key, public_key, created_at) VALUES (?, ?, ?, ?)`)
+ tx, err := s.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
}
- _, err := s.db.ExecContext(ctx, query, kid, priv, pub, time.Now().UnixMilli())
- return err
+ defer func() { _ = tx.Rollback() }()
+
+ if _, err := tx.ExecContext(ctx, `UPDATE rotation_lock SET next_rotation_at = next_rotation_at WHERE id = 1`); err != nil {
+ return err
+ }
+ query := s.rebind(`INSERT INTO oidc_keyring (kid, private_key, public_key, created_at) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM oidc_keyring WHERE expiration IS NULL)`)
+ if _, err := tx.ExecContext(ctx, query, kid, priv, pub, time.Now().UnixMilli()); err != nil {
+ return err
+ }
+ return tx.Commit()
}
// SetOIDCKeyring implements Store.
diff --git a/internal/storage/sql_store_test.go b/internal/storage/sql_store_test.go
index 31040ddf..4a4c2e76 100644
--- a/internal/storage/sql_store_test.go
+++ b/internal/storage/sql_store_test.go
@@ -893,3 +893,54 @@ func TestResolveEnrollmentRequestOnlyWhilePending(t *testing.T) {
t.Errorf("losing resolution overwrote the request: %+v", got)
}
}
+
+func TestSaveInitialKeyIsIdempotentWhenActiveKeyExists(t *testing.T) {
+ store := newTestStore(t)
+ defer func() { _ = store.Close() }()
+ ctx := context.Background()
+
+ pub1, priv1, err := ed25519.GenerateKey(rand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pub2, priv2, err := ed25519.GenerateKey(rand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if err := store.SaveInitialKey(ctx, priv1, pub1); err != nil {
+ t.Fatalf("first SaveInitialKey: %v", err)
+ }
+ if err := store.SaveInitialKey(ctx, priv2, pub2); err != nil {
+ t.Fatalf("second SaveInitialKey: %v", err)
+ }
+
+ gotPriv, gotPub, err := store.GetCurrentKey(ctx)
+ if err != nil {
+ t.Fatalf("GetCurrentKey: %v", err)
+ }
+ if !bytes.Equal(gotPub, pub1) || !bytes.Equal(gotPriv, priv1) {
+ t.Fatalf("current key was overwritten by second SaveInitialKey: got %x, want %x", gotPub, pub1)
+ }
+ validKeys, err := store.GetAllValidPublicKeys(ctx)
+ if err != nil {
+ t.Fatalf("GetAllValidPublicKeys: %v", err)
+ }
+ if len(validKeys) != 1 || !bytes.Equal(validKeys[0], pub1) {
+ t.Fatalf("GetAllValidPublicKeys = %d keys, want 1 (%x)", len(validKeys), pub1)
+ }
+
+ if err := store.SaveInitialOIDCKey(ctx, "kid-1", []byte("priv-1"), []byte("pub-1")); err != nil {
+ t.Fatalf("first SaveInitialOIDCKey: %v", err)
+ }
+ if err := store.SaveInitialOIDCKey(ctx, "kid-2", []byte("priv-2"), []byte("pub-2")); err != nil {
+ t.Fatalf("second SaveInitialOIDCKey: %v", err)
+ }
+ oidcKeys, err := store.GetAllValidOIDCKeys(ctx)
+ if err != nil {
+ t.Fatalf("GetAllValidOIDCKeys: %v", err)
+ }
+ if len(oidcKeys) != 1 || oidcKeys[0].Kid != "kid-1" {
+ t.Fatalf("GetAllValidOIDCKeys = %+v, want single kid-1", oidcKeys)
+ }
+}