forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rego: make wasmtime-go dependency "more optional" (open-policy-agent#…
…3708) Users of OPA as a library are concerned about big binary blobs in their vendor/ directories. Even more so if they don't use them. This is the case for anyone using OPA as library, but not using the wasm-backed evaluation feature. With this change, importers of any packages other than `server` and `cmd` will have to explicitly opt-in to using wasm evaluation features by having an underscore import somewhere: import _ "github.com/open-policy-agent/opa/features/wasm" Fixes open-policy-agent#3545. Signed-off-by: Stephan Renatus <[email protected]>
- Loading branch information
Showing
18 changed files
with
246 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021 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 opa_wasm | ||
|
||
package cmd | ||
|
||
import _ "github.com/open-policy-agent/opa/features/wasm" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2021 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. | ||
|
||
// Import this package to enable evaluation of rego code using the | ||
// built-in wasm engine. | ||
package wasm | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/open-policy-agent/opa/internal/rego/opa" | ||
wopa "github.com/open-policy-agent/opa/internal/wasm/sdk/opa" | ||
) | ||
|
||
func init() { | ||
opa.RegisterEngine("wasm", &factory{}) | ||
} | ||
|
||
// OPA is an implementation of the OPA SDK. | ||
type OPA struct { | ||
opa *wopa.OPA | ||
} | ||
|
||
type factory struct{} | ||
|
||
// New constructs a new OPA instance. | ||
func (*factory) New() opa.EvalEngine { | ||
return &OPA{opa: wopa.New()} | ||
} | ||
|
||
// WithPolicyBytes configures the compiled policy to load. | ||
func (o *OPA) WithPolicyBytes(policy []byte) opa.EvalEngine { | ||
o.opa = o.opa.WithPolicyBytes(policy) | ||
return o | ||
} | ||
|
||
// WithDataJSON configures the JSON data to load. | ||
func (o *OPA) WithDataJSON(data interface{}) opa.EvalEngine { | ||
o.opa = o.opa.WithDataJSON(data) | ||
return o | ||
} | ||
|
||
// Init initializes the OPA instance. | ||
func (o *OPA) Init() (opa.EvalEngine, error) { | ||
i, err := o.opa.Init() | ||
if err != nil { | ||
return nil, err | ||
} | ||
o.opa = i | ||
return o, nil | ||
} | ||
|
||
func (o *OPA) Entrypoints(ctx context.Context) (map[string]int32, error) { | ||
return o.opa.Entrypoints(ctx) | ||
} | ||
|
||
// Eval evaluates the policy. | ||
func (o *OPA) Eval(ctx context.Context, opts opa.EvalOpts) (*opa.Result, error) { | ||
evalOptions := wopa.EvalOpts{ | ||
Input: opts.Input, | ||
Metrics: opts.Metrics, | ||
Entrypoint: opts.Entrypoint, | ||
Time: opts.Time, | ||
Seed: opts.Seed, | ||
InterQueryBuiltinCache: opts.InterQueryBuiltinCache, | ||
} | ||
|
||
res, err := o.opa.Eval(ctx, evalOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &opa.Result{Result: res.Result}, nil | ||
} | ||
|
||
func (o *OPA) SetData(ctx context.Context, data interface{}) error { | ||
return o.opa.SetData(ctx, data) | ||
} | ||
|
||
func (o *OPA) SetDataPath(ctx context.Context, path []string, data interface{}) error { | ||
return o.opa.SetDataPath(ctx, path, data) | ||
} | ||
|
||
func (o *OPA) RemoveDataPath(ctx context.Context, path []string) error { | ||
return o.opa.RemoveDataPath(ctx, path) | ||
} | ||
|
||
func (o *OPA) Close() { | ||
o.opa.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2021 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. | ||
|
||
package opa | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ErrEngineNotFound is returned by LookupEngine if no wasm engine was | ||
// registered by that name. | ||
var ErrEngineNotFound error = &errEngineNotFound{} | ||
|
||
type errEngineNotFound struct{} | ||
|
||
func (*errEngineNotFound) Error() string { return "engine not found" } | ||
func (*errEngineNotFound) Details() string { | ||
return `WebAssembly runtime not supported in this build. | ||
---------------------------------------------------------------------------------- | ||
Please download an OPA binary with Wasm enabled from | ||
https://www.openpolicyagent.org/docs/latest/#running-opa | ||
or build it yourself (with Wasm enabled). | ||
---------------------------------------------------------------------------------- | ||
` | ||
} | ||
|
||
// Engine repesents a factory for instances of EvalEngine implementations | ||
type Engine interface { | ||
New() EvalEngine | ||
} | ||
|
||
// EvalEngine is the interface implemented by an engine used to eval a policy | ||
type EvalEngine interface { | ||
Init() (EvalEngine, error) | ||
Entrypoints(context.Context) (map[string]int32, error) | ||
WithPolicyBytes([]byte) EvalEngine | ||
WithDataJSON(interface{}) EvalEngine | ||
Eval(context.Context, EvalOpts) (*Result, error) | ||
SetData(context.Context, interface{}) error | ||
SetDataPath(context.Context, []string, interface{}) error | ||
RemoveDataPath(context.Context, []string) error | ||
Close() | ||
} | ||
|
||
var engines = map[string]Engine{} | ||
|
||
// RegisterEngine registers an evaluation engine by its target name. | ||
// Note that the "rego" target is always available. | ||
func RegisterEngine(name string, e Engine) { | ||
if engines[name] != nil { | ||
panic("duplicate engine registration") | ||
} | ||
engines[name] = e | ||
} | ||
|
||
// LookupEngine allows retrieving an engine registered by name | ||
func LookupEngine(name string) (Engine, error) { | ||
e, ok := engines[name] | ||
if !ok { | ||
return nil, ErrEngineNotFound | ||
} | ||
return e, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.