diff --git a/capetest/capetest.go b/capetest/capetest.go index 79aa3fca..a55bd8c5 100644 --- a/capetest/capetest.go +++ b/capetest/capetest.go @@ -2,8 +2,11 @@ package capetest import ( "crypto/tls" + "encoding/json" "net/http" + log "github.com/sirupsen/logrus" + "github.com/gorilla/websocket" "github.com/capeprivacy/cli/attest" @@ -31,6 +34,11 @@ type Message struct { Message []byte `json:"message"` } +// TODO -- cmd package also defines this +type ErrorMsg struct { + Error string `json:"error"` +} + // TODO -- cmd package also defines this func websocketDial(url string, insecure bool) (*websocket.Conn, *http.Response, error) { if insecure { @@ -43,8 +51,15 @@ func websocketDial(url string, insecure bool) (*websocket.Conn, *http.Response, } func CapeTest(testReq TestRequest, endpoint string, insecure bool) (*RunResults, error) { - conn, _, err := websocketDial(endpoint, insecure) + conn, resp, err := websocketDial(endpoint, insecure) + defer resp.Body.Close() if err != nil { + log.Error("error dialing websocket", err) + var e ErrorMsg + if err := json.NewDecoder(resp.Body).Decode(&e); err != nil { + return nil, err + } + log.Errorf("error code: %d, reason: %s", resp.StatusCode, e.Error) return nil, err } diff --git a/cmd/cape/cmd/deploy.go b/cmd/cape/cmd/deploy.go index 3161e29c..5ef0c6e8 100644 --- a/cmd/cape/cmd/deploy.go +++ b/cmd/cape/cmd/deploy.go @@ -4,6 +4,7 @@ import ( "archive/zip" "bytes" "crypto/tls" + "encoding/json" "fmt" "io" "net/http" @@ -24,6 +25,10 @@ import ( czip "github.com/capeprivacy/cli/zip" ) +type ErrorMsg struct { + Error string `json:"error"` +} + type DeployRequest struct { Nonce string `json:"nonce"` AuthToken string `json:"auth_token"` @@ -172,9 +177,15 @@ func doDeploy(url string, name string, reader io.Reader, insecure bool) (string, s.Prefix = "Deploying function to Cape " s.Start() - conn, _, err := websocketDial(endpoint, insecure) + conn, res, err := websocketDial(endpoint, insecure) + defer res.Body.Close() if err != nil { log.Error("error dialing websocket", err) + var e ErrorMsg + if err := json.NewDecoder(res.Body).Decode(&e); err != nil { + return "", err + } + log.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error) return "", err } diff --git a/cmd/cape/cmd/run.go b/cmd/cape/cmd/run.go index 58db992d..bfa195f2 100644 --- a/cmd/cape/cmd/run.go +++ b/cmd/cape/cmd/run.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "encoding/json" "fmt" "io" "io/ioutil" @@ -113,11 +114,16 @@ func doRun(url string, functionID string, data []byte, insecure bool) ([]byte, e endpoint := fmt.Sprintf("%s/v1/run/%s", url, functionID) c, res, err := websocketDial(endpoint, insecure) + defer res.Body.Close() if err != nil { - log.Println("error dialing websocket", res) + log.Error("error dialing websocket", err) + var e ErrorMsg + if err := json.NewDecoder(res.Body).Decode(&e); err != nil { + return nil, err + } + log.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error) return nil, err } - nonce, err := crypto.GetNonce() if err != nil { return nil, err @@ -134,6 +140,15 @@ func doRun(url string, functionID string, data []byte, insecure bool) ([]byte, e log.Println("error writing deploy request") return nil, err } + // Try to read message + t, socketMsg, err := c.ReadMessage() + if err != nil { + log.Error("failed to fetch response", err) + } + + if t == websocket.CloseMessage { + log.Errorf("failed to run with: %s", string(socketMsg)) + } var msg Message err = c.ReadJSON(&msg)