From 9eb3726889fe2ed76b2a9b87e691f5a441fc02db Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Tue, 24 Mar 2015 22:43:18 +0100 Subject: [PATCH] Makes SECRET_KEY secret To quote from Djangos documentation: "The secret key must be a large random value and it must be kept secret." https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/#secret-key This commit achieves that by generating a file containing a random string when running the first time and by using this string as SECRET_KEY from then on. --- .gitignore | 1 + binder/settings.py | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 47a8337..83e5248 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.pyc *.conf *~ +/binder/secret.txt diff --git a/binder/settings.py b/binder/settings.py index e866456..a787a6a 100644 --- a/binder/settings.py +++ b/binder/settings.py @@ -42,7 +42,18 @@ MEDIA_ROOT = os.path.join(SITE_ROOT, "files") MEDIA_URL = "/files/" STATIC_URL= "/static/" -SECRET_KEY = 'iuo-zka8nnv0o+b*7#_*fcep$@f^35=)c#7_20z6i8g0oc&r!g' +SECRET_FILE = os.path.join(SITE_ROOT, 'secret.txt') +try: + SECRET_KEY = open(SECRET_FILE).read().strip() +except IOError: + try: + from random import choice + SECRET_KEY = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)]) + secret = file(SECRET_FILE, 'w') + secret.write(SECRET_KEY) + secret.close() + except IOError: + Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE) # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = (