Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
chocapix-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
5
Issues
5
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Camille MASSET
chocapix-server
Commits
dedc7f7f
Commit
dedc7f7f
authored
Mar 24, 2015
by
Nadrieril
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Get django secret key from a file, and generate it if needed
parent
56c73271
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
1 deletion
+29
-1
.gitignore
.gitignore
+1
-0
bars_django/settings/common.py
bars_django/settings/common.py
+28
-1
No files found.
.gitignore
View file @
dedc7f7f
...
...
@@ -3,3 +3,4 @@
*.pyc
/.coverage
/htmlcov
/secret.key
bars_django/settings/common.py
View file @
dedc7f7f
...
...
@@ -13,7 +13,34 @@ import os
PROJECT_ROOT
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
os
.
path
.
dirname
(
__file__
)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY
=
')$w@s+3e_&cl19@c2-qbi^rr6fnzj4p*0%3u_xtltj0*-cc0&v'
"""
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`.
"""
try
:
SECRET_KEY
except
NameError
:
SECRET_FILE
=
os
.
path
.
join
(
PROJECT_ROOT
,
'secret.key'
)
try
:
SECRET_KEY
=
open
(
SECRET_FILE
).
read
().
strip
()
except
IOError
:
try
:
import
random
symbols
=
'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
SECRET_KEY
=
''
.
join
([
random
.
SystemRandom
().
choice
(
symbols
)
for
i
in
range
(
50
)])
with
open
(
SECRET_FILE
,
'w'
)
as
f
:
f
.
write
(
SECRET_KEY
)
except
IOError
:
Exception
(
'Please create a %s file with random characters
\
to generate your secret key!'
%
SECRET_FILE
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG
=
False
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment