2019-07-15 16:07:56 -07:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
2019-11-13 13:13:07 -08:00
|
|
|
"reflect"
|
2019-11-12 14:41:29 -08:00
|
|
|
"time"
|
|
|
|
|
2021-10-29 18:13:20 +02:00
|
|
|
v1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2021-07-23 21:46:50 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/event"
|
|
|
|
|
2020-07-09 11:48:34 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2021-05-15 23:33:41 +05:30
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
2020-11-09 11:26:12 -08:00
|
|
|
"github.com/kyverno/kyverno/pkg/policyreport"
|
2019-07-15 16:07:56 -07:00
|
|
|
v1beta1 "k8s.io/api/admission/v1beta1"
|
2020-05-19 00:14:23 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-07-15 16:07:56 -07:00
|
|
|
)
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
type validationHandler struct {
|
2021-07-14 12:18:59 -07:00
|
|
|
log logr.Logger
|
|
|
|
eventGen event.Interface
|
|
|
|
prGenerator policyreport.GeneratorInterface
|
2021-07-09 18:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleValidation handles validating webhook admission request
|
2019-07-15 16:07:56 -07:00
|
|
|
// If there are no errors in validating rule we apply generation rules
|
2019-08-23 18:34:23 -07:00
|
|
|
// patchedResource is the (resource + patches) after applying mutation rules
|
2021-07-09 18:01:46 -07:00
|
|
|
func (v *validationHandler) handleValidation(
|
2021-05-15 19:15:04 +05:30
|
|
|
promConfig *metrics.PromConfig,
|
2020-05-17 14:37:05 -07:00
|
|
|
request *v1beta1.AdmissionRequest,
|
2022-03-30 16:28:09 +02:00
|
|
|
policies []v1.PolicyInterface,
|
2021-07-09 18:01:46 -07:00
|
|
|
policyContext *engine.PolicyContext,
|
2021-05-15 19:15:04 +05:30
|
|
|
namespaceLabels map[string]string,
|
|
|
|
admissionRequestTimestamp int64) (bool, string) {
|
2020-05-17 14:37:05 -07:00
|
|
|
|
2020-07-02 12:49:10 -07:00
|
|
|
if len(policies) == 0 {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
resourceName := getResourceName(request)
|
|
|
|
logger := v.log.WithValues("action", "validate", "resource", resourceName, "operation", request.Operation, "gvk", request.Kind.String())
|
2019-07-21 14:13:00 -07:00
|
|
|
|
2020-05-19 00:14:23 -07:00
|
|
|
var deletionTimeStamp *metav1.Time
|
2021-07-09 18:01:46 -07:00
|
|
|
if reflect.DeepEqual(policyContext.NewResource, unstructured.Unstructured{}) {
|
|
|
|
deletionTimeStamp = policyContext.NewResource.GetDeletionTimestamp()
|
2020-05-19 00:14:23 -07:00
|
|
|
} else {
|
2021-07-09 18:01:46 -07:00
|
|
|
deletionTimeStamp = policyContext.OldResource.GetDeletionTimestamp()
|
2020-05-19 00:14:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if deletionTimeStamp != nil && request.Operation == v1beta1.Update {
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:10:07 -08:00
|
|
|
var engineResponses []*response.EngineResponse
|
2019-07-15 16:07:56 -07:00
|
|
|
for _, policy := range policies {
|
2022-03-30 16:28:09 +02:00
|
|
|
logger.V(3).Info("evaluating policy", "policy", policy.GetName())
|
2022-03-30 15:04:30 +02:00
|
|
|
policyContext.Policy = policy
|
2021-02-04 02:39:42 +05:30
|
|
|
policyContext.NamespaceLabels = namespaceLabels
|
2019-11-08 18:57:27 -08:00
|
|
|
engineResponse := engine.Validate(policyContext)
|
2019-12-30 17:08:50 -08:00
|
|
|
if reflect.DeepEqual(engineResponse, response.EngineResponse{}) {
|
2019-11-13 13:13:07 -08:00
|
|
|
// we get an empty response if old and new resources created the same response
|
|
|
|
// allow updates if resource update doesnt change the policy evaluation
|
|
|
|
continue
|
|
|
|
}
|
2020-11-30 11:22:20 -08:00
|
|
|
|
2021-07-23 21:46:50 +05:30
|
|
|
// registering the kyverno_policy_results_total metric concurrently
|
|
|
|
go registerPolicyResultsMetricValidation(promConfig, logger, string(request.Operation), policyContext.Policy, *engineResponse)
|
|
|
|
// registering the kyverno_policy_execution_duration_seconds metric concurrently
|
|
|
|
go registerPolicyExecutionDurationMetricValidate(promConfig, logger, string(request.Operation), policyContext.Policy, *engineResponse)
|
2021-05-15 19:15:04 +05:30
|
|
|
|
2019-08-23 18:34:23 -07:00
|
|
|
engineResponses = append(engineResponses, engineResponse)
|
2020-06-30 11:53:27 -07:00
|
|
|
if !engineResponse.IsSuccessful() {
|
2022-03-30 16:28:09 +02:00
|
|
|
logger.V(2).Info("validation failed", "policy", policy.GetName(), "failed rules", engineResponse.GetFailedRules())
|
2019-07-15 16:07:56 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-05-15 14:49:34 -07:00
|
|
|
|
2021-01-02 01:10:14 -08:00
|
|
|
if len(engineResponse.GetSuccessRules()) > 0 {
|
2022-03-30 16:28:09 +02:00
|
|
|
logger.V(2).Info("validation passed", "policy", policy.GetName())
|
2021-01-02 01:10:14 -08:00
|
|
|
}
|
2019-07-15 16:07:56 -07:00
|
|
|
}
|
2020-11-30 11:22:20 -08:00
|
|
|
|
2019-07-15 16:07:56 -07:00
|
|
|
// If Validation fails then reject the request
|
2020-01-06 14:41:02 -08:00
|
|
|
// no violations will be created on "enforce"
|
2020-03-17 11:05:20 -07:00
|
|
|
blocked := toBlockResource(engineResponses, logger)
|
2020-02-19 19:24:34 -08:00
|
|
|
|
|
|
|
// REPORTING EVENTS
|
|
|
|
// Scenario 1:
|
|
|
|
// resource is blocked, as there is a policy in "enforce" mode that failed.
|
|
|
|
// create an event on the policy to inform the resource request was blocked
|
|
|
|
// Scenario 2:
|
2020-12-04 10:04:46 -08:00
|
|
|
// some/all policies failed to apply on the resource. a policy violation is generated.
|
2020-02-19 19:24:34 -08:00
|
|
|
// create an event on the resource and the policy that failed
|
|
|
|
// Scenario 3:
|
2020-11-17 12:01:01 -08:00
|
|
|
// all policies were applied successfully.
|
2020-02-19 19:24:34 -08:00
|
|
|
// create an event on the resource
|
2020-03-17 11:05:20 -07:00
|
|
|
events := generateEvents(engineResponses, blocked, (request.Operation == v1beta1.Update), logger)
|
2021-07-09 18:01:46 -07:00
|
|
|
v.eventGen.Add(events...)
|
2022-01-21 18:06:44 +05:30
|
|
|
|
2020-01-06 14:41:02 -08:00
|
|
|
if blocked {
|
2020-03-17 11:05:20 -07:00
|
|
|
logger.V(4).Info("resource blocked")
|
2021-07-23 21:46:50 +05:30
|
|
|
//registering the kyverno_admission_review_duration_seconds metric concurrently
|
2021-05-15 23:33:41 +05:30
|
|
|
admissionReviewLatencyDuration := int64(time.Since(time.Unix(admissionRequestTimestamp, 0)))
|
2021-07-23 21:46:50 +05:30
|
|
|
go registerAdmissionReviewDurationMetricValidate(promConfig, logger, string(request.Operation), engineResponses, admissionReviewLatencyDuration)
|
|
|
|
//registering the kyverno_admission_requests_total metric concurrently
|
|
|
|
go registerAdmissionRequestsMetricValidate(promConfig, logger, string(request.Operation), engineResponses)
|
2020-01-16 11:57:28 -08:00
|
|
|
return false, getEnforceFailureErrorMsg(engineResponses)
|
2019-07-15 16:07:56 -07:00
|
|
|
}
|
2020-01-06 17:07:11 -08:00
|
|
|
|
2022-01-03 23:43:38 +08:00
|
|
|
// reports are generated for non-managed pods/jobs only
|
|
|
|
// no need to create rcr for managed resources
|
2020-12-08 23:04:16 -08:00
|
|
|
if request.Operation == v1beta1.Delete {
|
2022-01-03 23:43:38 +08:00
|
|
|
managed := true
|
|
|
|
for _, er := range engineResponses {
|
2022-03-30 15:04:30 +02:00
|
|
|
if er.Policy != nil && !engine.ManagedPodResource(er.Policy, er.PatchedResource) {
|
2022-01-03 23:43:38 +08:00
|
|
|
managed = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !managed {
|
|
|
|
v.prGenerator.Add(buildDeletionPrInfo(policyContext.OldResource))
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:51:35 -07:00
|
|
|
return true, ""
|
2020-11-09 11:26:12 -08:00
|
|
|
}
|
2020-12-08 23:04:16 -08:00
|
|
|
|
2021-04-16 13:51:35 -07:00
|
|
|
prInfos := policyreport.GeneratePRsFromEngineResponse(engineResponses, logger)
|
2021-07-09 18:01:46 -07:00
|
|
|
v.prGenerator.Add(prInfos...)
|
2021-04-16 13:51:35 -07:00
|
|
|
|
2021-07-23 21:46:50 +05:30
|
|
|
//registering the kyverno_admission_review_duration_seconds metric concurrently
|
2021-05-15 23:33:41 +05:30
|
|
|
admissionReviewLatencyDuration := int64(time.Since(time.Unix(admissionRequestTimestamp, 0)))
|
2021-07-23 21:46:50 +05:30
|
|
|
go registerAdmissionReviewDurationMetricValidate(promConfig, logger, string(request.Operation), engineResponses, admissionReviewLatencyDuration)
|
2021-05-15 23:33:41 +05:30
|
|
|
|
2021-07-23 21:46:50 +05:30
|
|
|
//registering the kyverno_admission_requests_total metric concurrently
|
|
|
|
go registerAdmissionRequestsMetricValidate(promConfig, logger, string(request.Operation), engineResponses)
|
2019-08-23 18:34:23 -07:00
|
|
|
return true, ""
|
2019-07-15 16:07:56 -07:00
|
|
|
}
|
2020-02-29 22:39:27 +05:30
|
|
|
|
2021-07-09 18:01:46 -07:00
|
|
|
func getResourceName(request *v1beta1.AdmissionRequest) string {
|
|
|
|
resourceName := request.Kind.Kind + "/" + request.Name
|
|
|
|
if request.Namespace != "" {
|
|
|
|
resourceName = request.Namespace + "/" + resourceName
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceName
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:04:19 -08:00
|
|
|
func buildDeletionPrInfo(oldR unstructured.Unstructured) policyreport.Info {
|
|
|
|
return policyreport.Info{
|
|
|
|
Namespace: oldR.GetNamespace(),
|
|
|
|
Results: []policyreport.EngineResponseResult{
|
|
|
|
{Resource: response.ResourceSpec{
|
|
|
|
Kind: oldR.GetKind(),
|
|
|
|
APIVersion: oldR.GetAPIVersion(),
|
|
|
|
Namespace: oldR.GetNamespace(),
|
|
|
|
Name: oldR.GetName(),
|
|
|
|
UID: string(oldR.GetUID()),
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|