Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validating admission policy to restrict upstream driver access #482

Merged
merged 25 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .github/workflows/helm-chart-ci-ignore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ jobs:
strategy:
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8

steps:
- run: 'echo "Skipping tests"'
Expand Down Expand Up @@ -74,9 +74,9 @@ jobs:
strategy:
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8
example:
- ${{ fromJson(needs.build-matrix.outputs.examples) }}

Expand All @@ -92,9 +92,9 @@ jobs:
strategy:
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8
example:
- ${{ fromJson(needs.build-matrix.outputs.integrationtests) }}

Expand All @@ -110,9 +110,9 @@ jobs:
strategy:
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8

steps:
- run: 'echo "Skipping upgrade-test"'
35 changes: 18 additions & 17 deletions .github/workflows/helm-chart-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ concurrency:
cancel-in-progress: true

env:
HELM_VERSION: v3.12.0
HELM_VERSION: v3.16.2
PYTHON_VERSION: 3.11.3
KIND_VERSION: v0.19.0
KIND_VERSION: v0.24.0
CHART_TESTING_VERSION: v3.8.0

jobs:
Expand Down Expand Up @@ -130,9 +130,9 @@ jobs:
# Kubernetes, but can go back farther as long as we don't need heroics
# to pull it off (i.e. kubectl version juggling).
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8

steps:
- name: Checkout
Expand Down Expand Up @@ -218,9 +218,9 @@ jobs:
fail-fast: false
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8
example:
- ${{ fromJson(needs.build-matrix.outputs.examples) }}

Expand All @@ -243,7 +243,7 @@ jobs:
# Only build a kind cluster if there are chart changes to test.
with:
version: ${{ env.KIND_VERSION }}
node_image: kindest/node:v1.26.4
node_image: kindest/node:${{ matrix.k8s }}
config: .github/kind/conf/kind-config.yaml
verbosity: 1

Expand All @@ -256,6 +256,7 @@ jobs:
kubectl create namespace spire-server
helm install -n spire-server spire-crds charts/spire-crds
fi
export K8S="${{ matrix.k8s }}"
${{ matrix.example }}/run-tests.sh

integration-test:
Expand All @@ -269,9 +270,9 @@ jobs:
fail-fast: false
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8
integrationtest:
- ${{ fromJson(needs.build-matrix.outputs.integrationtests) }}

Expand All @@ -294,7 +295,7 @@ jobs:
# Only build a kind cluster if there are chart changes to test.
with:
version: ${{ env.KIND_VERSION }}
node_image: kindest/node:v1.26.4
node_image: kindest/node:${{ matrix.k8s }}
config: .github/kind/conf/kind-config.yaml
verbosity: 1

Expand All @@ -314,9 +315,9 @@ jobs:
fail-fast: false
matrix:
k8s:
- v1.28.0
- v1.27.3
- v1.26.6
- v1.31.1
- v1.30.4
- v1.29.8

steps:
- name: Checkout
Expand All @@ -337,7 +338,7 @@ jobs:
# Only build a kind cluster if there are chart changes to test.
with:
version: ${{ env.KIND_VERSION }}
node_image: kindest/node:v1.26.4
node_image: kindest/node:${{ matrix.k8s }}
config: .github/kind/conf/kind-config.yaml
verbosity: 1

Expand Down
94 changes: 48 additions & 46 deletions charts/spire/charts/spiffe-csi-driver/README.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions charts/spire/charts/spiffe-csi-driver/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/}}
Expand Down
37 changes: 37 additions & 0 deletions charts/spire/charts/spiffe-csi-driver/templates/policy.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
7 changes: 7 additions & 0 deletions charts/spire/charts/spiffe-csi-driver/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ nameOverride: ""
## @param namespaceOverride Namespace to install spiffe-csi-driver
namespaceOverride: ""

## @param serverNamespaceOverride Override the namespace that the spire-server is installed into
serverNamespaceOverride: ""

validatingAdmissionPolicy:
## @param validatingAdmissionPolicy.enabled When set to auto, the validatingAdmissionPolicy will be enabled when the pluginName == "upstream.csi.spiffe.io" and k8s >= 1.30.0. Valid options are [auto, true, false]
enabled: auto

## @param fullnameOverride Full name override for spiffe-csi-driver
fullnameOverride: ""

Expand Down
1 change: 0 additions & 1 deletion examples/nested-full/child-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

tags:
nestedChildFull: true

4 changes: 3 additions & 1 deletion examples/nested-full/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ kubectl rollout status -n kube-system -w --timeout=1m deploy/coredns
for cluster in child other; do
KC="${SCRIPTPATH}/kubeconfig-${cluster}"

kind create cluster --name "${cluster}" --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}" --config "${SCRIPTPATH}/.test-files/${cluster}-kind-config.yaml"
kind create cluster --name "${cluster}" --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}" --config "${SCRIPTPATH}/.test-files/${cluster}-kind-config.yaml" --image "kindest/node:${K8S}"

kubectl version --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}"
md5sum "${KC}"
wc -l "${KC}"

Expand Down
3 changes: 2 additions & 1 deletion examples/nested-security/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ kubectl rollout status -n kube-system -w --timeout=1m deploy/coredns
for cluster in child; do
KC="${SCRIPTPATH}/kubeconfig-${cluster}"

kind create cluster --name "${cluster}" --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}" --config "${SCRIPTPATH}/.test-files/${cluster}-kind-config.yaml"
kind create cluster --name "${cluster}" --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}" --config "${SCRIPTPATH}/.test-files/${cluster}-kind-config.yaml" --image "kindest/node:${K8S}"
md5sum "${KC}"
wc -l "${KC}"

Expand Down Expand Up @@ -102,6 +102,7 @@ helm upgrade --install --create-namespace --namespace spire-mgmt --values "${COM
# The check is being too pedantic.
# shellcheck shell=bash disable=SC2043
for cluster in child; do
kubectl version --kubeconfig "${SCRIPTPATH}/kubeconfig-${cluster}"
KC="${SCRIPTPATH}/kubeconfig-${cluster}"
kubectl --kubeconfig "${KC}" get configmap -n spire-system spire-bundle-upstream -o yaml
kubectl --kubeconfig "${KC}" rollout restart daemonset spire-agent-downstream -n spire-system
Expand Down
Loading