Skip to content

Agent Mesh

feat(sam-node): fail closed on control-plane mismatch, add reset command

Antonio Ojea0e09f71eda65
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.go
index b3266500..9848bdfd 100644
--- a/cmd/sam-node/main.go
+++ b/cmd/sam-node/main.go
@@ -309,13 +309,31 @@ func main() {
if jwtStr == "" && bootstrapTokenFlag == "" && joinFlag {
token, _ := store.LoadIdentity()
+ identityExists := len(token) > 0
+ stored, _ := store.LoadControlPlaneURL()
+ mismatched := identityExists && controlPlaneAddr != "" && (stored == "" || normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored))
+ hasUsableIdentity := identityExists && len(controlPlanePubKey) > 0 && !mismatched
+
switch {
- case len(token) > 0 && len(controlPlanePubKey) > 0:
+ case hasUsableIdentity:
logger.Debug("--join set but a usable identity is already stored; ignoring")
+ case mismatched && !isInteractiveTerminal():
+ logger.Fatalf("--control-plane %q does not match the mesh this node is enrolled with (%s); switching meshes needs to be confirmed interactively. Run %q first, or re-run this interactively.", controlPlaneAddr, stored, "sam-node reset")
case !isInteractiveTerminal():
logger.Warn("--join set but no interactive terminal available; falling back to out-of-band enrollment via the unauthenticated MCP sidecar")
default:
- if len(token) > 0 {
+ if mismatched {
+ fmt.Printf("This node is currently enrolled with %s. Switching to %s replaces its stored identity (keeping the same PeerID). Continue? [y/N]: ", stored, controlPlaneAddr)
+ reader := bufio.NewReader(os.Stdin)
+ resp, _ := reader.ReadString('\n')
+ resp = strings.ToLower(strings.TrimSpace(resp))
+ if resp != "y" && resp != "yes" {
+ logger.Fatal("Aborted: not switching meshes.")
+ }
+ if err := store.ResetMeshIdentity(); err != nil {
+ logger.Fatalf("Failed to reset stored identity: %v", err)
+ }
+ } else if identityExists {
logger.Warn("Stored identity is missing its control plane public key; re-joining")
}
targetControlPlane := normalizeControlPlaneURL(defaultControlPlane(store, controlPlaneAddr))
@@ -344,6 +362,17 @@ func main() {
}
logger.Infoln("Using stored identity.")
+ if controlPlaneAddr != "" {
+ stored, _ := store.LoadControlPlaneURL()
+ if stored == "" || normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored) {
+ storedDisplay := stored
+ if storedDisplay == "" {
+ storedDisplay = "unknown (pre-dates control-plane URL tracking)"
+ }
+ logger.Fatalf("--control-plane %q does not match the mesh this node's stored identity was enrolled with (%s). A node's identity is only valid for the mesh that issued it: re-run with --join to switch interactively (%q clears it non-interactively), rather than pointing this one at a different mesh.", controlPlaneAddr, storedDisplay, "sam-node reset")
+ }
+ }
+
if len(controlPlanePubKey) == 0 {
logger.Fatal("Control plane public key not found in store and not provided. Re-run with --join to re-enroll, or pass --control-plane-public-key explicitly.")
}
@@ -684,6 +713,26 @@ func main() {
},
}
+ resetCmd := &cobra.Command{
+ Use: "reset",
+ Short: "Clear this node's stored mesh identity so it can join a different mesh",
+ Run: func(cmd *cobra.Command, args []string) {
+ store, err := node.NewStore(resolveDataDir())
+ if err != nil {
+ logger.Fatalf("Failed to open store: %v", err)
+ }
+ defer func() {
+ if err := store.Close(); err != nil {
+ logger.Errorf("closing store: %v", err)
+ }
+ }()
+ if err := store.ResetMeshIdentity(); err != nil {
+ logger.Fatalf("Failed to reset stored identity: %v", err)
+ }
+ fmt.Println("Cleared stored mesh identity (PeerID unchanged). Run 'sam-node join <control-plane-url>' or 'sam-node run --join' to enroll again.")
+ },
+ }
+
// Configure Flags
runCmd.Flags().StringSliceVar(&listenAddrs, "listen", []string{"/ip4/0.0.0.0/udp/5001/quic-v1", "/ip4/0.0.0.0/tcp/5002"}, "libp2p Listen Addrs")
runCmd.Flags().StringVar(&jwtFlag, "jwt", "", "Pre-fetched JWT token")
@@ -733,6 +782,7 @@ func main() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(joinCmd)
+ rootCmd.AddCommand(resetCmd)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
diff --git a/internal/node/store.go b/internal/node/store.go
index 7bc4427e..ec2d00c4 100644
--- a/internal/node/store.go
+++ b/internal/node/store.go
@@ -242,6 +242,36 @@ func (s *Store) LoadControlPlaneURL() (string, error) {
return string(val), err
}
+// ResetMeshIdentity clears everything about a node's mesh membership (its
+// Biscuit, mesh/control-plane config, and OIDC session state) so it can join
+// a different mesh. It keeps the long-lived libp2p key (node_private_key), so
+// the node's PeerID survives the switch.
+func (s *Store) ResetMeshIdentity() error {
+ return s.db.Update(func(tx *bbolt.Tx) error {
+ b := tx.Bucket([]byte(bucketIdentity))
+ for _, key := range []string{
+ keyBiscuit,
+ keyIdentityExp,
+ keyRefreshToken,
+ keyOidcIssuer,
+ keyOidcClientID,
+ keyOidcAudience,
+ "control_plane_public_key",
+ "router_addresses",
+ "control_plane_url",
+ // Legacy pre-rename keys, cleared for good measure.
+ "hub_public_key",
+ "hub_addresses",
+ "hub_url",
+ } {
+ if err := b.Delete([]byte(key)); err != nil {
+ return err
+ }
+ }
+ return nil
+ })
+}
+
func (s *Store) Close() error {
return s.db.Close()
}
diff --git a/internal/node/store_test.go b/internal/node/store_test.go
index 98a24ecb..5e9eb7af 100644
--- a/internal/node/store_test.go
+++ b/internal/node/store_test.go
@@ -288,6 +288,62 @@ func TestStore_ControlPlaneURL(t *testing.T) {
}
}
+func TestStore_ResetMeshIdentity(t *testing.T) {
+ store, err := NewStore(t.TempDir())
+ if err != nil {
+ t.Fatalf("Failed to create store: %v", err)
+ }
+ defer func() {
+ if err := store.Close(); err != nil {
+ t.Errorf("failed to close store: %v", err)
+ }
+ }()
+
+ if err := store.SaveIdentity([]byte("biscuit")); err != nil {
+ t.Fatalf("SaveIdentity failed: %v", err)
+ }
+ if err := store.SaveMeshConfig([]byte("pubkey"), []string{"/ip4/1.2.3.4/tcp/1"}); err != nil {
+ t.Fatalf("SaveMeshConfig failed: %v", err)
+ }
+ if err := store.SaveControlPlaneURL("https://cp.example.com"); err != nil {
+ t.Fatalf("SaveControlPlaneURL failed: %v", err)
+ }
+ if err := store.SaveOIDCConfig("issuer", "client", "audience"); err != nil {
+ t.Fatalf("SaveOIDCConfig failed: %v", err)
+ }
+ key := []byte("node-private-key-bytes")
+ if err := store.SaveKey(key); err != nil {
+ t.Fatalf("SaveKey failed: %v", err)
+ }
+
+ if err := store.ResetMeshIdentity(); err != nil {
+ t.Fatalf("ResetMeshIdentity failed: %v", err)
+ }
+
+ if _, err := store.LoadIdentity(); err == nil {
+ t.Error("expected identity to be cleared")
+ }
+ if pubKey, _, _ := store.LoadMeshConfig(); len(pubKey) != 0 {
+ t.Errorf("expected mesh config pubkey to be cleared, got %v", pubKey)
+ }
+ if url, _ := store.LoadControlPlaneURL(); url != "" {
+ t.Errorf("expected control plane URL to be cleared, got %q", url)
+ }
+ issuer, _, _, _ := store.LoadOIDCConfig()
+ if issuer != "" {
+ t.Errorf("expected OIDC config to be cleared, got issuer %q", issuer)
+ }
+
+ // The libp2p private key must survive the reset so the PeerID is stable.
+ loadedKey, err := store.LoadKey()
+ if err != nil {
+ t.Fatalf("LoadKey failed: %v", err)
+ }
+ if string(loadedKey) != string(key) {
+ t.Errorf("expected private key to survive reset, got %v want %v", loadedKey, key)
+ }
+}
+
func TestStore_IsBanned(t *testing.T) {
store, err := NewStore(t.TempDir())
if err != nil {
diff --git a/tests/integration/login_test.go b/tests/integration/login_test.go
index bcb2c4a0..7cdc2910 100644
--- a/tests/integration/login_test.go
+++ b/tests/integration/login_test.go
@@ -156,3 +156,90 @@ func TestSamNodeRunJoinNonInteractive(t *testing.T) {
t.Fatalf("expected the unauthenticated sidecar to start:\n%s", out)
}
}
+
+// TestSamNodeRunControlPlaneMismatch covers a node enrolled with one control
+// plane being pointed at a different one: it must fail loudly (never
+// silently keep using the originally-enrolled mesh), with or without
+// --join, and "reset" must clear the way for a fresh enrollment elsewhere.
+func TestSamNodeRunControlPlaneMismatch(t *testing.T) {
+ nodeBin := buildBinary(t, "./cmd/sam-node")
+ tmpHome := t.TempDir()
+ env := []string{
+ "HOME=" + tmpHome,
+ "XDG_CONFIG_HOME=" + filepath.Join(tmpHome, ".config"),
+ }
+
+ _, routerA := startMockRouter(t)
+ _, routerB := startMockRouter(t)
+
+ // Enroll against control plane A.
+ stdout, stderr, err := runCommand(
+ t, repoRoot(t), 3*time.Second, env, "",
+ nodeBin, "run", "--control-plane", routerA, "--jwt", "test-jwt",
+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
+ "--listen", "/ip4/127.0.0.1/tcp/0",
+ "--bind-addr", "127.0.0.1:0",
+ "--api-token-path", tokenPath(t, "dummy-token"),
+ )
+ if err != context.DeadlineExceeded {
+ t.Fatalf("expected initial enroll+run against A to keep running, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+
+ // Pointing the same store at B, without --join, must fail rather than
+ // silently keep talking to A.
+ stdout, stderr, err = runCommand(
+ t, repoRoot(t), 5*time.Second, env, "",
+ nodeBin, "run", "--control-plane", routerB,
+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
+ "--listen", "/ip4/127.0.0.1/tcp/0",
+ "--bind-addr", "127.0.0.1:0",
+ "--api-token-path", tokenPath(t, "dummy-token"),
+ )
+ if err == nil || err == context.DeadlineExceeded {
+ t.Fatalf("expected run against a mismatched control plane to fail fast, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+ out := stdout + stderr
+ if !strings.Contains(out, "does not match the mesh") {
+ t.Fatalf("expected a control-plane mismatch error, got:\n%s", out)
+ }
+
+ // Same mismatch with --join, but no TTY to confirm the switch: must also
+ // fail fast rather than silently ignore --join or block indefinitely.
+ stdout, stderr, err = runCommand(
+ t, repoRoot(t), 5*time.Second, env, "",
+ nodeBin, "run", "--join", "--control-plane", routerB,
+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
+ "--listen", "/ip4/127.0.0.1/tcp/0",
+ "--bind-addr", "127.0.0.1:0",
+ "--api-token-path", tokenPath(t, "dummy-token"),
+ )
+ if err == nil || err == context.DeadlineExceeded {
+ t.Fatalf("expected run --join against a mismatched control plane to fail fast without a TTY, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+ out = stdout + stderr
+ if !strings.Contains(out, "does not match the mesh") {
+ t.Fatalf("expected a control-plane mismatch error, got:\n%s", out)
+ }
+
+ // "reset" clears the way for a fresh enrollment against B.
+ stdout, stderr, err = runCommand(t, repoRoot(t), 3*time.Second, env, "", nodeBin, "reset")
+ if err != nil {
+ t.Fatalf("reset failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+ if !strings.Contains(stdout+stderr, "Cleared stored mesh identity") {
+ t.Fatalf("expected reset confirmation, got:\n%s", stdout+stderr)
+ }
+
+ stdout, stderr, err = runCommand(
+ t, repoRoot(t), 3*time.Second, env, "",
+ nodeBin, "run", "--control-plane", routerB, "--jwt", "test-jwt",
+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",
+ "--listen", "/ip4/127.0.0.1/tcp/0",
+ "--bind-addr", "127.0.0.1:0",
+ "--api-token-path", tokenPath(t, "dummy-token"),
+ )
+ if err != context.DeadlineExceeded {
+ t.Fatalf("expected re-enroll against B after reset to keep running, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+}
+