mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 07:57:07 +00:00
* refactor: introduce engine api package Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * status Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * refactor: clean engine api package Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * cleanup Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * more tests Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * refactor: remove PolicySpec from engine api Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * rm Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * constructor Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> * fix Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> --------- Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com> Co-authored-by: shuting <shuting@nirmata.com>
67 lines
2 KiB
Go
67 lines
2 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-logr/logr"
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
|
engineutils "github.com/kyverno/kyverno/pkg/utils/engine"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func getAction(hasViolations bool, i int) string {
|
|
action := "error"
|
|
if hasViolations {
|
|
action = "violation"
|
|
}
|
|
if i > 1 {
|
|
action = action + "s"
|
|
}
|
|
return action
|
|
}
|
|
|
|
// returns true -> if there is even one policy that blocks resource request
|
|
// returns false -> if all the policies are meant to report only, we dont block resource request
|
|
func BlockRequest(engineResponses []*engineapi.EngineResponse, failurePolicy kyvernov1.FailurePolicyType, log logr.Logger) bool {
|
|
for _, er := range engineResponses {
|
|
if engineutils.BlockRequest(er, failurePolicy) {
|
|
log.V(2).Info("blocking admission request", "policy", er.Policy.GetName())
|
|
return true
|
|
}
|
|
}
|
|
log.V(4).Info("allowing admission request")
|
|
return false
|
|
}
|
|
|
|
// GetBlockedMessages gets the error messages for rules with error or fail status
|
|
func GetBlockedMessages(engineResponses []*engineapi.EngineResponse) string {
|
|
if len(engineResponses) == 0 {
|
|
return ""
|
|
}
|
|
failures := make(map[string]interface{})
|
|
hasViolations := false
|
|
for _, er := range engineResponses {
|
|
ruleToReason := make(map[string]string)
|
|
for _, rule := range er.PolicyResponse.Rules {
|
|
if rule.Status != engineapi.RuleStatusPass {
|
|
ruleToReason[rule.Name] = rule.Message
|
|
if rule.Status == engineapi.RuleStatusFail {
|
|
hasViolations = true
|
|
}
|
|
}
|
|
}
|
|
if len(ruleToReason) != 0 {
|
|
failures[er.Policy.GetName()] = ruleToReason
|
|
}
|
|
}
|
|
if len(failures) == 0 {
|
|
return ""
|
|
}
|
|
r := engineResponses[0].PolicyResponse.Resource
|
|
resourceName := fmt.Sprintf("%s/%s/%s", r.Kind, r.Namespace, r.Name)
|
|
action := getAction(hasViolations, len(failures))
|
|
results, _ := yaml.Marshal(failures)
|
|
msg := fmt.Sprintf("\n\npolicy %s for resource %s: \n\n%s", resourceName, action, results)
|
|
return msg
|
|
}
|