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>
191 lines
5.5 KiB
Go
191 lines
5.5 KiB
Go
package context
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/distribution/distribution/reference"
|
|
"github.com/go-logr/logr"
|
|
"github.com/pkg/errors"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
type ImageInfo struct {
|
|
|
|
// Registry is the URL address of the image registry e.g. `docker.io`
|
|
Registry string `json:"registry,omitempty"`
|
|
|
|
// Name is the image name portion e.g. `busybox`
|
|
Name string `json:"name"`
|
|
|
|
// Path is the repository path and image name e.g. `some-repository/busybox`
|
|
Path string `json:"path"`
|
|
|
|
// Tag is the image tag e.g. `v2`
|
|
Tag string `json:"tag,omitempty"`
|
|
|
|
// Digest is the image digest portion e.g. `sha256:128c6e3534b842a2eec139999b8ce8aa9a2af9907e2b9269550809d18cd832a3`
|
|
Digest string `json:"digest,omitempty"`
|
|
|
|
// JSONPath is full JSON path to this image e.g. `/spec/containers/0/image`
|
|
JSONPath string `json:"jsonPath,omitempty"`
|
|
}
|
|
|
|
func (i *ImageInfo) String() string {
|
|
image := i.Registry + "/" + i.Path + ":" + i.Tag
|
|
if i.Digest != "" {
|
|
image = image + "@" + i.Digest
|
|
}
|
|
|
|
return image
|
|
}
|
|
|
|
type ContainerImage struct {
|
|
Name string
|
|
Image *ImageInfo
|
|
}
|
|
|
|
type Images struct {
|
|
InitContainers map[string]*ImageInfo `json:"initContainers,omitempty"`
|
|
Containers map[string]*ImageInfo `json:"containers"`
|
|
}
|
|
|
|
func newImages(initContainersImgs, containersImgs []*ContainerImage) *Images {
|
|
initContainers := make(map[string]*ImageInfo)
|
|
for _, resource := range initContainersImgs {
|
|
initContainers[resource.Name] = resource.Image
|
|
}
|
|
|
|
containers := make(map[string]*ImageInfo)
|
|
for _, resource := range containersImgs {
|
|
containers[resource.Name] = resource.Image
|
|
}
|
|
|
|
return &Images{
|
|
InitContainers: initContainers,
|
|
Containers: containers,
|
|
}
|
|
}
|
|
|
|
func extractImageInfo(resource *unstructured.Unstructured, log logr.Logger) (initContainersImgs, containersImgs []*ContainerImage) {
|
|
logger := log.WithName("extractImageInfo").WithValues("kind", resource.GetKind(), "ns", resource.GetNamespace(), "name", resource.GetName())
|
|
|
|
for _, tag := range []string{"initContainers", "containers"} {
|
|
switch resource.GetKind() {
|
|
case "Pod":
|
|
if containers, ok, _ := unstructured.NestedSlice(resource.UnstructuredContent(), "spec", tag); ok {
|
|
if tag == "initContainers" {
|
|
initContainersImgs = extractImageInfos(containers, initContainersImgs, "/spec/initContainers", logger)
|
|
} else {
|
|
containersImgs = extractImageInfos(containers, containersImgs, "/spec/containers", logger)
|
|
}
|
|
}
|
|
|
|
case "CronJob":
|
|
if containers, ok, _ := unstructured.NestedSlice(resource.UnstructuredContent(), "spec", "jobTemplate", "spec", "template", "spec", tag); ok {
|
|
if tag == "initContainers" {
|
|
initContainersImgs = extractImageInfos(containers, initContainersImgs, "/spec/jobTemplate/spec/template/spec/initContainers", logger)
|
|
} else {
|
|
containersImgs = extractImageInfos(containers, containersImgs, "/spec/jobTemplate/spec/template/spec/containers", logger)
|
|
}
|
|
}
|
|
|
|
// handles "Deployment", "DaemonSet", "Job", "StatefulSet", and custom controllers with the same pattern
|
|
default:
|
|
if containers, ok, _ := unstructured.NestedSlice(resource.UnstructuredContent(), "spec", "template", "spec", tag); ok {
|
|
if tag == "initContainers" {
|
|
initContainersImgs = extractImageInfos(containers, initContainersImgs, "/spec/template/spec/initContainers", logger)
|
|
} else {
|
|
containersImgs = extractImageInfos(containers, containersImgs, "/spec/template/spec/containers", logger)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func extractImageInfos(containers []interface{}, images []*ContainerImage, jsonPath string, log logr.Logger) []*ContainerImage {
|
|
img, err := convertToImageInfo(containers, jsonPath)
|
|
if err != nil {
|
|
log.Error(err, "failed to extract image info", "element", containers)
|
|
}
|
|
|
|
return append(images, img...)
|
|
}
|
|
|
|
func convertToImageInfo(containers []interface{}, jsonPath string) (images []*ContainerImage, err error) {
|
|
var errs []string
|
|
var index = 0
|
|
for _, ctr := range containers {
|
|
if container, ok := ctr.(map[string]interface{}); ok {
|
|
name := container["name"].(string)
|
|
image := container["image"].(string)
|
|
jp := strings.Join([]string{jsonPath, strconv.Itoa(index), "image"}, "/")
|
|
imageInfo, err := newImageInfo(image, jp)
|
|
if err != nil {
|
|
errs = append(errs, err.Error())
|
|
continue
|
|
}
|
|
|
|
images = append(images, &ContainerImage{
|
|
Name: name,
|
|
Image: imageInfo,
|
|
})
|
|
}
|
|
|
|
index++
|
|
}
|
|
|
|
if len(errs) == 0 {
|
|
return images, nil
|
|
}
|
|
|
|
return images, errors.Errorf("%s", strings.Join(errs, ";"))
|
|
}
|
|
|
|
func newImageInfo(image, jsonPath string) (*ImageInfo, error) {
|
|
image = addDefaultDomain(image)
|
|
ref, err := reference.Parse(image)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "bad image: %s", image)
|
|
}
|
|
|
|
var registry, path, name, tag, digest string
|
|
if named, ok := ref.(reference.Named); ok {
|
|
registry = reference.Domain(named)
|
|
path = reference.Path(named)
|
|
name = path[strings.LastIndex(path, "/")+1:]
|
|
}
|
|
|
|
if tagged, ok := ref.(reference.Tagged); ok {
|
|
tag = tagged.Tag()
|
|
}
|
|
|
|
if digested, ok := ref.(reference.Digested); ok {
|
|
digest = digested.Digest().String()
|
|
}
|
|
|
|
// set default tag - the domain is set via addDefaultDomain before parsing
|
|
if tag == "" {
|
|
tag = "latest"
|
|
}
|
|
|
|
return &ImageInfo{
|
|
Registry: registry,
|
|
Name: name,
|
|
Path: path,
|
|
Tag: tag,
|
|
Digest: digest,
|
|
JSONPath: jsonPath,
|
|
}, nil
|
|
}
|
|
|
|
func addDefaultDomain(name string) string {
|
|
i := strings.IndexRune(name, '/')
|
|
if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost" && strings.ToLower(name[:i]) == name[:i]) {
|
|
return "docker.io/" + name
|
|
}
|
|
|
|
return name
|
|
}
|