Skip to content

Commit d94296f

Browse files
committed
ffactor: update match policy method signature
1 parent c3f115d commit d94296f

File tree

2 files changed

+12
-67
lines changed

2 files changed

+12
-67
lines changed

validator/policyeval.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package validator
22

33
import (
4-
"bufio"
5-
"bytes"
64
"context"
75
"fmt"
86
"github.com/open-policy-agent/opa/ast"
97
"github.com/open-policy-agent/opa/rego"
10-
"io/ioutil"
11-
"strings"
128
)
139

1410
//Evaluator OPA evaluate interface
1511
type Evaluator interface {
16-
EvaluatePolicy(evalProperty []string, policy string, data string) ([]*ValidateResult, error)
12+
EvaluatePolicy(queryParam []string, policy string, data string) ([]*ValidateResult, error)
1713
}
1814

1915
//policyEval opa evaluate object
@@ -27,8 +23,7 @@ func NewPolicyEval() Evaluator {
2723

2824
//EvaluatePolicy evaluate opa policy against given json input , accept opa pkg name ,policy rule(deny/allow),policy and input data
2925
// return evaluation result in a bool form
30-
func (pe policyEval) EvaluatePolicy(evalProperty []string, policy string, data string) ([]*ValidateResult, error) {
31-
pkgName := pe.detectPkgName(policy)
26+
func (pe policyEval) EvaluatePolicy(queryParam []string, policy string, data string) ([]*ValidateResult, error) {
3227
ctx := context.Background()
3328
var inputObject interface{}
3429
// try to read data as json format
@@ -48,18 +43,14 @@ func (pe policyEval) EvaluatePolicy(evalProperty []string, policy string, data s
4843
}
4944
// Compile the module. The keys are used as identifiers in error messages.
5045
compiler, err := ast.CompileModules(map[string]string{
51-
fmt.Sprintf("%s.rego", pkgName): policy,
46+
fmt.Sprintf("%s.rego", "eval"): policy,
5247
})
5348
if err != nil {
5449
return nil, err
5550
}
5651
regoFunc := make([]func(r *rego.Rego), 0)
57-
for _, pr := range evalProperty {
58-
if len(pkgName) > 0 {
59-
regoFunc = append(regoFunc, rego.Query(fmt.Sprintf("data.%s.%s", pkgName, pr)))
60-
} else {
61-
regoFunc = append(regoFunc, rego.Query(policy))
62-
}
52+
for _, pr := range queryParam {
53+
regoFunc = append(regoFunc, rego.Query(fmt.Sprintf("data.%s", pr)))
6354
}
6455
regoFunc = append(regoFunc, rego.Compiler(compiler))
6556
regoFunc = append(regoFunc, rego.Input(inputObject))
@@ -75,28 +66,6 @@ func (pe policyEval) EvaluatePolicy(evalProperty []string, policy string, data s
7566
return validateResult, nil
7667
}
7768

78-
func (pe policyEval) detectPkgName(policy string) string {
79-
var pkgName string
80-
const policyPackage = "package"
81-
reader := ioutil.NopCloser(bytes.NewReader([]byte(policy)))
82-
defer func() {
83-
err := reader.Close()
84-
if err != nil {
85-
fmt.Println(err)
86-
}
87-
}()
88-
scanner := bufio.NewScanner(reader)
89-
// optionally, resize scanner's capacity for lines over 64K, see next example
90-
for scanner.Scan() {
91-
line := scanner.Text()
92-
if strings.HasPrefix(line, policyPackage) {
93-
pkgName = strings.TrimSpace(strings.Replace(line, policyPackage, "", -1))
94-
break
95-
}
96-
}
97-
return pkgName
98-
}
99-
10069
//ValidateResult opa validation results
10170
type ValidateResult struct {
10271
Value bool

validator/policyeval_test.go

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func Test_PolicyEval(t *testing.T) {
1616
want bool
1717
wantError error
1818
}{
19-
{name: "test validate policy deny pod name json format", data: "./fixture/pod.json", pkgName: "example", policyRule: []string{"deny"}, policy: "./fixture/pod_policy_deny", want: true, wantError: nil},
20-
{name: "test validate policy deny pod name yaml format", data: "./fixture/pod.yaml", pkgName: "example", policyRule: []string{"deny"}, policy: "./fixture/pod_policy_deny", want: true, wantError: nil},
21-
{name: "test validate policy allow pod name", data: "./fixture/allow_pod.json", pkgName: "example", policyRule: []string{"deny"}, policy: "./fixture/pod_policy_deny", want: false, wantError: nil},
22-
{name: "test validate policy bad data", data: "./fixture/badJson.json", pkgName: "example", policyRule: []string{"deny"}, policy: "./fixture/pod_policy_deny", want: false, wantError: nil},
23-
{name: "test validate policy bad policy", data: "./fixture/badJson.json", pkgName: "example", policyRule: []string{"deny"}, policy: "./fixture/pod_policy_deny_bad", want: false, wantError: fmt.Errorf("1 error occurred: .rego:5: rego_parse_error: unexpected } token\n\t}\n\t^")},
19+
{name: "test validate policy deny pod name json format", data: "./fixture/pod.json", policyRule: []string{"example.deny"}, policy: "./fixture/pod_policy_deny", want: true, wantError: nil},
20+
{name: "test validate policy deny pod name yaml format", data: "./fixture/pod.yaml", policyRule: []string{"example.deny"}, policy: "./fixture/pod_policy_deny", want: true, wantError: nil},
21+
{name: "test validate policy allow pod name", data: "./fixture/allow_pod.json", policyRule: []string{"example.deny"}, policy: "./fixture/pod_policy_deny", want: false, wantError: nil},
22+
{name: "test validate policy bad data", data: "./fixture/badJson.json", policyRule: []string{"example.deny"}, policy: "./fixture/pod_policy_deny", want: false, wantError: nil},
23+
{name: "test validate policy bad policy", data: "./fixture/badJson.json", policyRule: []string{"example.deny"}, policy: "./fixture/pod_policy_deny_bad", want: false, wantError: fmt.Errorf("1 error occurred: eval.rego:5: rego_parse_error: unexpected } token\n\t}\n\t^")},
2424
}
2525
for _, tt := range tests {
2626
t.Run(tt.name, func(t *testing.T) {
@@ -32,42 +32,18 @@ func Test_PolicyEval(t *testing.T) {
3232
if err != nil {
3333
t.Fatal(err)
3434
}
35-
got, err := NewPolicyEval().EvaluatePolicy([]string{"deny"}, string(policy), string(data))
35+
got, err := NewPolicyEval().EvaluatePolicy(tt.policyRule, string(policy), string(data))
3636
if err != nil {
3737
goErr := err.Error()
3838
if goErr != tt.wantError.Error() {
3939
t.Fatal(err)
4040
}
4141
}
42-
if len(got) > 0 {
42+
if err == nil {
4343
if got[0].Value != tt.want {
4444
t.Errorf("Test_PolicyEval() = %v, want %v", got[0], tt.want)
4545
}
4646
}
4747
})
4848
}
4949
}
50-
51-
func Test_DetectPkgName(t *testing.T) {
52-
tests := []struct {
53-
name string
54-
policy string
55-
want string
56-
}{
57-
{name: "detect pkg name exist", policy: "./fixture/pod_policy_deny", want: "example"},
58-
{name: "detect pkg name not exist", policy: "./fixture/pod_policy_deny_bad", want: ""},
59-
}
60-
for _, tt := range tests {
61-
t.Run(tt.name, func(t *testing.T) {
62-
policy, err := ioutil.ReadFile(tt.policy)
63-
if err != nil {
64-
t.Fatal(err)
65-
}
66-
pe := &policyEval{}
67-
got := pe.detectPkgName(string(policy))
68-
if got != tt.want {
69-
t.Errorf("Test_DetectPkgName() = %v, want %v", got[0], tt.want)
70-
}
71-
})
72-
}
73-
}

0 commit comments

Comments
 (0)