Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

REFACTOR (security): run main application as non-root user

Jim Brännlund8d8754e96c22
diff --git a/Dockerfile b/Dockerfile
index f5faa95a..569538db 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -159,6 +159,9 @@ RUN groupadd -g 999 postgres || true && \
mkdir -p /databasus-data/pgdata && \
chown -R postgres:postgres /databasus-data/pgdata
+# Create non-root user for the main application process
+RUN useradd -r -s /usr/sbin/nologin -u 65532 databasus
+
WORKDIR /app
# Copy Goose from build stage
@@ -293,7 +296,12 @@ echo "Setting up data directory permissions..."
mkdir -p /databasus-data/pgdata
mkdir -p /databasus-data/temp
mkdir -p /databasus-data/backups
-chown -R postgres:postgres /databasus-data
+chown databasus:databasus /databasus-data
+chown -R postgres:postgres /databasus-data/pgdata
+chown -R databasus:databasus /databasus-data/temp /databasus-data/backups
+# Upgrade path: secret.key and instance.json may be owned by root or postgres
+# from older images. Re-own them so the non-root main process can read/write.
+chown databasus:databasus /databasus-data/secret.key /databasus-data/instance.json 2>/dev/null || true
chmod 700 /databasus-data/temp
# ========= Start Valkey (internal cache) =========
@@ -442,7 +450,7 @@ if [ -n "\${DANGEROUS_VALKEY_HOST:-}" ]; then
echo ""
fi
-exec ./main
+exec gosu databasus ./main
EOF
LABEL org.opencontainers.image.source="https://github.com/databasus/databasus"
diff --git a/deploy/helm/templates/statefulset.yaml b/deploy/helm/templates/statefulset.yaml
index 7e73a0c3..16df9868 100644
--- a/deploy/helm/templates/statefulset.yaml
+++ b/deploy/helm/templates/statefulset.yaml
@@ -39,10 +39,18 @@ spec:
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
+ {{- with .Values.podSecurityContext }}
+ securityContext:
+ {{- toYaml . | nindent 8 }}
+ {{- end }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
+ {{- with .Values.securityContext }}
+ securityContext:
+ {{- toYaml . | nindent 12 }}
+ {{- end }}
{{- if .Values.customRootCA }}
env:
- name: SSL_CERT_FILE
diff --git a/deploy/helm/values.yaml b/deploy/helm/values.yaml
index a0ae702e..269f013d 100644
--- a/deploy/helm/values.yaml
+++ b/deploy/helm/values.yaml
@@ -23,6 +23,16 @@ service:
# Headless service for StatefulSet
headless:
enabled: true
+# Security context for the pod (e.g. fsGroup for volume permissions)
+# NOTE: The entrypoint runs as root to bootstrap PostgreSQL and Valkey,
+# then drops privileges to a non-root user via gosu. Do not set
+# runAsNonRoot or runAsUser here -- it will prevent the container from starting.
+podSecurityContext: {}
+
+# Security context for the databasus container
+# See podSecurityContext note above regarding root requirement.
+securityContext: {}
+
# Resource limits and requests
resources:
requests: