Skip to content

Commit

Permalink
rego: Fix panic when partial evaluating w/ tracers
Browse files Browse the repository at this point in the history
There was a typo from a while back that caused problems with this. It
would only occur in cases where the Rego object was created without
tracers, prepared, and then partially evaluated with tracers supplied
as part of the evaluation context. Any of the other code paths would
actually work as the original Rego object would still have them and
the (wrong) reference wouldn't cause a panic.

At some point more recently we changed `opa eval` to split up its
options for the Rego object and evaluation. Doing this caused the
problem to surface when doing anything with `opa eval -p --explain ..`

Fixes: #2007
Signed-off-by: Patrick East <east.patrick@gmail.com>
  • Loading branch information
patrick-east committed Jan 22, 2020
1 parent ca18a95 commit 9b7c645
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ func (r *Rego) partial(ctx context.Context, ectx *EvalContext) (*PartialQueries,
WithIndexing(ectx.indexing)

for i := range ectx.tracers {
q = q.WithTracer(r.tracers[i])
q = q.WithTracer(ectx.tracers[i])
}

if ectx.parsedInput != nil {
Expand Down
40 changes: 40 additions & 0 deletions rego/rego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,46 @@ func TestPartialResultWithInput(t *testing.T) {
assertEval(t, r2, "[[true]]")
}

func TestPreparedPartialResultWithTracer(t *testing.T) {
mod := `
package test
default p = false
p {
input.x = 1
}
`
r := New(
Query("data.test.p == true"),
Module("test.rego", mod),
)

tracer := topdown.NewBufferTracer()

ctx := context.Background()
pq, err := r.PrepareForPartial(ctx)
if err != nil {
t.Fatalf("unexpected error from Rego.PrepareForPartial(): %s", err.Error())
}

pqs, err := pq.Partial(ctx, EvalTracer(tracer))
if err != nil {
t.Fatalf("unexpected error from PreparedEvalQuery.Partial(): %s", err.Error())
}

expectedQuery := "input.x = 1"
if len(pqs.Queries) != 1 {
t.Errorf("expected 1 query but found %d: %+v", len(pqs.Queries), pqs)
}
if pqs.Queries[0].String() != expectedQuery {
t.Errorf("unexpected query in result, expected='%s' found='%s'",
expectedQuery, pqs.Queries[0].String())
}

if len(*tracer) == 0 {
t.Errorf("Expected buffer tracer to contain > 0 traces")
}
}

func TestMissingLocation(t *testing.T) {

// Create a query programmatically and evaluate it. The Location information
Expand Down

0 comments on commit 9b7c645

Please sign in to comment.