2019-07-15 16:07:56 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
2020-08-14 12:21:06 -07:00
|
|
|
"time"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
policyvalidate "github.com/kyverno/kyverno/pkg/policy"
|
2020-03-06 03:00:18 +05:30
|
|
|
|
2019-07-15 16:07:56 -07:00
|
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
2020-03-17 17:23:18 -07:00
|
|
|
|
2019-07-15 16:07:56 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
//HandlePolicyValidation performs the validation check on policy resource
|
2020-04-10 23:24:54 +05:30
|
|
|
func (ws *WebhookServer) policyValidation(request *v1beta1.AdmissionRequest) *v1beta1.AdmissionResponse {
|
2020-07-09 11:48:34 -07:00
|
|
|
logger := ws.log.WithValues("action", "policyvalidation", "uid", request.UID, "kind", request.Kind, "namespace", request.Namespace, "name", request.Name, "operation", request.Operation)
|
|
|
|
|
2020-08-14 12:21:06 -07:00
|
|
|
startTime := time.Now()
|
|
|
|
logger.V(3).Info("start validating policy")
|
|
|
|
defer logger.V(3).Info("finished validating policy", "time", time.Since(startTime).String())
|
|
|
|
|
2019-08-27 16:44:10 -07:00
|
|
|
//TODO: can this happen? wont this be picked by OpenAPI spec schema ?
|
2020-03-29 09:09:26 +05:30
|
|
|
if err := policyvalidate.Validate(request.Object.Raw, ws.client, false, ws.openAPIController); err != nil {
|
2020-10-29 15:00:22 -07:00
|
|
|
logger.Error(err, "failed to validate policy")
|
2020-03-24 23:12:45 +05:30
|
|
|
return &v1beta1.AdmissionResponse{
|
2019-09-26 18:02:24 -07:00
|
|
|
Allowed: false,
|
|
|
|
Result: &metav1.Status{
|
|
|
|
Message: err.Error(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 23:12:45 +05:30
|
|
|
|
|
|
|
// if the policy contains mutating & validation rules and it config does not exist we create one
|
|
|
|
// queue the request
|
|
|
|
ws.resourceWebhookWatcher.RegisterResourceWebhook()
|
|
|
|
return &v1beta1.AdmissionResponse{
|
|
|
|
Allowed: true,
|
2019-09-04 13:43:12 -07:00
|
|
|
}
|
2019-07-15 16:07:56 -07:00
|
|
|
}
|