Skip to content

Agent Mesh

fix: address gemini-code-assist review on the node catalog PR

Fernando Marinoea782bcc151a
diff --git a/internal/console/public/app.js b/internal/console/public/app.js
index a621c3a0..503e5c1a 100644
--- a/internal/console/public/app.js
+++ b/internal/console/public/app.js
@@ -400,7 +400,9 @@ function renderServicesTable(nodeCatalog, labelsByPeer) {
const entry = nodeCatalog[peerID] || {};
const services = entry.services || [];
for (const svc of services) {
- rows.push({ peerID, reportedAt: entry.reported_at, svc });
+ if (svc) {
+ rows.push({ peerID, reportedAt: entry.reported_at, svc });
+ }
}
}
@@ -415,7 +417,7 @@ function renderServicesTable(nodeCatalog, labelsByPeer) {
<td>${escapeHTML(SERVICE_TYPE_NAMES[svc.type] || 'unknown')}</td>
<td>${escapeHTML(svc.description || '')}</td>
<td>${peerCell(peerID, (labelsByPeer || {})[peerID])}</td>
- <td>${escapeHTML(reportedAt || '')}</td>
+ <td>${reportedAt ? escapeHTML(new Date(reportedAt).toLocaleString()) : '-'}</td>
</tr>
`).join('');
}
diff --git a/internal/controlplane/catalog.go b/internal/controlplane/catalog.go
index e92d289d..dcf75c07 100644
--- a/internal/controlplane/catalog.go
+++ b/internal/controlplane/catalog.go
@@ -120,9 +120,19 @@ func (s *Server) HandleNodeCatalog(w http.ResponseWriter, r *http.Request) {
return
}
+ // A malformed report (e.g. {"services": [null]}) unmarshals into a nil
+ // element rather than failing - filter those out so a bad report from one
+ // node can't crash rendering for every node's entry in the console.
+ var validServices []*api.ServiceInfo
+ for _, svc := range req.Services {
+ if svc != nil {
+ validServices = append(validServices, svc)
+ }
+ }
+
s.catalogMu.Lock()
s.catalog[peerID.String()] = nodeCatalogEntry{
- Services: req.Services,
+ Services: validServices,
ReportedAt: time.Now(),
}
s.catalogMu.Unlock()
diff --git a/internal/controlplane/catalog_test.go b/internal/controlplane/catalog_test.go
new file mode 100644
index 00000000..469492ea
--- /dev/null
+++ b/internal/controlplane/catalog_test.go
@@ -0,0 +1,161 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package controlplane
+
+import (
+ "bytes"
+ "context"
+ "encoding/base64"
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/google/sam/api"
+ "github.com/google/sam/internal/identity"
+ "github.com/libp2p/go-libp2p/core/crypto"
+ "github.com/libp2p/go-libp2p/core/peer"
+)
+
+// postCatalog POSTs a raw catalog report body to /nodes/catalog under the
+// given biscuit and returns the response status.
+func postCatalog(t *testing.T, cpURL string, biscuit []byte, body []byte) int {
+ t.Helper()
+
+ req, err := http.NewRequest(http.MethodPost, cpURL+"/nodes/catalog", bytes.NewReader(body))
+ if err != nil {
+ t.Fatalf("NewRequest: %v", err)
+ }
+ if biscuit != nil {
+ req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString(biscuit))
+ }
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatalf("Do: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+ return resp.StatusCode
+}
+
+func TestHandleNodeCatalog(t *testing.T) {
+ t.Parallel()
+
+ srv, store, cpURL := setupTestServer(t, "")
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ ctx := context.Background()
+ _, biscuitBytes := enrollRefreshTestNode(t, ctx, store)
+
+ body := []byte(`{"services":[{"type":1,"name":"stvv-compliance-docs","description":"doc lookup"},null,{"type":1,"name":"everything","description":"reference server"}]}`)
+ if got := postCatalog(t, cpURL, biscuitBytes, body); got != http.StatusNoContent {
+ t.Fatalf("HandleNodeCatalog: got status %d, want %d", got, http.StatusNoContent)
+ }
+
+ snap := srv.catalogSnapshot()
+ if len(snap) != 1 {
+ t.Fatalf("expected exactly one reporting peer in the catalog, got %d", len(snap))
+ }
+ for peerID, entry := range snap {
+ if len(entry.Services) != 2 {
+ t.Fatalf("expected the null service element to be filtered out, got %d services: %+v", len(entry.Services), entry.Services)
+ }
+ for i, svc := range entry.Services {
+ if svc == nil {
+ t.Fatalf("service at index %d is nil, filtering failed", i)
+ }
+ }
+ if entry.ReportedAt.IsZero() {
+ t.Errorf("ReportedAt was not set for peer %s", peerID)
+ }
+ }
+}
+
+func TestHandleNodeCatalog_MissingAuth(t *testing.T) {
+ t.Parallel()
+
+ srv, store, cpURL := setupTestServer(t, "")
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ body := []byte(`{"services":[]}`)
+ if got := postCatalog(t, cpURL, nil, body); got != http.StatusUnauthorized {
+ t.Fatalf("missing Authorization header: got status %d, want %d", got, http.StatusUnauthorized)
+ }
+}
+
+func TestHandleNodeCatalog_UnenrolledPeer(t *testing.T) {
+ t.Parallel()
+
+ srv, store, cpURL := setupTestServer(t, "")
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ ctx := context.Background()
+ cpPriv, _, err := store.GetCurrentKey(ctx)
+ if err != nil {
+ t.Fatalf("GetCurrentKey: %v", err)
+ }
+
+ // A biscuit minted for a peer that was never enrolled (or whose
+ // enrollment record has since been removed) must be rejected: a node can
+ // only ever report a catalog for itself, and self-reporting requires an
+ // admitted enrollment record to attribute the report to.
+ priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatalf("GenerateKeyPair: %v", err)
+ }
+ strangerPeer, err := peer.IDFromPrivateKey(priv)
+ if err != nil {
+ t.Fatalf("IDFromPrivateKey: %v", err)
+ }
+ strangerBiscuit, err := identity.MintBootstrapBiscuitToken(
+ cpPriv, strangerPeer, api.RoleNode, time.Now().Add(api.BiscuitTokenTTL), nil, nil,
+ )
+ if err != nil {
+ t.Fatalf("MintBootstrapBiscuitToken: %v", err)
+ }
+
+ body := []byte(`{"services":[]}`)
+ if got := postCatalog(t, cpURL, strangerBiscuit, body); got != http.StatusUnauthorized {
+ t.Fatalf("unenrolled peer: got status %d, want %d", got, http.StatusUnauthorized)
+ }
+}
+
+func TestHandleNodeCatalog_MethodNotAllowed(t *testing.T) {
+ t.Parallel()
+
+ srv, store, cpURL := setupTestServer(t, "")
+ defer func() {
+ _ = srv.Close()
+ _ = store.Close()
+ }()
+
+ resp, err := http.Get(cpURL + "/nodes/catalog")
+ if err != nil {
+ t.Fatalf("Get: %v", err)
+ }
+ defer func() { _ = resp.Body.Close() }()
+ if resp.StatusCode != http.StatusMethodNotAllowed {
+ t.Fatalf("GET /nodes/catalog: got status %d, want %d", resp.StatusCode, http.StatusMethodNotAllowed)
+ }
+}
diff --git a/internal/node/controlplane_test.go b/internal/node/controlplane_test.go
index e47508e3..d4adc201 100644
--- a/internal/node/controlplane_test.go
+++ b/internal/node/controlplane_test.go
@@ -17,6 +17,8 @@ package node
import (
"context"
"crypto/ed25519"
+ "encoding/base64"
+ "encoding/json"
"net/http"
"net/http/httptest"
"reflect"
@@ -220,3 +222,54 @@ func TestSyncMeshConfig(t *testing.T) {
t.Errorf("Expected saved addrs %v, got %v", expectedInfo.RouterAddresses, savedAddrsStr)
}
}
+
+func TestReportNodeCatalog(t *testing.T) {
+ services := []*api.ServiceInfo{
+ {Type: api.ServiceType_SERVICE_TYPE_MCP, Name: "stvv-compliance-docs", Description: "doc lookup"},
+ }
+
+ var gotAuth string
+ var gotReq nodeCatalogRequest
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ t.Errorf("Expected POST, got %s", r.Method)
+ }
+ if r.URL.Path != "/nodes/catalog" {
+ t.Errorf("Expected path /nodes/catalog, got %s", r.URL.Path)
+ }
+ gotAuth = r.Header.Get("Authorization")
+ if err := json.NewDecoder(r.Body).Decode(&gotReq); err != nil {
+ t.Errorf("failed to decode request body: %v", err)
+ }
+ w.WriteHeader(http.StatusNoContent)
+ }))
+ defer server.Close()
+
+ biscuitToken := []byte("fake-biscuit-bytes")
+ if err := ReportNodeCatalog(context.Background(), server.URL, biscuitToken, services); err != nil {
+ t.Fatalf("ReportNodeCatalog failed: %v", err)
+ }
+
+ wantAuth := "Bearer " + base64.StdEncoding.EncodeToString(biscuitToken)
+ if gotAuth != wantAuth {
+ t.Errorf("Expected Authorization header %q, got %q", wantAuth, gotAuth)
+ }
+ if len(gotReq.Services) != 1 || gotReq.Services[0].Name != "stvv-compliance-docs" {
+ t.Errorf("Expected relayed services %v, got %v", services, gotReq.Services)
+ }
+}
+
+func TestReportNodeCatalog_HTTPError(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ http.Error(w, "node not enrolled or not admitted", http.StatusUnauthorized)
+ }))
+ defer server.Close()
+
+ err := ReportNodeCatalog(context.Background(), server.URL, []byte("fake-biscuit-bytes"), nil)
+ if err == nil {
+ t.Fatal("Expected error, got nil")
+ }
+ if !strings.Contains(err.Error(), "control plane returned status 401") {
+ t.Errorf("Expected error to mention status 401, got %v", err)
+ }
+}