Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix(sam-node): pass store into interactiveJoin to persist refresh token
Antonio Ojea91983f4c3a48
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.goindex 9848bdfd..5eda9fd4 100644--- a/cmd/sam-node/main.go+++ b/cmd/sam-node/main.go@@ -153,9 +153,10 @@ func normalizeControlPlaneURL(url string) string {// interactiveJoin discovers a control plane's OIDC settings and completes an// interactive browser/device-code login against it. Shared by "join" and-// "run --join"; targetControlPlane must already be a normalized URL.-func interactiveJoin(ctx context.Context, targetControlPlane string) (string, *api.ControlPlaneInfoResponse, error) {- dummyNode := &node.SamNode{}+// "run --join"; targetControlPlane must already be a normalized URL. store is+// used to persist the OIDC refresh token when --offline-access is set.+func interactiveJoin(ctx context.Context, store *node.Store, targetControlPlane string) (string, *api.ControlPlaneInfoResponse, error) {+ dummyNode := &node.SamNode{Store: store}fmt.Printf("Discovering control plane info from %s...\n", targetControlPlane)info, err := node.FetchControlPlaneInfo(ctx, targetControlPlane)@@ -337,7 +338,7 @@ func main() {logger.Warn("Stored identity is missing its control plane public key; re-joining")}targetControlPlane := normalizeControlPlaneURL(defaultControlPlane(store, controlPlaneAddr))- jwtStr, controlPlaneInfo, err = interactiveJoin(ctx, targetControlPlane)+ jwtStr, controlPlaneInfo, err = interactiveJoin(ctx, store, targetControlPlane)if err != nil {logger.Fatalf("Failed to join: %v", err)}@@ -622,7 +623,7 @@ func main() {var jwtStr stringvar controlPlaneInfo *api.ControlPlaneInfoResponseif bootstrapTokenFlag == "" {- jwtStr, controlPlaneInfo, err = interactiveJoin(ctx, targetControlPlane)+ jwtStr, controlPlaneInfo, err = interactiveJoin(ctx, store, targetControlPlane)if err != nil {logger.Fatalf("Failed to join: %v", err)}diff --git a/tests/integration/login_test.go b/tests/integration/login_test.goindex 7cdc2910..2ade3c4e 100644--- a/tests/integration/login_test.go+++ b/tests/integration/login_test.go@@ -24,6 +24,8 @@ import ("strings""testing""time"++ "github.com/google/sam/internal/node")func TestSamNodeJoin(t *testing.T) {@@ -118,6 +120,85 @@ func TestSamNodeJoin(t *testing.T) {}}+// TestSamNodeJoinOfflineAccessSavesRefreshToken guards against a real bug:+// interactiveJoin used a *node.SamNode with no Store, so InteractiveLogin's+// "if n.Store != nil" guard silently skipped persisting the refresh token,+// breaking --offline-access for both "join" and "run --join".+func TestSamNodeJoinOfflineAccessSavesRefreshToken(t *testing.T) {+ nodeBin := buildBinary(t, "./cmd/sam-node")+ tmpHome := t.TempDir()+ env := append(os.Environ(),+ "HOME="+tmpHome,+ "XDG_CONFIG_HOME="+filepath.Join(tmpHome, ".config"),+ "BROWSER=echo",+ )++ mux := http.NewServeMux()+ mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ if err := json.NewEncoder(w).Encode(map[string]interface{}{+ "issuer": "http://" + r.Host,+ "token_endpoint": "http://" + r.Host + "/token",+ "authorization_endpoint": "http://" + r.Host + "/auth",+ }); err != nil {+ t.Errorf("Failed to encode response: %v", err)+ }+ })+ mux.HandleFunc("/device/code", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ if err := json.NewEncoder(w).Encode(map[string]interface{}{+ "device_code": "dev_code_123",+ "user_code": "ABCD-1234",+ "verification_uri": "http://example.com/verify",+ "verification_uri_complete": "http://example.com/verify?code=ABCD-1234",+ "expires_in": 60,+ "interval": 1,+ }); err != nil {+ t.Errorf("Failed to encode response: %v", err)+ }+ })+ mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ if err := json.NewEncoder(w).Encode(map[string]string{+ "access_token": "test-jwt-token",+ "id_token": "test-jwt-token",+ "refresh_token": "test-refresh-token",+ }); err != nil {+ t.Errorf("Failed to encode response: %v", err)+ }+ })+ oidcServer := httptest.NewServer(mux)+ defer oidcServer.Close()++ _, routerAddr := startMockRouterWithOIDC(t, oidcServer.URL)++ stdout, stderr, err := runCommandWithCallback(+ t, repoRoot(t), 5*time.Second, env, "",+ nodeBin, "join", "--offline-access", routerAddr,+ )+ if err != nil {+ t.Fatalf("join command failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout, stderr)+ }+ if !strings.Contains(stdout+stderr, "Successfully joined the Sovereign Agent Mesh!") {+ t.Fatalf("join did not succeed:\n%s", stdout+stderr)+ }++ store, err := node.NewStore(filepath.Join(tmpHome, ".config", "sam-mesh"))+ if err != nil {+ t.Fatalf("failed to open store: %v", err)+ }+ defer func() { _ = store.Close() }()++ refreshToken, err := store.LoadRefreshToken()+ if err != nil {+ t.Fatalf("expected a refresh token to be saved after --offline-access join, got error: %v", err)+ }+ if refreshToken != "test-refresh-token" {+ t.Errorf("expected saved refresh token %q, got %q", "test-refresh-token", refreshToken)+ }+}++// TestSamNodeRunJoinNonInteractive covers the safe fallback: since the test// harness gives the child process no TTY (stdin defaults to /dev/null),// "run --join" must not attempt (and block on) an interactive OIDC login; it