Skip to content

Commit

Permalink
feat: add support for readonly dir mounting (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez authored Jul 25, 2024
1 parent 1fd5d5e commit 4ad86bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"

"github.com/tetratelabs/wazero"
Expand Down Expand Up @@ -389,8 +390,12 @@ func NewPlugin(
fs := wazero.NewFSConfig()

for host, guest := range manifest.AllowedPaths {
// TODO: wazero supports read-only mounting, do we want to support that too?
fs = fs.WithDirMount(host, guest)
if strings.HasPrefix(host, "ro:") {
trimmed := strings.TrimPrefix(host, "ro:")
fs = fs.WithReadOnlyDirMount(trimmed, guest)
} else {
fs = fs.WithDirMount(host, guest)
}
}

moduleConfig := config.ModuleConfig
Expand Down
26 changes: 26 additions & 0 deletions extism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,32 @@ func TestFS(t *testing.T) {
}
}

func TestReadOnlyMount(t *testing.T) {
manifest := manifest("read_write.wasm")
manifest.AllowedPaths = map[string]string{
"ro:testdata": "/mnt",
}

if plugin, ok := plugin(t, manifest); ok {
defer plugin.Close()
plugin.Config["path"] = "/mnt/test.txt"

exit, output, err := plugin.Call("try_read", []byte{})

if assertCall(t, err, exit) {
actual := string(output)
expected := "hello world!"

assert.Equal(t, expected, actual)
}

_, _, err = plugin.Call("try_write", []byte("hello hello!"))

assert.NotNil(t, err, "Write must fail")
assert.Contains(t, err.Error(), "Failed to write file")
}
}

func TestCountVowels(t *testing.T) {
manifest := manifest("count_vowels.wasm")

Expand Down
Binary file added wasm/read_write.wasm
Binary file not shown.

0 comments on commit 4ad86bc

Please sign in to comment.