Skip to content

Commit

Permalink
Don't deploy PSPs when PodSecurityPolicy plugin is disabled (#587)
Browse files Browse the repository at this point in the history
* Don't deploy PSPs if it's disabled in the shoot

* Add unit tests
  • Loading branch information
shafeeqes authored Aug 8, 2022
1 parent 86fc639 commit 555c277
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ rules:
- apiGroups: ["storage.k8s.io"]
resources: ["volumeattachments"]
verbs: ["get", "list", "watch", "update", "patch"]
{{- if not .Values.pspDisabled }}
- apiGroups: ["policy", "extensions"]
resourceNames: ["{{ include "csi-driver-node.extensionsGroup" . }}.{{ include "csi-driver-node.name" . }}.csi-driver-node"]
resources: ["podsecuritypolicies"]
verbs: ["use"]
{{- end }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{- if not .Values.pspDisabled }}
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
Expand Down Expand Up @@ -26,3 +27,4 @@ spec:
fsGroup:
rule: RunAsAny
readOnlyRootFilesystem: false
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ resources:
memory: 32Mi
limits:
memory: 300Mi

pspDisabled: false
2 changes: 1 addition & 1 deletion example/10-fake-shoot-controlplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ spec:
- command:
- /hyperkube
- apiserver
- --enable-admission-plugins=Priority,NamespaceLifecycle,LimitRanger,PodSecurityPolicy,ServiceAccount,NodeRestriction,DefaultStorageClass,Initializers,DefaultTolerationSeconds,ResourceQuota,StorageObjectInUseProtection,MutatingAdmissionWebhook,ValidatingAdmissionWebhook
- --enable-admission-plugins=Priority,NamespaceLifecycle,LimitRanger,ServiceAccount,NodeRestriction,DefaultStorageClass,Initializers,DefaultTolerationSeconds,ResourceQuota,StorageObjectInUseProtection,MutatingAdmissionWebhook,ValidatingAdmissionWebhook
- --disable-admission-plugins=PersistentVolumeLabel
- --allow-privileged=true
- --anonymous-auth=false
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controlplane/valuesprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func getControlPlaneShootChartValues(
"url": "https://" + aws.CSISnapshotValidation + "." + cp.Namespace + "/volumesnapshot",
"caBundle": string(caSecret.Data[secretutils.DataKeyCertificateBundle]),
},
"pspDisabled": gardencorev1beta1helper.IsPSPDisabled(cluster.Shoot),
}

if value, ok := cluster.Shoot.Annotations[aws.VolumeAttachLimit]; ok {
Expand Down
58 changes: 58 additions & 0 deletions pkg/controller/controlplane/valuesprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ var _ = Describe("ValuesProvider", func() {
"url": "https://" + aws.CSISnapshotValidation + "." + cp.Namespace + "/volumesnapshot",
"caBundle": "",
},
"pspDisabled": false,
}),
}))
})
Expand All @@ -341,6 +342,63 @@ var _ = Describe("ValuesProvider", func() {
"url": "https://" + aws.CSISnapshotValidation + "." + cp.Namespace + "/volumesnapshot",
"caBundle": "",
},
"pspDisabled": false,
}),
}))
})
})

Context("podSecurityPolicy", func() {
It("should return correct shoot control plane chart when PodSecurityPolicy admission plugin is not disabled in the shoot", func() {
clusterK8sAtLeast118.Shoot.Spec.Kubernetes.KubeAPIServer = &gardencorev1beta1.KubeAPIServerConfig{
AdmissionPlugins: []gardencorev1beta1.AdmissionPlugin{
{
Name: "PodSecurityPolicy",
},
},
}
values, err := vp.GetControlPlaneShootChartValues(ctx, cp, clusterK8sAtLeast118, fakeSecretsManager, nil)
Expect(err).NotTo(HaveOccurred())
Expect(values).To(Equal(map[string]interface{}{
aws.CloudControllerManagerName: enabledTrue,
aws.CSINodeName: utils.MergeMaps(enabledTrue, map[string]interface{}{
"kubernetesVersion": "1.18.1",
"vpaEnabled": true,
"driver": map[string]interface{}{
"volumeAttachLimit": "42",
},
"webhookConfig": map[string]interface{}{
"url": "https://" + aws.CSISnapshotValidation + "." + cp.Namespace + "/volumesnapshot",
"caBundle": "",
},
"pspDisabled": false,
}),
}))
})
It("should return correct shoot control plane chart when PodSecurityPolicy admission plugin is disabled in the shoot", func() {
clusterK8sAtLeast118.Shoot.Spec.Kubernetes.KubeAPIServer = &gardencorev1beta1.KubeAPIServerConfig{
AdmissionPlugins: []gardencorev1beta1.AdmissionPlugin{
{
Name: "PodSecurityPolicy",
Disabled: pointer.Bool(true),
},
},
}
values, err := vp.GetControlPlaneShootChartValues(ctx, cp, clusterK8sAtLeast118, fakeSecretsManager, nil)
Expect(err).NotTo(HaveOccurred())
Expect(values).To(Equal(map[string]interface{}{
aws.CloudControllerManagerName: enabledTrue,
aws.CSINodeName: utils.MergeMaps(enabledTrue, map[string]interface{}{
"kubernetesVersion": "1.18.1",
"vpaEnabled": true,
"driver": map[string]interface{}{
"volumeAttachLimit": "42",
},
"webhookConfig": map[string]interface{}{
"url": "https://" + aws.CSISnapshotValidation + "." + cp.Namespace + "/volumesnapshot",
"caBundle": "",
},
"pspDisabled": true,
}),
}))
})
Expand Down

0 comments on commit 555c277

Please sign in to comment.