mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
Merge pull request #738 from shravanshetty1/725_validation_enforce_error
#725 - anyPattern error improvements
This commit is contained in:
commit
77b1294c38
3 changed files with 22 additions and 12 deletions
|
@ -234,7 +234,12 @@ func validatePatterns(ctx context.EvalInterface, resource unstructured.Unstructu
|
||||||
errorStr = append(errorStr, err.Error())
|
errorStr = append(errorStr, err.Error())
|
||||||
}
|
}
|
||||||
resp.Success = false
|
resp.Success = false
|
||||||
resp.Message = fmt.Sprintf("Validation rule '%s' failed. %s", rule.Name, errorStr)
|
glog.V(4).Infof("Validation rule '%s' failed. %s", rule.Name, errorStr)
|
||||||
|
if rule.Validation.Message == "" {
|
||||||
|
resp.Message = fmt.Sprintf("Validation rule '%s' has failed", rule.Name)
|
||||||
|
} else {
|
||||||
|
resp.Message = rule.Validation.Message
|
||||||
|
}
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -294,7 +294,7 @@ func TestValidate_Fail_anyPattern(t *testing.T) {
|
||||||
resourceUnstructured, err := utils.ConvertToUnstructured(rawResource)
|
resourceUnstructured, err := utils.ConvertToUnstructured(rawResource)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
er := Validate(PolicyContext{Policy: policy, NewResource: *resourceUnstructured})
|
er := Validate(PolicyContext{Policy: policy, NewResource: *resourceUnstructured})
|
||||||
msgs := []string{"Validation rule 'check-default-namespace' failed. [anyPattern[0] failed; Validation rule failed at '/metadata/namespace/' to validate value '<nil>' with pattern '?*' anyPattern[1] failed; Validation rule failed at '/metadata/namespace/' to validate value '<nil>' with pattern '!default']"}
|
msgs := []string{"A namespace is required"}
|
||||||
for index, r := range er.PolicyResponse.Rules {
|
for index, r := range er.PolicyResponse.Rules {
|
||||||
assert.Equal(t, r.Message, msgs[index])
|
assert.Equal(t, r.Message, msgs[index])
|
||||||
}
|
}
|
||||||
|
@ -1582,5 +1582,5 @@ func Test_VariableSubstitutionPathNotExistInAnyPattern_AllPathPresent_NonePatter
|
||||||
|
|
||||||
// expectedMsg := "Validation error: ; Validation rule test-path-not-exist anyPattern[0] failed at path /spec/template/spec/containers/0/name/. Validation rule test-path-not-exist anyPattern[1] failed at path /spec/template/spec/containers/0/name/."
|
// expectedMsg := "Validation error: ; Validation rule test-path-not-exist anyPattern[0] failed at path /spec/template/spec/containers/0/name/. Validation rule test-path-not-exist anyPattern[1] failed at path /spec/template/spec/containers/0/name/."
|
||||||
assert.Assert(t, !er.PolicyResponse.Rules[0].Success)
|
assert.Assert(t, !er.PolicyResponse.Rules[0].Success)
|
||||||
assert.Equal(t, er.PolicyResponse.Rules[0].Message, "Validation rule 'test-path-not-exist' failed. [anyPattern[0] failed; Validation rule failed at '/spec/template/spec/containers/0/name/' to validate value 'pod-test-pod' with pattern 'test*' anyPattern[1] failed; Validation rule failed at '/spec/template/spec/containers/0/name/' to validate value 'pod-test-pod' with pattern 'test*']")
|
assert.Equal(t, er.PolicyResponse.Rules[0].Message, "Validation rule 'test-path-not-exist' has failed")
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
yamlv2 "gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
||||||
"github.com/nirmata/kyverno/pkg/engine/response"
|
"github.com/nirmata/kyverno/pkg/engine/response"
|
||||||
|
@ -37,22 +39,25 @@ func toBlockResource(engineReponses []response.EngineResponse) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getEnforceFailureErrorMsg gets the error messages for failed enforce policy
|
// getEnforceFailureErrorMsg gets the error messages for failed enforce policy
|
||||||
func getEnforceFailureErrorMsg(engineReponses []response.EngineResponse) string {
|
func getEnforceFailureErrorMsg(engineResponses []response.EngineResponse) string {
|
||||||
var str []string
|
policyToRule := make(map[string]interface{})
|
||||||
var resourceInfo string
|
var resourceName string
|
||||||
|
for _, er := range engineResponses {
|
||||||
for _, er := range engineReponses {
|
|
||||||
if !er.IsSuccesful() && er.PolicyResponse.ValidationFailureAction == Enforce {
|
if !er.IsSuccesful() && er.PolicyResponse.ValidationFailureAction == Enforce {
|
||||||
resourceInfo = fmt.Sprintf("%s/%s/%s", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
|
ruleToReason := make(map[string]string)
|
||||||
str = append(str, fmt.Sprintf("failed policy %s:", er.PolicyResponse.Policy))
|
|
||||||
for _, rule := range er.PolicyResponse.Rules {
|
for _, rule := range er.PolicyResponse.Rules {
|
||||||
if !rule.Success {
|
if !rule.Success {
|
||||||
str = append(str, rule.ToString())
|
ruleToReason[rule.Name] = rule.Message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
resourceName = fmt.Sprintf("%s/%s/%s", er.PolicyResponse.Resource.Kind, er.PolicyResponse.Resource.Namespace, er.PolicyResponse.Resource.Name)
|
||||||
|
|
||||||
|
policyToRule[er.PolicyResponse.Policy] = ruleToReason
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("Resource %s %s", resourceInfo, strings.Join(str, ";"))
|
|
||||||
|
result, _ := yamlv2.Marshal(policyToRule)
|
||||||
|
return "\n\nresource " + resourceName + " was blocked due to the following policies\n\n" + string(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getErrorMsg gets all failed engine response message
|
// getErrorMsg gets all failed engine response message
|
||||||
|
|
Loading…
Add table
Reference in a new issue