1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 07:57:07 +00:00
kyverno/pkg/webhooks/policyvalidation.go

102 lines
2.9 KiB
Go
Raw Normal View History

2019-07-15 16:07:56 -07:00
package webhooks
import (
"encoding/json"
"fmt"
2019-08-20 17:56:02 -07:00
"reflect"
2019-07-15 16:07:56 -07:00
"github.com/golang/glog"
2019-08-14 10:01:47 -07:00
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1alpha1"
2019-07-15 16:07:56 -07:00
"github.com/nirmata/kyverno/pkg/utils"
v1beta1 "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//HandlePolicyValidation performs the validation check on policy resource
func (ws *WebhookServer) handlePolicyValidation(request *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse {
var policy *kyverno.Policy
2019-08-08 13:09:40 -07:00
admissionResp := &v1beta1.AdmissionResponse{
Allowed: true,
}
2019-08-21 16:42:42 -07:00
//TODO: can this happen? wont this be picked by OpenAPI spec schema ?
2019-08-21 16:42:42 -07:00
raw := request.Object.Raw
2019-08-08 13:09:40 -07:00
if err := json.Unmarshal(raw, &policy); err != nil {
glog.Errorf("Failed to unmarshal policy admission request, err %v\n", err)
2019-08-21 12:38:15 -07:00
return &v1beta1.AdmissionResponse{Allowed: false,
Result: &metav1.Status{
Message: fmt.Sprintf("Failed to unmarshal policy admission request err %v", err),
}}
2019-08-08 13:09:40 -07:00
}
2019-08-21 16:42:42 -07:00
// check for uniqueness of rule names while CREATE/DELET
admissionResp = ws.validateUniqueRuleName(policy)
if admissionResp.Allowed {
2019-08-08 13:09:40 -07:00
ws.manageWebhookConfigurations(*policy, request.Operation)
}
2019-08-08 13:09:40 -07:00
return admissionResp
2019-07-15 16:07:56 -07:00
}
2019-08-20 17:56:02 -07:00
func (ws *WebhookServer) validatePolicy(policy *kyverno.Policy) *v1beta1.AdmissionResponse {
admissionResp := ws.validateUniqueRuleName(policy)
if !admissionResp.Allowed {
return admissionResp
}
return ws.validateOverlayPattern(policy)
}
func (ws *WebhookServer) validateOverlayPattern(policy *kyverno.Policy) *v1beta1.AdmissionResponse {
for _, rule := range policy.Spec.Rules {
2019-08-21 12:38:15 -07:00
if reflect.DeepEqual(rule.Validation, kyverno.Validation{}) {
continue
}
if rule.Validation.Pattern == nil && len(rule.Validation.AnyPattern) == 0 {
return &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: fmt.Sprintf("Invalid policy, neither pattern nor anyPattern found in validate rule %s", rule.Name),
},
2019-08-20 17:56:02 -07:00
}
}
2019-08-21 14:06:06 -07:00
if rule.Validation.Pattern != nil && len(rule.Validation.AnyPattern) != 0 {
return &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: fmt.Sprintf("Invalid policy, either pattern or anyPattern is allowed in validate rule %s", rule.Name),
},
}
}
2019-08-20 17:56:02 -07:00
}
return &v1beta1.AdmissionResponse{Allowed: true}
}
2019-07-15 16:07:56 -07:00
// Verify if the Rule names are unique within a policy
func (ws *WebhookServer) validateUniqueRuleName(policy *kyverno.Policy) *v1beta1.AdmissionResponse {
2019-07-15 16:07:56 -07:00
var ruleNames []string
for _, rule := range policy.Spec.Rules {
if utils.Contains(ruleNames, rule.Name) {
msg := fmt.Sprintf(`The policy "%s" is invalid: duplicate rule name: "%s"`, policy.Name, rule.Name)
glog.Errorln(msg)
return &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: msg,
},
}
}
ruleNames = append(ruleNames, rule.Name)
}
2019-08-21 18:47:49 -07:00
glog.V(4).Infof("Policy validation passed")
2019-07-15 16:07:56 -07:00
return &v1beta1.AdmissionResponse{
Allowed: true,
}
}