2023-03-30 11:51:16 +02:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
2023-04-03 06:57:48 +02:00
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
2023-03-30 11:51:16 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
|
|
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/handlers"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
|
|
|
"github.com/kyverno/kyverno/pkg/pss"
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
type validatePssHandler struct{}
|
|
|
|
|
|
|
|
func NewValidatePssHandler() handlers.Handler {
|
|
|
|
return validatePssHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h validatePssHandler) Process(
|
|
|
|
ctx context.Context,
|
|
|
|
logger logr.Logger,
|
|
|
|
policyContext engineapi.PolicyContext,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
rule kyvernov1.Rule,
|
2023-04-03 06:57:48 +02:00
|
|
|
contextLoader engineapi.EngineContextLoader,
|
2023-03-30 11:51:16 +02:00
|
|
|
) (unstructured.Unstructured, []engineapi.RuleResponse) {
|
2023-04-03 06:57:48 +02:00
|
|
|
// load context
|
|
|
|
if err := contextLoader(ctx, rule.Context, policyContext.JSONContext()); err != nil {
|
|
|
|
if _, ok := err.(gojmespath.NotFoundError); ok {
|
|
|
|
logger.V(3).Info("failed to load context", "reason", err.Error())
|
|
|
|
} else {
|
|
|
|
logger.Error(err, "failed to load context")
|
|
|
|
}
|
|
|
|
return resource, handlers.RuleResponses(internal.RuleError(rule, engineapi.Validation, "failed to load context", err))
|
|
|
|
}
|
|
|
|
// check preconditions
|
|
|
|
preconditionsPassed, err := internal.CheckPreconditions(logger, policyContext.JSONContext(), rule.GetAnyAllConditions())
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.RuleResponses(internal.RuleError(rule, engineapi.Validation, "failed to evaluate preconditions", err))
|
|
|
|
}
|
|
|
|
if !preconditionsPassed {
|
|
|
|
return resource, handlers.RuleResponses(internal.RuleSkip(rule, engineapi.Validation, "preconditions not met"))
|
|
|
|
}
|
2023-03-30 11:51:16 +02:00
|
|
|
// Marshal pod metadata and spec
|
2023-04-03 06:57:48 +02:00
|
|
|
podSecurity := rule.Validation.PodSecurity
|
2023-03-30 11:51:16 +02:00
|
|
|
podSpec, metadata, err := getSpec(resource)
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.RuleResponses(internal.RuleError(rule, engineapi.Validation, "Error while getting new resource", err))
|
|
|
|
}
|
|
|
|
pod := &corev1.Pod{
|
|
|
|
Spec: *podSpec,
|
|
|
|
ObjectMeta: *metadata,
|
|
|
|
}
|
|
|
|
allowed, pssChecks, err := pss.EvaluatePod(podSecurity, pod)
|
|
|
|
if err != nil {
|
|
|
|
return resource, handlers.RuleResponses(internal.RuleError(rule, engineapi.Validation, "failed to parse pod security api version", err))
|
|
|
|
}
|
|
|
|
podSecurityChecks := &engineapi.PodSecurityChecks{
|
|
|
|
Level: podSecurity.Level,
|
|
|
|
Version: podSecurity.Version,
|
|
|
|
Checks: pssChecks,
|
|
|
|
}
|
|
|
|
if allowed {
|
|
|
|
msg := fmt.Sprintf("Validation rule '%s' passed.", rule.Name)
|
|
|
|
rspn := internal.RulePass(rule, engineapi.Validation, msg)
|
|
|
|
rspn.PodSecurityChecks = podSecurityChecks
|
|
|
|
return resource, handlers.RuleResponses(rspn)
|
|
|
|
} else {
|
|
|
|
msg := fmt.Sprintf(`Validation rule '%s' failed. It violates PodSecurity "%s:%s": %s`, rule.Name, podSecurity.Level, podSecurity.Version, pss.FormatChecksPrint(pssChecks))
|
|
|
|
rspn := internal.RuleResponse(rule, engineapi.Validation, msg, engineapi.RuleStatusFail)
|
|
|
|
rspn.PodSecurityChecks = podSecurityChecks
|
|
|
|
return resource, handlers.RuleResponses(rspn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSpec(resource unstructured.Unstructured) (podSpec *corev1.PodSpec, metadata *metav1.ObjectMeta, err error) {
|
|
|
|
kind := resource.GetKind()
|
|
|
|
|
|
|
|
if kind == "DaemonSet" || kind == "Deployment" || kind == "Job" || kind == "StatefulSet" || kind == "ReplicaSet" || kind == "ReplicationController" {
|
|
|
|
var deployment appsv1.Deployment
|
|
|
|
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(resourceBytes, &deployment)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
podSpec = &deployment.Spec.Template.Spec
|
|
|
|
metadata = &deployment.Spec.Template.ObjectMeta
|
|
|
|
return podSpec, metadata, nil
|
|
|
|
} else if kind == "CronJob" {
|
|
|
|
var cronJob batchv1.CronJob
|
|
|
|
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(resourceBytes, &cronJob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
podSpec = &cronJob.Spec.JobTemplate.Spec.Template.Spec
|
|
|
|
metadata = &cronJob.Spec.JobTemplate.ObjectMeta
|
|
|
|
} else if kind == "Pod" {
|
|
|
|
var pod corev1.Pod
|
|
|
|
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(resourceBytes, &pod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
podSpec = &pod.Spec
|
|
|
|
metadata = &pod.ObjectMeta
|
|
|
|
return podSpec, metadata, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return podSpec, metadata, err
|
|
|
|
}
|