-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddsecretkey.py
More file actions
executable file
·31 lines (26 loc) · 1.04 KB
/
Copy pathaddsecretkey.py
File metadata and controls
executable file
·31 lines (26 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
########################################
# Create secret key file
# https://gist.github.com/ndarville/3452907
########################################
"""
Two things are wrong with Django's default `SECRET_KEY` system:
1. It is not random but pseudo-random
2. It saves and displays the SECRET_KEY in `settings.py`
This snippet
1. uses `SystemRandom()` instead to generate a random key
2. saves a local `secret.txt`
The result is a random and safely hidden `SECRET_KEY`.
"""
import os
envfiles = [ 'app/.env.dev', 'app/.env.prod' ]
for envfile in envfiles:
SECRET_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), envfile)
try:
import random
SECRET_KEY = ''.join([random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(70)])
secret = open(SECRET_FILE, 'a')
secret.write("SECRET_KEY=" + SECRET_KEY + "\n")
secret.close()
except IOError:
Exception('Please create a %s file with random characters \
to generate your secret key!' % SECRET_FILE)