-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add other policies in CEL expressions - Part 6 (#970)
* add CI test for directories starting with res in other-cel Signed-off-by: Chandan-DK <[email protected]> * copy restrict-loadbalancer Signed-off-by: Chandan-DK <[email protected]> * convert restrict-loadbalancer Signed-off-by: Chandan-DK <[email protected]> * copy restrict-networkpolicy-empty-podselector Signed-off-by: Chandan-DK <[email protected]> * convert restrict-networkpolicy-empty-podselector Signed-off-by: Chandan-DK <[email protected]> * copy restrict-node-affinity Signed-off-by: Chandan-DK <[email protected]> * convert restrict-node-affinity Signed-off-by: Chandan-DK <[email protected]> * copy restrict-sa-automount-sa-token Signed-off-by: Chandan-DK <[email protected]> * convert restrict-sa-automount-sa-token Signed-off-by: Chandan-DK <[email protected]> * copy restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * add kyverno tests for restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * convert restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * copy restrict-service-port-range Signed-off-by: Chandan-DK <[email protected]> * convert restrict-service-port-range Signed-off-by: Chandan-DK <[email protected]> * copy restrict-secrets-by-name Signed-off-by: Chandan-DK <[email protected]> * convert restrict-secrets-by-name Signed-off-by: Chandan-DK <[email protected]> * copy restrict-storageclass Signed-off-by: Chandan-DK <[email protected]> * convert restrict-storageclass Signed-off-by: Chandan-DK <[email protected]> * copy restrict-usergroup-fsgroup-id Signed-off-by: Chandan-DK <[email protected]> * add kyverno tests for restrict-usergroup-fsgroup-id Signed-off-by: Chandan-DK <[email protected]> * convert restrict-usergroup-fsgroup-id Signed-off-by: Chandan-DK <[email protected]> * copy restrict-wildcard-resources Signed-off-by: Chandan-DK <[email protected]> * add kyverno tests for restrict-wildcard-resources Signed-off-by: Chandan-DK <[email protected]> * convert restrict-wildcard-resources Signed-off-by: Chandan-DK <[email protected]> * copy restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * add kyverno tests for restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * convert restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * rename files for clarity Signed-off-by: Chandan-DK <[email protected]> * add new lines at the end of file Signed-off-by: Chandan-DK <[email protected]> * fix cel test Signed-off-by: Chandan-DK <[email protected]> * add test case for pod creation without securityContext field Signed-off-by: Chandan-DK <[email protected]> * handle case where rules is null in restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * add edge cases to chainsaw test for restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * add kyverno tests with edge cases for restrict-wildcard-verbs Signed-off-by: Chandan-DK <[email protected]> * handle case where rules is null for restrict-wildcard-resources Signed-off-by: Chandan-DK <[email protected]> * add edge cases to chainsaw tests for restrict-wildcard-resources Signed-off-by: Chandan-DK <[email protected]> * handle case where rules is null for restrict-clusterrole-nodesproxy Signed-off-by: Chandan-DK <[email protected]> * add kyverno test with edge cases for restrict-clusterrole-nodesproxy Signed-off-by: Chandan-DK <[email protected]> * add chainsaw edge cases for restrict-clusterrole-nodesproxy Signed-off-by: Chandan-DK <[email protected]> * handle case where rules is null in restrict-escalation-verbs-roles Signed-off-by: Chandan-DK <[email protected]> * add edge case for kyverno tests in restrict-escalation-verbs-roles Signed-off-by: Chandan-DK <[email protected]> * add edge cases to chainsaw tests for restrict-escalation-verbs-roles Signed-off-by: Chandan-DK <[email protected]> * handle case where rules is null in restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * add edge cases for restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * add edge cases for chainsaw tests in restrict-secret-role-verbs Signed-off-by: Chandan-DK <[email protected]> * rename kyverno test resources Signed-off-by: Chandan-DK <[email protected]> * elaborate comment Signed-off-by: Chandan-DK <[email protected]> --------- Signed-off-by: Chandan-DK <[email protected]> Co-authored-by: Mariam Fahmy <[email protected]>
- Loading branch information
1 parent
d6950b7
commit 35b992d
Showing
116 changed files
with
4,323 additions
and
14 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
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
50 changes: 50 additions & 0 deletions
50
other-cel/restrict-clusterrole-nodesproxy/.kyverno-test/resource.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,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 | ||
--- |
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
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
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
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
32 changes: 32 additions & 0 deletions
32
other-cel/restrict-loadbalancer/.chainsaw-test/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,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 | ||
|
7 changes: 7 additions & 0 deletions
7
other-cel/restrict-loadbalancer/.chainsaw-test/policy-ready.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,7 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: no-loadbalancer-service | ||
status: | ||
ready: true | ||
|
12 changes: 12 additions & 0 deletions
12
other-cel/restrict-loadbalancer/.chainsaw-test/svc-bad.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,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
13
other-cel/restrict-loadbalancer/.chainsaw-test/svc-good.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,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
22
other-cel/restrict-loadbalancer/.kyverno-test/kyverno-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,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
26
other-cel/restrict-loadbalancer/.kyverno-test/resource.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,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 | ||
|
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,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
33
other-cel/restrict-loadbalancer/restrict-loadbalancer.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,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." | ||
|
Oops, something went wrong.