1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00

fix: disable autogen in foreach mutation with json patches (#6996)

* fix: disable autogen in foreach mutation with json patches

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* kuttl

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-04-25 10:32:19 +02:00 committed by GitHub
parent 6d7f19688a
commit f2cd3791ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 0 deletions

View file

@ -73,6 +73,11 @@ func CanAutoGen(spec *kyvernov1.Spec) (applyAutoGen bool, controllers string) {
if rule.Mutation.PatchesJSON6902 != "" || rule.HasGenerate() { if rule.Mutation.PatchesJSON6902 != "" || rule.HasGenerate() {
return false, "none" return false, "none"
} }
for _, foreach := range rule.Mutation.ForEachMutation {
if foreach.PatchesJSON6902 != "" {
return false, "none"
}
}
match, exclude := rule.MatchResources, rule.ExcludeResources match, exclude := rule.MatchResources, rule.ExcludeResources
if !checkAutogenSupport(&needed, match.ResourceDescription, exclude.ResourceDescription) { if !checkAutogenSupport(&needed, match.ResourceDescription, exclude.ResourceDescription) {
debug.Info("skip generating rule on pod controllers: Name / Selector in resource description may not be applicable.", "rule", rule.Name) debug.Info("skip generating rule on pod controllers: Name / Selector in resource description may not be applicable.", "rule", rule.Name)

View file

@ -0,0 +1,8 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
apply:
- policy.yaml
assert:
- policy-assert.yaml
error:
- policy-error.yaml

View file

@ -0,0 +1,11 @@
## Description
This test creates a cluster policy with a mutation rule containing a foreach and json patch.
## Expected Behavior
No autogen rules should be present in the status as json patches are supposed to disable autogen.
## Reference Issue(s)
- https://github.com/kyverno/kyverno/issues/4731

View file

@ -0,0 +1,10 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: mutate-pod-require-non-root-user
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready
autogen: {}

View file

@ -0,0 +1,13 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: mutate-pod-require-non-root-user
status:
conditions:
- reason: Succeeded
status: "True"
type: Ready
autogen:
rules:
- name: autogen-require-non-root-user
- name: autogen-cronjob-require-non-root-user

View file

@ -0,0 +1,28 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: mutate-pod-require-non-root-user
spec:
schemaValidation: false
rules:
- name: require-non-root-user
match:
all:
- resources:
kinds:
- Pod
mutate:
foreach:
- list: request.object.spec.containers
preconditions:
all:
# skip images that are exempt (allowed to run as a root user);
# escape quotes where the replaced value may contain hyphens
- key: "{{images.containers.\"{{element.name}}\".path}}"
operator: AnyNotIn
value:
- myorg/exempt-image-name
patchesJson6902: |-
- path: /spec/containers/{{elementIndex}}/securityContext/runAsNonRoot
op: add
value: true