Files
helm-charts-hardened/charts/spire/charts/spire-agent/templates/daemonset.yaml
T

454 lines
20 KiB
YAML

{{- $configSum := (include (print $.Template.BasePath "/configmap.yaml") . | sha256sum) }}
{{- $root := . }}
{{- if hasKey .Values.nodeAttestor "k8sPsat" }}
{{- fail "k8sPsat was renamed to k8sPSAT. Please update your config." }}
{{- end }}
{{- if hasKey .Values.sds "defaultSvidName" }}
{{- fail "defaultSvidName was renamed to defaultSVIDName. Please update your config." }}
{{- end }}
{{- if hasKey .Values.sds "disableSpiffeCertValidation" }}
{{- fail "disableSpiffeCertValidation was renamed to disableSPIFFECertValidation. Please update your config." }}
{{- end }}
{{- if and .Values.keyManager.disk.enabled (ne .Values.persistence.type "hostPath") }}
{{- fail "keyManager.disk.enabled is true but persistence.type is not hostPath. Ensure persistence.type is hostPath when keyManager.disk.enabled is true." }}
{{- end }}
{{- range $name := (concat (list "default") (keys .Values.agents)) | uniq }}
{{- with (dict "Release" $root.Release "Chart" $root.Chart "Values" (deepCopy $root.Values)) }}
{{- $nameSuffix := "" }}
{{- if ne $name "default" }}
{{- $nameSuffix = printf "-%s" $name }}
{{- end }}
{{- if hasKey $root.Values.agents $name }}
{{- $_ := set . "Values" (mergeOverwrite .Values (index $root.Values.agents $name)) }}
{{- end }}
{{- $podSecurityContext := fromYaml (include "spire-lib.podsecuritycontext" .) }}
{{- $mainSecurityContext := deepCopy .Values.securityContext }}
{{- if .Values.nodeAttestor.tpmDirect.enabled }}
{{- $podSecurityContext = mergeOverwrite (dict "runAsUser" 0 "runAsGroup" 0) .Values.podSecurityContext }}
{{- $_ := set $mainSecurityContext "privileged" true }}
{{- end }}
{{- $resolvedMode := include "spire-agent.kubelet-address-mode-resolved" . }}
{{- $cbh := or (eq $resolvedMode "hostname") (eq $resolvedMode "hostip") }}
{{- $hostNetwork := not $cbh }}
{{- $hostNetworkValue := .Values.hostNetwork | toString }}
{{- if and (ne $hostNetworkValue "") (ne $hostNetworkValue "auto") }}
{{- $hostNetwork = eq $hostNetworkValue "true" }}
{{- end }}
{{- $dnsPolicy := .Values.dnsPolicy }}
{{- if (and (eq $dnsPolicy "") $hostNetwork) }}
{{- $dnsPolicy = "ClusterFirstWithHostNet" }}
{{- end }}
{{- $socketAlternateNames := index (include "spire-agent.socket-alternate-names" . | fromYaml) "names" }}
{{- $socketPath := include "spire-agent.socket-path" . }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: {{ printf "%s%s" (include "spire-agent.fullname" .) $nameSuffix | quote }}
namespace: {{ include "spire-agent.namespace" . | quote}}
labels:
{{- include "spire-agent.labels" . | nindent 4 }}
app.kubernetes.io/component: {{ $name | quote }}
spec:
selector:
matchLabels:
{{- include "spire-agent.selectorLabels" . | nindent 6 }}
app.kubernetes.io/component: {{ $name | quote }}
{{- with .Values.updateStrategy }}
updateStrategy:
{{- if not (has .type (list "RollingUpdate" "OnDelete")) }}
{{- fail "updateStrategy.type can only be RollingUpdate or OnDelete"}}
{{- end }}
type: {{ .type }}
{{- if eq .type "RollingUpdate" }}
rollingUpdate:
maxUnavailable: {{ .rollingUpdate.maxUnavailable }}
{{- end }}
{{- end }}
template:
metadata:
annotations:
kubectl.kubernetes.io/default-container: spire-agent
checksum/config: {{ $configSum | quote }}
{{- with .Values.podAnnotations }}
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "spire-agent.selectorLabels" . | nindent 8 }}
app.kubernetes.io/component: {{ $name | quote }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- with (coalesce .Values.imagePullSecrets .Values.global.imagePullSecrets) }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
hostPID: true
{{- if $hostNetwork }}
hostNetwork: true
{{- end }}
{{- if ne $dnsPolicy "" }}
dnsPolicy: {{ $dnsPolicy }}
{{- end }}
serviceAccountName: {{ include "spire-agent.serviceAccountName" . | quote }}
securityContext:
{{- toYaml $podSecurityContext | nindent 8 }}
{{- include "spire-lib.default_node_priority_class_name" . | nindent 6 }}
{{- if ne (len .Values.hostAliases) 0 }}
hostAliases:
{{- toYaml .Values.hostAliases | nindent 8 }}
{{- end }}
initContainers:
{{- if or (eq .Values.workloadAttestors.k8s.verification.type "hostCert") (eq .Values.workloadAttestors.k8s.verification.type "auto") }}
- name: gather-host-cert
securityContext:
{{- $mainSecurityContext | toYaml | nindent 12 }}
image: {{ template "spire-lib.image" (dict "image" .Values.hostCert.image "global" .Values.global) }}
imagePullPolicy: {{ .Values.hostCert.image.pullPolicy | quote }}
command: ["bash", "-xc"]
args:
- |
{{- if ne .Values.workloadAttestors.k8s.verification.hostCert.fileName "" }}
openssl x509 -in {{ printf "%s/%s" .Values.workloadAttestors.k8s.verification.hostCert.basePath .Values.workloadAttestors.k8s.verification.hostCert.fileName | quote }} -out /hostCert/kubelet.crt
{{- else }}
if [ -f "{{ .Values.workloadAttestors.k8s.verification.hostCert.basePath }}/kubelet-server-current.pem" ]; then
openssl x509 -in {{ printf "%s/kubelet-server-current.pem" .Values.workloadAttestors.k8s.verification.hostCert.basePath | quote }} -out /hostCert/kubelet.crt
elif [ -f "{{ .Values.workloadAttestors.k8s.verification.hostCert.basePath }}/kubelet.crt" ]; then
openssl x509 -in {{ printf "%s/kubelet.crt" .Values.workloadAttestors.k8s.verification.hostCert.basePath | quote }} -out /hostCert/kubelet.crt
else
{{- if eq .Values.workloadAttestors.k8s.verification.type "auto" }}
{{- if $cbh }}
URL="https://$KUBELET_ADDR:10250/spec/"
{{- else if eq $resolvedMode "custom" }}
if [ -z "$KUBELET_ADDR" ]; then
echo "ERROR: kubeletAddress.mode=custom requires KUBELET_ADDR environment variable to be set via extraEnvVars"
exit 1
fi
URL="https://$KUBELET_ADDR:10250/spec/"
{{- else }}
URL="https://localhost:10250/spec/"
{{- end }}
curl --capath /var/run/secrets/kubernetes.io/serviceaccount/ca.crt "$URL"
if [ $? -eq 0 ]; then
echo Mode detected as apiServerCA.
ln -s /var/run/secrets/kubernetes.io/serviceaccount/ca.crt /hostCert/kubelet.crt
exit 0
fi
{{- end }}
echo Could not find certificate.
exit 1
fi
{{- end }}
{{- if eq .Values.workloadAttestors.k8s.verification.type "auto" }}
echo Mode detected as hostCert.
{{- end }}
chmod 644 /hostCert/kubelet.crt
resources:
{{- toYaml .Values.resources | nindent 12 }}
env:
{{- if $cbh }}
- name: KUBELET_ADDR
valueFrom:
fieldRef:
{{- if eq $resolvedMode "hostname" }}
fieldPath: spec.nodeName
{{- else if eq $resolvedMode "hostip" }}
fieldPath: status.hostIP
{{- end }}
{{- end }}
{{- with .Values.extraEnvVars }}
{{- toYaml . | nindent 12 }}
{{- end }}
volumeMounts:
- name: host-cert-isolated
mountPath: /hostCert
- name: host-cert
mountPath: {{ .Values.workloadAttestors.k8s.verification.hostCert.basePath | quote }}
readOnly: true
{{- end }}
{{- if .Values.nodeAttestor.tpmDirect.enabled }}
{{- if .Values.nodeAttestor.tpmDirect.pubHash.enabled }}
- name: fingerprint-tpm
securityContext:
{{- $mainSecurityContext | toYaml | nindent 12 }}
image: {{ template "spire-lib.image" (dict "appVersion" $.Chart.AppVersion "image" .Values.nodeAttestor.tpmDirect.pubHash.image "global" .Values.global) }}
command:
- sh
- -c
- |
# Fetch the TPM's fingerprint from the TPM and output it on the Computers Console. This enables secure bootstrapping of the Node with the Server.
TPM=`/app/get_tpm_pubhash 2> /dev/null`
if [ $? -eq 0 ]; then
echo "TPM Fingerprint: $TPM"
(echo -n '<1>'; echo "TPM Fingerprint: $TPM") | tee /dev/kmsg
else
/app/get_tpm_pubhash
fi
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: tpm
mountPath: /dev/tpmrm0
readOnly: true
- name: kmsg
mountPath: /dev/kmsg
imagePullPolicy: {{ .Values.nodeAttestor.tpmDirect.pubHash.image.pullPolicy | quote }}
{{- end }}
- name: init-tpm-direct
securityContext:
{{- .Values.securityContext | toYaml | nindent 12 }}
image: {{ template "spire-lib.image" (dict "appVersion" $.Chart.AppVersion "image" .Values.nodeAttestor.tpmDirect.plugin.image "global" .Values.global) }}
command:
- sh
- -ec
- |
# SPIRE must be able to fork the plugin directly within its container. Copy the plugin into a volume that can be mounted where SPIRE can execute it.
cp -a {{ .Values.nodeAttestor.tpmDirect.plugin.path | quote }} /tpm/tpm_attestor_agent
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: tpm-direct
mountPath: /tpm
imagePullPolicy: {{ .Values.nodeAttestor.tpmDirect.plugin.image.pullPolicy | quote }}
{{- end }}
{{- if $socketAlternateNames }}
- name: ensure-alternate-names
image: {{ template "spire-lib.image" (dict "image" .Values.socketAlternate.image "global" .Values.global) }}
imagePullPolicy: {{ .Values.socketAlternate.image.pullPolicy | quote }}
command: ["bash", "-xc"]
{{- /* 1. Look for symlinks pointing at the wrong place and remove them. 2. Make symlinks that don't exist. 3. If new socket is pointing at an existing symlink, remove old symlink. */}}
args:
- |
cd {{ $socketPath | dir }}
{{- range $socketAlternateNames }}
L=`readlink {{ . }}`
[ "x$L" != "x{{ $socketPath | base }}" ] && rm -f {{ . }}
[ ! -L {{ . }} ] && ln -s {{ $socketPath | base }} {{ . }}
{{- end }}
[ -L {{ $socketPath | base }} ] && rm -f {{ $socketPath | base }}
exit 0
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: spire-agent-socket-dir
mountPath: {{ $socketPath | dir }}
securityContext:
runAsUser: 0
runAsGroup: 0
{{- end }}
{{- if gt (int (dig "fsGroup" 0 $podSecurityContext)) 0 }}
- name: fsgroupfix
image: {{ template "spire-lib.image" (dict "image" .Values.fsGroupFix.image "global" .Values.global) }}
imagePullPolicy: {{ .Values.fsGroupFix.image.pullPolicy | quote }}
command: ["bash", "-c"]
args:
- |
chown -R {{ printf "%v:%v" $podSecurityContext.runAsUser $podSecurityContext.fsGroup | quote }} {{ $socketPath | dir }} /tmp/spire-agent/private
chown -R {{ printf "%v:%v" $podSecurityContext.runAsUser $podSecurityContext.fsGroup | quote }} /var/lib/spire
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: spire-agent-socket-dir
mountPath: {{ $socketPath | dir }}
- name: spire-agent-persistence
mountPath: /var/lib/spire
- name: spire-agent-admin-socket-dir
mountPath: /tmp/spire-agent/private
securityContext:
runAsUser: 0
runAsGroup: 0
{{- end }}
{{- if gt (len .Values.initContainers) 0 }}
{{- toYaml .Values.initContainers | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name | quote }}
image: {{ template "spire-lib.image" (dict "appVersion" $.Chart.AppVersion "image" .Values.image "global" .Values.global) }}
imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
args: ["-config", "/opt/spire/conf/agent/agent.conf"]
securityContext:
{{- $mainSecurityContext | toYaml | nindent 12 }}
env:
- name: PATH
value: "/opt/spire/bin:/bin"
{{- if eq $resolvedMode "hostname" }}
- name: KUBELET_ADDR
valueFrom:
fieldRef:
fieldPath: spec.nodeName
{{- else if eq $resolvedMode "hostip" }}
- name: KUBELET_ADDR
valueFrom:
fieldRef:
fieldPath: status.hostIP
{{- end }}
{{- /* Note: For localhost and custom modes, KUBELET_ADDR is not set here */ -}}
{{- /* For custom mode, user sets KUBELET_ADDR via extraEnvVars */ -}}
{{- with .Values.extraEnvVars }}
{{- toYaml . | nindent 12 }}
{{- end }}
ports:
- containerPort: {{ .Values.healthChecks.port }}
name: healthz
{{- if or (dig "telemetry" "prometheus" "enabled" .Values.telemetry.prometheus.enabled .Values.global) (and (dig "spire" "recommendations" "enabled" false .Values.global) (dig "spire" "recommendations" "prometheus" true .Values.global)) }}
- containerPort: {{ .Values.telemetry.prometheus.port }}
name: prom
{{- end }}
volumeMounts:
- name: spire-config
mountPath: /opt/spire/conf/agent
readOnly: true
{{- if .Values.keyManager.disk.enabled }}
- name: spire-key-manager
mountPath: {{ .Values.persistence.hostPath }}
readOnly: false
{{- end }}
- name: spire-agent-persistence
mountPath: /var/lib/spire
{{- if .Values.sockets.admin.enabled }}
- name: spire-agent-admin-socket-dir
mountPath: /tmp/spire-agent/private
readOnly: false
{{- end }}
{{- if eq (len .Values.trustBundleURL) 0 }}
- name: spire-bundle
readOnly: true
{{- if ne (len .Values.trustBundleHostPath) 0 }}
mountPath: {{ .Values.trustBundleHostPath | dir | quote }}
{{- else }}
mountPath: /run/spire/bundle
{{- end }}
{{- end }}
{{- if .Values.nodeAttestor.tpmDirect.enabled }}
- name: tpm-direct
mountPath: /tpm
readOnly: true
- name: tpm
mountPath: /dev/tpmrm0
readOnly: true
{{- end }}
- name: spire-agent-socket-dir
mountPath: /tmp/spire-agent/public
readOnly: false
- name: spire-token
mountPath: /var/run/secrets/tokens
{{- if or (eq .Values.workloadAttestors.k8s.verification.type "hostCert") (eq .Values.workloadAttestors.k8s.verification.type "auto") }}
- name: host-cert-isolated
mountPath: /hostCert
readOnly: true
{{- end }}
{{- if gt (len .Values.extraVolumeMounts) 0 }}
{{- toYaml .Values.extraVolumeMounts | nindent 12 }}
{{- end }}
livenessProbe:
httpGet:
path: /live
port: healthz
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
httpGet:
path: /ready
port: healthz
{{- toYaml .Values.readinessProbe | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- if gt (len .Values.extraContainers) 0 }}
{{- toYaml .Values.extraContainers | nindent 8 }}
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
- name: spire-config
configMap:
name: {{ include "spire-agent.fullname" . }}
{{- if .Values.keyManager.disk.enabled }}
- name: spire-key-manager
hostPath:
path: {{ .Values.persistence.hostPath }}
type: DirectoryOrCreate
{{- end }}
{{- if .Values.sockets.admin.mountOnHost }}
- name: spire-agent-admin-socket-dir
hostPath:
{{- if .Values.upstream }}
path: {{ printf "%s/upstream.csi.spiffe.io/admin" .Values.sockets.hostBasePath | quote }}
{{- else }}
path: {{ printf "%s/csi.spiffe.io/admin" .Values.sockets.hostBasePath | quote }}
{{- end }}
type: DirectoryOrCreate
{{- else }}
- name: spire-agent-admin-socket-dir
emptyDir: {}
{{- end }}
{{- if eq .Values.persistence.type "hostPath" }}
- name: spire-agent-persistence
hostPath:
{{- if .Values.upstream }}
path: {{ printf "%s/upstream.csi.spiffe.io" .Values.persistence.hostPath | quote }}
{{- else }}
path: {{ printf "%s/csi.spiffe.io" .Values.persistence.hostPath | quote }}
{{- end }}
type: DirectoryOrCreate
{{- else }}
- name: spire-agent-persistence
emptyDir: {}
{{- end }}
{{- if eq (len .Values.trustBundleURL) 0 }}
- name: spire-bundle
{{- if ne (len .Values.trustBundleHostPath) 0 }}
hostPath:
path: {{ .Values.trustBundleHostPath | dir | quote }}
{{- else }}
configMap:
name: {{ printf "%s%s" (include "spire-lib.bundle-configmap" .) $nameSuffix | quote }}
{{- end }}
{{- end }}
{{- if .Values.nodeAttestor.tpmDirect.enabled }}
- name: tpm-direct
emptyDir: {}
{{- end }}
- name: spire-token
projected:
sources:
- serviceAccountToken:
path: spire-agent
expirationSeconds: 7200
audience: spire-server
- name: spire-agent-socket-dir
hostPath:
path: {{ $socketPath | dir }}
type: DirectoryOrCreate
{{- if .Values.nodeAttestor.tpmDirect.enabled }}
- name: tpm
hostPath:
path: /dev/tpmrm0
type: CharDevice
- name: kmsg
hostPath:
path: /dev/kmsg
type: CharDevice
{{- end }}
{{- if or (eq .Values.workloadAttestors.k8s.verification.type "hostCert") (eq .Values.workloadAttestors.k8s.verification.type "auto") }}
- name: host-cert-isolated
emptyDir: {}
- name: host-cert
hostPath:
path: {{ .Values.workloadAttestors.k8s.verification.hostCert.basePath | quote }}
{{- end }}
{{- if gt (len .Values.extraVolumes) 0 }}
{{- toYaml .Values.extraVolumes | nindent 8 }}
{{- end }}
{{- end }}
{{- end }}