Skip to content

Commit 202006a

Browse files
committed
Remove hard-coded SECRET_KEY from production settings
This change moves the hard-coded key to vulnerablecode/dev.py for use during development (e.g. DJANGO_DEV is set). Otherwise the key is read from an environment variable with the same name. The advantage of this approach over using a gitignored file containing the secret is that it works with deployment environments that use ephemeral filesystems, such as Heroku. closes #46 Signed-off-by: Haiko Schol <hs@haikoschol.com>
1 parent ec15085 commit 202006a

4 files changed

Lines changed: 25 additions & 10 deletions

File tree

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ services:
77
install:
88
- pip install -r requirements.txt
99

10+
env:
11+
- SECRET_KEY="i1bn=oly)w*2yl-5yc&f!vvgt)p)fh3_2$r#spa!*sw36f5ov7"
12+
1013
before_script:
1114
- pycodestyle --exclude=migrations,settings.py,lib,tests --max-line-length=100 .
1215
- psql -c "CREATE DATABASE vulnerablecode;" -U postgres

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ Activate a virtualenv, install dependencies, and run the database migrations:
4141
python3 -m venv venv
4242
source venv/bin/activate
4343
pip install -r requirements.txt
44-
python manage.py migrate
44+
DJANGO_DEV=1 python manage.py migrate
4545
```
4646

47+
The environment variable `DJANGO_DEV` is used to load settings suitable for development, defined in `vulnerablecode/dev.py`. If you don't want to type
48+
it every time use `export DJANGO_DEV=1` instead.
49+
50+
When not running in development mode, an environment variable named `SECRET_KEY` needs to be set. The recommended way to generate this key is to use
51+
the code Django includes for this purpose: `SECRET_KEY=$(python -c "from django.core.management import utils; print(utils.get_random_secret_key())")`.
52+
4753
## Tests
4854

4955
```
@@ -53,13 +59,13 @@ python -m pytest -v vulnerabilities/tests/test_scrapers.py vulnerabilities/tests
5359

5460
For Django based tests
5561
```
56-
python manage.py test vulnerabilities/tests
62+
DJANGO_DEV=1 python manage.py test vulnerabilities/tests
5763
```
5864

5965
## Data import
6066

6167
```
62-
python manage.py shell
68+
DJANGO_DEV=1 python manage.py shell
6369
```
6470

6571
```
@@ -109,10 +115,12 @@ https://devcenter.heroku.com/articles/deploying-python#how-to-keep-build-artifac
109115

110116
5. Create Heroku app: `heroku create`
111117

112-
6. Deploy: `git push heroku <branch>:master`
118+
6. Generate a secret key and pass it as an environment variable: `heroku config:set SECRET_KEY=$(python -c "from django.core.management import utils; print(utils.get_random_secret_key())")`
119+
120+
7. Deploy: `git push heroku <branch>:master`
113121

114-
7. Migrate the database: `heroku run python manage.py migrate`
122+
8. Migrate the database: `heroku run python manage.py migrate`
115123

116-
8. Load the data referring to chapter "Data import" above.
124+
9. Load the data referring to chapter "Data import" above.
117125

118-
9. To check the logs: `heroku logs --tail`
126+
10. To check the logs: `heroku logs --tail`

vulnerablecode/dev.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
DEBUG = True
44

55
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '::1']
6+
7+
SECRET_KEY = 'hhismo6l@1uj)nr6@o7$b2u68w5*_o^liji+=uzq=954-f$8_1'

vulnerablecode/settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212

1313
import os
1414

15+
DEV_MODE = os.environ.get('DJANGO_DEV', False)
16+
1517
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1618
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1719

1820
# Quick-start development settings - unsuitable for production
1921
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
2022

2123
# SECURITY WARNING: keep the secret key used in production secret!
22-
SECRET_KEY = 'hhismo6l@1uj)nr6@o7$b2u68w5*_o^liji+=uzq=954-f$8_1'
24+
if not DEV_MODE:
25+
SECRET_KEY = os.environ['SECRET_KEY']
2326

2427
# SECURITY WARNING: don't run with debug turned on in production!
2528
DEBUG = False
@@ -28,7 +31,6 @@
2831
'.herokuapp.com',
2932
]
3033

31-
3234
# Application definition
3335

3436
INSTALLED_APPS = [
@@ -159,5 +161,5 @@
159161

160162

161163
# Set `DJANGO_DEV=1` in env to enable dev mode
162-
if os.environ.get('DJANGO_DEV', False):
164+
if DEV_MODE:
163165
from vulnerablecode.dev import *

0 commit comments

Comments
 (0)