Skip to content

Commit 17d8f3d

Browse files
committed
feat: 1st return key it the match policy
1 parent 956ca26 commit 17d8f3d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

eval/cmdeval.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,33 @@ func (cv commandEvaluate) EvalCommandPolicy(commands []string, evalExpr string,
5454
if err != nil {
5555
return CmdEvalResult{Match: false}
5656
}
57-
val, err := cv.evalPolicy(commands, cmdExec, evalExpr, policy, pep.EvalParamNum, []string{pep.PolicyQueryParam}, pep.ReturnKeys)
57+
val, err := cv.evalPolicy(commands, cmdExec, evalExpr, policy, pep)
5858
if err != nil {
5959
return CmdEvalResult{Match: false, Error: err}
6060
}
61-
return CmdEvalResult{Match: val.EvalExpResult == 0, Error: err, PolicyResult: val.PolicyResult}
61+
return CmdEvalResult{Match: val.EvalExpResult == 0, Error: err, PolicyResult: val.PolicyResult, ReturnKeys: pep.ReturnKeys}
6262
}
6363

64-
func (cv commandEvaluate) evalPolicy(commands []string, cmdExec cmd, evalExpr string, policy string, compareComm int, propertyEval []string, ReturnFields []string) (*FinalResult, error) {
64+
func (cv commandEvaluate) evalPolicy(commands []string, cmdExec cmd, evalExpr string, policy string, pep *utils.PolicyEvalParams) (*FinalResult, error) {
6565
resMap, cmdTotalRes := cv.ExecCommands(commands, cmdExec, evalExpr)
6666
policyEvalResults := make([]utils.PolicyResult, 0)
6767
var policyRes int
68-
if val, ok := resMap[compareComm]; ok {
68+
if val, ok := resMap[pep.EvalParamNum]; ok {
6969
var policyResult utils.PolicyResult
7070
for _, cmdRes := range val {
71-
res, err := validator.NewPolicyEval().EvaluatePolicy(propertyEval, policy, cmdRes)
71+
res, err := validator.NewPolicyEval().EvaluatePolicy([]string{pep.PolicyQueryParam}, policy, cmdRes)
7272
if err != nil {
7373
return nil, err
7474
}
7575
if len(res) > 0 {
76-
policyResult = utils.MatchPolicy(res[0].ExpressionValue[0].Value, ReturnFields)
76+
policyResult = utils.MatchPolicy(res[0].ExpressionValue[0].Value, pep.ReturnKeys)
7777
} else {
78-
policyResult = utils.PolicyResult{ReturnValues: map[string]string{propertyEval[0]: "false"}}
78+
policyResult = utils.PolicyResult{ReturnValues: map[string]string{pep.ReturnKeys[0]: "false"}}
7979
}
8080
policyEvalResults = append(policyEvalResults, policyResult)
8181
}
8282
for _, per := range policyEvalResults {
83-
if returnVal, ok := per.ReturnValues[propertyEval[0]]; ok {
83+
if returnVal, ok := per.ReturnValues[pep.ReturnKeys[0]]; ok {
8484
val, err := strconv.ParseBool(returnVal)
8585
if err != nil {
8686
continue
@@ -140,6 +140,7 @@ type CmdEvalResult struct {
140140
Match bool
141141
CmdEvalExpr string
142142
PolicyResult []utils.PolicyResult
143+
ReturnKeys []string
143144
Error error
144145
}
145146

0 commit comments

Comments
 (0)