Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
cmd/sam-node,cmd/sam-box: never join a public mesh without explicit ack
Antonio Ojeac3ae1eedc32d
diff --git a/cmd/sam-box/main.go b/cmd/sam-box/main.goindex 6675a98c..10dc1680 100644--- a/cmd/sam-box/main.go+++ b/cmd/sam-box/main.go@@ -213,8 +213,6 @@ func main() {if displayControlPlane == "" {if h, err := store.LoadControlPlaneURL(); err == nil && h != "" {displayControlPlane = h- } else {- displayControlPlane = "https://bananas.sam-mesh.dev"}}logger.Infof("No identity found. Starting unauthenticated sidecar for enrollment over MCP...")@@ -392,7 +390,7 @@ func main() {ctx := cmd.Context()targetControlPlane := controlPlaneAddrif targetControlPlane == "" {- targetControlPlane = "https://bananas.sam-mesh.dev"+ logger.Fatal("join needs a control plane to enroll with: pass --control-plane <url> (e.g. https://bananas.sam-mesh.dev for the public testnet).")}if !strings.HasPrefix(targetControlPlane, "http://") && !strings.HasPrefix(targetControlPlane, "https://") {targetControlPlane = "https://" + targetControlPlanediff --git a/cmd/sam-node/daemonize.go b/cmd/sam-node/daemonize.goindex 234bfceb..ae59a336 100644--- a/cmd/sam-node/daemonize.go+++ b/cmd/sam-node/daemonize.go@@ -280,12 +280,10 @@ func confirmPurge(dataDir string) error {if err != nil {return fmt.Errorf("reading the confirmation: %w", err)}- switch strings.ToLower(strings.TrimSpace(answer)) {- case "y", "yes":- return nil- default:+ if !isYesResponse(answer) {return fmt.Errorf("aborted: nothing was deleted")}+ return nil}// nodeStateFiles are the files a node creates in its data directory. Resetdiff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 43474d1e..bf164e0e 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -136,6 +136,13 @@ func isInteractiveTerminal() bool {return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)}+// Public community meshes a node can join without any private control plane+// of its own. Neither is the default without explicit user confirmation.+const (+ publicTestnetControlPlane = "https://bananas.sam-mesh.dev" // open to anyone, deployed from the tip of main+ publicProductionControlPlane = "https://hub.sam-mesh.dev" // open to anyone, deployed from the latest release tag+)+// defaultControlPlane resolves which control plane to use when none was// explicitly passed: the previously stored one, or the public testnet.func defaultControlPlane(store *node.Store, explicit string) string {@@ -145,7 +152,102 @@ func defaultControlPlane(store *node.Store, explicit string) string {if h, err := store.LoadControlPlaneURL(); err == nil && h != "" {return h}- return "https://bananas.sam-mesh.dev"+ return publicTestnetControlPlane+}++// choosePublicMesh explains what bananas.sam-mesh.dev and hub.sam-mesh.dev+// are and asks which (if either) to join, since a node must never join a+// public mesh without the user's explicit ack; passing --control-plane <url>+// is the silent, explicit alternative. Returns the chosen URL, or "" if the+// user declined (the default).+func choosePublicMesh() string {+ fmt.Printf(+ "No control plane specified. Join a public community mesh?\n"++ " 1) %s - open to anyone, deployed from the tip of main (may be unstable)\n"++ " 2) %s - open to anyone, deployed from the latest release tag\n"++ "Choice [1/2] (default: don't join): ",+ publicTestnetControlPlane, publicProductionControlPlane)+ reader := bufio.NewReader(os.Stdin)+ response, err := reader.ReadString('\n')+ if err != nil {+ return ""+ }+ return parseMeshChoice(response)+}++// parseMeshChoice maps a raw prompt answer to the chosen public mesh's URL,+// or "" for anything other than an explicit "1" or "2".+func parseMeshChoice(response string) string {+ switch strings.TrimSpace(response) {+ case "1":+ return publicTestnetControlPlane+ case "2":+ return publicProductionControlPlane+ default:+ return ""+ }+}++// isYesResponse reports whether a raw prompt answer is an explicit "y"/"yes"+// (case/whitespace-insensitive). Anything else, including a blank answer, is+// a "no".+func isYesResponse(response string) bool {+ switch strings.ToLower(strings.TrimSpace(response)) {+ case "y", "yes":+ return true+ default:+ return false+ }+}++// joinAction is the outcome of deciding what "--join" should do, given the+// node's current identity state and how it was invoked. Deciding this is+// pure/side-effect-free so it can be unit tested directly, instead of only+// through slow end-to-end runs of the CLI across every flag combination.+type joinAction int++const (+ // joinSkip: a usable identity is already stored; --join is a no-op.+ joinSkip joinAction = iota+ // joinFatalMismatchNoTTY: --control-plane conflicts with the stored+ // mesh and there's no terminal to confirm switching; must fail fast.+ joinFatalMismatchNoTTY+ // joinFallbackNoTTY: no usable identity and no terminal to run an+ // interactive login; fall back to the unauthenticated MCP sidecar.+ joinFallbackNoTTY+ // joinNeedsConfirmSwitch: --control-plane conflicts with the stored+ // mesh; an interactive terminal must confirm resetting and rejoining.+ joinNeedsConfirmSwitch+ // joinNeedsChooseMesh: no explicit or stored control plane; an+ // interactive terminal must pick a public mesh (or decline).+ joinNeedsChooseMesh+ // joinProceed: enough is known to go straight to interactiveJoin.+ joinProceed+)++// decideJoinAction is the decision table behind "--join": given the node's+// identity state (identityExists, hasPubKey), whether stdin is a real+// terminal, and the explicit vs. stored control planes, it picks exactly one+// of the joinAction outcomes above. It has no side effects.+func decideJoinAction(identityExists, hasPubKey, interactive bool, controlPlaneAddr, stored string) joinAction {+ mismatched := identityExists && controlPlaneAddr != "" && stored != "" &&+ normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored)+ hasUsableIdentity := identityExists && hasPubKey && !mismatched++ switch {+ case hasUsableIdentity:+ return joinSkip+ case mismatched && !interactive:+ return joinFatalMismatchNoTTY+ case !interactive:+ return joinFallbackNoTTY+ case mismatched:+ return joinNeedsConfirmSwitch+ case controlPlaneAddr == "" && stored == "":+ return joinNeedsChooseMesh+ default:+ return joinProceed+ }}// normalizeControlPlaneURL adds the https:// scheme if missing and drops any@@ -326,36 +428,36 @@ func main() {identityExists := len(token) > 0stored, _ := store.LoadControlPlaneURL()mismatched := identityExists && controlPlaneAddr != "" && stored != "" && normalizeControlPlaneURL(controlPlaneAddr) != normalizeControlPlaneURL(stored)- hasUsableIdentity := identityExists && len(controlPlanePubKey) > 0 && !mismatched- switch {- case hasUsableIdentity:+ switch decideJoinAction(identityExists, len(controlPlanePubKey) > 0, isInteractiveTerminal(), controlPlaneAddr, stored) {+ case joinSkip: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():+ case joinFatalMismatchNoTTY: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():+ case joinFallbackNoTTY:logger.Warn("--join set but no interactive terminal available; falling back to out-of-band enrollment via the unauthenticated MCP sidecar")- default:- 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 {+ case joinNeedsConfirmSwitch:+ 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')+ if !isYesResponse(resp) {+ logger.Fatal("Aborted: not switching meshes.")+ }+ if err := store.ResetMeshIdentity(); err != nil {+ logger.Fatalf("Failed to reset stored identity: %v", err)+ }+ fallthrough+ case joinNeedsChooseMesh, joinProceed:+ if !mismatched && identityExists && len(controlPlanePubKey) == 0 {logger.Warn("Stored identity is missing its control plane public key; re-joining")}+ if controlPlaneAddr == "" && stored == "" {+ chosen := choosePublicMesh()+ if chosen == "" {+ logger.Fatal("Aborted: no control plane specified.")+ }+ controlPlaneAddr = chosen+ }targetControlPlane := normalizeControlPlaneURL(defaultControlPlane(store, controlPlaneAddr))jwtStr, controlPlaneInfo, err = interactiveJoin(ctx, store, targetControlPlane)if err != nil {@@ -368,7 +470,14 @@ func main() {if jwtStr == "" && bootstrapTokenFlag == "" {token, _ := store.LoadIdentity()if len(token) == 0 {- displayControlPlane := defaultControlPlane(store, controlPlaneAddr)+ // Explicit-or-stored only: never suggest the public testnet+ // as a join target without the user's explicit ack.+ displayControlPlane := controlPlaneAddr+ if displayControlPlane == "" {+ if h, err := store.LoadControlPlaneURL(); err == nil && h != "" {+ displayControlPlane = h+ }+ }logger.Infof("No identity found. Starting unauthenticated sidecar for enrollment over MCP...")unauthSrv, err := node.StartUnauthSidecarServer(displayControlPlane, bindAddrFlag, resolveSocketPath(cmd), tlsCertFlag, tlsKeyFlag)if err != nil {@@ -388,12 +497,6 @@ func main() {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)- }- }}if len(controlPlanePubKey) == 0 {@@ -611,19 +714,12 @@ func main() {}if targetControlPlane == "" {- fmt.Print("No control plane URL provided. Do you want to join the default community testing network (https://bananas.sam-mesh.dev)? [y/N]: ")- reader := bufio.NewReader(os.Stdin)- response, err := reader.ReadString('\n')- if err != nil {- fmt.Println("\nAborting: failed to read input.")- return- }- response = strings.ToLower(strings.TrimSpace(response))- if response != "y" && response != "yes" {+ chosen := choosePublicMesh()+ if chosen == "" {fmt.Println("Aborting join operation.")return}- targetControlPlane = "https://bananas.sam-mesh.dev"+ targetControlPlane = chosen}targetControlPlane = normalizeControlPlaneURL(targetControlPlane)diff --git a/cmd/sam-node/main_test.go b/cmd/sam-node/main_test.goindex 51692842..7ffc7441 100644--- a/cmd/sam-node/main_test.go+++ b/cmd/sam-node/main_test.go@@ -104,8 +104,8 @@ func TestDefaultControlPlane(t *testing.T) {if got := defaultControlPlane(store, "https://example.com"); got != "https://example.com" {t.Errorf("explicit control plane should win: got %q", got)}- if got := defaultControlPlane(store, ""); got != "https://bananas.sam-mesh.dev" {- t.Errorf("no explicit and no stored URL should default to the testnet: got %q", got)+ if got := defaultControlPlane(store, ""); got != publicTestnetControlPlane {+ t.Errorf("no explicit and no stored URL should default to the public testnet: got %q", got)}if err := store.SaveControlPlaneURL("https://stored.example.com"); err != nil {@@ -115,3 +115,75 @@ func TestDefaultControlPlane(t *testing.T) {t.Errorf("no explicit should fall back to the stored URL: got %q", got)}}++func TestIsYesResponse(t *testing.T) {+ tests := map[string]bool{+ "y": true,+ "Y": true,+ "yes": true,+ "YES\n": true,+ " yes \n": true,+ "n": false,+ "no": false,+ "": false,+ "\n": false,+ "yep": false,+ }+ for in, want := range tests {+ if got := isYesResponse(in); got != want {+ t.Errorf("isYesResponse(%q) = %v, want %v", in, got, want)+ }+ }+}++func TestParseMeshChoice(t *testing.T) {+ tests := map[string]string{+ "1": publicTestnetControlPlane,+ "1\n": publicTestnetControlPlane,+ " 1 \n": publicTestnetControlPlane,+ "2": publicProductionControlPlane,+ "2\n": publicProductionControlPlane,+ "3": "",+ "": "",+ "\n": "",+ "y": "",+ }+ for in, want := range tests {+ if got := parseMeshChoice(in); got != want {+ t.Errorf("parseMeshChoice(%q) = %q, want %q", in, got, want)+ }+ }+}++func TestDecideJoinAction(t *testing.T) {+ const a, b = "https://a.example.com", "https://b.example.com"++ tests := []struct {+ name string+ identityExists, hasPubKey, interactive bool+ controlPlaneAddr, stored string+ want joinAction+ }{+ {"no identity, interactive, no urls: choose mesh", false, false, true, "", "", joinNeedsChooseMesh},+ {"no identity, interactive, explicit url: proceed", false, false, true, a, "", joinProceed},+ {"no identity, non-interactive: fall back", false, false, false, "", "", joinFallbackNoTTY},+ {"usable identity: skip regardless of terminal", true, true, false, "", a, joinSkip},+ {"usable identity, matching explicit url: skip", true, true, true, a, a, joinSkip},+ {"identity missing pubkey, interactive: proceed (re-join)", true, false, true, "", a, joinProceed},+ {"identity missing pubkey, non-interactive: fall back", true, false, false, "", a, joinFallbackNoTTY},+ {"mismatched, interactive: confirm switch", true, true, true, b, a, joinNeedsConfirmSwitch},+ {"mismatched, non-interactive: fatal", true, true, false, b, a, joinFatalMismatchNoTTY},+ {"explicit equals stored (normalized): not a mismatch", true, true, true, a + "/", a, joinSkip},+ {"no identity, interactive, only stored url: proceed", false, false, true, "", a, joinProceed},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := decideJoinAction(tt.identityExists, tt.hasPubKey, tt.interactive, tt.controlPlaneAddr, tt.stored)+ if got != tt.want {+ t.Errorf("decideJoinAction(%v, %v, %v, %q, %q) = %v, want %v",+ tt.identityExists, tt.hasPubKey, tt.interactive, tt.controlPlaneAddr, tt.stored, got, tt.want)+ }+ })+ }+}diff --git a/internal/node/mcp.go b/internal/node/mcp.goindex 1585feec..1af9dcee 100644--- a/internal/node/mcp.go+++ b/internal/node/mcp.go@@ -162,11 +162,16 @@ func NewUnauthenticatedMCPServer(controlPlaneURL string) *mcp.Server {Version: "0.1.0",}, &mcp.ServerOptions{Instructions: "This node is unauthenticated. Use the get_login_instructions tool or help_user_login prompt."})+ joinCmd := "sam-node join"+ if controlPlaneURL != "" {+ joinCmd += " " + controlPlaneURL+ }+mcp.AddTool(mcpServer, &mcp.Tool{Name: "get_login_instructions",Description: "Get instructions on how to authenticate this node so it can join the SAM mesh.",}, func(ctx context.Context, req *mcp.CallToolRequest, params map[string]any) (*mcp.CallToolResult, any, error) {- msg := fmt.Sprintf("The node is unauthenticated. Please open a regular terminal and run:\n\n sam-node join %s\n\nOnce complete, restart this MCP client.", controlPlaneURL)+ msg := fmt.Sprintf("The node is unauthenticated. Please open a regular terminal and run:\n\n %s\n\nOnce complete, restart this MCP client.", joinCmd)return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: msg},@@ -184,7 +189,7 @@ func NewUnauthenticatedMCPServer(controlPlaneURL string) *mcp.Server {{Role: "user",Content: &mcp.TextContent{- Text: fmt.Sprintf("Please open a terminal and run:\n\n`sam-node join %s`\n\nAfter you finish, please restart this MCP connection.", controlPlaneURL),+ Text: fmt.Sprintf("Please open a terminal and run:\n\n`%s`\n\nAfter you finish, please restart this MCP connection.", joinCmd),},},},