NetBox automatic Django migrations on service startup – missing permission boundary to restrict who can trigger migrations
0 reputation · 03 Mar 2020, 12:03 UTC
0 reputation · 03 Mar 2020, 12:03 UTC
The goal is to ensure that only authorized operators can initiate schema migrations when NetBox services start, preventing unintended or unauthorized database changes.
Currently, NetBox runs Django migrations automatically on startup with no built‑in role‑based access control or configuration option to gate this behavior. The service user that starts the application can unconditionally trigger migrations, and there is no documented mechanism to require approval, a token, or a separate privileged process before the migration step executes.
35275 reputation · 03 Mar 2020, 15:03 UTC
Yes – NetBox can be configured to skip its automatic Django migrations on startup and require a manual, privileged invocation.
./manage.py migrate by default.NETBOX_SKIP_MIGRATIONS=1 (or true) disables this automatic step in NetBox 3.3 and later../manage.py migrate inside the container or from a separate process.NetBox does not provide a built‑in approval token or role‑based gate for migrations. To enforce that only authorized operators can trigger the migration step, you must wrap the startup sequence with a check (e.g., for a secret file, environment variable, or external approval signal) before calling manage.py migrate. This wrapper can be a simple shell script that replaces the container’s entrypoint.
NETBOX_SKIP_MIGRATIONS=1 to the container’s environment (e.g., in a Deployment spec or docker run command)../manage.py migrate. This task should use a dedicated ServiceAccount with limited permissions.netbox-migrator.netbox-app).#!/bin/sh
if [ -f /run/secrets/migration_approved ]; then
exec ./manage.py migrate
else
echo "Migration not approved; exiting."
exit 1
fi
Mount the approval token as a secret at /run/secrets/migration_approved and restrict who can create/update that secret.docker logs <container> | grep -i migrate should show none.kubectl logs job/netbox-migrate.To confirm that NETBOX_SKIP_MIGRATIONS is available, please specify the NetBox version you are running (e.g., via docker exec <container> python -c "import netbox; print(netbox.__version__)"). If you are on a pre‑3.3 release, the flag will be ignored and a different approach (custom entrypoint or upstream patch) will be required.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 03 Mar 2020, 19:11 UTC
NetBox’s official container images (v3.x–v4.x) run python manage.py migrate in the entrypoint before starting gunicorn, so the process that launches the web service must hold DDL privileges. The only supported way to gate this is external: run migrations in a distinct step—Kubernetes initContainer, a dedicated Job, or a CI/CD pipeline task—using a short-lived database credential with CREATE/ALTER rights, then start the main NetBox pod with a least-privilege role limited to DML.
Verify by inspecting the image’s docker-entrypoint.sh (it calls migrate unconditionally) and by testing startup against a database user that lacks DDL grants; the container will fail at the migration phase, confirming the boundary. Overriding the entrypoint works but is fragile across upgrades; the separate-job pattern is the documented production approach.