mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
ab5171cee5
* add verifyDigest to check all tags are converted to digests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add required to check for image verification annotation Signed-off-by: Jim Bugwadia <jim@nirmata.com> * make fmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate CRD Signed-off-by: Jim Bugwadia <jim@nirmata.com> * adding imageverify true/false patch Signed-off-by: anushkamittal20 <anumittal4641@gmail.com> * patch addition logic Signed-off-by: anushkamittal20 <anumittal4641@gmail.com> * image verify CLI tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fixes and unit tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix digest mutate Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * make codegen Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix policy cache Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: anushkamittal20 <anumittal4641@gmail.com>
91 lines
2.7 KiB
Go
91 lines
2.7 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-logr/logr"
|
|
gojmespath "github.com/jmespath/go-jmespath"
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func processImageValidationRule(log logr.Logger, ctx *PolicyContext, rule *kyverno.Rule) *response.RuleResponse {
|
|
if err := LoadContext(log, rule.Context, ctx, rule.Name); err != nil {
|
|
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)
|
|
}
|
|
|
|
preconditionsPassed, err := checkPreconditions(log, ctx, rule.RawAnyAllConditions)
|
|
if err != nil {
|
|
return ruleError(rule, response.Validation, "failed to evaluate preconditions", err)
|
|
}
|
|
|
|
if !preconditionsPassed {
|
|
if ctx.Policy.GetSpec().ValidationFailureAction == kyverno.Audit {
|
|
return nil
|
|
}
|
|
|
|
return ruleResponse(*rule, response.Validation, "preconditions not met", response.RuleStatusSkip, nil)
|
|
}
|
|
|
|
for _, v := range rule.VerifyImages {
|
|
imageVerify := v.Convert()
|
|
for _, infoMap := range ctx.JSONContext.ImageInfo() {
|
|
for _, imageInfo := range infoMap {
|
|
image := imageInfo.String()
|
|
if !imageMatches(image, imageVerify.ImageReferences) {
|
|
log.V(4).Info("image does not match pattern", "image", image, "patterns", imageVerify.ImageReferences)
|
|
return nil
|
|
}
|
|
|
|
if err := validateImage(ctx, imageVerify, imageInfo); err != nil {
|
|
return ruleResponse(*rule, response.ImageVerify, err.Error(), response.RuleStatusFail, nil)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return ruleResponse(*rule, response.Validation, "image verified", response.RuleStatusPass, nil)
|
|
}
|
|
|
|
func validateImage(ctx *PolicyContext, imageVerify *kyverno.ImageVerification, imageInfo kubeutils.ImageInfo) error {
|
|
image := imageInfo.String()
|
|
if imageVerify.VerifyDigest && imageInfo.Digest == "" {
|
|
return fmt.Errorf("missing digest for %s", image)
|
|
}
|
|
|
|
if imageVerify.Required {
|
|
verified, err := isImageVerified(ctx, imageInfo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !verified {
|
|
return fmt.Errorf("unverified image %s", image)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func isImageVerified(ctx *PolicyContext, imageInfo kubeutils.ImageInfo) (bool, error) {
|
|
key := makeAnnotationKeyForJMESPath(imageInfo.Name, imageInfo.Digest)
|
|
data, err := ctx.JSONContext.Query(key)
|
|
if err != nil {
|
|
return false, errors.Wrapf(err, "failed to query annotation for %s", key)
|
|
}
|
|
|
|
result, ok := data.(string)
|
|
if !ok {
|
|
return false, errors.Wrapf(err, "failed to convert data %s", key)
|
|
}
|
|
|
|
return result == "true", nil
|
|
}
|