-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add celPreconditions in autogen rules (#11542)
Signed-off-by: Mariam Fahmy <[email protected]>
- Loading branch information
1 parent
827183e
commit ca3c2de
Showing
6 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/conformance/chainsaw/autogen/cel-preconditions/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
## Description | ||
|
||
The policy should contain autogen rules with cel preconditions correctly adjusted. | ||
|
||
## Expected Behavior | ||
|
||
The policy contains autogen rules with cel preconditions correctly adjusted. | ||
|
||
## Related Issue(s) | ||
|
||
- https://github.com/kyverno/kyverno/issues/11421 |
25 changes: 25 additions & 0 deletions
25
test/conformance/chainsaw/autogen/cel-preconditions/chainsaw-test.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json | ||
apiVersion: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: cel-preconditions | ||
spec: | ||
steps: | ||
- name: create policy | ||
use: | ||
template: ../../_step-templates/create-policy.yaml | ||
with: | ||
bindings: | ||
- name: file | ||
value: policy.yaml | ||
- name: wait policy ready | ||
use: | ||
template: ../../_step-templates/cluster-policy-ready.yaml | ||
with: | ||
bindings: | ||
- name: name | ||
value: disallow-privilege-escalation | ||
- name: check autogen | ||
try: | ||
- assert: | ||
file: check-autogen.yaml |
63 changes: 63 additions & 0 deletions
63
test/conformance/chainsaw/autogen/cel-preconditions/check-autogen.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: disallow-privilege-escalation | ||
status: | ||
autogen: | ||
rules: | ||
- celPreconditions: | ||
- expression: has(object.spec.template.metadata.labels) && has(object.spec.template.metadata.labels.prod) | ||
&& object.spec.template.metadata.labels.prod == 'true' | ||
name: Only for prod | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- DaemonSet | ||
- Deployment | ||
- Job | ||
- ReplicaSet | ||
- ReplicationController | ||
- StatefulSet | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: {} | ||
name: autogen-privilege-escalation | ||
skipBackgroundRequests: true | ||
validate: | ||
allowExistingViolations: true | ||
cel: | ||
expressions: | ||
- expression: object.spec.template.spec.containers.all(container, has(container.securityContext) | ||
&& has(container.securityContext.allowPrivilegeEscalation) && container.securityContext.allowPrivilegeEscalation | ||
== false) | ||
message: Privilege escalation is disallowed. The field spec.containers[*].securityContext.allowPrivilegeEscalation | ||
must be set to `false`. | ||
failureAction: Enforce | ||
- celPreconditions: | ||
- expression: has(object.spec.jobTemplate.spec.template.metadata.labels) && | ||
has(object.spec.jobTemplate.spec.template.metadata.labels.prod) && object.spec.jobTemplate.spec.template.metadata.labels.prod | ||
== 'true' | ||
name: Only for prod | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- CronJob | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
resources: {} | ||
name: autogen-cronjob-privilege-escalation | ||
skipBackgroundRequests: true | ||
validate: | ||
allowExistingViolations: true | ||
cel: | ||
expressions: | ||
- expression: object.spec.jobTemplate.spec.template.spec.containers.all(container, | ||
has(container.securityContext) && has(container.securityContext.allowPrivilegeEscalation) | ||
&& container.securityContext.allowPrivilegeEscalation == false) | ||
message: Privilege escalation is disallowed. The field spec.containers[*].securityContext.allowPrivilegeEscalation | ||
must be set to `false`. | ||
failureAction: Enforce |
29 changes: 29 additions & 0 deletions
29
test/conformance/chainsaw/autogen/cel-preconditions/policy.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: disallow-privilege-escalation | ||
spec: | ||
rules: | ||
- name: privilege-escalation | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- Pod | ||
operations: | ||
- CREATE | ||
- UPDATE | ||
celPreconditions: | ||
- name: "Only for prod" | ||
expression: "has(object.metadata.labels) && has(object.metadata.labels.prod) && object.metadata.labels.prod == 'true'" | ||
validate: | ||
failureAction: Enforce | ||
cel: | ||
expressions: | ||
- expression: >- | ||
object.spec.containers.all(container, has(container.securityContext) && | ||
has(container.securityContext.allowPrivilegeEscalation) && | ||
container.securityContext.allowPrivilegeEscalation == false) | ||
message: >- | ||
Privilege escalation is disallowed. The field | ||
spec.containers[*].securityContext.allowPrivilegeEscalation must be set to `false`. |