diff --git a/chart/files/zitadel-database-config.sh b/chart/files/zitadel-database-config.sh new file mode 100644 index 0000000..385e8c5 --- /dev/null +++ b/chart/files/zitadel-database-config.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# Copyright (c) 2026 VH & Co BV. Licensed under the Business Source License 1.1. See LICENSE for details. +# +# Gives Zitadel its database connection as one connection string (Database.Postgres.DSN) with +# every value quoted, and creates Zitadel's database when it does not exist yet. Zitadel joins its +# separate Host, User and Password settings into a connection string without quoting them, so a +# password holding a space, a quote or a backslash would not reach PostgreSQL unchanged. Values are +# quoted the way the API quotes its own (quoteDSNValue in core/repository/database.go). +# +# Runs in the postgres image, which has pg_isready and psql. It reads the libpq variables PGHOST, +# PGPORT, PGUSER, PGPASSWORD and PGSSLMODE (psql reads them itself, so the password is never put on +# a command line or in a query), and: +# ZITADEL_DATABASE Zitadel's database name (default zitadel) +# ZITADEL_DB_CONFIG the Zitadel configuration file to write (default /zitadel-db/database.yaml) +# +# The Helm chart and the Docker Compose example ship this same file. +set -eu + +: "${PGHOST:?PGHOST is required}" +: "${PGUSER:?PGUSER is required}" +: "${PGPASSWORD:?PGPASSWORD is required}" +export PGPORT="${PGPORT:-5432}" +export PGSSLMODE="${PGSSLMODE:-disable}" +database="${ZITADEL_DATABASE:-zitadel}" +config="${ZITADEL_DB_CONFIG:-/zitadel-db/database.yaml}" + +for name in PGHOST PGPORT PGUSER PGPASSWORD PGSSLMODE ZITADEL_DATABASE; do + eval "value=\${$name:-}" + case "$value" in + *[[:cntrl:]]*) + echo "$name contains a control character, which a Zitadel configuration value cannot hold" >&2 + exit 1 ;; + esac +done + +# escape CHARS VALUE prints VALUE with a backslash before every character listed in CHARS. +escape() { + rest="$2" out="" + while [ -n "$rest" ]; do + tail="${rest#?}" + char="${rest%"$tail"}" + rest="$tail" + case "$1" in *"$char"*) out="$out\\$char" ;; *) out="$out$char" ;; esac + done + printf '%s' "$out" +} + +# quote VALUE prints VALUE as a quoted connection string value: in single quotes, with backslash +# and single quote escaped. +quote() { + printf "'%s'" "$(escape "\\'" "$1")" +} + +until pg_isready -q -d postgres; do + echo "Waiting for PostgreSQL at $PGHOST:$PGPORT..." + sleep 2 +done + +# Zitadel creates its database itself only from the separate settings; with a connection string it +# expects the database to exist. The connecting user needs CREATEDB for a new installation, as it +# did when Zitadel created the database. +psql -X -q -v ON_ERROR_STOP=1 -v database="$database" -d postgres <<'SQL' +SELECT format('CREATE DATABASE %I', :'database') +WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = :'database')\gexec +SQL + +dsn="host=$(quote "$PGHOST") port=$(quote "$PGPORT") user=$(quote "$PGUSER") password=$(quote "$PGPASSWORD") dbname=$(quote "$database") sslmode=$(quote "$PGSSLMODE")" + +umask 077 +printf 'Database:\n Postgres:\n DSN: "%s"\n' "$(escape '\"' "$dsn")" >"$config.tmp" +mv "$config.tmp" "$config" +echo "Wrote Zitadel's database connection to $config" diff --git a/chart/templates/zitadel/configmap-zitadel.yaml b/chart/templates/zitadel/configmap-zitadel.yaml index 8ebb7d1..f1e0a04 100644 --- a/chart/templates/zitadel/configmap-zitadel.yaml +++ b/chart/templates/zitadel/configmap-zitadel.yaml @@ -24,19 +24,8 @@ data: Hostname: Enabled: true - Database: - Postgres: - Host: {{ include "stackweaver.postgresql.host" . }} - Port: {{ include "stackweaver.postgresql.port" . }} - Database: zitadel - Admin: - Username: {{ include "stackweaver.postgresql.username" . }} - SSL: - Mode: {{ include "stackweaver.postgresql.sslmode" . }} - User: - Username: {{ include "stackweaver.postgresql.username" . }} - SSL: - Mode: {{ include "stackweaver.postgresql.sslmode" . }} + # The database connection (Database.Postgres.DSN) comes from a second configuration file the + # database-config init container writes from the PostgreSQL Secret, with every value quoted. # Allow Zitadel's outgoing HTTP calls (Actions V2 targets, IdP discovery) # to reach internal/cluster-local URLs - Zitadel → API SSO group-sync @@ -80,6 +69,8 @@ data: RefreshTokenIdleExpiration: {{ .Values.zitadel.config.refreshTokenIdleExpiration | default "720h" }} RefreshTokenExpiration: {{ .Values.zitadel.config.refreshTokenExpiration | default "2160h" }} + database-config.sh: | +{{ .Files.Get "files/zitadel-database-config.sh" | indent 4 }} init-steps.yaml: | FirstInstance: Skip: false diff --git a/chart/templates/zitadel/zitadel-deployment.yaml b/chart/templates/zitadel/zitadel-deployment.yaml index a240f57..960ef02 100644 --- a/chart/templates/zitadel/zitadel-deployment.yaml +++ b/chart/templates/zitadel/zitadel-deployment.yaml @@ -33,8 +33,10 @@ spec: seccompProfile: type: RuntimeDefault initContainers: - - name: wait-for-postgresql - image: busybox:1.36 + # Writes Zitadel's database connection as one quoted connection string from the PostgreSQL + # Secret, and creates Zitadel's database when it is missing (files/zitadel-database-config.sh). + - name: database-config + image: "{{ .Values.postgresql.image.repository }}:{{ .Values.postgresql.image.tag }}" securityContext: readOnlyRootFilesystem: true resources: @@ -42,17 +44,35 @@ spec: cpu: 10m memory: 16Mi limits: - cpu: 50m - memory: 32Mi + cpu: 100m + memory: 64Mi command: - sh - - -c - - | - until nc -z {{ include "stackweaver.postgresql.host" . }} {{ include "stackweaver.postgresql.port" . }}; do - echo "Waiting for PostgreSQL at {{ include "stackweaver.postgresql.host" . }}:{{ include "stackweaver.postgresql.port" . }}..." - sleep 2 - done - echo "PostgreSQL is reachable." + - /etc/zitadel/database-config.sh + env: + - name: PGHOST + value: {{ include "stackweaver.postgresql.host" . | quote }} + - name: PGPORT + value: {{ include "stackweaver.postgresql.port" . | quote }} + - name: PGUSER + value: {{ include "stackweaver.postgresql.username" . | quote }} + - name: PGPASSWORD + valueFrom: + secretKeyRef: + name: {{ include "stackweaver.secrets.postgresql" . }} + key: {{ .Values.secrets.postgresql.keys.password }} + - name: PGSSLMODE + value: {{ include "stackweaver.postgresql.sslmode" . | quote }} + - name: ZITADEL_DATABASE + value: zitadel + - name: ZITADEL_DB_CONFIG + value: /zitadel-db/database.yaml + volumeMounts: + - name: config + mountPath: /etc/zitadel + readOnly: true + - name: zitadel-db + mountPath: /zitadel-db containers: - name: zitadel image: "{{ .Values.zitadel.image.repository }}:{{ .Values.zitadel.image.tag }}" @@ -62,6 +82,8 @@ spec: - start-from-init - --config - /etc/zitadel/defaults.yaml + - --config + - /zitadel-db/database.yaml - --steps - /etc/zitadel/init-steps.yaml - --masterkey @@ -78,25 +100,12 @@ spec: secretKeyRef: name: {{ include "stackweaver.secrets.zitadel" . }} key: {{ .Values.secrets.zitadel.keys.masterkey }} - # Override DB password placeholders from configmap - - name: ZITADEL_DATABASE_POSTGRES_ADMIN_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "stackweaver.secrets.postgresql" . }} - key: {{ .Values.secrets.postgresql.keys.password }} - - name: ZITADEL_DATABASE_POSTGRES_USER_PASSWORD - valueFrom: - secretKeyRef: - name: {{ include "stackweaver.secrets.postgresql" . }} - key: {{ .Values.secrets.postgresql.keys.password }} # Override the first instance admin password from init-steps - name: ZITADEL_FIRSTINSTANCE_ORG_HUMAN_PASSWORD valueFrom: secretKeyRef: name: {{ include "stackweaver.secrets.zitadel" . }} key: {{ .Values.secrets.zitadel.keys.adminPassword }} - - name: ZITADEL_DATABASE_POSTGRES_HOST - value: {{ include "stackweaver.postgresql.host" . | quote }} - name: ZITADEL_LOGIN_UI_BASE_URL value: {{ include "stackweaver.loginUI.baseURL" . | quote }} - name: ZITADEL_LOGIN_UI_ORIGIN @@ -105,6 +114,9 @@ spec: - name: config mountPath: /etc/zitadel readOnly: true + - name: zitadel-db + mountPath: /zitadel-db + readOnly: true - name: pat mountPath: /pat - name: tmp @@ -266,6 +278,11 @@ spec: - name: config configMap: name: {{ include "stackweaver.fullname" . }}-zitadel-config + # Holds the database connection file, password included, in memory only. + - name: zitadel-db + emptyDir: + medium: Memory + sizeLimit: 1Mi - name: pat emptyDir: {} - name: tmp