-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
282 lines (232 loc) · 7.61 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cmd
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"github.com/gorilla/websocket"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/capeprivacy/cli/attest"
"github.com/capeprivacy/cli/crypto"
"github.com/capeprivacy/cli/entities"
"github.com/capeprivacy/cli/protocol"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run function_id [input data]",
Short: "Run a deployed function with data",
Long: "Run a deployed function with data, takes function id, path to data, and (optional) function hash.\n" +
"Run will also read input data from stdin, example: \"echo '1234' | cape run id\".\n" +
"Results are output to stdout so you can easily pipe them elsewhere.",
RunE: func(cmd *cobra.Command, args []string) error {
err := run(cmd, args)
if _, ok := err.(UserError); !ok {
cmd.SilenceUsage = true
}
return err
},
}
type RunResponse struct {
Type string `json:"type"`
Message []byte `json:"message"`
}
type AttestationResponse struct {
AttestationDoc string `json:"attestation_doc"`
}
type Message struct {
Type string `json:"type"`
Message []byte `json:"message"`
}
type ErrorRunResponse struct {
Message string `json:"message"`
}
func init() {
rootCmd.AddCommand(runCmd)
runCmd.PersistentFlags().StringP("token", "t", "", "function token to use")
runCmd.PersistentFlags().StringP("file", "f", "", "input data file")
runCmd.PersistentFlags().StringP("function-hash", "", "", "function hash to attest")
runCmd.PersistentFlags().StringP("key-policy-hash", "", "", "key policy hash to attest")
runCmd.PersistentFlags().StringSliceP("pcr", "p", []string{""}, "pass multiple PCRs to validate against")
}
func run(cmd *cobra.Command, args []string) error {
u := C.EnclaveHost
insecure := C.Insecure
if len(args) < 1 {
return UserError{Msg: "you must pass a function ID", Err: fmt.Errorf("invalid number of input arguments")}
}
functionID := args[0]
var input []byte
file, err := cmd.Flags().GetString("file")
if err != nil {
return UserError{Msg: "error retrieving file flag", Err: err}
}
funcHashArg, err := cmd.Flags().GetString("function-hash")
if err != nil {
return UserError{Msg: "error retrieving function_hash flag", Err: err}
}
pcrSlice, err := cmd.Flags().GetStringSlice("pcr")
if err != nil {
return UserError{Msg: "error retrieving pcr flags", Err: err}
}
funcHash, err := hex.DecodeString(funcHashArg)
if err != nil {
return UserError{Msg: "error reading function hash", Err: err}
}
keyPolicyHashArg, err := cmd.Flags().GetString("key-policy-hash")
if err != nil {
return UserError{Msg: "error retrieving key_policy_hash flag", Err: err}
}
keyPolicyHash, err := hex.DecodeString(keyPolicyHashArg)
if err != nil {
return UserError{Msg: "error reading key policy hash", Err: err}
}
functionToken, _ := cmd.Flags().GetString("token")
switch {
case file != "":
// input file was provided
input, err = os.ReadFile(file)
if err != nil {
return UserError{Msg: "unable to read data file", Err: err}
}
case len(args) == 2:
// read input from command line string
input = []byte(args[1])
default:
// read input from stdin
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, cmd.InOrStdin()); err != nil {
return UserError{Msg: "unable to read data from stdin", Err: err}
}
input = buf.Bytes()
}
t, err := getAuthToken()
if err != nil {
return err
}
auth := entities.FunctionAuth{Type: entities.AuthenticationTypeAuth0, Token: t}
if functionToken != "" {
auth.Type = entities.AuthenticationTypeFunctionToken
auth.Token = functionToken
}
results, err := doRun(u, functionID, input, insecure, funcHash, auth, keyPolicyHash, pcrSlice)
if err != nil {
return fmt.Errorf("error processing data: %w", err)
}
fmt.Println(string(results))
return nil
}
// This function is exported for tuner to use.
func Run(url string, token string, functionID string, file string, insecure bool) ([]byte, error) {
input, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to read data file: %w", err)
}
a := entities.FunctionAuth{
Token: token,
Type: entities.AuthenticationTypeAuth0,
}
// TODO: Tuner may want to verify function hash later.
res, err := doRun(url, functionID, input, insecure, nil, a, nil, []string{})
if err != nil {
return nil, fmt.Errorf("error processing data: %w", err)
}
return res, nil
}
func doRun(url string, functionID string, data []byte, insecure bool, funcHash []byte, auth entities.FunctionAuth, keyPolicyHash []byte, pcrSlice []string) ([]byte, error) { //nolint:gocognit
endpoint := fmt.Sprintf("%s/v1/run/%s", url, functionID)
authProtocolType := "cape.runtime"
if auth.Type == entities.AuthenticationTypeFunctionToken {
authProtocolType = "cape.function"
}
c, res, err := websocketDial(endpoint, insecure, authProtocolType, auth.Token)
if err != nil {
log.Error("error dialing websocket: ", err)
// This check is necessary because we don't necessarily return an http response from sentinel.
// Http error code and message is returned if network routing fails.
if res != nil {
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return nil, err
}
res.Body.Close()
return nil, fmt.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error)
}
return nil, err
}
defer c.Close()
nonce, err := crypto.GetNonce()
if err != nil {
return nil, err
}
p := protocol.Protocol{Websocket: c}
req := entities.StartRequest{Nonce: []byte(nonce), AuthToken: auth.Token}
log.Debug("\n> Sending Nonce and Auth Token")
err = p.WriteStart(req)
if err != nil {
return nil, errors.Wrap(err, "error writing run request")
}
log.Debug("* Waiting for attestation document...")
attestDoc, err := p.ReadAttestationDoc()
if err != nil {
log.Println("error reading attestation doc")
return nil, err
}
log.Debug("< Downloading AWS Root Certificate")
rootCert, err := attest.GetRootAWSCert()
if err != nil {
return nil, err
}
log.Debug("< Auth Completed. Received Attestation Document")
doc, userData, err := attest.Attest(attestDoc, rootCert)
if err != nil {
log.Println("error attesting")
return nil, err
}
if userData.FuncHash == nil && len(funcHash) > 0 {
return nil, fmt.Errorf("did not receive function hash from enclave")
}
// If function hash as an optional parameter has not been specified by the user, then we don't check the value.
if len(funcHash) > 0 && !reflect.DeepEqual(funcHash, userData.FuncHash) {
return nil, fmt.Errorf("returned function hash did not match provided, got: %x, want %x", userData.FuncHash, funcHash)
}
if userData.KeyPolicyHash == nil && len(keyPolicyHash) > 0 {
return nil, fmt.Errorf("did not receive key policy hash from enclave")
}
if len(keyPolicyHash) > 0 && !reflect.DeepEqual(keyPolicyHash, userData.KeyPolicyHash) {
return nil, fmt.Errorf("returned key policy hash did not match provided, got: %x, want %x", userData.KeyPolicyHash, keyPolicyHash)
}
encryptedData, err := crypto.LocalEncrypt(*doc, data)
if err != nil {
log.Println("error encrypting")
return nil, err
}
log.Debug("\n> Sending Encrypted Inputs")
err = writeData(c, encryptedData)
if err != nil {
return nil, err
}
log.Debug("* Waiting for function results...")
resData, err := p.ReadRunResults()
if err != nil {
return nil, err
}
log.Debugf("< Received Function Results.")
return resData.Message, nil
}
func writeData(conn *websocket.Conn, data []byte) error {
w, err := conn.NextWriter(websocket.BinaryMessage)
if err != nil {
return err
}
defer w.Close()
_, err = w.Write(data)
if err != nil {
return err
}
return nil
}