Skip to content

Agent Mesh

storage: a node list carries no blobs, and the SQL arms compose

Antonio Ojeab6b75d3004ec
diff --git a/internal/controlplane/ui.go b/internal/controlplane/ui.go
index ebecbcb9..cd94d018 100644
--- a/internal/controlplane/ui.go
+++ b/internal/controlplane/ui.go
@@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"net/http"
+ "slices"
"strconv"
"strings"
"time"
@@ -200,7 +201,9 @@ func parseNodeStates(raw []string) ([]storage.NodeState, error) {
state := storage.NodeState(value)
switch state {
case storage.NodeAdmitted, storage.NodeExpired, storage.NodeBanned:
- states = append(states, state)
+ if !slices.Contains(states, state) {
+ states = append(states, state)
+ }
default:
return nil, fmt.Errorf("state must be admitted, expired or banned, not %q", value)
}
diff --git a/internal/storage/round_trip_test.go b/internal/storage/round_trip_test.go
index 8fab2052..d6cc8d9d 100644
--- a/internal/storage/round_trip_test.go
+++ b/internal/storage/round_trip_test.go
@@ -140,6 +140,7 @@ func TestEnrolledNodeRoundTripsEveryField(t *testing.T) {
requireFieldsRoundTrip(t, want, got, "Banned")
// ListNodes selects its own column list, so it can drift from GetNode.
+ // It leaves the two blobs out on purpose: a list is a view of the rows.
listed, err := store.ListNodes(ctx, NodeFilter{})
if err != nil {
t.Fatalf("ListNodes: %v", err)
@@ -147,7 +148,10 @@ func TestEnrolledNodeRoundTripsEveryField(t *testing.T) {
if len(listed) != 1 {
t.Fatalf("ListNodes returned %d nodes, want 1", len(listed))
}
- requireFieldsRoundTrip(t, want, &listed[0], "Banned")
+ requireFieldsRoundTrip(t, want, &listed[0], "Banned", "PublicKey", "Biscuit")
+ if len(listed[0].PublicKey) != 0 || len(listed[0].Biscuit) != 0 {
+ t.Errorf("ListNodes read the blobs: public key %d bytes, biscuit %d bytes; GetNode is what reads them", len(listed[0].PublicKey), len(listed[0].Biscuit))
+ }
}
// Autonomous recovery is a per-node, server-side opt-in: it must default
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 4394e090..b0587033 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -1705,7 +1705,9 @@ func (s *SQLStore) ListNodes(ctx context.Context, filter NodeFilter) ([]Enrolled
if err != nil {
return nil, err
}
- query := `SELECT peer_id, public_key, biscuit_token, role, enrollment_type, claims_json, owner_id, labels_json, enrolled_at, expires_at, banned, autonomous_recovery FROM nodes` + where + ` ORDER BY enrolled_at DESC, peer_id DESC`
+ // Neither blob is read: a list is a view of the rows, and the biscuit is
+ // a 2 KB column per row that only GetNode's single-row callers need.
+ query := `SELECT peer_id, role, enrollment_type, claims_json, owner_id, labels_json, enrolled_at, expires_at, banned, autonomous_recovery FROM nodes` + where + ` ORDER BY enrolled_at DESC, peer_id DESC`
if filter.Limit > 0 {
query += ` LIMIT ?`
args = append(args, filter.Limit)
@@ -1723,8 +1725,6 @@ func (s *SQLStore) ListNodes(ctx context.Context, filter NodeFilter) ([]Enrolled
var enrolledAtUnix, expiresAtUnix int64
err := rows.Scan(
&node.PeerID,
- &node.PublicKey,
- &node.Biscuit,
&node.Role,
&node.EnrollmentType,
&claimsJSON,
@@ -1751,15 +1751,6 @@ func (s *SQLStore) ListNodes(ctx context.Context, filter NodeFilter) ([]Enrolled
}
node.EnrolledAt = time.UnixMilli(enrolledAtUnix)
node.ExpiresAt = time.UnixMilli(expiresAtUnix)
-
- pubCopy := make([]byte, len(node.PublicKey))
- copy(pubCopy, node.PublicKey)
- node.PublicKey = pubCopy
-
- biscuitCopy := make([]byte, len(node.Biscuit))
- copy(biscuitCopy, node.Biscuit)
- node.Biscuit = biscuitCopy
-
nodes = append(nodes, node)
}
if err := rows.Err(); err != nil {
@@ -1818,7 +1809,8 @@ func nodeStateWhere(state NodeState, now time.Time) (string, []any, error) {
case NodeExpired:
return "(banned = ? AND expires_at > 0 AND expires_at < ?)", []any{false, ms}, nil
case NodeAdmitted:
- return "(banned = ? AND expires_at <= 0) OR (banned = ? AND expires_at >= ?)", []any{false, false, ms}, nil
+ // Parenthesised like the other arms, so a caller may AND it with more.
+ return "((banned = ? AND expires_at <= 0) OR (banned = ? AND expires_at >= ?))", []any{false, false, ms}, nil
}
return "", nil, fmt.Errorf("unknown node state %q", state)
}
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index 37338dd8..251b399c 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -353,6 +353,7 @@ type Store interface {
// ListNodes retrieves the enrolled nodes the filter selects, newest
// enrollment first. Peer ID breaks ties, so pages neither skip nor repeat a row.
+ // A listed node carries no PublicKey and no Biscuit: GetNode reads those.
ListNodes(ctx context.Context, filter NodeFilter) ([]EnrolledNode, error)
// CountNodes counts every enrolled node by role and by its state at now.