Skip to content

Allow the Django secret key to be set via DJANGO_SECRET_KEY env var#748

Closed
pimzand wants to merge 1 commit into
frePPLe:masterfrom
macroscoop:fix/secret-key-from-env
Closed

Allow the Django secret key to be set via DJANGO_SECRET_KEY env var#748
pimzand wants to merge 1 commit into
frePPLe:masterfrom
macroscoop:fix/secret-key-from-env

Conversation

@pimzand
Copy link
Copy Markdown
Contributor

@pimzand pimzand commented May 29, 2026

Fixes #747.

Problem

contrib/docker/entrypoint.sh rewrites SECRET_KEY in djangosettings.py on every container start. Where /etc/frepple is persisted (Linux install, docker-compose named volume) this is a harmless first-boot stamp — once a real key is written the placeholder-anchored sed no longer matches. But the official Kubernetes manifest in contrib/kubernetes does not persist /etc/frepple, so the config dir resets to the shipped placeholder on every pod (re)start and a fresh key is minted each time — invalidating all sessions and rotating SECRET_WEBTOKEN_KEY (the Odoo SSO bridge). The issue explains why simply persisting /etc/frepple on Kubernetes isn't a clean answer.

Change

Let the entrypoint honor a DJANGO_SECRET_KEY environment variable: use it when set, fall back to the existing random key when unset. This is purely additive — with the variable unset, behavior is identical to today, so no existing deployment (Linux, docker-compose, current Kubernetes) changes. It keeps the pod stateless and /etc/frepple image-owned and writable, and lets SECRET_KEY be supplied the same way POSTGRES_*, ODOO_*, and FREPPLE_* already are.

  • contrib/docker/entrypoint.sh — honor DJANGO_SECRET_KEY when set. The substitution still only matches the shipped placeholder, so an already-stamped (persisted) key is never touched. Reworked from sed to a small Python snippet that injects the value via repr(), so arbitrary key characters can't break the substitution.
  • contrib/kubernetes/frepple-deployment.yaml — set DJANGO_SECRET_KEY from a new frepple-secret Secret (ships a placeholder value with a generate-your-own note).
  • doc/installation-guide/advanced/kubernetes.rst, doc/installation-guide/docker-container.rst — document the variable and the Secret.

docker-compose is left unchanged — its named volume already persists the key.

The entrypoint rewrites SECRET_KEY in djangosettings.py on every start. On a
persisted /etc/frepple (Linux install, docker-compose named volume) this is a
harmless first-boot stamp. But the Kubernetes manifest in contrib/kubernetes
does not persist /etc/frepple, so the config dir resets to the shipped
placeholder on every pod (re)start and a fresh key is minted each time —
logging out all sessions and rotating SECRET_WEBTOKEN_KEY, the Odoo SSO bridge.

Honor a DJANGO_SECRET_KEY environment variable when set (purely additive: unset
keeps the previous random-key behavior, so no existing deployment changes), and
set it from a Secret in the k8s manifest. Inject via python repr() so arbitrary
key characters can't break the substitution. Document the variable for the
docker image and the secret for the k8s deployment.
@pimzand pimzand force-pushed the fix/secret-key-from-env branch from a6f67bf to da6c00b Compare May 29, 2026 15:50
@pimzand pimzand marked this pull request as draft May 29, 2026 20:06
@pimzand
Copy link
Copy Markdown
Contributor Author

pimzand commented May 29, 2026

Superseded by #749. The right fix is to persist /etc/frepple in the Kubernetes manifest (parity with how docker-compose already persists it on a named volume), not to add a DJANGO_SECRET_KEY env var — the rotating secret was only the visible symptom; installed apps, scenario count and display settings are lost on pod restart too. See the explanation on #747.

@pimzand pimzand closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Django SECRET_KEY regenerated on every container start — unstable on Kubernetes

1 participant