1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 10:55:05 +00:00

precondition failure will skip rule independent of audit or enforce mode (#4163)

* precondition fails will skip rule independent of audit or enforce mode

Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>

* Added cli-test cases

Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>

* small fix

Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>

Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
This commit is contained in:
vivek kumar sahu 2022-07-14 09:35:27 +05:30 committed by GitHub
parent e71493e5cc
commit f6c131cfcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 2 deletions

View file

@ -215,7 +215,7 @@ func (v *validator) validate() *response.RuleResponse {
return ruleError(v.rule, response.Validation, "failed to evaluate preconditions", err)
}
if !preconditionsPassed && (v.ctx.Policy.GetSpec().ValidationFailureAction != kyvernov1.Audit || store.GetMock()) {
if !preconditionsPassed {
return ruleResponse(*v.rule, response.Validation, "preconditions not met", response.RuleStatusSkip, nil)
}
@ -256,7 +256,7 @@ func (v *validator) validateForEach() *response.RuleResponse {
preconditionsPassed, err := checkPreconditions(v.log, v.ctx, v.anyAllConditions)
if err != nil {
return ruleError(v.rule, response.Validation, "failed to evaluate preconditions", err)
} else if !preconditionsPassed && (v.ctx.Policy.GetSpec().ValidationFailureAction != kyvernov1.Audit || store.GetMock()) {
} else if !preconditionsPassed {
return ruleResponse(*v.rule, response.Validation, "preconditions not met", response.RuleStatusSkip, nil)
}

View file

@ -0,0 +1,17 @@
name: disallow-naked-pods
policies:
- policy.yaml
resources:
- resource.yaml
variables: values.yaml
results:
- policy: disallow-naked-pods
rule: validate-naked-pods
resource: blank-skip
kind: Pod
result: skip
- policy: disallow-naked-pods
rule: validate-naked-pods
resource: blank-fail
kind: Pod
result: fail

View file

@ -0,0 +1,32 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-naked-pods
spec:
validationFailureAction: audit
background: false
rules:
- name: validate-naked-pods
match:
any:
- resources:
kinds:
- Pod
context:
- name: ignorepolicy
apiCall:
urlPath: "/api/v1/namespaces/{{request.namespace}}"
jmesPath: "metadata.annotations.\"policies.example.ignore-policy/disallow-naked-pods\" || ''"
preconditions:
all:
- key: "{{ignorepolicy}}"
operator: NotEquals
value: "ignore"
validate:
message: "naked pods are not allowed"
deny:
conditions:
any:
- key: ownerReferences
operator: AnyNotIn
value: "{{request.object.metadata.keys(@)}}"

View file

@ -0,0 +1,27 @@
apiVersion: v1
kind: Pod
metadata:
name: blank-skip
spec:
hostIPC: true
containers:
- name: busybox
image: busyboxasdfasdf:1.28
args:
- sleep
- "9999"
securityContext:
runAsUser: 12345
---
apiVersion: v1
kind: Pod
metadata:
name: blank-fail
labels:
foo: bar
spec:
hostIPC: true
containers:
- name: busybox
image: nginx

View file

@ -0,0 +1,14 @@
policies:
- name: disallow-naked-pods
resources:
- name: blank-skip
# It doesn't satifies the precondition. Therefore can not proceed
# further for validation.
values:
ignorepolicy: "ignore"
- name: blank-fail
# It satisfies the precondition. Therefore can proceed
# further for validation against policy.
values:
ignorepolicy: "allowit"