Add validating admission policy to restrict upstream driver access (#482)

* Add validating admission policy to restrict upstream driver access

Signed-off-by: Kevin Fox <[email protected]>

* Update charts/spire/charts/spiffe-csi-driver/templates/policy.yaml

Signed-off-by: kfox1111 <[email protected]>

* Fix logic

Signed-off-by: Kevin Fox <[email protected]>

* Upgrade helm

Signed-off-by: Kevin Fox <[email protected]>

* Upgrade kind and fix node version

Signed-off-by: Kevin Fox <[email protected]>

* Upgrade kind and fix node version

Signed-off-by: Kevin Fox <[email protected]>

* Upgrade kind and fix node version

Signed-off-by: Kevin Fox <[email protected]>

* Upgrade kind and fix node version

Signed-off-by: Kevin Fox <[email protected]>

* Add version info

Signed-off-by: Kevin Fox <[email protected]>

* Add version info

Signed-off-by: Kevin Fox <[email protected]>

* Fix kind version

Signed-off-by: Kevin Fox <[email protected]>

* Fix kind version

Signed-off-by: Kevin Fox <[email protected]>

* Fix kind version

Signed-off-by: Kevin Fox <[email protected]>

* Fix detection

Signed-off-by: Kevin Fox <[email protected]>

* Just for testing...

Signed-off-by: Kevin Fox <[email protected]>

* Just for testing...

Signed-off-by: Kevin Fox <[email protected]>

* Just for testing

Signed-off-by: Kevin Fox <[email protected]>

* Fix policy

Signed-off-by: Kevin Fox <[email protected]>

* Fix policy

Signed-off-by: Kevin Fox <[email protected]>

* Fix policy

Signed-off-by: Kevin Fox <[email protected]>

* Incorperate feedback

Signed-off-by: Kevin Fox <[email protected]>

* Update charts/spire/charts/spiffe-csi-driver/values.yaml

Co-authored-by: Faisal Memon <[email protected]>
Signed-off-by: kfox1111 <[email protected]>

* Update docs

Signed-off-by: Kevin Fox <[email protected]>

---------

Signed-off-by: Kevin Fox <[email protected]>
Signed-off-by: kfox1111 <[email protected]>
Co-authored-by: Faisal Memon <[email protected]>
This commit is contained in:
kfox1111
2024-10-26 07:40:29 -07:00
committed by GitHub
co-authored by Faisal Memon
parent a7368eee1f
commit a360646842
9 changed files with 144 additions and 78 deletions
@@ -40,6 +40,23 @@ Allow the release namespace to be overridden for multi-namespace deployments in
{{- end -}}
{{- end -}}
{{/*
Allow the release namespace to be overridden for multi-namespace deployments in combined charts
*/}}
{{- define "spiffe-csi-driver.server-namespace" -}}
{{- if .Values.serverNamespaceOverride -}}
{{- .Values.serverNamespaceOverride -}}
{{- else if and (dig "spire" "recommendations" "enabled" false .Values.global) (dig "spire" "recommendations" "namespaceLayout" true .Values.global) }}
{{- if ne (len (dig "spire" "namespaces" "server" "name" "" .Values.global)) 0 }}
{{- .Values.global.spire.namespaces.server.name }}
{{- else }}
{{- printf "spire-server" }}
{{- end }}
{{- else -}}
{{- .Release.Namespace -}}
{{- end -}}
{{- end -}}
{{/*
Create chart name and version as used by the chart label.
*/}}
@@ -0,0 +1,37 @@
{{- $upstream := eq .Values.pluginName "upstream.csi.spiffe.io" }}
{{- $detectedValidation := semverCompare ">=1.30-0" .Capabilities.KubeVersion.GitVersion -}}
{{- $policyEnabled := .Values.validatingAdmissionPolicy.enabled | toString }}
{{- $auto := eq $policyEnabled "auto" }}
{{- if or (eq $policyEnabled "true") (and $auto $upstream $detectedValidation) }}
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: {{ .Values.pluginName | quote }}
spec:
failurePolicy: Fail
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
validations:
- expression: |
!object.spec.volumes.exists(c, has(c.csi) && has(c.csi.driver) && c.csi.driver == {{ .Values.pluginName | quote }})
message: 'you may not use the upstream.csi.spiffe.io csi driver'
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
name: {{ .Values.pluginName | quote }}
spec:
policyName: {{ .Values.pluginName | quote }}
validationActions: ["Deny"]
matchResources:
namespaceSelector:
matchExpressions:
- key: "kubernetes.io/metadata.name"
operator: NotIn
values:
- {{ include "spiffe-csi-driver.server-namespace" . | quote }}
{{- end }}