Skip to content

Commit 1e14b80

Browse files
authored
Do not override FS config if no allowed paths are provided (#91)
This PR allows users to use FS config from `ModuleConfig` (FS is overridden only if Manifest specifies `AllowedPaths`)
1 parent bef00f3 commit 1e14b80

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

extism.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type Runtime struct {
4545

4646
// PluginInstanceConfig contains configuration options for the Extism plugin.
4747
type PluginInstanceConfig struct {
48+
// ModuleConfig allows the user to specify custom module configuration.
49+
//
50+
// NOTE: Module name and start functions are ignored as they are overridden by Extism, also if Manifest contains
51+
// non-empty AllowedPaths, then FS is also ignored. If EXTISM_ENABLE_WASI_OUTPUT is set, then stdout and stderr are
52+
// set to os.Stdout and os.Stderr respectively (ignoring user defined module config).
4853
ModuleConfig wazero.ModuleConfig
4954
}
5055

@@ -110,8 +115,6 @@ type Plugin struct {
110115
close []func(ctx context.Context) error
111116
extism api.Module
112117

113-
//Runtime *Runtime
114-
//Main Module
115118
module api.Module
116119
Timeout time.Duration
117120
Config map[string]string

plugin.go

+32-25
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
observe "github.com/dylibso/observe-sdk/go"
8-
"github.com/tetratelabs/wazero"
9-
"github.com/tetratelabs/wazero/api"
10-
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
117
"os"
128
"strconv"
139
"strings"
1410
"sync/atomic"
1511
"time"
12+
13+
observe "github.com/dylibso/observe-sdk/go"
14+
"github.com/tetratelabs/wazero"
15+
"github.com/tetratelabs/wazero/api"
16+
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1617
)
1718

1819
type CompiledPlugin struct {
@@ -55,6 +56,10 @@ type PluginConfig struct {
5556
// When plugins are built using NewCompiledPlugin, the ModuleConfig has no
5657
// effect because the instance is not created. Instead, the ModuleConfig is
5758
// passed directly in calls to the CompiledPlugin.Instance method.
59+
//
60+
// NOTE: Module name and start functions are ignored as they are overridden by Extism, also if Manifest contains
61+
// non-empty AllowedPaths, then FS is also ignored. If EXTISM_ENABLE_WASI_OUTPUT is set, then stdout and stderr are
62+
// set to os.Stdout and os.Stderr respectively (ignoring user defined module config).
5863
ModuleConfig wazero.ModuleConfig
5964
}
6065

@@ -96,27 +101,25 @@ func NewCompiledPlugin(
96101
return nil, fmt.Errorf("manifest can't be empty")
97102
}
98103

99-
var cfg wazero.RuntimeConfig
100-
if config.RuntimeConfig == nil {
101-
cfg = wazero.NewRuntimeConfig()
102-
} else {
103-
cfg = config.RuntimeConfig
104+
runtimeConfig := config.RuntimeConfig
105+
if runtimeConfig == nil {
106+
runtimeConfig = wazero.NewRuntimeConfig()
104107
}
105108

106109
// Make sure function calls are cancelled if the context is cancelled
107110
if manifest.Timeout > 0 {
108-
cfg = cfg.WithCloseOnContextDone(true)
111+
runtimeConfig = runtimeConfig.WithCloseOnContextDone(true)
109112
}
110113

111114
if manifest.Memory != nil {
112115
if manifest.Memory.MaxPages > 0 {
113-
cfg = cfg.WithMemoryLimitPages(manifest.Memory.MaxPages)
116+
runtimeConfig = runtimeConfig.WithMemoryLimitPages(manifest.Memory.MaxPages)
114117
}
115118
}
116119

117120
p := CompiledPlugin{
118121
manifest: manifest,
119-
runtime: wazero.NewRuntimeWithConfig(ctx, cfg),
122+
runtime: wazero.NewRuntimeWithConfig(ctx, runtimeConfig),
120123
observeAdapter: config.ObserveAdapter,
121124
observeOptions: config.ObserveOptions,
122125
enableHttpResponseHeaders: config.EnableHttpResponseHeaders,
@@ -220,24 +223,28 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
220223
if moduleConfig == nil {
221224
moduleConfig = wazero.NewModuleConfig()
222225
}
223-
moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))
224226

225-
// NOTE: this is only necessary for guest modules because
226-
// host modules have the same access privileges as the host itself
227-
fs := wazero.NewFSConfig()
228-
for host, guest := range p.manifest.AllowedPaths {
229-
if strings.HasPrefix(host, "ro:") {
230-
trimmed := strings.TrimPrefix(host, "ro:")
231-
fs = fs.WithReadOnlyDirMount(trimmed, guest)
232-
} else {
233-
fs = fs.WithDirMount(host, guest)
234-
}
235-
}
227+
moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))
236228

237229
// NOTE: we don't want wazero to call the start function, we will initialize
238230
// the guest runtime manually.
239231
// See: https://github.com/extism/go-sdk/pull/1#issuecomment-1650527495
240-
moduleConfig = moduleConfig.WithStartFunctions().WithFSConfig(fs)
232+
moduleConfig = moduleConfig.WithStartFunctions()
233+
234+
if len(p.manifest.AllowedPaths) > 0 {
235+
// NOTE: this is only necessary for guest modules because
236+
// host modules have the same access privileges as the host itself
237+
fs := wazero.NewFSConfig()
238+
for host, guest := range p.manifest.AllowedPaths {
239+
if strings.HasPrefix(host, "ro:") {
240+
trimmed := strings.TrimPrefix(host, "ro:")
241+
fs = fs.WithReadOnlyDirMount(trimmed, guest)
242+
} else {
243+
fs = fs.WithDirMount(host, guest)
244+
}
245+
}
246+
moduleConfig = moduleConfig.WithFSConfig(fs)
247+
}
241248

242249
_, wasiOutput := os.LookupEnv("EXTISM_ENABLE_WASI_OUTPUT")
243250
if p.hasWasi && wasiOutput {

0 commit comments

Comments
 (0)