Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 58af17f

Browse files
committed
add restrict-volume-types policy
Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
1 parent fb5c1ca commit 58af17f

File tree

14 files changed

+2801
-0
lines changed

14 files changed

+2801
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
SPDX-FileCopyrightText: The vap-collection Authors
3+
SPDX-License-Identifier: Apache-2.0
4+
-->
5+
6+
# restrict-volume-types
7+
8+
Verifies that pods only specify volumes using the following types:
9+
10+
- `configMap`
11+
- `csi`
12+
- `downwardAPI`
13+
- `emptyDir`
14+
- `ephemeral`
15+
- `persistentVolumeClaim`
16+
- `projected`
17+
- `secret`
18+
19+
Use the following query to list all pods in your cluster along with their current volume type usage:
20+
21+
```shell
22+
kubectl get pods --all-namespaces --output yaml | yq '.items[] | select(.spec.volumes[] | (has("configMap") | not and has("csi") | not and has("downwardAPI") | not and has("emptyDir") | not and has("ephemeral") | not and has("persistentVolumeClaim") | not and has("projected") | not and has("secret") | not)) | .metadata.namespace + "/" + .metadata.name'
23+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: The vap-collection Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
apiVersion: admissionregistration.k8s.io/v1
4+
kind: ValidatingAdmissionPolicyBinding
5+
metadata:
6+
name: restrict-volume-types
7+
labels:
8+
app.kubernetes.io/name: restrict-volume-types
9+
app.kubernetes.io/component: pod-security-standards-baseline
10+
spec:
11+
policyName: restrict-volume-types
12+
validationActions: [Deny]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: The vap-collection Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
apiVersion: kustomize.config.k8s.io/v1alpha1
4+
kind: Component
5+
resources:
6+
- binding.yaml
7+
- policy.yaml
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SPDX-FileCopyrightText: The vap-collection Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
apiVersion: admissionregistration.k8s.io/v1
4+
kind: ValidatingAdmissionPolicy
5+
metadata:
6+
name: restrict-volume-types
7+
labels:
8+
app.kubernetes.io/name: restrict-volume-types
9+
app.kubernetes.io/component: pod-security-standards-baseline
10+
spec:
11+
failurePolicy: Fail
12+
matchConstraints:
13+
resourceRules:
14+
- apiGroups: [""]
15+
apiVersions: ["v1"]
16+
operations: ["CREATE", "UPDATE"]
17+
resources: ["pods"]
18+
scope: "Namespaced"
19+
- apiGroups: ["apps"]
20+
apiVersions: ["v1"]
21+
operations: ["CREATE", "UPDATE"]
22+
resources: ["daemonsets", "deployments", "statefulsets"]
23+
scope: "Namespaced"
24+
- apiGroups: ["batch"]
25+
apiVersions: ["v1"]
26+
operations: ["CREATE", "UPDATE"]
27+
resources: ["cronjobs", "jobs"]
28+
scope: "Namespaced"
29+
variables:
30+
- expression: object.spec.?template.?spec.?volumes.orValue([])
31+
name: controllerVolumes
32+
- expression: object.spec.?jobTemplate.?spec.?template.?spec.?volumes.orValue([])
33+
name: cronJobVolumes
34+
- expression: object.spec.?volumes.orValue([])
35+
name: podVolumes
36+
- expression: variables.controllerVolumes + variables.cronJobVolumes + variables.podVolumes
37+
name: allUsedVolumes
38+
validations:
39+
- expression:
40+
variables.allUsedVolumes.all(volume,
41+
has(volume.configMap) ||
42+
has(volume.csi) ||
43+
has(volume.downwardAPI) ||
44+
has(volume.emptyDir) ||
45+
has(volume.ephemeral) ||
46+
has(volume.persistentVolumeClaim) ||
47+
has(volume.projected) ||
48+
has(volume.secret))
49+
message: 'Only the following types of volumes may be used: configMap, csi, downwardAPI, emptyDir, ephemeral, persistentVolumeClaim, projected, and secret.'
50+
reason: Invalid
51+
auditAnnotations:
52+
- key: name
53+
valueExpression: string(object.metadata.name)

0 commit comments

Comments
 (0)