diff --git a/templates/auto-unseal.yaml b/templates/auto-unseal.yaml new file mode 100644 index 0000000..c28e916 --- /dev/null +++ b/templates/auto-unseal.yaml @@ -0,0 +1,120 @@ +{{- if .Values.autoUnseal.enabled }} +# Unseals OpenBao automatically after it (re)starts sealed. Runs the same steps as +# scripts/unseal.sh, but from a CronJob and over the network: +# status -> (if sealed) reset progress -> send the keys on stdin -> verify. +# +# SECURITY MODEL: the unseal keys are read from the Secret `{{ .Values.autoUnseal.keysSecretName }}`, +# i.e. they live in the same cluster as OpenBao - exactly like the manual procedure that +# already reads them from there. This removes the manual step, not the trust assumption: +# anyone who can read Secrets in this namespace can unseal OpenBao. Set +# autoUnseal.enabled=false to go back to unsealing by hand. +apiVersion: batch/v1 +kind: CronJob +metadata: + name: openbao-auto-unseal + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/name: openbao-auto-unseal +spec: + schedule: {{ .Values.autoUnseal.schedule | quote }} + concurrencyPolicy: Forbid + startingDeadlineSeconds: 60 + successfulJobsHistoryLimit: 1 + failedJobsHistoryLimit: 3 + jobTemplate: + spec: + backoffLimit: 0 + activeDeadlineSeconds: 55 + template: + metadata: + labels: + app.kubernetes.io/name: openbao-auto-unseal + spec: + restartPolicy: Never + automountServiceAccountToken: false + securityContext: + runAsNonRoot: true + runAsUser: 100 + runAsGroup: 1000 + fsGroup: 1000 + seccompProfile: + type: RuntimeDefault + containers: + - name: unseal + image: {{ .Values.autoUnseal.image }} + env: + # The pod itself (headless Service), because the regular Service only lists + # READY pods and a sealed OpenBao is never ready. + - name: BAO_ADDR + value: "https://{{ include "openbao-gitops.openbaoFullname" . }}-0.{{ include "openbao-gitops.openbaoFullname" . }}-internal.{{ .Release.Namespace }}.svc:8200" + # Verify against the cert-manager CA; the certificate names the Service, not the pod. + - name: BAO_CACERT + value: /tls/ca.crt + - name: BAO_TLS_SERVER_NAME + value: {{ include "openbao-gitops.openbaoFullname" . }}.{{ .Release.Namespace }}.svc + - name: HOME + value: /tmp + - name: KEYS_FILE + value: /keys/{{ .Values.autoUnseal.keysSecretKey }} + command: + - sh + - -c + - | + set -u + is_true() { grep -Eq "\"$1\":[[:space:]]*true"; } + json_int() { sed -nE "s/.*\"$1\":[[:space:]]*([0-9]+).*/\1/p" | head -n1; } + + # `bao status` exits 2 while sealed but still prints the JSON. + STATUS=$(bao status -format=json 2>/dev/null /dev/null &1) \ + || { echo "ERROR: unseal request failed at key $count"; exit 1; } + if printf '%s' "$RESP" | is_true sealed; then + echo " key $count accepted (progress $(printf '%s' "$RESP" | json_int progress)/$THRESHOLD)" + else + echo " key $count accepted"; break + fi + done < "$KEYS_FILE" + + if bao status -format=json 2>/dev/null