Skip to content

Agent Mesh

controlplane: log an enrollment refused for a role not bound to the identity

HosniBelfekia9af0d646feb
diff --git a/internal/controlplane/register_refusal_log_test.go b/internal/controlplane/register_refusal_log_test.go
new file mode 100644
index 00000000..a94648b1
--- /dev/null
+++ b/internal/controlplane/register_refusal_log_test.go
@@ -0,0 +1,108 @@
+// 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"
+ "io"
+ "net/http"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/google/agentmesh/api"
+ "github.com/google/agentmesh/internal/logging"
+ golog "github.com/ipfs/go-log/v2"
+ "github.com/libp2p/go-libp2p/core/crypto"
+ "github.com/libp2p/go-libp2p/core/peer"
+ "google.golang.org/protobuf/proto"
+)
+
+// An enrollment the policy refuses is a decision about a member, and is
+// logged like the other refusals of /register (a banned node or identity, a
+// label the role does not grant): an operator reading the control plane's
+// log must be able to tell a missing binding from a ban, and see which
+// identity was refused and what it did resolve to.
+func TestRegisterLogsARoleNotBoundToTheIdentity(t *testing.T) {
+ prev := golog.GetConfig()
+ logging.Setup(logging.Options{RingBuffer: true})
+ t.Cleanup(func() { golog.SetupLogging(prev) })
+
+ issuer, mintToken := startCustomMockOIDC(t)
+ srv, store, baseURL := setupTestServer(t, issuer)
+ t.Cleanup(func() {
+ _ = srv.Close()
+ _ = store.Close()
+ })
+
+ // Only group admins may enroll a node; the caller is in no group.
+ roles := []*api.PolicyRole{{Name: api.RoleNode, AllowedServices: []string{"mcp://*"}}}
+ bindings := []*api.PolicyBinding{{Role: api.RoleNode, Members: []string{"group:admins"}}}
+ if err := store.SaveMeshPolicy(context.Background(), roles, bindings); err != nil {
+ t.Fatalf("SaveMeshPolicy: %v", err)
+ }
+
+ priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pID, err := peer.IDFromPrivateKey(priv)
+ if err != nil {
+ t.Fatal(err)
+ }
+ pubBytes, err := crypto.MarshalPublicKey(priv.GetPublic())
+ if err != nil {
+ t.Fatal(err)
+ }
+ ts, sig := registerPoP(t, priv, pID.String())
+ body, err := proto.Marshal(&api.EnrollRequest{
+ Jwt: mintToken(map[string]interface{}{"sub": "node-bob"}),
+ PeerId: pID.String(),
+ PublicKey: pubBytes,
+ RequestedRole: api.RoleNode,
+ ChallengeUnixMs: ts,
+ ChallengeSignature: sig,
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ client := &http.Client{Timeout: 5 * time.Second}
+ resp, err := client.Post(baseURL+"/register", "application/x-protobuf", bytes.NewReader(body))
+ if err != nil {
+ t.Fatalf("POST /register: %v", err)
+ }
+ _, _ = io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusForbidden {
+ t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusForbidden)
+ }
+
+ var refusal string
+ for _, line := range logging.RecentLogs() {
+ if strings.Contains(line, "Rejected enrollment for a role not bound to the identity") && strings.Contains(line, pID.String()) {
+ refusal = line
+ }
+ }
+ if refusal == "" {
+ t.Fatalf("no log line for the refused enrollment of %s; recent logs:\n%s", pID, strings.Join(logging.RecentLogs(), "\n"))
+ }
+ for _, want := range []string{api.RoleNode, issuer + "|node-bob"} {
+ if !strings.Contains(refusal, want) {
+ t.Errorf("refusal log line = %q, want it to name %q", refusal, want)
+ }
+ }
+}
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index bce3d55b..fd51071e 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -1039,6 +1039,7 @@ func (s *Server) HandleRegister(w http.ResponseWriter, r *http.Request) {
// mesh:role:node — a mesh that wants open enrollment says so by binding it
// to mesh:system:authenticated, instead of getting it by omission.
if !resolvedMap[req.RequestedRole] {
+ logger.Warnw("Rejected enrollment for a role not bound to the identity", "peer_id", canonical, "requested_role", req.RequestedRole, "identity", oidcIdentityKey(claims), "resolved_roles", resolvedRoles)
http.Error(w, fmt.Sprintf("requested role %q is not bound to this identity; bind it in mesh policy (to mesh:system:authenticated to open enrollment to every authenticated identity)", req.RequestedRole), http.StatusForbidden)
return
}