bundle: Dedicate policy.wasm for the compiled policy.

Signed-off-by: Teemu Koponen <koponen@styra.com>
This commit is contained in:
Teemu Koponen
2020-02-21 15:24:45 -08:00
committed by Torin Sandall
parent b4cc1a4652
commit 500faefdc4
3 changed files with 28 additions and 1 deletions
+22 -1
View File
@@ -30,6 +30,7 @@ import (
// Common file extensions and file names.
const (
RegoExt = ".rego"
WasmFile = "/policy.wasm"
manifestExt = ".manifest"
dataFile = "data.json"
yamlDataFile = "data.yaml"
@@ -42,6 +43,7 @@ type Bundle struct {
Manifest Manifest
Data map[string]interface{}
Modules []ModuleFile
Wasm []byte
}
// Manifest represents the manifest from a bundle. The manifest may contain
@@ -209,6 +211,9 @@ func (r *Reader) Read() (Bundle, error) {
}
bundle.Modules = append(bundle.Modules, mf)
} else if path == WasmFile {
bundle.Wasm = buf.Bytes()
} else if filepath.Base(path) == dataFile {
var value interface{}
@@ -302,6 +307,10 @@ func Write(w io.Writer, bundle Bundle) error {
}
}
if err := writeWasm(tw, bundle); err != nil {
return err
}
if err := writeManifest(tw, bundle); err != nil {
return err
}
@@ -313,6 +322,14 @@ func Write(w io.Writer, bundle Bundle) error {
return gw.Close()
}
func writeWasm(tw *tar.Writer, bundle Bundle) error {
if len(bundle.Wasm) == 0 {
return nil
}
return archive.WriteFile(tw, WasmFile, bundle.Wasm)
}
func writeManifest(tw *tar.Writer, bundle Bundle) error {
var buf bytes.Buffer
@@ -357,7 +374,11 @@ func (b Bundle) Equal(other Bundle) bool {
return false
}
}
return true
if (b.Wasm == nil && other.Wasm != nil) || (b.Wasm != nil && other.Wasm == nil) {
return false
}
return bytes.Equal(b.Wasm, other.Wasm)
}
func (b *Bundle) insert(key []string, value interface{}) error {
+3
View File
@@ -30,6 +30,7 @@ func testReadBundle(t *testing.T, baseDir string) {
{"/a/b/d/data.json", "true"},
{"/a/b/y/data.yaml", `foo: 1`},
{"/example/example.rego", `package example`},
{"/policy.wasm", `modules-compiled-as-wasm-binary`},
{"/data.json", `{"x": {"y": true}, "a": {"b": {"z": true}}}}`},
}
@@ -72,6 +73,7 @@ func testReadBundle(t *testing.T, baseDir string) {
Raw: []byte(module),
},
},
Wasm: []byte("modules-compiled-as-wasm-binary"),
}
if !exp.Equal(bundle) {
@@ -268,6 +270,7 @@ func TestRoundtrip(t *testing.T) {
Raw: []byte(`package foo.corge`),
},
},
Wasm: []byte("modules-compiled-as-wasm-binary"),
Manifest: Manifest{
Revision: "quickbrownfaux",
},
+3
View File
@@ -163,6 +163,9 @@ http/example/authz/authz.rego
In this example, the bundle contains one policy file (`authz.rego`) and two
data files (`roles/bindings/data.json` and `roles/permissions/data.json`).
The bundle may also contain an optional wasm binary file (`policy.wasm`).
It stores the WebAssembly compiled version of all the Rego policy files within
the bundle.
Bundle files may contain an optional `.manifest` file that stores bundle
metadata. The file should contain a JSON serialized object, with the following