Skip to content

Commit c896ce6

Browse files
ObadaSObada HaddadDidayolo
authored
Autogenerate SECRET_KEY or use given one (#2267)
* make secret key auto generate and write into .env if it does not already exist, otherwise use exsiting key * remove secret_key in .env_sample and .env_circleci * Make code more robust * add back '' to fix rare warnings --------- Co-authored-by: Obada Haddad <obada.haddad@lisn.fr> Co-authored-by: didayolo <adrien.pavao@gmail.com>
1 parent f0a8146 commit c896ce6

File tree

5 files changed

+64
-37
lines changed

5 files changed

+64
-37
lines changed

.env_circleci

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
SECRET_KEY=change-this-secret
2-
31
DB_HOST=db
42
DB_NAME=postgres
53
DB_USERNAME=postgres
@@ -35,4 +33,4 @@ DJANGO_SUPERUSER_EMAIL=test@test.com
3533
DJANGO_SUPERUSER_USERNAME=codabench
3634
DOMAIN_NAME=localhost:80
3735
TLS_EMAIL=your@email.com
38-
SUBMISSIONS_API_URL=http://django:8000/api
36+
SUBMISSIONS_API_URL=http://django:8000/api

.env_sample

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
SECRET_KEY=change-this-secret
2-
31
# For local setup and debug
4-
DEBUG=True
2+
DEBUG=False
53

64
# Database
75
DB_HOST=db

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dependencies = [
6666
"django-filter==25.1",
6767
"django-cors-headers==4.9.0",
6868
"nh3==0.3.3",
69+
"configobj==5.0.9",
6970
]
7071

7172
[tool.uv]

src/settings/base.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from celery import signals
66
import dj_database_url
77
from .logs_loguru import configure_logging
8+
from django.core.management.utils import get_random_secret_key
9+
import configobj
810

911

1012
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -125,7 +127,24 @@
125127
USE_I18N = True
126128
USE_L10N = True
127129
USE_TZ = True
128-
SECRET_KEY = os.environ.get("SECRET_KEY", '(*0&74%ihg0ui+400+@%2pe92_c)x@w2m%6s(jhs^)dc$&&g93')
130+
131+
# =============================================================================
132+
# Secret key (generate one if none is given, otherwise use given key)
133+
# =============================================================================
134+
# This is needed when the secret key is generated for the first time as it won't be loaded as an environment variable
135+
config = configobj.ConfigObj('.env')
136+
with open(".env", "a+") as f:
137+
secret_key_count = 0
138+
f.seek(0)
139+
for x in f:
140+
if x.strip().startswith("SECRET_KEY="):
141+
secret_key_count = 1
142+
SECRET_KEY = os.environ.get("SECRET_KEY", config['SECRET_KEY'])
143+
break
144+
if secret_key_count == 0:
145+
SECRET_KEY = get_random_secret_key()
146+
f.write(f"\nSECRET_KEY='{SECRET_KEY}'\n")
147+
129148
LOGIN_REDIRECT_URL = '/'
130149
LOGOUT_REDIRECT_URL = '/'
131150

uv.lock

Lines changed: 41 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)