resolver: Refactor wasmer dependency to be conditional

This commit updates the resolver package to make the wasmer dependency
conditional at build-time. If CGO is enabled then the wasmer
dependency will be included. If CGO is not enabled, the wasmer
dependency will be excluded and the OPA binary will not be able to run
wasm-compiled policies. This allows us to continue building statically
linked OPA executables that can be distributed and used.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
This commit is contained in:
Torin Sandall
2020-11-03 11:39:38 -05:00
parent 98e7a6f50f
commit f71dc2b209
6 changed files with 111 additions and 34 deletions
+10
View File
@@ -46,6 +46,16 @@ func generateCmdOutput(out io.Writer, check bool) {
fmt.Fprintln(out, "Build Hostname: "+version.Hostname)
fmt.Fprintln(out, "Go Version: "+version.GoVersion)
var wasmAvailable string
if version.WasmRuntimeAvailable {
wasmAvailable = "available"
} else {
wasmAvailable = "unavailable"
}
fmt.Fprintln(out, "WebAssembly: "+wasmAvailable)
if check {
err := checkOPAUpdate(out)
if err != nil {
+40 -34
View File
@@ -10,23 +10,27 @@ import (
"net/http"
"net/http/httptest"
"os"
"sort"
"strings"
"github.com/open-policy-agent/opa/internal/report"
"github.com/open-policy-agent/opa/version"
"testing"
)
func TestGenerateCmdOutputDisableCheckFlag(t *testing.T) {
var stdout bytes.Buffer
setTestVersion()
generateCmdOutput(&stdout, false)
expected := getTestVersion()
if stdout.String() != expected {
t.Fatalf("Expected output:%v but got %v", expected, stdout.String())
}
expectOutputKeys(t, stdout.String(), []string{
"Version",
"Build Commit",
"Build Timestamp",
"Build Hostname",
"Go Version",
"WebAssembly",
})
}
func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) {
@@ -40,13 +44,23 @@ func TestGenerateCmdOutputWithCheckFlagNoError(t *testing.T) {
baseURL, teardown := getTestServer(exp, http.StatusOK)
defer teardown()
banner := "Latest Upstream Version: 100.0.0\n" +
"Download: https://openpolicyagent.org/downloads/v100.0.0/opa_darwin_amd64\n" +
"Release Notes: https://github.com/open-policy-agent/opa/releases/tag/v100.0.0\n"
os.Setenv("OPA_TELEMETRY_SERVICE_URL", baseURL)
expected := getTestVersion() + banner
var stdout bytes.Buffer
testGenerateCmdOutput(t, baseURL, expected)
generateCmdOutput(&stdout, true)
expectOutputKeys(t, stdout.String(), []string{
"Version",
"Build Commit",
"Build Timestamp",
"Build Hostname",
"Go Version",
"WebAssembly",
"Latest Upstream Version",
"Release Notes",
"Download",
})
}
func TestCheckOPAUpdateBadURL(t *testing.T) {
@@ -59,35 +73,27 @@ func TestCheckOPAUpdateBadURL(t *testing.T) {
}
}
func testGenerateCmdOutput(t *testing.T, url, expected string) {
func expectOutputKeys(t *testing.T, stdout string, expectedKeys []string) {
t.Helper()
os.Setenv("OPA_TELEMETRY_SERVICE_URL", url)
var gotKeys []string
var stdout bytes.Buffer
setTestVersion()
generateCmdOutput(&stdout, true)
if stdout.String() != expected {
t.Fatalf("Expected output:\"%v\" but got \"%v\"", expected, stdout.String())
for _, line := range strings.Split(strings.Trim(stdout, "\n"), "\n") {
gotKeys = append(gotKeys, strings.Split(line, ":")[0])
}
}
func setTestVersion() {
version.Version = "v0.20.0"
version.Vcs = "12345"
version.Timestamp = "2020-05-14T06:22:38Z"
version.Hostname = "foo"
version.GoVersion = "1.14.7"
}
sort.Strings(expectedKeys)
sort.Strings(gotKeys)
func getTestVersion() string {
return "Version: v0.20.0\n" +
"Build Commit: 12345\n" +
"Build Timestamp: 2020-05-14T06:22:38Z\n" +
"Build Hostname: foo\n" +
"Go Version: 1.14.7\n"
if len(expectedKeys) != len(gotKeys) {
t.Fatalf("expected %v but got %v", expectedKeys, gotKeys)
}
for i := range expectedKeys {
if expectedKeys[i] != gotKeys[i] {
t.Fatalf("expected %v but got %v", expectedKeys, gotKeys)
}
}
}
func getTestServer(update interface{}, statusCode int) (baseURL string, teardownFn func()) {
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// +build !cgo
package wasm
import (
"context"
"errors"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/resolver"
)
type Resolver struct {
}
func (r *Resolver) Entrypoints() []ast.Ref {
panic("unreachable")
}
func (r *Resolver) Close() {
panic("unreachable")
}
func (r *Resolver) Eval(ctx context.Context, input resolver.Input) (resolver.Result, error) {
panic("unreachable")
}
func (r *Resolver) SetData(data interface{}) error {
panic("unreachable")
}
func New(entrypoints []ast.Ref, policy []byte, data interface{}) (*Resolver, error) {
return nil, errors.New("WebAssembly runtime not supported in this build")
}
+2
View File
@@ -2,6 +2,8 @@
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// +build cgo
package wasm
import (
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// +build cgo
package version
// WasmRuntimeAvailable indicates if a wasm runtime is available in this OPA.
const WasmRuntimeAvailable = true
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// +build !cgo
package version
// WasmRuntimeAvailable indicates if a wasm runtime is available in this OPA.
const WasmRuntimeAvailable = false