-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Johan Fylling <[email protected]>
- Loading branch information
1 parent
1318f4b
commit fff4204
Showing
16 changed files
with
377 additions
and
15 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
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
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,6 @@ | ||
// Copyright 2024 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. | ||
|
||
// Deprecated: Use [github.com/open-policy-agent/opa/v1/repl] instead. | ||
package repl |
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,16 @@ | ||
// Copyright 2016 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 repl | ||
|
||
import v1 "github.com/open-policy-agent/opa/v1/repl" | ||
|
||
// Error is the error type returned by the REPL. | ||
type Error = v1.Error | ||
|
||
const ( | ||
// BadArgsErr indicates bad arguments were provided to a built-in REPL | ||
// command. | ||
BadArgsErr string = v1.BadArgsErr | ||
) |
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,26 @@ | ||
// Copyright 2016 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 repl implements a Read-Eval-Print-Loop (REPL) for interacting with the policy engine. | ||
// | ||
// The REPL is typically used from the command line, however, it can also be used as a library. | ||
// nolint: goconst // String reuse here doesn't make sense to deduplicate. | ||
package repl | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/storage" | ||
v1 "github.com/open-policy-agent/opa/v1/repl" | ||
) | ||
|
||
// REPL represents an instance of the interactive shell. | ||
type REPL = v1.REPL | ||
|
||
// New returns a new instance of the REPL. | ||
func New(store storage.Store, historyPath string, output io.Writer, outputFormat string, errLimit int, banner string) *REPL { | ||
return v1.New(store, historyPath, output, outputFormat, errLimit, banner). | ||
WithRegoVersion(ast.DefaultRegoVersion) | ||
} |
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,152 @@ | ||
// Copyright 2024 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 repl | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/open-policy-agent/opa/storage" | ||
"github.com/open-policy-agent/opa/storage/inmem" | ||
"github.com/open-policy-agent/opa/util" | ||
) | ||
|
||
func TestOneShot_DefaultRegoVersion(t *testing.T) { | ||
type action struct { | ||
line string | ||
expOutput string | ||
expErrs []string | ||
} | ||
|
||
tests := []struct { | ||
note string | ||
actions []action | ||
}{ | ||
{ | ||
note: "v1 keywords used", | ||
actions: []action{ | ||
{ | ||
line: "a contains 2 if { true }", | ||
expErrs: []string{ | ||
"rego_unsafe_var_error: var a is unsafe", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
note: "v1 keywords not used", | ||
actions: []action{ | ||
{ | ||
line: "a[2] { true }", | ||
expOutput: "Rule 'a' defined in package repl. Type 'show' to see rules.\n", | ||
}, | ||
}, | ||
}, | ||
{ | ||
note: "v1 keywords imported", | ||
actions: []action{ | ||
{ | ||
line: "import future.keywords", | ||
}, | ||
{ | ||
line: "a contains 2 if { true }", | ||
expOutput: "Rule 'a' defined in package repl. Type 'show' to see rules.\n", | ||
}, | ||
}, | ||
}, | ||
{ | ||
note: "rego.v1 imported", | ||
actions: []action{ | ||
{ | ||
line: "import rego.v1", | ||
}, | ||
{ | ||
line: "a contains 2 if { true }", | ||
expOutput: "Rule 'a' defined in package repl. Type 'show' to see rules.\n", | ||
}, | ||
}, | ||
}, | ||
{ | ||
note: "v1 keywords", | ||
actions: []action{ | ||
{ | ||
line: "a contains 2 if { true }", | ||
expErrs: []string{ | ||
"rego_unsafe_var_error: var a is unsafe", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.note, func(t *testing.T) { | ||
ctx := context.Background() | ||
store := newTestStore() | ||
var buffer bytes.Buffer | ||
repl := newRepl(store, &buffer) | ||
|
||
for _, action := range tc.actions { | ||
err := repl.OneShot(ctx, action.line) | ||
|
||
if len(action.expErrs) != 0 { | ||
if err == nil { | ||
t.Fatalf("Expected error but got: %s", buffer.String()) | ||
} | ||
|
||
for _, e := range action.expErrs { | ||
if !strings.Contains(err.Error(), e) { | ||
t.Fatalf("Expected error to contain:\n\n%q\n\nbut got:\n\n%v", e, err) | ||
} | ||
} | ||
} else { | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
expectOutput(t, buffer.String(), action.expOutput) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func expectOutput(t *testing.T, output string, expected string) { | ||
t.Helper() | ||
if output != expected { | ||
t.Errorf("Repl output: expected %#v but got %#v", expected, output) | ||
} | ||
} | ||
|
||
func newRepl(store storage.Store, buffer *bytes.Buffer) *REPL { | ||
repl := New(store, "", buffer, "", 0, "") | ||
return repl | ||
} | ||
|
||
func newTestStore() storage.Store { | ||
input := ` | ||
{ | ||
"a": [ | ||
{ | ||
"b": { | ||
"c": [true,2,false] | ||
} | ||
}, | ||
{ | ||
"b": { | ||
"c": [false,true,1] | ||
} | ||
} | ||
] | ||
} | ||
` | ||
var data map[string]interface{} | ||
err := util.UnmarshalJSON([]byte(input), &data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return inmem.NewFromObject(data) | ||
} |
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
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.