Skip to content

Commit d49cee3

Browse files
committed
manage.py: Catch issue with users running manage.py as the wrong user.
Apparently, if you tried running manage.py as a non-root user that isn't the main zulip user, you'll get a confusing Django exception about `SECRET_KEY` not being configured. This change provides a clean, understandable exception for this case.
1 parent 02ae71f commit d49cee3

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

manage.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
import scripts.lib.setup_path_on_import
88

99
if __name__ == "__main__":
10+
if 'posix' in os.name and os.geteuid() == 0:
11+
print("manage.py should not be run as root. Use `su zulip` to drop root.")
12+
sys.exit(1)
13+
if (os.access('/etc/zulip/zulip.conf', os.R_OK) and not
14+
os.access('/etc/zulip/zulip-secrets.conf', os.R_OK)):
15+
# The best way to detect running manage.py as another user in
16+
# production before importing anything that would require that
17+
# access is to check for access to /etc/zulip/zulip.conf (in
18+
# which case it's a production server, not a dev environment)
19+
# and lack of access for /etc/zulip/zulip-secrets.conf (which
20+
# should be only readable by root and zulip)
21+
print("Error accessing Zulip secrets; manage.py in production must be run as the zulip user.")
22+
sys.exit(1)
23+
1024
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
1125
from django.conf import settings
1226
from django.core.management import execute_from_command_line
1327
from django.core.management.base import CommandError
1428
from scripts.lib.zulip_tools import log_management_command
1529

16-
if 'posix' in os.name and os.geteuid() == 0:
17-
raise CommandError("manage.py should not be run as root. Use `su zulip` to drop root.")
18-
1930
log_management_command(" ".join(sys.argv), settings.MANAGEMENT_LOG_PATH)
2031

2132
os.environ.setdefault("PYTHONSTARTUP", os.path.join(BASE_DIR, "scripts/lib/pythonrc.py"))

0 commit comments

Comments
 (0)