@@ -17,7 +17,6 @@ limitations under the License.
1717package argocdclient
1818
1919import (
20- "bytes"
2120 "crypto/tls"
2221 "encoding/json"
2322 "errors"
@@ -33,72 +32,6 @@ import (
3332 "github.com/gorilla/websocket"
3433)
3534
36- type ArgoRestClient struct {
37- endpoint string
38- username string
39- password string
40- token string
41- client * http.Client
42- }
43-
44- // NewArgoClient returns a new client for Argo CD's REST API
45- func NewArgoClient (endpoint , username , password string ) * ArgoRestClient {
46- ac := & ArgoRestClient {
47- endpoint : endpoint ,
48- username : username ,
49- password : password ,
50- client : & http.Client {
51- Transport : & http.Transport {
52- TLSClientConfig : & tls.Config {
53- InsecureSkipVerify : true , // #nosec G402
54- },
55- },
56- },
57- }
58- return ac
59- }
60-
61- // Login creates a new Argo CD session
62- func (c * ArgoRestClient ) Login () error {
63- // Get session token from API
64- authStr := fmt .Sprintf (`{"username": "%s", "password": "%s"}` , c .username , c .password )
65- payload := io .NopCloser (bytes .NewReader ([]byte (authStr )))
66- res , err := c .client .Do (& http.Request {
67- Method : http .MethodPost ,
68- URL : & url.URL {Scheme : "https" , Host : c .endpoint , Path : "/api/v1/session" },
69- Body : payload ,
70- Header : http.Header {"Content-Type" : []string {"application/json" }},
71- ContentLength : int64 (len (authStr )),
72- })
73- if err != nil {
74- return err
75- }
76- defer func () {
77- _ = res .Body .Close ()
78- }()
79- if res .StatusCode != 200 {
80- return fmt .Errorf ("expected HTTP 200, got %d" , res .StatusCode )
81- }
82- body , err := io .ReadAll (res .Body )
83- if err != nil {
84- return err
85- }
86-
87- type tokenResponse struct {
88- Token string `json:"token"`
89- }
90- token := & tokenResponse {}
91- err = json .Unmarshal (body , token )
92- if err != nil {
93- return err
94- }
95- if token .Token == "" {
96- return errors .New ("empty token received" )
97- }
98- c .token = token .Token
99- return nil
100- }
101-
10235// TerminalClient represents a test client for terminal WebSocket connections.
10336type TerminalClient struct {
10437 wsConn * websocket.Conn
@@ -111,15 +44,10 @@ type TerminalClient struct {
11144// ExecTerminal opens a terminal session to a pod via WebSocket.
11245// This replicates the behavior of the ArgoCD UI when a user opens a terminal session to an application.
11346// ArgoCD decides which shell to use based on the configured allowed shells.
114- func (c * ArgoRestClient ) ExecTerminal (app * v1alpha1.Application , namespace , podName , container string ) (* TerminalClient , error ) {
115- if err := c .ensureToken (); err != nil {
116- return nil , err
117- }
118-
119- // Build the exec URL
47+ func ExecTerminal (endpoint , token string , app * v1alpha1.Application , namespace , podName , container string ) (* TerminalClient , error ) {
12048 u := & url.URL {
12149 Scheme : "wss" ,
122- Host : c . endpoint ,
50+ Host : endpoint ,
12351 Path : "/terminal" ,
12452 }
12553
@@ -132,18 +60,15 @@ func (c *ArgoRestClient) ExecTerminal(app *v1alpha1.Application, namespace, podN
13260 q .Set ("namespace" , namespace )
13361 u .RawQuery = q .Encode ()
13462
135- // Create WebSocket dialer with TLS config
13663 dialer := websocket.Dialer {
13764 TLSClientConfig : & tls.Config {
13865 InsecureSkipVerify : true , // #nosec G402
13966 },
14067 }
14168
142- // Set token as cookie - ArgoCD expects auth token in argocd.token cookie
14369 headers := http.Header {}
144- headers .Set ("Cookie" , fmt .Sprintf ("argocd.token=%s" , c . token ))
70+ headers .Set ("Cookie" , fmt .Sprintf ("argocd.token=%s" , token ))
14571
146- // Connect to WebSocket
14772 wsConn , resp , err := dialer .Dial (u .String (), headers )
14873 if err != nil {
14974 if resp != nil {
@@ -158,20 +83,11 @@ func (c *ArgoRestClient) ExecTerminal(app *v1alpha1.Application, namespace, podN
15883 wsConn : wsConn ,
15984 }
16085
161- // Start reading output in background
16286 go session .readOutput ()
16387
16488 return session , nil
16589}
16690
167- // ensureToken makes sure we have a valid authentication token
168- func (c * ArgoRestClient ) ensureToken () error {
169- if c .token == "" {
170- return c .Login ()
171- }
172- return nil
173- }
174-
17591// terminalMessage is the JSON message format used by ArgoCD terminal WebSocket
17692type terminalMessage struct {
17793 Operation string `json:"operation"`
0 commit comments