mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 07:57:07 +00:00
137 lines
3.9 KiB
Go
137 lines
3.9 KiB
Go
package processor
|
|
|
|
import (
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/policy/annotations"
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
|
"k8s.io/api/admissionregistration/v1alpha1"
|
|
)
|
|
|
|
type ResultCounts struct {
|
|
Pass int
|
|
Fail int
|
|
Warn int
|
|
Error int
|
|
Skip int
|
|
}
|
|
|
|
func (rc *ResultCounts) IncrementError(inc int) {
|
|
rc.Error += inc
|
|
}
|
|
|
|
func (rc *ResultCounts) addEngineResponses(auditWarn bool, responses ...engineapi.EngineResponse) {
|
|
for _, response := range responses {
|
|
rc.addEngineResponse(auditWarn, response)
|
|
}
|
|
}
|
|
|
|
func (rc *ResultCounts) addEngineResponse(auditWarn bool, response engineapi.EngineResponse) {
|
|
if !response.IsEmpty() {
|
|
genericPolicy := response.Policy()
|
|
if polType := genericPolicy.GetType(); polType == engineapi.ValidatingAdmissionPolicyType {
|
|
return
|
|
}
|
|
policy := genericPolicy.AsKyvernoPolicy()
|
|
scored := annotations.Scored(policy.GetAnnotations())
|
|
for _, rule := range autogen.ComputeRules(policy, "") {
|
|
if rule.HasValidate() || rule.HasVerifyImageChecks() || rule.HasVerifyImages() {
|
|
for _, valResponseRule := range response.PolicyResponse.Rules {
|
|
if rule.Name == valResponseRule.Name() {
|
|
switch valResponseRule.Status() {
|
|
case engineapi.RuleStatusPass:
|
|
rc.Pass++
|
|
case engineapi.RuleStatusFail:
|
|
if !scored {
|
|
rc.Warn++
|
|
break
|
|
} else if auditWarn && response.GetValidationFailureAction().Audit() {
|
|
rc.Warn++
|
|
} else {
|
|
rc.Fail++
|
|
}
|
|
case engineapi.RuleStatusError:
|
|
rc.Error++
|
|
case engineapi.RuleStatusWarn:
|
|
rc.Warn++
|
|
case engineapi.RuleStatusSkip:
|
|
rc.Skip++
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rc *ResultCounts) addGenerateResponse(auditWarn bool, resPath string, response engineapi.EngineResponse) {
|
|
genericPolicy := response.Policy()
|
|
if polType := genericPolicy.GetType(); polType == engineapi.ValidatingAdmissionPolicyType {
|
|
return
|
|
}
|
|
policy := genericPolicy.AsKyvernoPolicy()
|
|
for _, policyRule := range autogen.ComputeRules(policy, "") {
|
|
for _, ruleResponse := range response.PolicyResponse.Rules {
|
|
if policyRule.Name == ruleResponse.Name() {
|
|
if ruleResponse.Status() == engineapi.RuleStatusPass {
|
|
rc.Pass++
|
|
} else {
|
|
if auditWarn && response.GetValidationFailureAction().Audit() {
|
|
rc.Warn++
|
|
} else {
|
|
rc.Fail++
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (rc *ResultCounts) addMutateResponse(resourcePath string, response engineapi.EngineResponse) bool {
|
|
genericPolicy := response.Policy()
|
|
if polType := genericPolicy.GetType(); polType == engineapi.ValidatingAdmissionPolicyType {
|
|
return false
|
|
}
|
|
policy := genericPolicy.AsKyvernoPolicy()
|
|
var policyHasMutate bool
|
|
for _, rule := range autogen.ComputeRules(policy, "") {
|
|
if rule.HasMutate() {
|
|
policyHasMutate = true
|
|
}
|
|
}
|
|
if !policyHasMutate {
|
|
return false
|
|
}
|
|
printMutatedRes := false
|
|
for _, policyRule := range autogen.ComputeRules(policy, "") {
|
|
for _, mutateResponseRule := range response.PolicyResponse.Rules {
|
|
if policyRule.Name == mutateResponseRule.Name() {
|
|
if mutateResponseRule.Status() == engineapi.RuleStatusPass {
|
|
rc.Pass++
|
|
printMutatedRes = true
|
|
} else if mutateResponseRule.Status() == engineapi.RuleStatusSkip {
|
|
rc.Skip++
|
|
} else if mutateResponseRule.Status() == engineapi.RuleStatusError {
|
|
rc.Error++
|
|
} else {
|
|
rc.Fail++
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
return printMutatedRes
|
|
}
|
|
|
|
func (rc *ResultCounts) addValidatingAdmissionResponse(vap v1alpha1.ValidatingAdmissionPolicy, engineResponse engineapi.EngineResponse) {
|
|
for _, ruleResp := range engineResponse.PolicyResponse.Rules {
|
|
if ruleResp.Status() == engineapi.RuleStatusPass {
|
|
rc.Pass++
|
|
} else if ruleResp.Status() == engineapi.RuleStatusFail {
|
|
rc.Fail++
|
|
} else if ruleResp.Status() == engineapi.RuleStatusError {
|
|
rc.Error++
|
|
}
|
|
}
|
|
}
|