Skip to content
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

feat: add other policies in CEL expressions - Part 5 #968

Merged
merged 44 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e5567fa
add CI test for directories starting with res in other-cel
Chandan-DK Apr 12, 2024
c413c8c
copy restrict-annotations
Chandan-DK Apr 12, 2024
d0ca136
convert restrict-annotations to cel
Chandan-DK Apr 12, 2024
5fd5514
rename wrong pod names and annotation key
Chandan-DK Apr 12, 2024
5f70d1a
copy restrict-binding-clusteradmin
Chandan-DK Apr 12, 2024
2c86142
add kyverno tests for restrict-binding-clusteradmin
Chandan-DK Apr 12, 2024
7e00479
convert restrict-binding-clusteradmin to cel
Chandan-DK Apr 12, 2024
d23f921
copy restrict-binding-system-groups
Chandan-DK Apr 12, 2024
9ff03c8
add kyverno tests for restrict-binding-system-groups
Chandan-DK Apr 12, 2024
7e70859
fix kind in chainsaw test resource
Chandan-DK Apr 12, 2024
3b5d501
convert restrict-binding-system-groups to cel
Chandan-DK Apr 12, 2024
e485af8
copy restrict-clusterrole-nodesproxy
Chandan-DK Apr 13, 2024
b6112fc
add kyverno tests for restrict-clusterrole-nodesproxy
Chandan-DK Apr 13, 2024
7c7152f
convert restrict-clusterrole-nodesproxy
Chandan-DK Apr 13, 2024
6f79c70
copy restrict-controlplane-scheduling
Chandan-DK Apr 13, 2024
695d76f
add test resources for restrict-controlplane-scheduling
Chandan-DK Apr 13, 2024
bcca786
convert restrict-controlplane-scheduling
Chandan-DK Apr 13, 2024
b28f55e
copy restrict-deprecated-registry
Chandan-DK Apr 13, 2024
2ba60ae
convert restrict-deprecated-registry
Chandan-DK Apr 13, 2024
8749018
Merge branch 'main' into other-policies-cel-part-5
Chandan-DK Apr 13, 2024
482a572
copy restrict-escalation-verbs-roles
Chandan-DK Apr 14, 2024
004d750
add edge case to chainsaw test for restrict-escalation-verbs-roles
Chandan-DK Apr 14, 2024
ec90c98
convert restrict-escalation-verbs-roles to cel
Chandan-DK Apr 14, 2024
6debfaf
copy restrict-ingress-classes
Chandan-DK Apr 14, 2024
f020e18
convert restrict-ingress-classes
Chandan-DK Apr 14, 2024
7ef7aa4
copy restrict-ingress-defaultbackend
Chandan-DK Apr 14, 2024
45a7fc6
convert restrict-ingress-defaultbackend
Chandan-DK Apr 14, 2024
fe46873
copy restrict-ingress-wildcard
Chandan-DK Apr 15, 2024
d4efdc6
convert restrict-ingress-wildcard
Chandan-DK Apr 15, 2024
110b550
fix invalid host name
Chandan-DK Apr 15, 2024
8f43af5
rename wrong pod names and annotation key for restrict-annotations
Chandan-DK Apr 16, 2024
a260c86
add corresponding missing kyverno tests in other folder
Chandan-DK Apr 16, 2024
9ec0dfa
make corrections in other folder chainsaw tests
Chandan-DK Apr 16, 2024
79fa650
add edge case in chainsaw test for restrict-escalation-verbs-roles in…
Chandan-DK Apr 16, 2024
1824c6b
rename files for clarity
Chandan-DK Apr 16, 2024
bee3ebc
add a new line at the end of files
Chandan-DK Apr 16, 2024
cb0967f
remove extra lines and comment
Chandan-DK Apr 23, 2024
3481d0b
Merge branch 'main' into other-policies-cel-part-5
Chandan-DK May 17, 2024
d4e8d96
reorder test in workflow
Chandan-DK May 17, 2024
204b737
Merge branch 'main' into other-policies-cel-part-5
MariamFahmy98 May 22, 2024
b0821c6
Merge branch 'main' into other-policies-cel-part-5
MariamFahmy98 May 30, 2024
d86183d
fix cel test
Chandan-DK May 30, 2024
ddbe18a
Merge branch 'main' into other-policies-cel-part-5
MariamFahmy98 Jun 3, 2024
d8435a0
Merge branch 'main' into other-policies-cel-part-5
MariamFahmy98 Jun 3, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
- ^other$/^re[c-q]
- ^other$/^res
- ^other$/^[s-z]
- ^other-cel$/^res
- ^pod-security$
- ^pod-security-cel$
- ^psa$
Expand Down
39 changes: 39 additions & 0 deletions other-cel/restrict-annotations/.chainsaw-test/chainsaw-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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:
creationTimestamp: null
name: restrict-annotations
spec:
steps:
- name: step-01
try:
- apply:
file: ../restrict-annotations.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-annotations
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: pod-good.yaml
- apply:
expect:
- check:
($error != null): true
file: pod-bad.yaml
- apply:
file: podcontroller-good.yaml
- apply:
expect:
- check:
($error != null): true
file: podcontroller-bad.yaml

37 changes: 37 additions & 0 deletions other-cel/restrict-annotations/.chainsaw-test/pod-bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
fluxcd.io/foo: bar
name: badpod01
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
annotations:
bar: foo
fluxcd.io/foo: bar
foo: bar
name: badpod02
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
annotations:
bar: foo
fluxcd.io/hello: bar
foo: bar
name: badpod03
spec:
containers:
- name: busybox
image: busybox:1.35

22 changes: 22 additions & 0 deletions other-cel/restrict-annotations/.chainsaw-test/pod-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: Pod
metadata:
name: goodpod01
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: v1
kind: Pod
metadata:
annotations:
bar: foo
flux.io/foo: bar
foo: bar
name: goodpod02
spec:
containers:
- name: busybox
image: busybox:1.35

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
fluxcd.io/foo: bar
labels:
app: busybox
name: baddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
annotations:
foo: bar
fluxcd.io/foo: bar
bar: foo
name: badcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox:1.35
restartPolicy: OnFailure

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
flux.io/foo: bar
labels:
app: busybox
name: gooddeployment01
spec:
replicas: 1
selector:
matchLabels:
app: busybox
strategy: {}
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:1.35
---
apiVersion: batch/v1
kind: CronJob
metadata:
annotations:
foo: bar
name: goodcronjob01
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: busybox
image: busybox:1.35
restartPolicy: OnFailure

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-annotations
status:
ready: true

28 changes: 28 additions & 0 deletions other-cel/restrict-annotations/.kyverno-test/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: restrict-annotations
policies:
- ../restrict-annotations.yaml
resources:
- resource.yaml
results:
- kind: Deployment
policy: restrict-annotations
resources:
- mydeploy
result: fail
rule: block-flux-v1
- kind: Pod
policy: restrict-annotations
resources:
- myapp-pod
result: fail
rule: block-flux-v1
- kind: CronJob
policy: restrict-annotations
resources:
- hello
result: pass
rule: block-flux-v1

54 changes: 54 additions & 0 deletions other-cel/restrict-annotations/.kyverno-test/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: v1
kind: Pod
metadata:
labels:
app: myapp
name: myapp-pod
annotations:
fluxcd.io/title: Annotation for pods
spec:
containers:
- image: nginx
name: myapp-pod

---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myapp
name: mydeploy
annotations:
fluxcd.io/title: Annotation for deployment
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: nginx
name: nginx

---
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
annotations:
gauss.io/title: Annotation for CronJob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
restartPolicy: OnFailure

24 changes: 24 additions & 0 deletions other-cel/restrict-annotations/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: restrict-annotations-cel
version: 1.0.0
displayName: Restrict Annotations in CEL expressions
description: >-
Some annotations control functionality driven by other cluster-wide tools and are not normally set by some class of users. This policy prevents the use of an annotation beginning with `fluxcd.io/`. This can be useful to ensure users either don't set reserved annotations or to force them to use a newer version of an annotation.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other-cel/restrict-annotations/restrict-annotations.yaml
```
keywords:
- kyverno
- Sample
- CEL Expressions
readme: |
Some annotations control functionality driven by other cluster-wide tools and are not normally set by some class of users. This policy prevents the use of an annotation beginning with `fluxcd.io/`. This can be useful to ensure users either don't set reserved annotations or to force them to use a newer version of an annotation.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Sample in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Pod, Annotation"
digest: 55b4953e1ca6aa6038d407aae539705d9a8c1136d2ffc277df1686a08ac7c9a8
createdAt: "2024-04-12T15:55:04Z"

37 changes: 37 additions & 0 deletions other-cel/restrict-annotations/restrict-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-annotations
annotations:
policies.kyverno.io/title: Restrict Annotations in CEL expressions
policies.kyverno.io/category: Sample in CEL
policies.kyverno.io/minversion: 1.11.0
policies.kyverno.io/subject: Pod, Annotation
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/description: >-
Some annotations control functionality driven by other cluster-wide tools and are not
normally set by some class of users. This policy prevents the use of an annotation beginning
with `fluxcd.io/`. This can be useful to ensure users either
don't set reserved annotations or to force them to use a newer version of an annotation.
pod-policies.kyverno.io/autogen-controllers: none
spec:
validationFailureAction: Audit
background: true
rules:
- name: block-flux-v1
match:
any:
- resources:
kinds:
- Deployment
- CronJob
- Job
- StatefulSet
- DaemonSet
- Pod
validate:
cel:
expressions:
- expression: "!has(object.metadata.annotations) || !object.metadata.annotations.exists(annotation, annotation.startsWith('fluxcd.io/'))"
message: Cannot use Flux v1 annotation.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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:
creationTimestamp: null
name: restrict-binding-clusteradmin
spec:
steps:
- name: step-01
try:
- apply:
file: ../restrict-binding-clusteradmin.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-binding-clusteradmin
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: rb-good.yaml
- apply:
expect:
- check:
($error != null): true
file: rb-bad.yaml
- apply:
file: crb-good.yaml
- apply:
expect:
- check:
($error != null): true
file: crb-bad.yaml

Loading
Loading