|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package argocdclient |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto/tls" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "io" |
| 25 | + "net/http" |
| 26 | + "net/url" |
| 27 | + "strings" |
| 28 | + "sync" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" |
| 32 | + "github.com/gorilla/websocket" |
| 33 | +) |
| 34 | + |
| 35 | +// TerminalClient represents a test client for terminal WebSocket connections. |
| 36 | +type TerminalClient struct { |
| 37 | + wsConn *websocket.Conn |
| 38 | + mu sync.Mutex |
| 39 | + closed bool |
| 40 | + output strings.Builder |
| 41 | + outputMu sync.Mutex |
| 42 | +} |
| 43 | + |
| 44 | +// ExecTerminal opens a terminal session to a pod via WebSocket. |
| 45 | +// This replicates the behavior of the ArgoCD UI when a user opens a terminal session to an application. |
| 46 | +// ArgoCD decides which shell to use based on the configured allowed shells. |
| 47 | +func ExecTerminal(endpoint, token string, app *v1alpha1.Application, namespace, podName, container string) (*TerminalClient, error) { |
| 48 | + u := &url.URL{ |
| 49 | + Scheme: "wss", |
| 50 | + Host: endpoint, |
| 51 | + Path: "/terminal", |
| 52 | + } |
| 53 | + |
| 54 | + q := u.Query() |
| 55 | + q.Set("pod", podName) |
| 56 | + q.Set("container", container) |
| 57 | + q.Set("appName", app.Name) |
| 58 | + q.Set("appNamespace", app.Namespace) |
| 59 | + q.Set("projectName", app.Spec.Project) |
| 60 | + q.Set("namespace", namespace) |
| 61 | + u.RawQuery = q.Encode() |
| 62 | + |
| 63 | + dialer := websocket.Dialer{ |
| 64 | + TLSClientConfig: &tls.Config{ |
| 65 | + InsecureSkipVerify: true, // #nosec G402 |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + headers := http.Header{} |
| 70 | + headers.Set("Cookie", fmt.Sprintf("argocd.token=%s", token)) |
| 71 | + |
| 72 | + wsConn, resp, err := dialer.Dial(u.String(), headers) |
| 73 | + if err != nil { |
| 74 | + if resp != nil { |
| 75 | + defer resp.Body.Close() |
| 76 | + body, _ := io.ReadAll(resp.Body) |
| 77 | + return nil, fmt.Errorf("failed to connect to terminal WebSocket: %w (status: %d, body: %s)", err, resp.StatusCode, string(body)) |
| 78 | + } |
| 79 | + return nil, fmt.Errorf("failed to connect to terminal WebSocket: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + session := &TerminalClient{ |
| 83 | + wsConn: wsConn, |
| 84 | + } |
| 85 | + |
| 86 | + go session.readOutput() |
| 87 | + |
| 88 | + return session, nil |
| 89 | +} |
| 90 | + |
| 91 | +// terminalMessage is the JSON message format used by ArgoCD terminal WebSocket |
| 92 | +type terminalMessage struct { |
| 93 | + Operation string `json:"operation"` |
| 94 | + Data string `json:"data"` |
| 95 | + Rows uint16 `json:"rows"` |
| 96 | + Cols uint16 `json:"cols"` |
| 97 | +} |
| 98 | + |
| 99 | +// readOutput continuously reads output from the WebSocket connection |
| 100 | +func (s *TerminalClient) readOutput() { |
| 101 | + for { |
| 102 | + _, message, err := s.wsConn.ReadMessage() |
| 103 | + if err != nil { |
| 104 | + // Connection closed or error |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + if len(message) < 1 { |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + // Parse JSON message |
| 113 | + var msg terminalMessage |
| 114 | + if err := json.Unmarshal(message, &msg); err != nil { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + switch msg.Operation { |
| 119 | + case "stdout": |
| 120 | + s.outputMu.Lock() |
| 121 | + s.output.WriteString(msg.Data) |
| 122 | + s.outputMu.Unlock() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// SendInput sends input to the terminal session |
| 128 | +func (s *TerminalClient) SendInput(input string) error { |
| 129 | + s.mu.Lock() |
| 130 | + defer s.mu.Unlock() |
| 131 | + |
| 132 | + if s.closed { |
| 133 | + return errors.New("session is closed") |
| 134 | + } |
| 135 | + |
| 136 | + // ArgoCD terminal uses JSON messages (includes rows/cols like the UI) |
| 137 | + msg, err := json.Marshal(terminalMessage{ |
| 138 | + Operation: "stdin", |
| 139 | + Data: input, |
| 140 | + Rows: 24, |
| 141 | + Cols: 80, |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + return s.wsConn.WriteMessage(websocket.TextMessage, msg) |
| 147 | +} |
| 148 | + |
| 149 | +// SendResize sends a terminal resize message |
| 150 | +func (s *TerminalClient) SendResize(cols, rows uint16) error { |
| 151 | + s.mu.Lock() |
| 152 | + defer s.mu.Unlock() |
| 153 | + |
| 154 | + if s.closed { |
| 155 | + return errors.New("session is closed") |
| 156 | + } |
| 157 | + |
| 158 | + // ArgoCD terminal uses JSON messages |
| 159 | + msg, err := json.Marshal(terminalMessage{ |
| 160 | + Operation: "resize", |
| 161 | + Cols: cols, |
| 162 | + Rows: rows, |
| 163 | + }) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + return s.wsConn.WriteMessage(websocket.TextMessage, msg) |
| 168 | +} |
| 169 | + |
| 170 | +// GetOutput returns all captured output so far |
| 171 | +func (s *TerminalClient) GetOutput() string { |
| 172 | + s.outputMu.Lock() |
| 173 | + defer s.outputMu.Unlock() |
| 174 | + return s.output.String() |
| 175 | +} |
| 176 | + |
| 177 | +// WaitForOutput waits until the output contains the expected string or timeout |
| 178 | +func (s *TerminalClient) WaitForOutput(expected string, timeout time.Duration) bool { |
| 179 | + deadline := time.Now().Add(timeout) |
| 180 | + for time.Now().Before(deadline) { |
| 181 | + if strings.Contains(s.GetOutput(), expected) { |
| 182 | + return true |
| 183 | + } |
| 184 | + time.Sleep(100 * time.Millisecond) |
| 185 | + } |
| 186 | + return false |
| 187 | +} |
| 188 | + |
| 189 | +// Close closes the terminal session |
| 190 | +func (s *TerminalClient) Close() error { |
| 191 | + s.mu.Lock() |
| 192 | + defer s.mu.Unlock() |
| 193 | + |
| 194 | + if s.closed { |
| 195 | + return nil |
| 196 | + } |
| 197 | + s.closed = true |
| 198 | + return s.wsConn.Close() |
| 199 | +} |
0 commit comments