Skip to content

Commit e01dc44

Browse files
committed
Make Settings consistent with scancodeio
Inspired by scancodeio, we now have a consistent ALLOWED_HOSTS environment variable support. SECRET_KEY no longer defaults to insecure values and is required to be generated. Static files are now served in /var/vulnerablecode/static which is consistent to scancodeio. Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent d24dcb2 commit e01dc44

22 files changed

Lines changed: 26 additions & 37 deletions

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ python-dateutil>=2.8.1
1616
toml>=0.10.2
1717
lxml>=4.6.3
1818
gunicorn>=20.1.0
19+
django-environ==0.4.5

vulnerablecode/settings.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
1-
"""
2-
Django settings for app project.
1+
from pathlib import Path
32

4-
Generated by 'django-admin startproject' using Django 1.11.
3+
import environ
54

6-
For more information on this file, see
7-
https://docs.djangoproject.com/en/1.11/topics/settings/
5+
PROJECT_DIR = Path(__file__).resolve().parent
6+
ROOT_DIR = PROJECT_DIR.parent
87

9-
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/1.11/ref/settings/
11-
"""
8+
# Environment
129

13-
import os
10+
env_file = str(ROOT_DIR.joinpath(".env"))
11+
if Path(env_file).exists():
12+
environ.Env.read_env(env_file)
1413

14+
env = environ.Env(TRAVIS=(bool, False))
1515

16-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
17-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16+
SECRET_KEY = env.str("SECRET_KEY")
1817

19-
# Quick-start development settings - unsuitable for production
20-
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
21-
22-
# SECURITY WARNING: keep the secret key used in production secret!
23-
DEBUG = False
24-
SECRET_KEY = os.environ.get(
25-
"SECRET_KEY", default="this-is-a-dummy-key-and-its-overridden-for-prod-servers"
26-
)
27-
28-
# SECURITY WARNING: don't run with debug turned on in production!
29-
30-
DEBUG_PROPAGATE_EXCEPTIONS = True
31-
32-
ALLOWED_HOSTS = os.environ.get("VC_ALLOWED_HOSTS", "*").split(":")
18+
ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[".localhost", "127.0.0.1", "[::1]"])
3319

3420
# Application definition
3521

@@ -62,7 +48,7 @@
6248
TEMPLATES = [
6349
{
6450
"BACKEND": "django.template.backends.django.DjangoTemplates",
65-
"DIRS": [os.path.join(BASE_DIR, "templates")],
51+
"DIRS": [str(PROJECT_DIR.joinpath("templates"))],
6652
"APP_DIRS": True,
6753
"OPTIONS": {
6854
"debug": False,
@@ -81,20 +67,19 @@
8167

8268

8369
# Database
84-
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
8570

8671
DATABASES = {
8772
"default": {
88-
"ENGINE": "django.db.backends.postgresql",
89-
"NAME": os.environ.get("VC_DB_NAME", "vulnerablecode"),
90-
"USER": os.environ.get("VC_DB_USER", "vulnerablecode"),
91-
"PASSWORD": os.environ.get("VC_DB_PASSWORD", "vulnerablecode"),
92-
"HOST": os.environ.get("VC_DB_HOST", "localhost"),
93-
"PORT": os.environ.get("VC_DB_PORT", "5432"),
73+
"ENGINE": env.str("VULNERABLECODE_DB_ENGINE", "django.db.backends.postgresql"),
74+
"NAME": env.str("VULNERABLECODE_DB_NAME", "vulnerablecode"),
75+
"USER": env.str("VULNERABLECODE_DB_USER", "vulnerablecode"),
76+
"PASSWORD": env.str("VULNERABLECODE_DB_PASSWORD", "vulnerablecode"),
77+
"HOST": env.str("VULNERABLECODE_DB_HOST", "localhost"),
78+
"PORT": env.str("VULNERABLECODE_DB_PORT", "5432"),
9479
}
9580
}
9681

97-
if "TRAVIS" in os.environ:
82+
if env("TRAVIS"):
9883
DATABASES["default"]["USER"] = "postgres"
9984
DATABASES["default"]["PASSWORD"] = ""
10085

@@ -122,7 +107,6 @@
122107

123108

124109
# Internationalization
125-
# https://docs.djangoproject.com/en/1.11/topics/i18n/
126110

127111
LANGUAGE_CODE = "en-us"
128112

@@ -136,11 +120,15 @@
136120

137121

138122
# Static files (CSS, JavaScript, Images)
139-
# https://docs.djangoproject.com/en/1.11/howto/static-files/
140123

141-
STATIC_ROOT = os.path.join(BASE_DIR, "static")
142124
STATIC_URL = "/static/"
143125

126+
STATIC_ROOT = "/var/vulnerablecode/static"
127+
128+
STATICFILES_DIRS = [
129+
str(PROJECT_DIR.joinpath("static")),
130+
]
131+
144132
# REST API
145133
REST_FRAMEWORK = {
146134
"DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)