Skip to content

Commit 62e3947

Browse files
committed
plugin: allow selecting wasm target for evaluation
This turns the simple grpc tests scenario into a matrix test for both evaluation variants. Fixes open-policy-agent/opa#3716. Signed-off-by: Stephan Renatus <[email protected]>
1 parent 50b620e commit 62e3947

File tree

7 files changed

+49
-10
lines changed

7 files changed

+49
-10
lines changed

Diff for: .github/workflows/checks.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ jobs:
9292
e2e-envoy-grpc:
9393
name: Envoy gRPC End-to-End Test
9494
runs-on: ubuntu-18.04
95+
strategy:
96+
matrix:
97+
include:
98+
- eval: wasm
99+
- eval: rego
95100
steps:
96101
- name: Check out code
97102
uses: actions/checkout@v2
@@ -104,7 +109,7 @@ jobs:
104109
working-directory: examples/grpc
105110

106111
- name: Run test
107-
run: make test-setup test
112+
run: make test-setup test EVAL_TARGET=${{ matrix.eval }}
108113
working-directory: examples/grpc
109114

110115
- name: Run test log dump and cleanup

Diff for: envoyauth/evaluation.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ type EvalContext interface {
2727
SetPreparedQuery(*rego.PreparedEvalQuery)
2828
}
2929

30-
//Eval - Evaluates an input against a provided EvalContext and yields result
30+
// EvalContextWithTarget is the interface that allows setting the evaluation's target engine.
31+
type EvalContextWithTarget interface {
32+
Target() string
33+
}
34+
35+
// Eval evaluates an input against a provided EvalContext and yields result
3136
func Eval(ctx context.Context, evalContext EvalContext, input ast.Value, result *EvalResult, opts ...func(*rego.Rego)) error {
3237
var err error
3338

@@ -87,13 +92,18 @@ func constructPreparedQuery(evalContext EvalContext, txn storage.Transaction, m
8792
var pq rego.PreparedEvalQuery
8893

8994
evalContext.PreparedQueryDoOnce().Do(func() {
95+
target := "rego"
96+
if ec, ok := evalContext.(EvalContextWithTarget); ok {
97+
target = ec.Target()
98+
}
9099
opts = append(opts,
91100
rego.Metrics(m),
92101
rego.ParsedQuery(evalContext.ParsedQuery()),
93102
rego.Compiler(evalContext.Compiler()),
94103
rego.Store(evalContext.Store()),
95104
rego.Transaction(txn),
96-
rego.Runtime(evalContext.Runtime()))
105+
rego.Runtime(evalContext.Runtime()),
106+
rego.Target(target))
97107

98108
r := rego.New(opts...)
99109

Diff for: examples/grpc/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SHELL:=bash
22
GRPCURL_IMAGE:=fullstorydev/grpcurl:v1.7.0
33
GRPCURL=docker run --network=host -i --rm -v $$(pwd)/testsrv.pb:/testsrv.pb $(GRPCURL_IMAGE) \
44
-d @ -plaintext -protoset /testsrv.pb 127.0.0.1:51051
5+
EVAL_TARGET?=rego
56

67
all: testsrv.pb testsrv-image test-setup test test-teardown
78

@@ -14,7 +15,7 @@ testsrv.pb: testsrv/test.proto
1415

1516
.PHONY: test-setup
1617
test-setup:
17-
docker-compose up -d
18+
EVAL_TARGET=$(EVAL_TARGET) docker-compose up -d
1819

1920
.PHONY: test-teardown
2021
test-teardown:

Diff for: examples/grpc/docker-compose.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ services:
1414
command:
1515
- run
1616
- --server
17-
- --config-file=/opa.yaml
17+
- --config-file=/opa-${EVAL_TARGET}.yaml
1818
- /policy.rego
1919
volumes:
2020
- ./testsrv.pb:/testsrv.pb
2121
- ./policy.rego:/policy.rego
22-
- ./opa.yaml:/opa.yaml
22+
- ./opa-wasm.yaml:/opa-wasm.yaml
23+
- ./opa-rego.yaml:/opa-rego.yaml
2324
testsrv:
2425
image: testsrv:latest
2526
ports:
File renamed without changes.

Diff for: examples/grpc/opa-wasm.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins:
2+
envoy_ext_authz_grpc:
3+
addr: ":9191"
4+
path: envoy/authz/allow
5+
dry-run: false
6+
enable-reflection: true
7+
proto-descriptor: /testsrv.pb
8+
eval-target: wasm
9+
decision_logs:
10+
console: true

Diff for: internal/internal.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ import (
4141
"github.com/open-policy-agent/opa/util"
4242
)
4343

44-
const defaultAddr = ":9191"
45-
const defaultPath = "envoy/authz/allow"
46-
const defaultDryRun = false
47-
const defaultEnableReflection = false
44+
const (
45+
defaultAddr = ":9191"
46+
defaultPath = "envoy/authz/allow"
47+
defaultDryRun = false
48+
defaultEnableReflection = false
49+
defaultEvalTarget = "rego"
50+
)
4851

4952
// PluginName is the name to register with the OPA plugin manager
5053
const PluginName = "envoy_ext_authz_grpc"
@@ -58,6 +61,7 @@ func Validate(m *plugins.Manager, bs []byte) (*Config, error) {
5861
Addr: defaultAddr,
5962
DryRun: defaultDryRun,
6063
EnableReflection: defaultEnableReflection,
64+
EvalTarget: defaultEvalTarget,
6165
}
6266

6367
if err := util.Unmarshal(bs, &cfg); err != nil {
@@ -133,6 +137,7 @@ type Config struct {
133137
Path string `json:"path"`
134138
DryRun bool `json:"dry-run"`
135139
EnableReflection bool `json:"enable-reflection"`
140+
EvalTarget string `json:"eval-target"`
136141
parsedQuery ast.Body
137142
ProtoDescriptor string `json:"proto-descriptor"`
138143
protoSet *protoregistry.Files
@@ -147,6 +152,8 @@ type envoyExtAuthzGrpcServer struct {
147152
interQueryBuiltinCache iCache.InterQueryCache
148153
}
149154

155+
var _ envoyauth.EvalContextWithTarget = (*envoyExtAuthzGrpcServer)(nil)
156+
150157
type envoyExtAuthzV2Wrapper struct {
151158
v3 *envoyExtAuthzGrpcServer
152159
}
@@ -167,6 +174,10 @@ func (p *envoyExtAuthzGrpcServer) Runtime() *ast.Term {
167174
return p.manager.Info
168175
}
169176

177+
func (p *envoyExtAuthzGrpcServer) Target() string {
178+
return p.cfg.EvalTarget
179+
}
180+
170181
func (p *envoyExtAuthzGrpcServer) PreparedQueryDoOnce() *sync.Once {
171182
return p.preparedQueryDoOnce
172183
}
@@ -243,6 +254,7 @@ func (p *envoyExtAuthzGrpcServer) listen() {
243254
"path": p.cfg.Path,
244255
"dry-run": p.cfg.DryRun,
245256
"enable-reflection": p.cfg.EnableReflection,
257+
"eval-target": p.cfg.EvalTarget,
246258
}).Info("Starting gRPC server.")
247259

248260
p.manager.UpdatePluginStatus(PluginName, &plugins.Status{State: plugins.StateOK})

0 commit comments

Comments
 (0)