2022-04-27 08:09:52 -07:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-12-09 14:45:11 +01:00
|
|
|
"context"
|
2022-04-27 08:09:52 -07:00
|
|
|
"fmt"
|
2022-05-01 16:02:49 -07:00
|
|
|
"reflect"
|
|
|
|
|
2022-04-27 08:09:52 -07:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2022-04-27 08:09:52 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
2022-12-07 16:08:37 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
2022-05-03 10:45:08 +02:00
|
|
|
apiutils "github.com/kyverno/kyverno/pkg/utils/api"
|
2022-04-27 08:09:52 -07:00
|
|
|
"github.com/pkg/errors"
|
2022-05-17 07:56:48 +02:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2022-04-27 08:09:52 -07:00
|
|
|
)
|
|
|
|
|
2022-12-09 14:45:11 +01:00
|
|
|
func processImageValidationRule(ctx context.Context, log logr.Logger, rclient registryclient.Client, enginectx *PolicyContext, rule *kyvernov1.Rule) *response.RuleResponse {
|
|
|
|
if isDeleteRequest(enginectx) {
|
2022-05-05 14:06:18 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log = log.WithValues("rule", rule.Name)
|
2022-12-09 14:45:11 +01:00
|
|
|
matchingImages, _, err := extractMatchingImages(enginectx, rule)
|
2022-11-18 13:57:34 +05:30
|
|
|
if err != nil {
|
2022-12-09 22:15:23 +05:30
|
|
|
return ruleResponse(*rule, response.Validation, err.Error(), response.RuleStatusError)
|
2022-11-18 13:57:34 +05:30
|
|
|
}
|
|
|
|
if len(matchingImages) == 0 {
|
2022-12-09 22:15:23 +05:30
|
|
|
return ruleResponse(*rule, response.Validation, "image verified", response.RuleStatusSkip)
|
2022-11-18 13:57:34 +05:30
|
|
|
}
|
2022-12-09 14:45:11 +01:00
|
|
|
if err := LoadContext(ctx, log, rclient, rule.Context, enginectx, rule.Name); err != nil {
|
2022-04-27 08:09:52 -07:00
|
|
|
if _, ok := err.(gojmespath.NotFoundError); ok {
|
|
|
|
log.V(3).Info("failed to load context", "reason", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Error(err, "failed to load context")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ruleError(rule, response.Validation, "failed to load context", err)
|
|
|
|
}
|
|
|
|
|
2022-12-09 14:45:11 +01:00
|
|
|
preconditionsPassed, err := checkPreconditions(log, enginectx, rule.RawAnyAllConditions)
|
2022-04-27 08:09:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return ruleError(rule, response.Validation, "failed to evaluate preconditions", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !preconditionsPassed {
|
2022-12-09 14:45:11 +01:00
|
|
|
if enginectx.policy.GetSpec().ValidationFailureAction.Audit() {
|
2022-04-27 08:09:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 22:15:23 +05:30
|
|
|
return ruleResponse(*rule, response.Validation, "preconditions not met", response.RuleStatusSkip)
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range rule.VerifyImages {
|
|
|
|
imageVerify := v.Convert()
|
2022-12-09 14:45:11 +01:00
|
|
|
for _, infoMap := range enginectx.jsonContext.ImageInfo() {
|
2022-05-05 14:06:18 -07:00
|
|
|
for name, imageInfo := range infoMap {
|
2022-04-27 08:09:52 -07:00
|
|
|
image := imageInfo.String()
|
2022-05-05 14:06:18 -07:00
|
|
|
log = log.WithValues("rule", rule.Name)
|
|
|
|
|
2022-04-27 08:09:52 -07:00
|
|
|
if !imageMatches(image, imageVerify.ImageReferences) {
|
2022-05-05 14:06:18 -07:00
|
|
|
log.V(4).Info("image does not match", "imageReferences", imageVerify.ImageReferences)
|
2022-04-27 08:09:52 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
log.V(4).Info("validating image", "image", image)
|
2022-12-09 14:45:11 +01:00
|
|
|
if err := validateImage(enginectx, imageVerify, name, imageInfo, log); err != nil {
|
2022-12-09 22:15:23 +05:30
|
|
|
return ruleResponse(*rule, response.ImageVerify, err.Error(), response.RuleStatusFail)
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
log.V(4).Info("validated image", "rule", rule.Name)
|
2022-12-09 22:15:23 +05:30
|
|
|
return ruleResponse(*rule, response.Validation, "image verified", response.RuleStatusPass)
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func validateImage(ctx *PolicyContext, imageVerify *kyvernov1.ImageVerification, name string, imageInfo apiutils.ImageInfo, log logr.Logger) error {
|
2022-04-27 08:09:52 -07:00
|
|
|
image := imageInfo.String()
|
|
|
|
if imageVerify.VerifyDigest && imageInfo.Digest == "" {
|
2022-08-18 18:54:59 +05:30
|
|
|
log.V(2).Info("missing digest", "image", imageInfo.String())
|
2022-04-27 08:09:52 -07:00
|
|
|
return fmt.Errorf("missing digest for %s", image)
|
|
|
|
}
|
|
|
|
|
2022-12-02 09:14:23 +01:00
|
|
|
if imageVerify.Required && !reflect.DeepEqual(ctx.newResource, unstructured.Unstructured{}) {
|
|
|
|
verified, err := isImageVerified(ctx.newResource, image, log)
|
2022-04-27 08:09:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verified {
|
|
|
|
return fmt.Errorf("unverified image %s", image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
func isImageVerified(resource unstructured.Unstructured, image string, log logr.Logger) (bool, error) {
|
|
|
|
if reflect.DeepEqual(resource, unstructured.Unstructured{}) {
|
|
|
|
return false, errors.Errorf("nil resource")
|
2022-05-01 16:02:49 -07:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
annotations := resource.GetAnnotations()
|
2022-05-01 16:02:49 -07:00
|
|
|
if len(annotations) == 0 {
|
|
|
|
return false, nil
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
key := imageVerifyAnnotationKey
|
2022-05-01 16:02:49 -07:00
|
|
|
data, ok := annotations[key]
|
2022-04-27 08:09:52 -07:00
|
|
|
if !ok {
|
2022-05-05 14:06:18 -07:00
|
|
|
log.V(2).Info("missing image metadata in annotation", "key", key)
|
2022-05-01 16:02:49 -07:00
|
|
|
return false, errors.Errorf("image is not verified")
|
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
ivm, err := parseImageMetadata(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err, "failed to parse image verification metadata", "data", data)
|
|
|
|
return false, errors.Wrapf(err, "failed to parse image metadata")
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|
|
|
|
|
2022-05-05 14:06:18 -07:00
|
|
|
return ivm.isVerified(image), nil
|
2022-04-27 08:09:52 -07:00
|
|
|
}
|