@@ -2,6 +2,7 @@ package bridge
22
33import (
44 "bytes"
5+ "context"
56 "encoding/base64"
67 "errors"
78 "io"
@@ -11,6 +12,7 @@ import (
1112
1213 "github.com/acarl005/stripansi"
1314 "github.com/hirochachacha/go-smb2"
15+ "github.com/limanmys/render-engine/pkg/logger"
1416 "github.com/masterzen/winrm"
1517 "github.com/pkg/sftp"
1618 "golang.org/x/crypto/ssh"
@@ -35,23 +37,36 @@ type Session struct {
3537
3638func (s * Session ) CloseAllConnections () {
3739 if s .SSH != nil {
38- s .SSH .Close ()
40+ err := s .SSH .Close ()
41+ if err != nil {
42+ logger .Sugar ().Warnw ("cannot close ssh session" )
43+ }
3944 }
4045
4146 if s .SFTP != nil {
42- s .SFTP .Close ()
47+ err := s .SFTP .Close ()
48+ if err != nil {
49+ logger .Sugar ().Warnw ("cannot close sftp session" )
50+ }
4351 }
4452
4553 if s .SMB != nil {
46- s .SMB .Logoff ()
54+ err := s .SMB .Logoff ()
55+ if err != nil {
56+ logger .Sugar ().Warnw ("cannot close smb session" )
57+ }
4758 }
4859}
4960
5061func (val * Session ) checkOutput (in io.Writer , output * bytes.Buffer ) bool {
5162 val .Mutex .Lock ()
5263 defer val .Mutex .Unlock ()
5364 if output != nil && output .Len () > 0 && strings .Contains (output .String (), "liman-pass-sudo" ) {
54- in .Write ([]byte (val .password + "\n " ))
65+ _ , err := in .Write ([]byte (val .password + "\n " ))
66+ if err != nil {
67+ logger .Sugar ().Warnw ("cannot write sudo password" )
68+ return false
69+ }
5570 return true
5671 }
5772 return false
@@ -99,7 +114,10 @@ func (val *Session) Run(command string) (string, error) {
99114 }
100115 }(in , stdoutB , endChan )
101116 }
102- sess .Run ("(" + command + ") 2> /dev/null" )
117+ err = sess .Run ("(" + command + ") 2> /dev/null" )
118+ if err != nil {
119+ return err .Error (), err
120+ }
103121
104122 tmp := strings .Split (stdoutB .String (), "liman-pass-sudo" )
105123 output := tmp [len (tmp )- 1 ]
@@ -110,7 +128,7 @@ func (val *Session) Run(command string) (string, error) {
110128 encoder := unicode .UTF16 (unicode .LittleEndian , unicode .IgnoreBOM ).NewEncoder ()
111129 encoded , _ := encoder .String (command )
112130 command = base64 .StdEncoding .EncodeToString ([]byte (encoded ))
113- stdout , stderr , _ , err := val .WinRM .RunWithString ( "powershell.exe -encodedCommand " + command , "" )
131+ stdout , stderr , _ , err := val .WinRM .RunWithContextWithString ( context . TODO (), "powershell.exe -encodedCommand " + command , "" )
114132 if err != nil {
115133 return "" , err
116134 }
0 commit comments