-
Notifications
You must be signed in to change notification settings - Fork 7
/
call.go
318 lines (278 loc) · 8.33 KB
/
call.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/extism/go-sdk"
"github.com/spf13/cobra"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/sys"
)
type callArgs struct {
args []string
input string
loop int
wasi bool
logLevel string
allowedPaths []string
allowedHosts []string
timeout uint64
memoryMaxPages int
memoryHttpMaxBytes int
memoryVarMaxBytes int
config []string
setConfig string
manifest bool
stdin bool
link []string
}
func readStdin() []byte {
var buf []byte = make([]byte, 4096)
var dest = []byte{}
for {
n, err := os.Stdin.Read(buf)
if err != nil || n == 0 {
break
}
dest = append(dest, buf[0:n]...)
}
return dest
}
func (a *callArgs) SetArgs(args []string) {
a.args = args
}
func (a *callArgs) getAllowedPaths() map[string]string {
allowedPaths := map[string]string{}
for _, path := range a.allowedPaths {
split := strings.Split(path, ":")
switch len(split) {
case 1:
allowedPaths[path] = path
case 2:
allowedPaths[split[0]] = split[1]
default:
allowedPaths[split[0]] = strings.Join(split[1:], ":")
}
}
return allowedPaths
}
func (a *callArgs) getConfig() (map[string]string, error) {
config := map[string]string{}
if a.setConfig != "" {
err := json.Unmarshal([]byte(a.setConfig), &config)
if err != nil {
return config,
errors.Join(errors.New("Invalid value for --set-config flag"), err)
}
}
for _, cfg := range a.config {
split := strings.SplitN(cfg, "=", 2)
switch len(split) {
case 1:
config[cfg] = ""
case 2:
config[split[0]] = split[1]
default:
continue
}
}
return config, nil
}
func (a *callArgs) getLinkModules() []extism.Wasm {
modules := []extism.Wasm{}
var name, path string
for _, x := range a.link {
split := strings.SplitN(x, "=", 2)
switch len(split) {
case 1:
fileName := filepath.Base(split[0])
name = strings.TrimSuffix(fileName, filepath.Ext(fileName))
path = split[0]
case 2:
name = split[0]
path = split[1]
default:
continue
}
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
modules = append(modules, extism.WasmUrl{Url: path, Name: name})
} else {
modules = append(modules, extism.WasmFile{Path: path, Name: name})
}
}
return modules
}
var globalPlugin *extism.Plugin
func runCall(cmd *cobra.Command, call *callArgs) error {
if len(call.args) < 1 {
return errors.New("An input file is required")
} else if len(call.args) < 2 {
return errors.New("A function name is required")
}
ctx := context.Background()
wasm := call.args[0]
funcName := call.args[1]
// Manifest
var manifest extism.Manifest
if call.manifest {
Log("Reading from manifest:", wasm)
f, err := os.Open(wasm)
if err != nil {
return err
}
err = json.NewDecoder(f).Decode(&manifest)
if err != nil {
return err
}
// Link additional modules from CLI
manifest.Wasm = append(call.getLinkModules(), manifest.Wasm...)
Log("Read manifest:", manifest)
defer f.Close()
} else {
manifest.Wasm = call.getLinkModules()
if strings.HasPrefix(wasm, "http://") || strings.HasPrefix(wasm, "https://") {
Log("Loading wasm file as url:", wasm)
manifest.Wasm = append(manifest.Wasm, extism.WasmUrl{Url: wasm})
} else {
Log("Adding wasm file to manifest:", wasm)
manifest.Wasm = append(manifest.Wasm, extism.WasmFile{Path: wasm})
}
}
// Allowed hosts
Log("Adding allowed hosts:", call.allowedHosts)
manifest.AllowedHosts = append(manifest.AllowedHosts, call.allowedHosts...)
// Allowed paths
if manifest.AllowedPaths == nil {
manifest.AllowedPaths = map[string]string{}
}
for k, v := range call.getAllowedPaths() {
Log("Adding path mapping:", k+":"+v)
manifest.AllowedPaths[k] = v
}
// Config
if manifest.Config == nil {
manifest.Config = map[string]string{}
}
config, err := call.getConfig()
if err != nil {
return err
}
for k, v := range config {
Log("Adding config key", k+"="+v)
manifest.Config[k] = v
}
// Memory
if call.memoryMaxPages > 0 {
if manifest.Memory == nil {
manifest.Memory = &extism.ManifestMemory{}
}
Log("Max pages", call.memoryMaxPages)
manifest.Memory.MaxPages = uint32(call.memoryMaxPages)
}
if call.memoryHttpMaxBytes >= 0 {
if manifest.Memory == nil {
manifest.Memory = &extism.ManifestMemory{}
}
Log("HTTP response max bytes", call.memoryHttpMaxBytes)
manifest.Memory.MaxHttpResponseBytes = int64(call.memoryHttpMaxBytes)
}
if call.memoryVarMaxBytes >= 0 {
if manifest.Memory == nil {
manifest.Memory = &extism.ManifestMemory{}
}
Log("Var store size", call.memoryVarMaxBytes)
manifest.Memory.MaxVarBytes = int64(call.memoryVarMaxBytes)
}
var logLevel extism.LogLevel = extism.LogLevelError
switch call.logLevel {
case "info":
logLevel = extism.LogLevelInfo
case "debug":
logLevel = extism.LogLevelDebug
case "warn":
logLevel = extism.LogLevelWarn
case "error":
logLevel = extism.LogLevelError
case "trace":
logLevel = extism.LogLevelTrace
}
extism.SetLogLevel(logLevel)
pluginConfig := extism.PluginConfig{
ModuleConfig: wazero.NewModuleConfig().WithSysWalltime(),
RuntimeConfig: wazero.NewRuntimeConfig().WithCloseOnContextDone(call.timeout > 0),
LogLevel: logLevel,
EnableWasi: call.wasi,
}
if call.timeout > 0 {
Log("Setting timeout", call.timeout)
manifest.Timeout = call.timeout
}
if globalPlugin == nil {
Log("Creating plugin")
globalPlugin, err = extism.NewPlugin(ctx, manifest, pluginConfig, []extism.HostFunction{})
if err != nil {
return err
}
//defer plugin.Close()
} else {
Log("Reusing Plugin")
}
input := []byte(call.input)
if call.stdin {
Log("Reading input from stdin")
input = readStdin()
}
Log("Got", len(input), "bytes of input data")
// Call the plugin in a loop
for i := 0; i < call.loop; i++ {
Log("Calling", funcName)
exit, res, err := globalPlugin.CallWithContext(ctx, funcName, input)
if err != nil {
if exit == sys.ExitCodeDeadlineExceeded {
return errors.New("timeout")
} else if exit != 0 {
return errors.Join(err, errors.New(fmt.Sprintf("Returned non-zero exit code: %d", exit)))
}
return err
}
Log("Call returned", len(res), "bytes")
fmt.Println(string(res))
if call.loop > 1 {
fmt.Println()
}
}
return nil
}
func CallCmd() *cobra.Command {
call := &callArgs{}
cmd :=
&cobra.Command{
Use: "call [flags] wasm_file function",
Short: "Call a plugin function",
SilenceUsage: true,
RunE: RunArgs(runCall, call),
Args: cobra.ExactArgs(2),
}
flags := cmd.Flags()
flags.StringVarP(&call.input, "input", "i", "", "Input data")
flags.BoolVar(&call.stdin, "stdin", false, "Read input from stdin")
flags.IntVar(&call.loop, "loop", 1, "Number of times to call the function")
flags.BoolVar(&call.wasi, "wasi", false, "Enable WASI")
flags.StringArrayVar(&call.allowedPaths, "allow-path", []string{}, "Allow a path to be accessed from inside the Wasm sandbox, a path can be either a plain path or a map from HOST_PATH:GUEST_PATH")
flags.StringArrayVar(&call.allowedHosts, "allow-host", []string{}, "Allow access to an HTTP host, if no hosts are listed then all requests will fail. Globs may be used for wildcards")
flags.Uint64Var(&call.timeout, "timeout", 0, "Timeout in milliseconds")
flags.IntVar(&call.memoryMaxPages, "memory-max", 0, "Maximum number of pages to allocate")
flags.IntVar(&call.memoryHttpMaxBytes, "http-response-max", -1, "Maximum HTTP response size in bytes when using `extism_http_request`")
flags.IntVar(&call.memoryVarMaxBytes, "var-max", -1, "Maximum size in bytes of Extism var store")
flags.StringArrayVar(&call.config, "config", []string{}, "Set config values, should be in KEY=VALUE format")
flags.StringVar(&call.setConfig, "set-config", "", "Create config object using JSON, this will be merged with any `config` arguments")
flags.BoolVarP(&call.manifest, "manifest", "m", false, "When set the input file will be parsed as a JSON encoded Extism manifest instead of a WASM file")
flags.StringVar(&call.logLevel, "log-level", "", "Set log level: trace, debug, warn, info, error")
flags.StringArrayVar(&call.link, "link", []string{}, "Additional modules to link")
cmd.MarkFlagsMutuallyExclusive("input", "stdin")
return cmd
}