8000 feat: support image extraction from old object (cherry-pick #13026) by gcp-cherry-pick-bot[bot] · Pull Request #13030 · kyverno/kyverno · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: support image extraction from old object (cherry-pick #13026) #13030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions pkg/cel/compiler/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ import (
var (
podImageExtractors = []v1alpha1.ImageExtractor{{
Name: "containers",
Expression: "has(object.spec.containers) ? object.spec.containers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.?containers.orValue([]).map(e, e.image)",
}, {
Name: "initContainers",
Expression: "has(object.spec.initContainers) ? object.spec.initContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.?initContainers.orValue([]).map(e, e.image)",
}, {
Name: "ephemeralContainers",
Expression: "has(object.spec.ephemeralContainers) ? object.spec.ephemeralContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.?ephemeralContainers.orValue([]).map(e, e.image)",
}}
podControllerImageExtractors = []v1alpha1.ImageExtractor{{
Name: "containers",
Expression: "has(object.spec.template.spec.containers) ? object.spec.template.spec.containers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.template.spec.?containers.orValue([]).map(e, e.image)",
}, {
Name: "initContainers",
Expression: "has(object.spec.template.spec.initContainers) ? object.spec.template.spec.initContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.template.spec.?initContainers.orValue([]).map(e, e.image)",
}, {
Name: "ephemeralContainers",
Expression: "has(object.spec.template.spec.ephemeralContainers) ? object.spec.template.spec.ephemeralContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.template.spec.?ephemeralContainers.orValue([]).map(e, e.image)",
}}
cronJobImageExtractors = []v1alpha1.ImageExtractor{{
Name: "containers",
Expression: "has(object.spec.jobTemplate.spec.template.spec.containers) ? object.spec.jobTemplate.spec.template.spec.containers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.jobTemplate.spec.template.spec.?containers.orValue([]).map(e, e.image)",
}, {
Name: "initContainers",
Expression: "has(object.spec.jobTemplate.spec.template.spec.initContainers) ? object.spec.jobTemplate.spec.template.spec.initContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.jobTemplate.spec.template.spec.?initContainers.orValue([]).map(e, e.image)",
}, {
Name: "ephemeralContainers",
Expression: "has(object.spec.jobTemplate.spec.template.spec.ephemeralContainers) ? object.spec.jobTemplate.spec.template.spec.ephemeralContainers.map(e, e.image) : []",
Expression: "(object != null ? object : oldObject).spec.jobTemplate.spec.template.spec.?ephemeralContainers.orValue([]).map(e, e.image)",
}}
)

Expand Down Expand Up @@ -79,7 +79,7 @@ func (c *ImageExtractor) GetImages(data map[string]any) ([]string, error) {
return result, nil
}

func CompileImageExtractors(path *field.Path, envOpts []cel.EnvOption, gvr *metav1.GroupVersionResource, imageExtractors ...v1alpha1.ImageExtractor) (map[string]ImageExtractor, field.ErrorList) {
func CompileImageExtractors(path *field.Path, env *cel.Env, gvr *metav1.GroupVersionResource, imageExtractors ...v1alpha1.ImageExtractor) (map[string]ImageExtractor, field.ErrorList) {
var extractors []v1alpha1.ImageExtractor
if gvr != nil {
extractors = append(extractors, getImageExtractorsFromGVR(*gvr)...)
Expand All @@ -90,10 +90,6 @@ func CompileImageExtractors(path *field.Path, envOpts []cel.EnvOption, gvr *meta
}
var allErrs field.ErrorList
compiled := make(map[string]ImageExtractor, len(extractors))
env, err := cel.NewEnv(envOpts...)
if err != nil {
return nil, append(allErrs, field.InternalError(path, err))
}
for i, m := range extractors {
path := path.Index(i).Child("expression")
ast, iss := env.Compile(m.Expression)
Expand Down
226 changes: 134 additions & 92 deletions pkg/cel/compiler/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,123 +21,165 @@ func Test_Match(t *testing.T) {
gvr *metav1.GroupVersionResource
wantResult map[string][]string
wantErr bool
}{
{
name: "standard",
imageExtractor: []v1alpha1.ImageExtractor{
{
Name: "one",
Expression: "request.images",
}{{
name: "standard",
imageExtractor: []v1alpha1.ImageExtractor{{
Name: "one",
Expression: "request.images",
}},
request: map[string]any{
"request": map[string][]string{
"images": {
"nginx:latest",
"alpine:latest",
},
},
request: map[string]any{
"request": map[string][]string{
"images": {
"nginx:latest",
"alpine:latest",
},
},
},
wantResult: map[string][]string{
"one": {
"nginx:latest",
"alpine:latest",
},
wantResult: map[string][]string{
"one": {
},
gvr: nil,
wantErr: false,
}, {
name: "pod image extraction",
imageExtractor: []v1alpha1.ImageExtractor{{
Name: "one",
Expression: "request.images",
}},
request: map[string]any{
"request": map[string]any{
"images": []string{
"nginx:latest",
"alpine:latest",
},
},
gvr: nil,
wantErr: false,
},
{
name: "pod image extraction",
imageExtractor: []v1alpha1.ImageExtractor{
{
Name: "one",
Expression: "request.images",
"object": map[string]any{
"spec": map[string]any{
"containers": []map[string]string{{
"image": "kyverno/image-one",
}, {
"image": "kyverno/image-two",
}},
"initContainers": []map[string]string{{
"image": "kyverno/init-image-one",
}, {
"image": "kyverno/init-image-two",
}},
"ephemeralContainers": []map[string]string{{
"image": "kyverno/ephr-image-one",
}, {
"image": "kyverno/ephr-image-two",
}},
},
},
request: map[string]any{
"request": map[string]any{
"images": []string{
"nginx:latest",
"alpine:latest",
},
},
"object": map[string]any{
"spec": map[string]any{
"containers": []map[string]string{
{
"image": "kyverno/image-one",
},
{
"image": "kyverno/image-two",
},
},
"initContainers": []map[string]string{
{
"image": "kyverno/init-image-one",
},
{
"image": "kyverno/init-image-two",
},
},
"ephemeralContainers": []map[string]string{
{
"image": "kyverno/ephr-image-one",
},
{
"image": "kyverno/ephr-image-two",
},
},
},
},
},
wantResult: map[string][]string{
"one": {
"nginx:latest",
"alpine:latest",
},
"containers": {
"kyverno/image-one",
"kyverno/image-two",
},
"initContainers": {
"kyverno/init-image-one",
"kyverno/init-image-two",
},
"ephemeralContainers": {
"kyverno/ephr-image-one",
"kyverno/ephr-image-two",
},
wantResult: map[string][]string{
"one": {
},
gvr: &metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
wantErr: false,
}, {
name: "pod image extraction (old object)",
imageExtractor: []v1alpha1.ImageExtractor{{
Name: "one",
Expression: "request.images",
}},
request: map[string]any{
"request": map[string]any{
"images": []string{
"nginx:latest",
"alpine:latest",
},
"containers": {
"kyverno/image-one",
"kyverno/image-two",
},
"initContainers": {
"kyverno/init-image-one",
"kyverno/init-image-two",
},
"ephemeralContainers": {
"kyverno/ephr-image-one",
"kyverno/ephr-image-two",
},
"object": nil,
"oldObject": map[string]any{
"spec": map[string]any{
"containers": []map[string]string{{
"image": "kyverno/image-one",
}, {
"image": "kyverno/image-two",
}},
"initContainers": []map[string]string{{
"image": "kyverno/init-image-one",
}, {
"image": "kyverno/init-image-two",
}},
"ephemeralContainers": []map[string]string{{
"image": "kyverno/ephr-image-one",
}, {
"image": "kyverno/ephr-image-two",
}},
},
},
gvr: &metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
wantErr: false,
},
{
name: "standard fail",
imageExtractor: []v1alpha1.ImageExtractor{
{
Name: "one",
Expression: "request.images",
},
wantResult: map[string][]string{
"one": {
"nginx:latest",
"alpine:latest",
},
request: map[string]any{
"request": map[string][]int{
"images": {0, 1},
},
"containers": {
"kyverno/image-one",
"kyverno/image-two",
},
"initContainers": {
"kyverno/init-image-one",
"kyverno/init-image-two",
},
"ephemeralContainers": {
"kyverno/ephr-image-one",
"kyverno/ephr-image-two",
},
gvr: nil,
wantErr: true,
},
}
gvr: &metav1.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
wantErr: false,
}, {
name: "standard fail",
imageExtractor: []v1alpha1.ImageExtractor{{
Name: "one",
Expression: "request.images",
}},
request: map[string]any{
"request": map[string][]int{
"images": {0, 1},
},
},
gvr: nil,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, errList := CompileImageExtractors(
base, err := NewBaseEnv()
assert.NoError(t, err)
env, err := base.Extend(
cel.Variable(RequestKey, types.DynType),
cel.Variable(ObjectKey, types.DynType),
cel.Variable(OldObjectKey, types.DynType),
)
c, errs := CompileImageExtractors(
field.NewPath("spec", "images"),
[]cel.EnvOption{cel.Variable(RequestKey, types.DynType), cel.Variable(ObjectKey, types.DynType)},
env,
tt.gvr,
tt.imageExtractor...,
)
assert.Nil(t, errList)
assert.Nil(t, errs)
images, err := ExtractImages(tt.request, c)
if tt.wantErr {
assert.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/imageverification/evaluator/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *compiler) Compile(ivpolicy *policiesv1alpha1.ImageValidatingPolicy, exc
return nil, append(allErrs, errs...)
}

imageExtractors, errs := engine.CompileImageExtractors(path.Child("images"), options, c.reqGVR, ivpolicy.Spec.ImageExtractors...)
imageExtractors, errs := engine.CompileImageExtractors(path.Child("images"), env, c.reqGVR, ivpolicy.Spec.ImageExtractors...)
if errs != nil {
return nil, append(allErrs, errs...)
}
Expand Down
Loading
0