Skip to content

Agent Mesh

fix(sam-node): don't treat an untracked control-plane URL as a mismatch

Antonio Ojeaff67a8c46024
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.go
index 5eda9fd4..da5cf902 100644
--- a/cmd/sam-node/main.go
+++ b/cmd/sam-node/main.go
@@ -312,12 +312,18 @@ func main() {
token, _ := store.LoadIdentity()
identityExists := len(token) > 0
stored, _ := store.LoadControlPlaneURL()
- mismatched := identityExists && controlPlaneAddr != "" && (stored == "" || normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored))
+ mismatched := identityExists && controlPlaneAddr != "" && stored != "" && normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored)
hasUsableIdentity := identityExists && len(controlPlanePubKey) > 0 && !mismatched
switch {
case hasUsableIdentity:
logger.Debug("--join set but a usable identity is already stored; ignoring")
+ // Legacy store, never tracked a control-plane URL: start tracking it now.
+ if controlPlaneAddr != "" && stored == "" {
+ if err := store.SaveControlPlaneURL(normalizeControlPlaneURL(controlPlaneAddr)); err != nil {
+ logger.Warnf("Failed to save control plane URL: %v", err)
+ }
+ }
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():
@@ -365,12 +371,15 @@ func main() {
if controlPlaneAddr != "" {
stored, _ := store.LoadControlPlaneURL()
- if stored == "" || normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored) {
- storedDisplay := stored
- if storedDisplay == "" {
- storedDisplay = "unknown (pre-dates control-plane URL tracking)"
+ normalizedAddr := normalizeControlPlaneURL(controlPlaneAddr)
+ if stored != "" && normalizedAddr != normalizeControlPlaneURL(stored) {
+ 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, stored, "sam-node reset")
+ }
+ // Legacy store, never tracked a control-plane URL: start tracking it now.
+ if stored == "" {
+ if err := store.SaveControlPlaneURL(normalizedAddr); err != nil {
+ logger.Warnf("Failed to save control plane URL: %v", err)
}
- 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")
}
}
diff --git a/tests/integration/login_test.go b/tests/integration/login_test.go
index 2ade3c4e..154a5d05 100644
--- a/tests/integration/login_test.go
+++ b/tests/integration/login_test.go
@@ -324,3 +324,85 @@ func TestSamNodeRunControlPlaneMismatch(t *testing.T) {
}
}
+// TestSamNodeRunLegacyStoreNoFalseMismatch covers nodes enrolled before
+// control-plane URL tracking existed (stored URL empty, identity/pubkey
+// still valid): passing --control-plane matching reality must not be
+// treated as a mismatch (this would break unattended upgrades/restarts of
+// pre-existing deployments), and the URL should be backfilled for next time.
+func TestSamNodeRunLegacyStoreNoFalseMismatch(t *testing.T) {
+ nodeBin := buildBinary(t, "./cmd/sam-node")
+ tmpHome := t.TempDir()
+ env := []string{
+ "HOME=" + tmpHome,
+ "XDG_CONFIG_HOME=" + filepath.Join(tmpHome, ".config"),
+ }
+
+ _, routerAddr := startMockRouter(t)
+
+ stdout, stderr, err := runCommand(
+ t, repoRoot(t), 3*time.Second, env, "",
+ nodeBin, "run", "--control-plane", routerAddr, "--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 to keep running, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+
+ // Simulate a legacy store: clear just the tracked control-plane URL,
+ // keeping the identity, pubkey, and router addresses.
+ dataDir := filepath.Join(tmpHome, ".config", "sam-mesh")
+ store, err := node.NewStore(dataDir)
+ if err != nil {
+ t.Fatalf("failed to open store: %v", err)
+ }
+ if err := store.SaveControlPlaneURL(""); err != nil {
+ t.Fatalf("failed to clear control plane URL: %v", err)
+ }
+ if err := store.Close(); err != nil {
+ t.Fatalf("failed to close store: %v", err)
+ }
+
+ // Passing the (correct) --control-plane on this now-legacy-shaped store
+ // must not be treated as a mismatch.
+ stdout, stderr, err = runCommand(
+ t, repoRoot(t), 3*time.Second, env, "",
+ nodeBin, "run", "--control-plane", routerAddr,
+ "--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 legacy store + matching --control-plane to keep running, got: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)
+ }
+ if out := stdout + stderr; strings.Contains(out, "does not match the mesh") {
+ t.Fatalf("legacy store must not be treated as a mismatch:\n%s", out)
+ }
+
+ // The URL should now be backfilled for future runs.
+ store, err = node.NewStore(dataDir)
+ if err != nil {
+ t.Fatalf("failed to reopen store: %v", err)
+ }
+ defer func() { _ = store.Close() }()
+ got, err := store.LoadControlPlaneURL()
+ if err != nil {
+ t.Fatalf("LoadControlPlaneURL: %v", err)
+ }
+ if got != normalizeControlPlaneURLForTest(routerAddr) {
+ t.Errorf("expected control plane URL to be backfilled to %q, got %q", normalizeControlPlaneURLForTest(routerAddr), got)
+ }
+}
+
+// normalizeControlPlaneURLForTest mirrors cmd/sam-node's normalizeControlPlaneURL,
+// duplicated here since that's an unexported function in package main.
+func normalizeControlPlaneURLForTest(url string) string {
+ if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
+ url = "https://" + url
+ }
+ return strings.TrimSuffix(url, "/")
+}
+