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 best practices policies in CEL expressions #925

Merged
merged 69 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
747b0e8
copy restrict-node-port
Chandan-DK Mar 6, 2024
9a4eca2
convert restrict-node-port to cel
Chandan-DK Mar 6, 2024
c87dea8
move resource files to test folders to avoid cross referencing
Chandan-DK Mar 6, 2024
66550fb
copy require-labels
Chandan-DK Mar 6, 2024
a19e614
convert require-labels to cel
Chandan-DK Mar 6, 2024
793c146
copy restrict-service-external-ips
Chandan-DK Mar 6, 2024
7a0fc6a
convert restrict-service-external-ips to cel
Chandan-DK Mar 7, 2024
2466c52
copy require-ro-rootfs
Chandan-DK Mar 7, 2024
8ca2823
convert require-ro-rootfs to cel
Chandan-DK Mar 7, 2024
cc534a2
copy restrict-image-registries
Chandan-DK Mar 7, 2024
70c4712
convert restrict-image-registries to cel
Chandan-DK Mar 7, 2024
9cbc613
copy disallow-latest-tag
Chandan-DK Mar 7, 2024
7266245
convert disallow-latest-tag to cel
Chandan-DK Mar 7, 2024
56680c9
copy disallow-default-namespace
Chandan-DK Mar 8, 2024
deefeee
convert disallow-default-namespace to cel
Chandan-DK Mar 8, 2024
c0b203a
copy disallow-helm-tiller
Chandan-DK Mar 8, 2024
2908df9
convert disallow-helm-tiller to cel
Chandan-DK Mar 8, 2024
5291e6d
Merge branch 'main' into convert-best-practices-to-cel
Chandan-DK Mar 9, 2024
cc5a3da
copy disallow-empty-ingress-host
Chandan-DK Mar 9, 2024
13f8cb5
set original disallow-empty-ingress-host to Audit
Chandan-DK Mar 9, 2024
b29888f
convert disallow-empty-ingress-host to cel
Chandan-DK Mar 9, 2024
1347c26
patch cel policy to set it to Enforce in chainsaw test
Chandan-DK Mar 9, 2024
638431a
fix: update semantically wrong chainsaw test resources in original re…
Chandan-DK Mar 10, 2024
c1cf234
copy require-drop-all
Chandan-DK Mar 10, 2024
625ee8e
convert require-drop-all to cel
Chandan-DK Mar 10, 2024
0283264
update workflow to test policies in best-practices-cel folder
Chandan-DK Mar 10, 2024
e206f7c
fix duplicate container names in require-probes chainsaw test
Chandan-DK Mar 10, 2024
c3b399e
copy require-probes
Chandan-DK Mar 10, 2024
13f20c0
convert require-probes to cel
Chandan-DK Mar 10, 2024
3405d61
require-ro-rootfs: fix selector does not match template labels
Chandan-DK Mar 14, 2024
6f0f536
require-ro-rootfs: fix duplicate container names
Chandan-DK Mar 14, 2024
28a0b2b
disallow-helm-tiller: fix invalid container naming
Chandan-DK Mar 14, 2024
4deb30c
require-labels: fix selector does not match template labels
Chandan-DK Mar 14, 2024
1ee5e25
restrict-image-registries: fix selector does not match template labels
Chandan-DK Mar 14, 2024
9527da4
Merge branch 'main' into convert-best-practices-to-cel
Chandan-DK Mar 14, 2024
e809be1
rename file for clarity
Chandan-DK Mar 14, 2024
62fc668
copy disallow-cri-sock-mount
Chandan-DK Mar 14, 2024
f26b1b2
convert disallow-cri-sock-mount to cel
Chandan-DK Mar 14, 2024
9579075
remove duplicate expressins in require-drop-all
Chandan-DK Mar 14, 2024
46574a1
rename file for clarity
Chandan-DK Mar 14, 2024
2d25227
require-drop-cap-net-raw: fix duplicate container names
Chandan-DK Mar 14, 2024
de2993a
copy require-drop-cap-net-raw
Chandan-DK Mar 14, 2024
057814d
rename pods to distinguish them
Chandan-DK Mar 15, 2024
618b7c8
convert require-drop-cap-net-raw to cel
Chandan-DK Mar 15, 2024
1fc12c0
copy require-pod-requests-limits
Chandan-DK Mar 15, 2024
fdb9a00
convert require-pod-requests-limits to cel
Chandan-DK Mar 15, 2024
ffe9192
rename files for clarity
Chandan-DK Mar 15, 2024
f3f84ec
add new line at end of file where not present
Chandan-DK Mar 15, 2024
42808ba
calculate digests
Chandan-DK Mar 15, 2024
c13bf5a
add new lines
Chandan-DK Mar 15, 2024
6298f7e
update digests
Chandan-DK Mar 15, 2024
b71dc85
remove celPreconditions until it behaves as expected
Chandan-DK Mar 15, 2024
8bef250
update digests
Chandan-DK Mar 15, 2024
48675be
remove wrong test step
Chandan-DK Mar 16, 2024
8c6b717
Merge branch 'main' into convert-best-practices-to-cel
chipzoller Mar 18, 2024
db6f0a4
Merge branch 'main' into convert-best-practices-to-cel
MariamFahmy98 Mar 25, 2024
51a0c3e
use variables to remove duplicate logic
Chandan-DK Mar 25, 2024
cc3be8a
remove unnecessary whitespace in require-ro-rootfs
Chandan-DK Mar 26, 2024
734f9f2
use namespaceObject variable
Chandan-DK Mar 26, 2024
9f493ed
Combine expressions into 1 rule to generate VAPs
Chandan-DK Apr 4, 2024
8e133b7
copy kyverno tests for disallow-default-namespace
Chandan-DK Apr 19, 2024
bc57d09
Merge branch 'main' into convert-best-practices-to-cel
Chandan-DK Apr 19, 2024
044a419
Merge branch 'main' into convert-best-practices-to-cel
JimBugwadia May 15, 2024
bb48b70
Merge branch 'main' into convert-best-practices-to-cel
MariamFahmy98 May 16, 2024
6a71ee2
Merge branch 'main' into convert-best-practices-to-cel
MariamFahmy98 May 16, 2024
cad31da
Merge branch 'main' into convert-best-practices-to-cel
MariamFahmy98 May 22, 2024
3cda1d5
Merge branch 'main' into convert-best-practices-to-cel
MariamFahmy98 May 30, 2024
d6ad7cd
fix issue caused in cel policies tests due to chainsaw templating
Chandan-DK May 30, 2024
8ca2e18
Merge branch 'main' into convert-best-practices-to-cel
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-nodeport
status:
ready: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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-node-port
spec:
steps:
- name: step-01
try:
- apply:
file: ../restrict-node-port.yaml
- patch:
resource:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-nodeport
spec:
validationFailureAction: Enforce
- assert:
file: chainsaw-step-01-assert-1.yaml
- name: step-02
try:
- apply:
file: ../../../best-practices/restrict-node-port/.chainsaw-test/good-services.yaml
Chandan-DK marked this conversation as resolved.
Show resolved Hide resolved
- apply:
expect:
- check:
($error != null): true
file: ../../../best-practices/restrict-node-port/.chainsaw-test/bad-service-nodeport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: cli.kyverno.io/v1alpha1
kind: Test
metadata:
name: restrict-node-port
policies:
- ../restrict-node-port.yaml
resources:
- ../../../best-practices/restrict-node-port/.kyverno-test/resource.yaml
results:
- kind: Service
policy: restrict-nodeport
resources:
- badservice01
result: fail
rule: validate-nodeport
- kind: Service
policy: restrict-nodeport
resources:
- goodservice01
- goodservice02
result: pass
rule: validate-nodeport
23 changes: 23 additions & 0 deletions best-practices-cel/restrict-node-port/artifacthub-pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: restrict-node-port-cel
version: 1.0.0
displayName: Disallow NodePort in CEL expressions
description: >-
A Kubernetes Service of type NodePort uses a host port to receive traffic from any source. A NetworkPolicy cannot be used to control traffic to host ports. Although NodePort Services can be useful, their use must be limited to Services with additional upstream security checks. This policy validates that any new Services do not use the `NodePort` type.
install: |-
```shell
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/best-practices-cel/restrict-node-port/restrict-node-port.yaml
```
keywords:
- kyverno
- Best Practices
- CEL Expressions
readme: |
A Kubernetes Service of type NodePort uses a host port to receive traffic from any source. A NetworkPolicy cannot be used to control traffic to host ports. Although NodePort Services can be useful, their use must be limited to Services with additional upstream security checks. This policy validates that any new Services do not use the `NodePort` type.

Refer to the documentation for more details on Kyverno annotations: https://artifacthub.io/docs/topics/annotations/kyverno/
annotations:
kyverno/category: "Best Practices in CEL"
kyverno/kubernetesVersion: "1.26-1.27"
kyverno/subject: "Service"
digest: 5c8a4a1f91ddc77b22ae5d75933746f421ee98330cd1c61b7023a0fd004f57b4
createdAt: "2024-03-06T14:04:34Z"
32 changes: 32 additions & 0 deletions best-practices-cel/restrict-node-port/restrict-node-port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-nodeport
annotations:
policies.kyverno.io/title: Disallow NodePort in CEL expressions
policies.kyverno.io/category: Best Practices in CEL
policies.kyverno.io/minversion: 1.11.0
kyverno.io/kubernetes-version: "1.26-1.27"
policies.kyverno.io/severity: medium
policies.kyverno.io/subject: Service
policies.kyverno.io/description: >-
A Kubernetes Service of type NodePort uses a host port to receive traffic from
any source. A NetworkPolicy cannot be used to control traffic to host ports.
Although NodePort Services can be useful, their use must be limited to Services
with additional upstream security checks. This policy validates that any new Services
do not use the `NodePort` type.
spec:
validationFailureAction: Audit
background: true
rules:
- name: validate-nodeport
match:
any:
- resources:
kinds:
- Service
validate:
cel:
expressions:
- expression: "has(object.spec.type) ? (object.spec.type != 'NodePort') : true"
message: "Services of type NodePort are not allowed."