Skip to content

Commit

Permalink
fix -o for cape run (#255)
Browse files Browse the repository at this point in the history
this previously always output plain output
  • Loading branch information
bendecoste authored Mar 22, 2023
1 parent 07784b6 commit d46aae6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 35 deletions.
6 changes: 6 additions & 0 deletions cape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cli

type RunResult struct {
Type string `json:"type"`
Message []byte `json:"message"`
}
4 changes: 2 additions & 2 deletions cmd/cape/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ func doList(url string, insecure bool, auth entities.FunctionAuth, limit int, of
return nil
}

if f, ok := formatters[format]; ok {
if f, ok := listFormatters[format]; ok {
return f(deploymentNames)
}

return printTable(deploymentNames)
}

var formatters = map[string]func([]entities.Deployment) error{
var listFormatters = map[string]func([]entities.Deployment) error{
"plain": printTable,
"json": printJSON,
}
Expand Down
33 changes: 31 additions & 2 deletions cmd/cape/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -12,6 +13,8 @@ import (

"github.com/spf13/cobra"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/cli/entities"
"github.com/capeprivacy/cli/sdk"
)
Expand Down Expand Up @@ -78,6 +81,15 @@ func init() {
func run(cmd *cobra.Command, args []string) error {
u := C.EnclaveHost
insecure := C.Insecure
output, err := cmd.Flags().GetString("output")
if err != nil {
return err
}

formatter, ok := runFormatters[output]
if !ok {
return fmt.Errorf("unknown output option: %s", output)
}

if len(args) < 1 {
return UserError{Msg: "you must pass a function ID or a function name", Err: fmt.Errorf("invalid number of input arguments")}
Expand Down Expand Up @@ -155,8 +167,7 @@ func run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("run request failed: %w", err)
}

fmt.Println(string(results))
return nil
return formatter(*results)
}

func isValidFunctionID(functionID string) bool {
Expand Down Expand Up @@ -204,3 +215,21 @@ func getInput(cmd *cobra.Command, args []string, file string) ([]byte, error) {
return nil, UserError{Msg: "invalid input", Err: errors.New("please provide input as a string, input file or stdin")}
}
}

var runFormatters = map[string]func(result cli.RunResult) error{
"plain": runPlain,
"json": runJSON,
}

func runPlain(result cli.RunResult) error {
fmt.Println(string(result.Message))
return nil
}

func runJSON(result cli.RunResult) error {
return json.NewEncoder(os.Stdout).Encode(struct {
Output string `json:"output"`
}{
Output: string(result.Message),
})
}
29 changes: 15 additions & 14 deletions cmd/cape/cmd/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/capeprivacy/cli/entities"
"github.com/capeprivacy/cli"

"github.com/capeprivacy/cli/sdk"
czip "github.com/capeprivacy/cli/zip"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestServerError(t *testing.T) {
cmd.SetArgs([]string{"test", "testdata/my_fn", "hello world"})

errMsg := "something went wrong"
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
return nil, errors.New(errMsg)
}
authToken = func() (string, error) {
Expand Down Expand Up @@ -114,9 +115,9 @@ func TestSuccess(t *testing.T) {
results := "success!"
var gotFn []byte
var gotInput []byte
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
gotFn, gotInput = testReq.Function, testReq.Input
return &entities.RunResults{Message: []byte(results)}, nil
return &cli.RunResult{Message: []byte(results)}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand Down Expand Up @@ -158,9 +159,9 @@ func TestSuccessStdin(t *testing.T) {
results := "success!"
var gotFn []byte
var gotInput []byte
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
gotFn, gotInput = testReq.Function, testReq.Input
return &entities.RunResults{Message: []byte(results)}, nil
return &cli.RunResult{Message: []byte(results)}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand Down Expand Up @@ -202,7 +203,7 @@ func TestWSConnection(t *testing.T) {
type msg struct {
Message []byte `json:"msg"`
}
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
c, _, err := websocket.DefaultDialer.Dial(endpoint, nil)
if err != nil {
return nil, err
Expand All @@ -214,7 +215,7 @@ func TestWSConnection(t *testing.T) {
return nil, err
}

return &entities.RunResults{Message: m.Message}, nil
return &cli.RunResult{Message: m.Message}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand Down Expand Up @@ -260,9 +261,9 @@ func TestWSConnection(t *testing.T) {
func TestEndpoint(t *testing.T) {
// ensure that `cape test` hits the `/v1/test` endpoint
endpointHit := ""
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
endpointHit = endpoint
return &entities.RunResults{Message: []byte("good job")}, nil
return &cli.RunResult{Message: []byte("good job")}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand All @@ -288,9 +289,9 @@ func TestEndpoint(t *testing.T) {
func TestEnvVarConfigEndpoint(t *testing.T) {
// ensure that env var overrides work for hostname
endpointHit := ""
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
endpointHit = endpoint
return &entities.RunResults{Message: []byte("good job")}, nil
return &cli.RunResult{Message: []byte("good job")}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand Down Expand Up @@ -322,9 +323,9 @@ func TestFileConfigEndpoint(t *testing.T) {
fileEndpoint := "https://foo_file.capeprivacy.com"
oldEndpoint := os.Getenv("CAPE_ENCLAVE_HOST")

test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
test = func(testReq sdk.TestRequest, verifier sdk.Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
endpointHit = endpoint
return &entities.RunResults{Message: []byte("good job")}, nil
return &cli.RunResult{Message: []byte("good job")}, nil
}
authToken = func() (string, error) {
return "so logged in", nil
Expand Down
5 changes: 0 additions & 5 deletions entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ type AttestationWrapper struct {
Message []byte `json:"message"`
}

type RunResults struct {
Type string `json:"type"`
Message []byte `json:"message"`
}

type AuthenticationType string

func (a AuthenticationType) Validate() error {
Expand Down
8 changes: 5 additions & 3 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/gorilla/websocket"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/cli/entities"
)

Expand Down Expand Up @@ -78,11 +80,11 @@ func (p Protocol) ReadDeploymentResults() (*entities.SetDeploymentIDRequest, err
return readMsg[entities.SetDeploymentIDRequest](p.Websocket)
}

func (p Protocol) ReadRunResults() (*entities.RunResults, error) {
return readMsg[entities.RunResults](p.Websocket)
func (p Protocol) ReadRunResults() (*cli.RunResult, error) {
return readMsg[cli.RunResult](p.Websocket)
}

func (p Protocol) WriteRunResults(results entities.RunResults) error {
func (p Protocol) WriteRunResults(results cli.RunResult) error {
return writeMsg(p.Websocket, results)
}

Expand Down
4 changes: 3 additions & 1 deletion sdk/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/attest/attest"
"github.com/capeprivacy/cli/crypto"
"github.com/capeprivacy/cli/entities"
Expand All @@ -19,7 +21,7 @@ import (
type protocol interface {
WriteStart(request entities.StartRequest) error
ReadAttestationDoc() ([]byte, error)
ReadRunResults() (*entities.RunResults, error)
ReadRunResults() (*cli.RunResult, error)
WriteBinary([]byte) error
WriteFunctionInfo(name string, public bool) error
ReadDeploymentResults() (*entities.SetDeploymentIDRequest, error)
Expand Down
8 changes: 5 additions & 3 deletions sdk/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/attest/attest"
"github.com/capeprivacy/cli/crypto"
"github.com/capeprivacy/cli/entities"
Expand All @@ -29,7 +31,7 @@ type RunRequest struct {
}

// Run loads the given function into a secure enclave and invokes it on the given data, then returns the result.
func Run(req RunRequest) ([]byte, error) {
func Run(req RunRequest) (*cli.RunResult, error) {
conn, doc, err := connect(req.URL, req.FunctionID, req.FunctionAuth, req.FuncChecksum, req.KeyChecksum, req.PcrSlice, req.Insecure)
if err != nil {
return nil, err
Expand Down Expand Up @@ -115,7 +117,7 @@ func connect(url string, functionID string, functionAuth entities.FunctionAuth,
return conn, doc, nil
}

func invoke(doc *attest.AttestationDoc, conn *websocket.Conn, data []byte) ([]byte, error) {
func invoke(doc *attest.AttestationDoc, conn *websocket.Conn, data []byte) (*cli.RunResult, error) {
if doc == nil {
log.Error("missing attestation document, you may need to run cape.Connect()")
return nil, errors.New("missing attestation document")
Expand Down Expand Up @@ -145,7 +147,7 @@ func invoke(doc *attest.AttestationDoc, conn *websocket.Conn, data []byte) ([]by
}
log.Debugf("< Received Function Results.")

return resData.Message, nil
return resData, nil
}

func writeData(conn *websocket.Conn, data []byte) error {
Expand Down
4 changes: 3 additions & 1 deletion sdk/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/attest/attest"
"github.com/capeprivacy/cli/crypto"
"github.com/capeprivacy/cli/entities"
Expand All @@ -38,7 +40,7 @@ type ErrorMsg struct {
// Test simulates the workflow of Deploy and Run, without storing the function.
// It loads the given function into an enclave, runs it on the given data, and returns the result.
// Use Test to verify that your function will work before storing it via Deploy.
func Test(testReq TestRequest, verifier Verifier, endpoint string, pcrSlice []string) (*entities.RunResults, error) {
func Test(testReq TestRequest, verifier Verifier, endpoint string, pcrSlice []string) (*cli.RunResult, error) {
conn, err := doDial(endpoint, testReq.Insecure, "cape.runtime", testReq.AuthToken)
if err != nil {
return nil, err
Expand Down
10 changes: 6 additions & 4 deletions sdk/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

"github.com/gorilla/websocket"

"github.com/capeprivacy/cli"

"github.com/capeprivacy/attest/attest"
"github.com/capeprivacy/cli/entities"
"github.com/capeprivacy/cli/mocks"
Expand All @@ -16,7 +18,7 @@ import (
type testProtocol struct {
start func(req entities.StartRequest) error
attest func() ([]byte, error)
results func() (*entities.RunResults, error)
results func() (*cli.RunResult, error)
binary func(b []byte) error
}

Expand All @@ -32,7 +34,7 @@ func (t testProtocol) WriteStart(request entities.StartRequest) error {
return t.start(request)
}
func (t testProtocol) ReadAttestationDoc() ([]byte, error) { return t.attest() }
func (t testProtocol) ReadRunResults() (*entities.RunResults, error) {
func (t testProtocol) ReadRunResults() (*cli.RunResult, error) {
return t.results()
}
func (t testProtocol) WriteBinary(bytes []byte) error { return t.binary(bytes) }
Expand All @@ -57,8 +59,8 @@ func TestCapeTest(t *testing.T) {
return testProtocol{
start: func(req entities.StartRequest) error { return nil },
attest: func() ([]byte, error) { return []byte{}, nil },
results: func() (*entities.RunResults, error) {
return &entities.RunResults{Message: []byte("good job")}, nil
results: func() (*cli.RunResult, error) {
return &cli.RunResult{Message: []byte("good job")}, nil
},
binary: func(b []byte) error { return nil },
}
Expand Down

0 comments on commit d46aae6

Please sign in to comment.