Skip to content

Commit

Permalink
Merge pull request #83 from nirmata/NDEV-16720-helm-customize
Browse files Browse the repository at this point in the history
parameterize kyverno-policies helm charts
  • Loading branch information
anusha94 authored Jul 4, 2023
2 parents db6aeea + 71f1d54 commit 9837721
Show file tree
Hide file tree
Showing 70 changed files with 369 additions and 176 deletions.
2 changes: 1 addition & 1 deletion charts/best-practices-k8s/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: kubernetes-best-practice-policies
description: Kubernetes Best Practice policy set
type: application
version: 0.2.0
version: 0.2.1
appVersion: 0.1.0
keywords:
- kubernetes
Expand Down
30 changes: 30 additions & 0 deletions charts/best-practices-k8s/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{/*
Validation failure action for policy or default validation failure action
*/}}

{{- define "vfa-for-pol" -}}

{{- with index .Values "validationFailureActionByPolicy" .polname }}
{{- toYaml . }}
{{- else }}
{{- .Values.validationFailureAction }}
{{- end }}

{{- end}}


{{/*
Background scan for policy or default background value
*/}}
{{- define "bgscan-for-pol" -}}
{{- $flagStr := lower (toString (index .Values "backgroundByPolicy" .polname)) }}

{{- if eq "false" $flagStr }}
{{- false }}
{{- else if eq "true" $flagStr }}
{{- true }}
{{- else }}
{{- .Values.background }}
{{- end }}

{{- end}}
4 changes: 0 additions & 4 deletions charts/best-practices-k8s/templates/club-pols.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "disallow-container-sock-mounts" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-container-sock-mounts
name: {{ $name }}
annotations:
policies.kyverno.io/title: Disallow CRI socket mounts
policies.kyverno.io/category: Best Practices, EKS Best Practices
Expand All @@ -14,8 +15,8 @@ metadata:
outside of Kubernetes, and hence should not be allowed. This policy validates that
the sockets used for CRI engines Docker, Containerd, and CRI-O are not used.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-docker-sock-mount
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "disallow-default-namespace" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-default-namespace
name: {{ $name }}
annotations:
pod-policies.kyverno.io/autogen-controllers: none
policies.kyverno.io/title: Disallow Default Namespace
Expand All @@ -18,8 +19,8 @@ metadata:
due to Pod controllers need to specify the `namespace` field under the top-level `metadata`
object and not at the Pod template level.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-namespace
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "disallow-empty-ingress-host" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-empty-ingress-host
name: {{ $name }}
annotations:
policies.kyverno.io/title: Disallow empty Ingress host
policies.kyverno.io/category: Best Practices
Expand All @@ -12,8 +13,8 @@ metadata:
in order to be valid. This policy ensures that there is a
hostname for each rule defined.
spec:
validationFailureAction: audit
background: false
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: disallow-empty-ingress-host
match:
Expand All @@ -24,6 +25,6 @@ spec:
message: "The Ingress host name must be defined, not empty."
deny:
conditions:
- key: "{{ request.object.spec.rules[].host || `[]` | length(@) }}"
- key: "{{`{{`}} request.object.spec.rules[].host || `[]` | length(@) {{`}}`}}"
operator: NotEquals
value: "{{ request.object.spec.rules[].http || `[]` | length(@) }}"
value: "{{`{{`}} request.object.spec.rules[].http || `[]` | length(@) {{`}}`}}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "disallow-latest-tag" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: disallow-latest-tag
name: {{ $name }}
annotations:
policies.kyverno.io/title: Disallow Latest Tag
policies.kyverno.io/category: Best Practices
Expand All @@ -13,8 +14,8 @@ metadata:
a specific version of an application Pod. This policy validates that the image
specifies a tag and that it is not called `latest`.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: require-image-tag
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "drop-all-capabilities" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: drop-all-capabilities
name: {{ $name }}
annotations:
policies.kyverno.io/title: Drop All Capabilities
policies.kyverno.io/category: Best Practices
Expand All @@ -15,8 +16,8 @@ metadata:
ability. Note that this policy also illustrates how to cover drop entries in any
case although this may not strictly conform to the Pod Security Standards.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: require-drop-all
match:
Expand All @@ -26,7 +27,7 @@ spec:
- Pod
preconditions:
all:
- key: "{{ request.operation || 'BACKGROUND' }}"
- key: "{{`{{`}} request.operation || 'BACKGROUND' {{`}}`}}"
operator: NotEquals
value: DELETE
validate:
Expand All @@ -39,4 +40,4 @@ spec:
all:
- key: ALL
operator: AnyNotIn
value: "{{ element.securityContext.capabilities.drop[].to_upper(@) || `[]` }}"
value: "{{`{{`}} element.securityContext.capabilities.drop[].to_upper(@) || `[]` {{`}}`}}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "drop-cap-net-raw" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: drop-cap-net-raw
name: {{ $name }}
annotations:
policies.kyverno.io/title: Drop CAP_NET_RAW
policies.kyverno.io/category: Best Practices
Expand All @@ -16,8 +17,8 @@ metadata:
ability. Note that this policy also illustrates how to cover drop entries in any
case although this may not strictly conform to the Pod Security Standards.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: require-drop-cap-net-raw
match:
Expand All @@ -27,7 +28,7 @@ spec:
- Pod
preconditions:
all:
- key: "{{ request.operation || 'BACKGROUND' }}"
- key: "{{`{{`}} request.operation || 'BACKGROUND' {{`}}`}}"
operator: NotEquals
value: DELETE
validate:
Expand All @@ -40,4 +41,4 @@ spec:
all:
- key: CAP_NET_RAW
operator: AnyNotIn
value: "{{ element.securityContext.capabilities.drop[].to_upper(@) || `[]` }}"
value: "{{`{{`}} element.securityContext.capabilities.drop[].to_upper(@) || `[]` {{`}}`}}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "require-labels" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-labels
name: {{ $name }}
annotations:
policies.kyverno.io/title: Require Labels
policies.kyverno.io/category: Best Practices
Expand All @@ -13,8 +14,8 @@ metadata:
all tools can understand. The recommended labels describe applications in a way that can be
queried. This policy validates that the label `app.kubernetes.io/name` is specified with some value.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: check-for-labels
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "require-requests-limits" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-requests-limits
name: {{ $name }}
annotations:
policies.kyverno.io/title: Require Limits and Requests
policies.kyverno.io/category: Best Practices, EKS Best Practices
Expand All @@ -16,8 +17,8 @@ metadata:
This policy validates that all containers have something specified for memory and CPU
requests and memory limits.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-resources
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "require-pod-probes" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-pod-probes
name: {{ $name }}
annotations:
pod-policies.kyverno.io/autogen-controllers: DaemonSet,Deployment,StatefulSet
policies.kyverno.io/title: Require Pod Probes
Expand All @@ -17,8 +18,8 @@ metadata:
This policy validates that all containers have one of livenessProbe, readinessProbe,
or startupProbe defined.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-probes
match:
Expand All @@ -35,10 +36,10 @@ spec:
all:
- key: livenessProbe
operator: AllNotIn
value: "{{ element.keys(@)[] }}"
value: "{{`{{`}} element.keys(@)[] {{`}}`}}"
- key: startupProbe
operator: AllNotIn
value: "{{ element.keys(@)[] }}"
value: "{{`{{`}} element.keys(@)[] {{`}}`}}"
- key: readinessProbe
operator: AllNotIn
value: "{{ element.keys(@)[] }}"
value: "{{`{{`}} element.keys(@)[] {{`}}`}}"
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "require-ro-rootfs" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-ro-rootfs
name: {{ $name }}
annotations:
policies.kyverno.io/title: Require Read-Only Root Filesystem
policies.kyverno.io/category: Best Practices, EKS Best Practices
Expand All @@ -15,8 +16,8 @@ metadata:
host system. This policy validates that containers define a securityContext
with `readOnlyRootFilesystem: true`.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-readOnlyRootFilesystem
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "restrict-external-ips" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-external-ips
name: {{ $name }}
annotations:
policies.kyverno.io/title: Restrict External IPs
policies.kyverno.io/category: Best Practices
Expand All @@ -13,8 +14,8 @@ metadata:
See: https://github.com/kyverno/kyverno/issues/1367. This policy validates
that the `externalIPs` field is not set on a Service.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: check-ips
match:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{- $name := "restrict-nodeport" }}
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: restrict-nodeport
name: {{ $name }}
annotations:
policies.kyverno.io/title: Disallow NodePort
policies.kyverno.io/category: Best Practices
Expand All @@ -14,8 +15,8 @@ metadata:
with additional upstream security checks. This policy validates that any new Services
do not use the `NodePort` type.
spec:
validationFailureAction: audit
background: true
validationFailureAction: {{ include "vfa-for-pol" (dict "Values" .Values "polname" $name) }}
background: {{ include "bgscan-for-pol" (dict "Values" .Values "polname" $name) }}
rules:
- name: validate-nodeport
match:
Expand Down
15 changes: 15 additions & 0 deletions charts/best-practices-k8s/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -- Policies background mode
background: true

# -- Default validationFailureAction
validationFailureAction: audit

# -- Define background for specific policies.
# Override the defined `background` with a individual background for individual Policies.
backgroundByPolicy: {}
# require-labels: false

# -- Define validationFailureActionByPolicy for specific policies.
# Override the defined `validationFailureAction` with a individual validationFailureAction for individual Policies.
validationFailureActionByPolicy: {}
# require-labels: enforce
2 changes: 1 addition & 1 deletion charts/best-practices-workload-security/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
name: workload-security-best-practice-policies
description: Workload Security Best Practice policy set
type: application
version: 0.1.2
version: 0.1.3
appVersion: 0.1.0
keywords:
- kubernetes
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ spec:
message: "The Ingress host name must be defined, not empty."
deny:
conditions:
- key: "{{ request.object.spec.rules[].host || `[]` | length(@) }}"
- key: "{{`{{`}} request.object.spec.rules[].host || `[]` | length(@) {{`}}`}}"
operator: NotEquals
value: "{{ request.object.spec.rules[].http || `[]` | length(@) }}"
value: "{{`{{`}} request.object.spec.rules[].http || `[]` | length(@) {{`}}`}}"
Loading

0 comments on commit 9837721

Please sign in to comment.