Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: support http(s) fetch of Wasm files #3005

Merged
merged 5 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/wasm/httpfetcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package wasm

import (
"fmt"
"io"
"net/http"
"net/url"

"golang.org/x/net/context"
)

// httpFetcher decorates an http.Client with convenience methods.
type httpFetcher struct {
c http.Client
}

// newHTTPFetcher is a constructor for httpFetcher.
//
// It is possible to plug a custom http.RoundTripper to handle other concerns (e.g. retries)
// Compression is handled transparently and automatically by http.Client.
func newHTTPFetcher(transport http.RoundTripper) *httpFetcher {
return &httpFetcher{
c: http.Client{Transport: transport},
}
}

// fetch returns a byte slice of the wasm module found at the given URL, or an error otherwise.
func (f *httpFetcher) fetch(ctx context.Context, u *url.URL) ([]byte, error) {
h := http.Header{}
// Clear default user agent.
h.Set("User-Agent", "")
req := &http.Request{Method: http.MethodGet, URL: u, Header: h}
resp, err := f.c.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("received %v status code from %q", resp.StatusCode, u)
}

bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
ItalyPaleAle marked this conversation as resolved.
Show resolved Hide resolved
return bytes, nil
}
72 changes: 72 additions & 0 deletions internal/wasm/httpfetcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package wasm

import (
"compress/gzip"
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/stretchr/testify/require"
)

var wasmMagicNumber = []byte{0x00, 0x61, 0x73, 0x6d}

func TestWasmHTTPFetch(t *testing.T) {
wasmBinary := append(wasmMagicNumber, 0x00, 0x00, 0x00, 0x00)
cases := []struct {
name string
handler http.HandlerFunc
expectedError string
}{
{
name: "plain wasm binary",
handler: func(w http.ResponseWriter, r *http.Request) {
w.Write(wasmBinary)
},
},
// Compressed payloads are handled automatically by http.Client.
{
name: "compressed payload",
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")

gw := gzip.NewWriter(w)
defer gw.Close()
gw.Write(wasmBinary)
},
},
{
name: "http error",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
expectedError: "received 500 status code",
},
}

for _, proto := range []string{"http", "https"} {
t.Run(proto, func(t *testing.T) {
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ts := httptest.NewServer(tc.handler)
defer ts.Close()
fetcher := newHTTPFetcher(http.DefaultTransport)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
parse, err := url.Parse(ts.URL)
require.NoError(t, err)
_, err = fetcher.fetch(ctx, parse)
if tc.expectedError != "" {
require.ErrorContains(t, err, tc.expectedError)
return
}
require.NoError(t, err, "Wasm download got an unexpected error: %v", err)
})
}
})
}
}
11 changes: 9 additions & 2 deletions internal/wasm/wasm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package wasm

import (
"context"
"crypto/rand"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -74,11 +76,16 @@ func GetInitMetadata(md metadata.Base) (*InitMetadata, error) {
case "oci":
return nil, fmt.Errorf("TODO %s", scheme)
case "http", "https":
_, err = url.Parse(m.URL)
u, err := url.Parse(m.URL)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("TODO %s", scheme)
fetcher := newHTTPFetcher(http.DefaultTransport)
m.Guest, err = fetcher.fetch(context.Background(), u)
if err != nil {
return nil, err
}
m.GuestName, _ = strings.CutSuffix(path.Base(u.Path), ".wasm")
case "file":
guestPath := m.URL[7:]
m.Guest, err = os.ReadFile(guestPath)
Expand Down
8 changes: 4 additions & 4 deletions internal/wasm/wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ func TestGetInitMetadata(t *testing.T) {
{
name: "TODO http",
metadata: metadata.Base{Properties: map[string]string{
"url": "http://foo/bar.wasm",
"url": "http://foo.invalid/bar.wasm",
}},
expectedErr: "TODO http",
expectedErr: "no such host",
},
{
name: "TODO https",
metadata: metadata.Base{Properties: map[string]string{
"url": "https://foo/bar.wasm",
"url": "https://foo.invalid/bar.wasm",
}},
expectedErr: "TODO https",
expectedErr: "no such host",
},
{
name: "unsupported scheme",
Expand Down
14 changes: 14 additions & 0 deletions middleware/http/wasm/httpwasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"github.com/dapr/kit/logger"
)

//go:embed internal/testdata/rewrite.wasm
var exampleWasmBin []byte

func Test_NewMiddleWare(t *testing.T) {
l := logger.NewLogger(t.Name())
require.Equal(t, &middleware{logger: l}, NewMiddleware(l))
Expand All @@ -37,6 +40,11 @@ func Test_middleware_log(t *testing.T) {

func Test_middleware_getHandler(t *testing.T) {
m := &middleware{logger: logger.NewLogger(t.Name())}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/example.wasm" {
w.Write(exampleWasmBin)
}
}))

type testCase struct {
name string
Expand All @@ -61,6 +69,12 @@ func Test_middleware_getHandler(t *testing.T) {
}},
expectedErr: "wasm: error compiling guest: invalid magic number",
},
{
name: "remote wasm url",
metadata: metadata.Base{Properties: map[string]string{
"url": ts.URL + "/example.wasm",
}},
},
{
name: "ok",
metadata: metadata.Base{Properties: map[string]string{
Expand Down
69 changes: 58 additions & 11 deletions middleware/http/wasm/internal/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal_test

import (
"bytes"
"compress/gzip"
"context"
"log"
"net/http"
Expand Down Expand Up @@ -128,19 +129,65 @@ func Test_EndToEnd(t *testing.T) {
},
}

for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer buf.Reset()
t.Run("local", func(t *testing.T) {
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer buf.Reset()

wasmPath := path.Join(t.TempDir(), "guest.wasm")
require.NoError(t, os.WriteFile(wasmPath, tc.guest, 0o600))
wasmPath := path.Join(t.TempDir(), "guest.wasm")
require.NoError(t, os.WriteFile(wasmPath, tc.guest, 0o600))

meta := metadata.Base{Properties: map[string]string{"url": "file://" + wasmPath}}
handlerFn, err := wasm.NewMiddleware(l).GetHandler(context.Background(), middleware.Metadata{Base: meta})
require.NoError(t, err)
handler := handlerFn(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
tc.test(t, handler, &buf)
meta := metadata.Base{Properties: map[string]string{"url": "file://" + wasmPath}}
handlerFn, err := wasm.NewMiddleware(l).GetHandler(context.Background(), middleware.Metadata{Base: meta})
require.NoError(t, err)
handler := handlerFn(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
tc.test(t, handler, &buf)
})
}
})

httptests := []struct {
name string
handler func(w http.ResponseWriter, r *http.Request, guest []byte)
}{
{
name: "http",
handler: func(w http.ResponseWriter, r *http.Request, guest []byte) {
w.Write(guest)
},
},
{
name: "http+gzip",
handler: func(w http.ResponseWriter, r *http.Request, guest []byte) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")

gw := gzip.NewWriter(w)
defer gw.Close()
gw.Write(guest)
},
},
}

for _, ht := range httptests {
t.Run(ht.name, func(t *testing.T) {
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
defer buf.Reset()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ht.handler(w, r, tc.guest)
}))
defer ts.Close()

meta := metadata.Base{Properties: map[string]string{"url": ts.URL + "/guest.wasm"}}
handlerFn, err := wasm.NewMiddleware(l).GetHandler(context.Background(), middleware.Metadata{Base: meta})
require.NoError(t, err)
handler := handlerFn(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
tc.test(t, handler, &buf)
})
}
})
}
}