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 6 #970

Merged
merged 50 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
88fde63
add CI test for directories starting with res in other-cel
Chandan-DK Apr 17, 2024
7660980
copy restrict-loadbalancer
Chandan-DK Apr 17, 2024
023a3f2
convert restrict-loadbalancer
Chandan-DK Apr 17, 2024
485b8fa
copy restrict-networkpolicy-empty-podselector
Chandan-DK Apr 17, 2024
d257586
convert restrict-networkpolicy-empty-podselector
Chandan-DK Apr 17, 2024
7edc5a7
copy restrict-node-affinity
Chandan-DK Apr 18, 2024
a4a63e4
convert restrict-node-affinity
Chandan-DK Apr 18, 2024
9abc05f
copy restrict-sa-automount-sa-token
Chandan-DK Apr 18, 2024
74ae9ea
convert restrict-sa-automount-sa-token
Chandan-DK Apr 18, 2024
af9f166
copy restrict-secret-role-verbs
Chandan-DK Apr 19, 2024
6a629d7
add kyverno tests for restrict-secret-role-verbs
Chandan-DK Apr 19, 2024
e606f6a
convert restrict-secret-role-verbs
Chandan-DK Apr 19, 2024
68d7113
copy restrict-service-port-range
Chandan-DK Apr 19, 2024
69ed0f8
convert restrict-service-port-range
Chandan-DK Apr 19, 2024
8a62492
copy restrict-secrets-by-name
Chandan-DK Apr 20, 2024
f8e2937
convert restrict-secrets-by-name
Chandan-DK Apr 20, 2024
40e64f8
copy restrict-storageclass
Chandan-DK Apr 20, 2024
5e0bdae
convert restrict-storageclass
Chandan-DK Apr 20, 2024
725eeed
copy restrict-usergroup-fsgroup-id
Chandan-DK Apr 20, 2024
73d9791
add kyverno tests for restrict-usergroup-fsgroup-id
Chandan-DK Apr 20, 2024
3bc9784
convert restrict-usergroup-fsgroup-id
Chandan-DK Apr 20, 2024
33971b7
copy restrict-wildcard-resources
Chandan-DK Apr 21, 2024
33647b7
add kyverno tests for restrict-wildcard-resources
Chandan-DK Apr 21, 2024
43a8693
convert restrict-wildcard-resources
Chandan-DK Apr 21, 2024
d48ae01
copy restrict-wildcard-verbs
Chandan-DK Apr 21, 2024
480f873
add kyverno tests for restrict-wildcard-verbs
Chandan-DK Apr 21, 2024
51a67ed
convert restrict-wildcard-verbs
Chandan-DK Apr 21, 2024
1bb6491
rename files for clarity
Chandan-DK Apr 21, 2024
91119aa
add new lines at the end of file
Chandan-DK Apr 21, 2024
afc6a65
Merge branch 'main' into other-policies-cel-part-6
Chandan-DK May 30, 2024
3bbf7e4
Merge branch 'main' into other-policies-cel-part-6
Chandan-DK Jun 4, 2024
2e5841d
fix cel test
Chandan-DK Jun 4, 2024
00be8c2
add test case for pod creation without securityContext field
Chandan-DK Jun 4, 2024
0baf7b9
handle case where rules is null in restrict-wildcard-verbs
Chandan-DK Jun 4, 2024
66138d8
add edge cases to chainsaw test for restrict-wildcard-verbs
Chandan-DK Jun 4, 2024
88ac835
add kyverno tests with edge cases for restrict-wildcard-verbs
Chandan-DK Jun 4, 2024
ce86235
handle case where rules is null for restrict-wildcard-resources
Chandan-DK Jun 4, 2024
15474aa
add edge cases to chainsaw tests for restrict-wildcard-resources
Chandan-DK Jun 4, 2024
81ab935
handle case where rules is null for restrict-clusterrole-nodesproxy
Chandan-DK Jun 4, 2024
a25ac37
add kyverno test with edge cases for restrict-clusterrole-nodesproxy
Chandan-DK Jun 4, 2024
b52fbd6
add chainsaw edge cases for restrict-clusterrole-nodesproxy
Chandan-DK Jun 4, 2024
760b816
handle case where rules is null in restrict-escalation-verbs-roles
Chandan-DK Jun 4, 2024
17eee49
add edge case for kyverno tests in restrict-escalation-verbs-roles
Chandan-DK Jun 4, 2024
8bfb6b9
add edge cases to chainsaw tests for restrict-escalation-verbs-roles
Chandan-DK Jun 4, 2024
cc59be2
handle case where rules is null in restrict-secret-role-verbs
Chandan-DK Jun 4, 2024
369dfa9
add edge cases for restrict-secret-role-verbs
Chandan-DK Jun 4, 2024
60c1696
add edge cases for chainsaw tests in restrict-secret-role-verbs
Chandan-DK Jun 4, 2024
f0b2342
rename kyverno test resources
Chandan-DK Jun 5, 2024
08b12b5
elaborate comment
Chandan-DK Jun 5, 2024
55f406e
Merge branch 'main' into other-policies-cel-part-6
MariamFahmy98 Jun 6, 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 @@ -60,6 +60,7 @@ jobs:
- ^other$/^res
- ^other-cel$/^res
- ^other$/^[s-z]
- ^other-cel$/^res
- ^pod-security$
- ^pod-security-cel$
- ^psa$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "watch", "list"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: empty-rules
rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: omitted-rules
---
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ metadata:
policies:
- ../restrict-clusterrole-nodesproxy.yaml
resources:
- ../.chainsaw-test/cr-bad.yaml
- ../.chainsaw-test/cr-good.yaml
- resource.yaml
results:
- policy: restrict-clusterrole-nodesproxy
rule: clusterrole-nodesproxy
Expand All @@ -21,5 +20,6 @@ results:
resources:
- goodcr01
- goodcr02
- default-rules
result: pass

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: badcr01
rules:
- apiGroups: [""]
resources: ["nodes/proxy", "namespaces"]
verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: badcr02
rules:
- apiGroups: [""]
resources: ["pods", "nodes/proxy"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: goodcr01
rules:
- apiGroups: [""]
resources: ["pods", "namespaces"]
verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: goodcr02
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "watch", "list"]
---
# In the manifest, if the 'rules' field is not specified or is specified as 'rules: ' without a value,
# it will be set to null by default when created in the cluster
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: default-rules
rules: null
---
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ annotations:
kyverno/category: "Sample in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "ClusterRole, RBAC"
digest: 54304b50ebe12dab7f36afa09eaadf5f591d39a2bfe3ee83c150df30cbf66c4b
digest: 5c78dc50201f3223c42e0ac414e23dcc418f487ae76031aa85eb4fbd6fa1a2c1
createdAt: "2024-04-13T16:12:56Z"

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ spec:
cel:
expressions:
- expression: >-
object.rules == null ||
!object.rules.exists(rule,
rule.resources.exists(resource, resource == 'nodes/proxy') &&
rule.apiGroups.exists(apiGroup, apiGroup == ''))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ rules:
- apiGroups: [""]
resources: ["pods", "namespaces"]
verbs: ["get", "watch", "list"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: empty-rules
rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: omitted-rules
---
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ rules:
- apiGroups: [""]
resources: ["pods", "namespaces"]
verbs: ["get", "watch", "list"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: empty-rules
rules:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: omitted-rules
---
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ results:
resources:
- goodclusterrole01
- goodclusterrole02
- default-rules
result: pass
rule: escalate
- kind: Role
policy: restrict-escalation-verbs-roles
resources:
- goodrole01
- default-rules
result: pass
rule: escalate

Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ rules:
verbs:
- '*'
---
# In the manifest, if the 'rules' field is not specified or is specified as 'rules: ' without a value,
# it will be set to null by default when created in the cluster
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: default-rules
rules: null
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
Expand All @@ -126,4 +134,12 @@ rules:
- roles
verbs:
- impersonate

---
# In the manifest, if the 'rules' field is not specified or is specified as 'rules: ' without a value,
# it will be set to null by default when created in the cluster
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: default-rules
rules: null
---
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ annotations:
kyverno/category: "Security in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Role, ClusterRole, RBAC"
digest: 79d9c85060d55996f3be6bbc06321edfed00daeaca5bd24a7f4436f23a96bd73
digest: 145bfa9745d524e77c11d35ea267c3c2323eb6d9d13c3b7c00632eb358da7d75
createdAt: "2024-04-14T15:40:58Z"

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ spec:
expression: "['*', 'bind', 'escalate', 'impersonate']"
expressions:
- expression: >-
object.rules == null ||
!object.rules.exists(rule,
rule.apiGroups.exists(apiGroup, apiGroup in variables.apiGroups) &&
rule.resources.exists(resource, resource in variables.resources) &&
Expand Down
32 changes: 32 additions & 0 deletions other-cel/restrict-loadbalancer/.chainsaw-test/chainsaw-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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-loadbalancer
spec:
steps:
- name: step-01
try:
- apply:
file: ../restrict-loadbalancer.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: no-loadbalancer-service
spec:
validationFailureAction: Enforce
- assert:
file: policy-ready.yaml
- name: step-02
try:
- apply:
file: svc-good.yaml
- apply:
expect:
- check:
($error != null): true
file: svc-bad.yaml

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

12 changes: 12 additions & 0 deletions other-cel/restrict-loadbalancer/.chainsaw-test/svc-bad.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: badsvc01
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancer

13 changes: 13 additions & 0 deletions other-cel/restrict-loadbalancer/.chainsaw-test/svc-good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: goodsvc01
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30007
type: NodePort

22 changes: 22 additions & 0 deletions other-cel/restrict-loadbalancer/.kyverno-test/kyverno-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: no-loadbalancer-service
policies:
- ../restrict-loadbalancer.yaml
resources:
- resource.yaml
results:
- kind: Service
policy: no-loadbalancer-service
resources:
- default/my-service-1
result: fail
rule: no-LoadBalancer
- kind: Service
policy: no-loadbalancer-service
resources:
- default/my-service-2
result: pass
rule: no-LoadBalancer

26 changes: 26 additions & 0 deletions other-cel/restrict-loadbalancer/.kyverno-test/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: v1
kind: Service
metadata:
name: my-service-1
spec:
selector:
app: myapp-1
ports:
- port: 80
targetPort: 80
type: LoadBalancer

---
apiVersion: v1
kind: Service
metadata:
name: my-service-2
spec:
selector:
app: MyApp
ports:
- port: 80
targetPort: 80
nodePort: 30007
type: NodePort

24 changes: 24 additions & 0 deletions other-cel/restrict-loadbalancer/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: restrict-loadbalancer-cel
version: 1.0.0
displayName: Disallow Service Type LoadBalancer in CEL expressions
description: >-
Especially in cloud provider environments, a Service having type LoadBalancer will cause the provider to respond by creating a load balancer somewhere in the customer account. This adds cost and complexity to a deployment. Without restricting this ability, users may easily overrun established budgets and security practices set by the organization. This policy restricts use of the Service type LoadBalancer.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/other-cel/restrict-loadbalancer/restrict-loadbalancer.yaml
```
keywords:
- kyverno
- Sample
- CEL Expressions
readme: |
Especially in cloud provider environments, a Service having type LoadBalancer will cause the provider to respond by creating a load balancer somewhere in the customer account. This adds cost and complexity to a deployment. Without restricting this ability, users may easily overrun established budgets and security practices set by the organization. This policy restricts use of the Service type LoadBalancer.

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: "Service"
digest: 33b5031b68eb2f05d6dc535516fff514947846c6b64b1944e1546c897afae750
createdAt: "2024-04-17T17:49:00Z"

33 changes: 33 additions & 0 deletions other-cel/restrict-loadbalancer/restrict-loadbalancer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: no-loadbalancer-service
annotations:
policies.kyverno.io/title: Disallow Service Type LoadBalancer in CEL expressions
policies.kyverno.io/category: Sample in CEL
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Service
policies.kyverno.io/minversion: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/description: >-
Especially in cloud provider environments, a Service having type LoadBalancer will cause the
provider to respond by creating a load balancer somewhere in the customer account. This adds
cost and complexity to a deployment. Without restricting this ability, users may easily
overrun established budgets and security practices set by the organization. This policy restricts
use of the Service type LoadBalancer.
spec:
validationFailureAction: Audit
background: true
rules:
- name: no-LoadBalancer
match:
any:
- resources:
kinds:
- Service
validate:
cel:
expressions:
- expression: "object.spec.type != 'LoadBalancer'"
message: "Service of type LoadBalancer is not allowed."

Loading
Loading