Skip to content

Commit

Permalink
have CLI process error values returned by error-sentinel and sentinel (
Browse files Browse the repository at this point in the history
…#88)

add http resp parsing
  • Loading branch information
eric-capeprivacy authored and gavinuhma committed Jul 14, 2022
1 parent d224f74 commit cee4200
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
17 changes: 16 additions & 1 deletion capetest/capetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
13 changes: 12 additions & 1 deletion cmd/cape/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 17 additions & 2 deletions cmd/cape/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit cee4200

Please sign in to comment.