mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-06 16:06:56 +00:00
* add image verification * inline policy list Signed-off-by: Jim Bugwadia <jim@nirmata.com> * cosign version and dependencies updates Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add registry initialization Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate deep copy and other fixtures Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix deep copy issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * mutate images to add digest Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add certificates to Kyverno container for HTTPS lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align flag syntax Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update docs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * patch image with digest and fix checks Signed-off-by: Jim Bugwadia <jim@nirmata.com> * hardcode image for demos Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add default registry (docker.io) before calling reference.Parse Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix definition Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase webhook timeout Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix args Signed-off-by: Jim Bugwadia <jim@nirmata.com> * run gofmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * rename for clarity Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix HasImageVerify check Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle API conflict and retry Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix reviewdog issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix make for unit tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * improve error message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix durations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle errors in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * print policy name Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add retries and duration to error log Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix time check in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * round creation times in test Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix retry loop Signed-off-by: Jim Bugwadia <jim@nirmata.com> * remove timing check for policy creation Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix e2e error - policy not found Signed-off-by: Shuting Zhao <shutting06@gmail.com> * update string comparison method Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix test Generate_Namespace_Label_Actions Signed-off-by: Shuting Zhao <shutting06@gmail.com> * add debug info for e2e tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix generate bug Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add check for update operations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase time for deleteing a resource Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix check Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Shuting Zhao <shutting06@gmail.com>
116 lines
3.8 KiB
Go
116 lines
3.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/go-logr/logr"
|
|
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
"github.com/kyverno/kyverno/pkg/cosign"
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
"github.com/kyverno/kyverno/pkg/engine/utils"
|
|
"github.com/minio/minio/pkg/wildcard"
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
|
"time"
|
|
)
|
|
|
|
func VerifyAndPatchImages(policyContext *PolicyContext) (resp *response.EngineResponse) {
|
|
resp = &response.EngineResponse{}
|
|
images := policyContext.JSONContext.ImageInfo()
|
|
if images == nil {
|
|
return
|
|
}
|
|
|
|
policy := policyContext.Policy
|
|
patchedResource := policyContext.NewResource
|
|
logger := log.Log.WithName("EngineVerifyImages").WithValues("policy", policy.Name,
|
|
"kind", patchedResource.GetKind(), "namespace", patchedResource.GetNamespace(), "name", patchedResource.GetName())
|
|
|
|
if ManagedPodResource(policy, patchedResource) {
|
|
logger.V(4).Info("container images for pods managed by workload controllers are already verified", "policy", policy.GetName())
|
|
resp.PatchedResource = patchedResource
|
|
return
|
|
}
|
|
|
|
startTime := time.Now()
|
|
defer func() {
|
|
buildResponse(logger, policyContext, resp, startTime)
|
|
logger.V(4).Info("finished policy processing", "processingTime", resp.PolicyResponse.ProcessingTime.String(), "rulesApplied", resp.PolicyResponse.RulesAppliedCount)
|
|
}()
|
|
|
|
policyContext.JSONContext.Checkpoint()
|
|
defer policyContext.JSONContext.Restore()
|
|
|
|
for i := range policyContext.Policy.Spec.Rules {
|
|
rule := policyContext.Policy.Spec.Rules[i]
|
|
if len(rule.VerifyImages) == 0 {
|
|
continue
|
|
}
|
|
|
|
if !matches(logger, rule, policyContext) {
|
|
continue
|
|
}
|
|
|
|
policyContext.JSONContext.Restore()
|
|
for _, imageVerify := range rule.VerifyImages {
|
|
verifyAndPatchImages(logger, &rule, imageVerify, images.Containers, resp)
|
|
verifyAndPatchImages(logger, &rule, imageVerify, images.InitContainers, resp)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func verifyAndPatchImages(logger logr.Logger, rule *v1.Rule, imageVerify *v1.ImageVerification, images map[string]*context.ImageInfo, resp *response.EngineResponse) {
|
|
imagePattern := imageVerify.Image
|
|
key := imageVerify.Key
|
|
|
|
for _, imageInfo := range images {
|
|
image := imageInfo.String()
|
|
if !wildcard.Match(imagePattern, image) {
|
|
logger.V(4).Info("image does not match pattern", "image", image, "pattern", imagePattern)
|
|
continue
|
|
}
|
|
|
|
logger.Info("verifying image", "image", image)
|
|
incrementAppliedCount(resp)
|
|
|
|
ruleResp := response.RuleResponse{
|
|
Name: rule.Name,
|
|
Type: utils.Validation.String(),
|
|
}
|
|
|
|
start := time.Now()
|
|
digest, err := cosign.Verify(image, []byte(key), logger)
|
|
if err != nil {
|
|
logger.Info("failed to verify image", "image", image, "key", key, "error", err, "duration", time.Since(start).Seconds())
|
|
ruleResp.Success = false
|
|
ruleResp.Message = fmt.Sprintf("image verification failed for %s: %v", image, err)
|
|
} else {
|
|
logger.V(3).Info("verified image", "image", image, "digest", digest, "duration", time.Since(start).Seconds())
|
|
ruleResp.Success = true
|
|
ruleResp.Message = fmt.Sprintf("image %s verified", image)
|
|
|
|
// add digest to image
|
|
if imageInfo.Digest == "" {
|
|
patch, err := makeAddDigestPatch(imageInfo, digest)
|
|
if err != nil {
|
|
logger.Error(err, "failed to patch image with digest", "image", imageInfo.String(), "jsonPath", imageInfo.JSONPath)
|
|
} else {
|
|
logger.V(4).Info("patching verified image with digest", "patch", string(patch))
|
|
ruleResp.Patches = [][]byte{patch}
|
|
}
|
|
}
|
|
}
|
|
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, ruleResp)
|
|
}
|
|
}
|
|
|
|
func makeAddDigestPatch(imageInfo *context.ImageInfo, digest string) ([]byte, error) {
|
|
var patch = make(map[string]interface{})
|
|
patch["op"] = "replace"
|
|
patch["path"] = imageInfo.JSONPath
|
|
patch["value"] = imageInfo.String() + "@" + digest
|
|
return json.Marshal(patch)
|
|
}
|