forked from kyverno/policies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into iterateAllContainerTypes
- Loading branch information
Showing
14 changed files
with
411 additions
and
3 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
best-practices-cel/check-deprecated-apis/.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,27 @@ | ||
apiVersion: cli.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: check-deprecated-apis | ||
policies: | ||
- ../check-deprecated-apis.yaml | ||
resources: | ||
- resource.yaml | ||
results: | ||
- kind: CronJob | ||
policy: check-deprecated-apis | ||
resources: | ||
- bad-cronjob | ||
result: fail | ||
rule: validate-v1-25-removals | ||
- kind: CronJob | ||
policy: check-deprecated-apis | ||
resources: | ||
- good-cronjob | ||
result: skip | ||
rule: validate-v1-25-removals | ||
- kind: FlowSchema | ||
policy: check-deprecated-apis | ||
resources: | ||
- bad-flowschema | ||
result: fail | ||
rule: validate-v1-29-removals |
52 changes: 52 additions & 0 deletions
52
best-practices-cel/check-deprecated-apis/.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,52 @@ | ||
apiVersion: batch/v1beta1 | ||
kind: CronJob | ||
metadata: | ||
name: bad-cronjob | ||
spec: | ||
schedule: "* * * * *" | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: hello | ||
image: busybox:1.28 | ||
imagePullPolicy: IfNotPresent | ||
command: | ||
- /bin/sh | ||
- -c | ||
- date; echo Hello from the Kubernetes cluster | ||
restartPolicy: OnFailure | ||
|
||
--- | ||
|
||
apiVersion: batch/v1 | ||
kind: CronJob | ||
metadata: | ||
name: good-cronjob | ||
spec: | ||
schedule: "* * * * *" | ||
jobTemplate: | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: hello | ||
image: busybox:1.28 | ||
imagePullPolicy: IfNotPresent | ||
command: | ||
- /bin/sh | ||
- -c | ||
- date; echo Hello from the Kubernetes cluster | ||
restartPolicy: OnFailure | ||
|
||
--- | ||
apiVersion: flowcontrol.apiserver.k8s.io/v1beta2 | ||
kind: FlowSchema | ||
metadata: | ||
name: bad-flowschema | ||
spec: | ||
matchingPrecedence: 1000 | ||
priorityLevelConfiguration: | ||
name: exempt | ||
|
23 changes: 23 additions & 0 deletions
23
best-practices-cel/check-deprecated-apis/artifacthub-pkg.yml
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,23 @@ | ||
name: check-deprecated-apis-cel | ||
version: 1.0.0 | ||
displayName: Check deprecated APIs in CEL expressions | ||
description: >- | ||
Kubernetes APIs are sometimes deprecated and removed after a few releases. As a best practice, older API versions should be replaced with newer versions. This policy validates for APIs that are deprecated or scheduled for removal. Note that checking for some of these resources may require modifying the Kyverno ConfigMap to remove filters. PodSecurityPolicy is removed in v1.25 so therefore the validate-v1-25-removals rule may not completely work on 1.25+. | ||
install: |- | ||
```shell | ||
kubectl apply -f https://raw.githubusercontent.com/kyverno/policies/main/best-practices-cel/check-deprecated-apis/check-deprecated-apis.yaml | ||
``` | ||
keywords: | ||
- kyverno | ||
- Best Practices | ||
- CEL Expressions | ||
readme: | | ||
Kubernetes APIs are sometimes deprecated and removed after a few releases. As a best practice, older API versions should be replaced with newer versions. This policy validates for APIs that are deprecated or scheduled for removal. Note that checking for some of these resources may require modifying the Kyverno ConfigMap to remove filters. PodSecurityPolicy is removed in v1.25 so therefore the validate-v1-25-removals rule may not completely work on 1.25+. | ||
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: "Kubernetes APIs" | ||
digest: da368de7982e748983a14198e8f8ef46d455023e8938031444f832919fabba6e | ||
createdAt: "2024-05-31T09:44:23Z" |
95 changes: 95 additions & 0 deletions
95
best-practices-cel/check-deprecated-apis/check-deprecated-apis.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,95 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: check-deprecated-apis | ||
annotations: | ||
policies.kyverno.io/title: Check deprecated APIs in CEL expressions | ||
policies.kyverno.io/category: Best Practices in CEL | ||
policies.kyverno.io/subject: Kubernetes APIs | ||
kyverno.io/kyverno-version: 1.12.1 | ||
kyverno.io/kubernetes-version: "1.26-1.27" | ||
policies.kyverno.io/description: >- | ||
Kubernetes APIs are sometimes deprecated and removed after a few releases. | ||
As a best practice, older API versions should be replaced with newer versions. | ||
This policy validates for APIs that are deprecated or scheduled for removal. | ||
Note that checking for some of these resources may require modifying the Kyverno | ||
ConfigMap to remove filters. PodSecurityPolicy is removed in v1.25 | ||
so therefore the validate-v1-25-removals rule may not completely work on 1.25+. | ||
spec: | ||
validationFailureAction: Audit | ||
background: true | ||
rules: | ||
- name: validate-v1-25-removals | ||
match: | ||
any: | ||
- resources: | ||
# NOTE: PodSecurityPolicy is completely removed in 1.25. | ||
kinds: | ||
- batch/*/CronJob | ||
- discovery.k8s.io/*/EndpointSlice | ||
- events.k8s.io/*/Event | ||
- policy/*/PodDisruptionBudget | ||
- policy/*/PodSecurityPolicy | ||
- node.k8s.io/*/RuntimeClass | ||
celPreconditions: | ||
- name: "allowed-api-versions" | ||
expression: "object.apiVersion in ['batch/v1beta1', 'discovery.k8s.io/v1beta1', 'events.k8s.io/v1beta1', 'policy/v1beta1', 'node.k8s.io/v1beta1']" | ||
validate: | ||
cel: | ||
expressions: | ||
- expression: "false" | ||
messageExpression: >- | ||
object.apiVersion + '/' + object.kind + ' is deprecated and will be removed in v1.25. | ||
See: https://kubernetes.io/docs/reference/using-api/deprecation-guide/' | ||
- name: validate-v1-26-removals | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- flowcontrol.apiserver.k8s.io/*/FlowSchema | ||
- flowcontrol.apiserver.k8s.io/*/PriorityLevelConfiguration | ||
- autoscaling/*/HorizontalPodAutoscaler | ||
celPreconditions: | ||
- name: "allowed-api-versions" | ||
expression: "object.apiVersion in ['flowcontrol.apiserver.k8s.io/v1beta1', 'autoscaling/v2beta2']" | ||
validate: | ||
cel: | ||
expressions: | ||
- expression: "false" | ||
messageExpression: >- | ||
object.apiVersion + '/' + object.kind + ' is deprecated and will be removed in v1.26. | ||
See: https://kubernetes.io/docs/reference/using-api/deprecation-guide/' | ||
- name: validate-v1-27-removals | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- storage.k8s.io/*/CSIStorageCapacity | ||
celPreconditions: | ||
- name: "allowed-api-versions" | ||
expression: "object.apiVersion in ['storage.k8s.io/v1beta1']" | ||
validate: | ||
cel: | ||
expressions: | ||
- expression: "false" | ||
messageExpression: >- | ||
object.apiVersion + '/' + object.kind + ' is deprecated and will be removed in v1.27. | ||
See: https://kubernetes.io/docs/reference/using-api/deprecation-guide/' | ||
- name: validate-v1-29-removals | ||
match: | ||
any: | ||
- resources: | ||
kinds: | ||
- flowcontrol.apiserver.k8s.io/*/FlowSchema | ||
- flowcontrol.apiserver.k8s.io/*/PriorityLevelConfiguration | ||
celPreconditions: | ||
- name: "object.apiVersion" | ||
expression: "object.apiVersion in ['flowcontrol.apiserver.k8s.io/v1beta2']" | ||
validate: | ||
cel: | ||
expressions: | ||
- expression: "false" | ||
messageExpression: >- | ||
object.apiVersion + '/' + object.kind + ' is deprecated and will be removed in v1.29. | ||
See: https://kubernetes.io/docs/reference/using-api/deprecation-guide/' | ||
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
6 changes: 6 additions & 0 deletions
6
windows-security/require-run-as-containeruser/.chainsaw-test/chainsaw-step-01-assert-1.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,6 @@ | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: require-run-as-containeruser | ||
status: | ||
ready: true |
37 changes: 37 additions & 0 deletions
37
windows-security/require-run-as-containeruser/.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,37 @@ | ||
# 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: require-run-as-containeruser | ||
spec: | ||
steps: | ||
- name: step-01 | ||
try: | ||
- apply: | ||
file: ../require-run-as-containeruser.yaml | ||
- patch: | ||
resource: | ||
apiVersion: kyverno.io/v1 | ||
kind: ClusterPolicy | ||
metadata: | ||
name: require-run-as-containeruser | ||
spec: | ||
validationFailureAction: Enforce | ||
- assert: | ||
file: chainsaw-step-01-assert-1.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 |
17 changes: 17 additions & 0 deletions
17
windows-security/require-run-as-containeruser/.chainsaw-test/pod-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,17 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: bad-windows-pod | ||
spec: | ||
nodeSelector: | ||
kubernetes.io/arch: amd64 | ||
kubernetes.io/os: windows | ||
securityContext: | ||
windowsOptions: | ||
hostProcess: true | ||
runAsUserName: "NT AUTHORITY\\Local service" | ||
hostNetwork: true | ||
containers: | ||
- name: windows-container | ||
image: mcr.microsoft.com/windows/servercore:ltsc2019 | ||
command: ["cmd", "/c", "echo", "Hello from Windows Container && timeout", "/t", "300"] |
18 changes: 18 additions & 0 deletions
18
windows-security/require-run-as-containeruser/.chainsaw-test/pod-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,18 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: good-windows-pod | ||
spec: | ||
nodeSelector: | ||
kubernetes.io/arch: amd64 | ||
kubernetes.io/os: windows | ||
securityContext: | ||
runAsNonRoot: true | ||
windowsOptions: | ||
hostProcess: false | ||
runAsUserName: "ContainerUser" | ||
hostNetwork: false | ||
containers: | ||
- name: windows-container | ||
image: mcr.microsoft.com/windows/servercore:ltsc2019 | ||
command: ["cmd", "/c", "echo", "Hello from Windows Container && timeout", "/t", "300"] |
26 changes: 26 additions & 0 deletions
26
windows-security/require-run-as-containeruser/.chainsaw-test/podcontroller-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,26 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: bad-windows-deployment | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: windows-app | ||
template: | ||
metadata: | ||
labels: | ||
app: windows-app | ||
spec: | ||
nodeSelector: | ||
kubernetes.io/arch: amd64 | ||
kubernetes.io/os: windows | ||
securityContext: | ||
windowsOptions: | ||
hostProcess: true | ||
runAsUserName: "NT AUTHORITY\\Local service" | ||
hostNetwork: true | ||
containers: | ||
- name: windows-container | ||
image: mcr.microsoft.com/windows/servercore:ltsc2019 | ||
command: ["cmd", "/c", "echo", "Hello from Windows Container && timeout", "/t", "300"] |
27 changes: 27 additions & 0 deletions
27
windows-security/require-run-as-containeruser/.chainsaw-test/podcontroller-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,27 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: good-windows-deployment | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: windows-app | ||
template: | ||
metadata: | ||
labels: | ||
app: windows-app | ||
spec: | ||
nodeSelector: | ||
kubernetes.io/arch: amd64 | ||
kubernetes.io/os: windows | ||
securityContext: | ||
runAsNonRoot: true | ||
windowsOptions: | ||
hostProcess: false | ||
runAsUserName: "ContainerUser" | ||
hostNetwork: false | ||
containers: | ||
- name: windows-container | ||
image: mcr.microsoft.com/windows/servercore:ltsc2019 | ||
command: ["cmd", "/c", "echo", "Hello from Windows Container && timeout", "/t", "300"] |
Oops, something went wrong.