Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve allowed host config #51

Merged
merged 4 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ COPY ./infoscience_exports /usr/src/app/infoscience_exports
# collectstatic
RUN DJANGO_SETTINGS_MODULE=settings.prod \
SECRET_KEY="not needed to collectstaticfiles" \
ALLOWED_HOST="not needed to collectstaticfiles" \
ALLOWED_HOSTS="not needed to collectstaticfiles" \
SITE_URL="not needed to collectstaticfiles" \
DATABASE_URL="not needed to collectstaticfiles" \
MOCKS_DATABASE_URL="not needed to collectstaticfiles" \
python infoscience_exports/manage.py collectstatic

VOLUME ["/usr/src/app/staticfiles", "/var/log/django"]
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ vars:
@echo 'Used by App:'
@echo ' SECRET_KEY=${SECRET_KEY}'
@echo ' DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}'
@echo ' ALLOWED_HOST=${ALLOWED_HOST}'
@echo ' SERVER_HOST=${SERVER_HOST}'
@echo ' DEV_PORT=${DEV_PORT}'
@echo ' SITE_PATH=${SITE_PATH}'
@echo ' SITE_URL=${SITE_URL}'
@echo ' ALLOWED_HOSTS=${ALLOWED_HOSTS}'
@echo ' DATABASE_URL=${DATABASE_URL}'
@echo ' MOCKS_DATABASE_URL=${MOCKS_DATABASE_URL}'
@echo ''
@echo 'Used by Makefile'
@echo ' SUPER_ADMIN_USERNAME=${SUPER_ADMIN_USERNAME}'
Expand All @@ -34,7 +34,6 @@ vars:
@echo ' DATABASE_USER=${DATABASE_USER}'
@echo ' DATABASE_PASSWORD=xxx'
@echo ' DB_NAME=${DB_NAME}'
@echo ' MOCKS_DB_NAME=${MOCKS_DB_NAME}'
@echo ''
@echo 'Defined as helpers'
@echo ' DB_URL=${DB_URL}'
Expand Down Expand Up @@ -71,8 +70,6 @@ init-db:
# create DB
docker-compose -f docker-compose-dev.yml exec postgres \
psql -c 'CREATE DATABASE "${DB_NAME}";' -U postgres
docker-compose -f docker-compose-dev.yml exec postgres \
psql -c 'CREATE DATABASE "${MOCKS_DB_NAME}";' -U postgres
# create DB user for app
docker-compose -f docker-compose-dev.yml exec postgres \
psql ${DB_NAME} -c "CREATE USER ${DATABASE_USER} WITH PASSWORD '${DATABASE_PASSWORD}';" -U postgres
Expand All @@ -83,8 +80,6 @@ init-db:
python infoscience_exports/manage.py makemigrations
docker-compose -f docker-compose-dev.yml exec web \
python infoscience_exports/manage.py migrate
docker-compose -f docker-compose-dev.yml exec web \
python infoscience_exports/manage.py migrate --database=mock
# create super admin in app
make superadmin
@echo " -> All set up! You can connect with your tequila acount or the admin (${SUPER_ADMIN_EMAIL})"
Expand Down
11 changes: 5 additions & 6 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ name = "pypi"

[packages]

django = "*"
Django = "*"
dj-database-url = "*"
django-dirtyfields = "*"
django-filter = "*"
django-model-utils = "*"
django-tequila = {git = "https://github.com/epfl-idevelop/django-tequila.git", editable = true}
"django-bootstrap4" = "*"
djangorestframework = "*"
gevent = "*"
gunicorn = "*"
markdown = "*"
Markdown = "*"
"psycopg2-binary" = "*"
pymarc = "*"
pytz = "*"
Expand All @@ -32,9 +31,9 @@ codecov = "*"
django-debug-toolbar = "*"
django-extensions = "*"
django-nose = "*"
"fabric3" = "*"
factory-boy = "*"
faker = "*"
"Fabric3" = "*"
factory_boy = "*"
Faker = "*"
"flake8" = "*"
graypy = "*"
ipdb = "*"
Expand Down
582 changes: 299 additions & 283 deletions Pipfile.lock

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions doc/DEV_INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,16 @@ create the db::

createdb -O django infoscience_exports

createdb -O django mock_infoscience_exports

## Django config

Do the first migration::

fab migrate
fab migrate_mock

It may be the good timing to set your environment variable::

export DATABASE_PASSWORD='django'

Load mock data::

python ./infoscience_exports/manage.py loaddata --app exports --database mock initial_data

Create your user::

python ./infoscience_exports/manage.py createsuperuser
Expand Down
6 changes: 4 additions & 2 deletions doc/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ You might want to change the default values for the following vars:

- DJANGO_SETTINGS_MODULE=settings.dev
- SITE_URL=https://your-host.epfl.ch
- ALLOWED_HOST=your-host
- SERVER_HOST=your-host
- ALLOWED_HOSTS=your-host,other-host,yet-another
- DEV_PORT=443

You can check what values will be taken into account with
Expand All @@ -44,7 +45,8 @@ You can check what values will be taken into account with
SECRET_KEY="SeLKDmig0mYF04WVkpZ6mowJ1FiodYkC0C4ZV6Rkuvc="
DJANGO_SETTINGS_MODULE=settings.dev
SITE_URL=https://your-host.epfl.ch
ALLOWED_HOST=your-host
SERVER_HOST=your-host
ALLOWED_HOSTS=your-host,other-host,yet-another
...

The `SECRET_KEY` has been generated by the make `init-venv` command
Expand Down
3 changes: 1 addition & 2 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ web:
# does not compute their value from the other variables
- SITE_URL=${SITE_URL}
- DATABASE_URL=${DATABASE_URL}
- MOCKS_DATABASE_URL=${MOCKS_DATABASE_URL}
command: gunicorn --reload -w 4 -b :${DEV_PORT} --chdir /usr/src/app/infoscience_exports wsgi:application

nginx:
Expand All @@ -33,7 +32,7 @@ nginx:
# used in nginx/sites-enabled/web.conf
- web
environment:
- ALLOWED_HOST=${ALLOWED_HOST}
- SERVER_HOST=${SERVER_HOST}
- SITE_PATH=${SITE_PATH}
- DEV_PORT=${DEV_PORT}
- DOLLAR=$$
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ web:
# does not compute their value from the other variables
- SITE_URL=${SITE_URL}
- DATABASE_URL=${DATABASE_URL}
- MOCKS_DATABASE_URL=${MOCKS_DATABASE_URL}
command: gunicorn --reload -w 4 -b :80 --chdir /usr/src/app/infoscience_exports wsgi:application

postgres:
Expand Down
18 changes: 12 additions & 6 deletions env/django.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
# Settings to use
DJANGO_SETTINGS_MODULE=settings.test

# See https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts
ALLOWED_HOST=127.0.0.1
# `ALLOWED_HOSTS` is a comma-separated list of domain from which the system should accept requests
# > See https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts
#
# It is also used as a security check when for the search queries that the user submit on the system
# In development, it is automatically enriched with localhost,127.0.0.1,infoscience.epfl.ch...
ALLOWED_HOSTS=127.0.0.1

# `SERVER_HOST` is the domain that serves the application. It should be included in ALLOWED_HOSTS above
# In developement it is used by the nginx container to serve static files
SERVER_HOST=${ALLOWED_HOSTS}

# `DEV_PORT` is the port where to access the application.
# -> 80 in production
Expand All @@ -16,7 +24,7 @@ DEV_PORT=8000
SITE_PATH=/publications-exports

# `SITE_URL` is the official URL of the site. if not 80, PORT shoud match DEV_PORT
SITE_URL=https://${ALLOWED_HOST}:${DEV_PORT}${SITE_PATH}
SITE_URL=https://${SERVER_HOST}:${DEV_PORT}${SITE_PATH}

# `SUPERADMIN_*` define the credentials for the creation of the admin account
SUPER_ADMIN_USERNAME=infoscience-exports
Expand All @@ -35,7 +43,6 @@ CHANGELOG_GITHUB_TOKEN=${GITHUB_KEY}
####

DB_NAME=infoscience_exports
MOCKS_DB_NAME=mock_infoscience_exports

# `DATABASE_HOST` & `DATABASE_PORT` needs to be filled from the application point of view !
# wich could be either from within docker
Expand All @@ -50,11 +57,10 @@ DATABASE_PORT=5432
DATABASE_USER=django
DATABASE_PASSWORD=django

# `(MOCK_)DATABASE_URL` are the connection strings used by the application
# `DATABASE_URL` are the connection strings used by the application
# -> dialect://username:password@host:port/database
DB_URL=postgres://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}
DATABASE_URL=${DB_URL}/${DB_NAME}
MOCKS_DATABASE_URL=${DB_URL}/${MOCKS_DB_NAME}

# Development tokens
CODECOV_TOKEN=your-token
Expand Down
5 changes: 0 additions & 5 deletions fabfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ def migrate():
local('python {}/manage.py migrate'.format(env.project_name))


def migrate_mock():
with shell_env(DATABASE_PASSWORD='django'):
local('python {}/manage.py migrate --database=mock'.format(env.project_name))


def serve():
with shell_env(DATABASE_PASSWORD='django'):
local('python {}/manage.py runserver'.format(env.project_name))
Expand Down
6 changes: 3 additions & 3 deletions infoscience_exports/exports/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

# the release comes from git and should not be modified
# => read-only
_release = '0.3.4-5-g086e59f'
_release = '0.3.5-12-g97af8f6'

# you can set the next version number manually
# if you do not, the system will make sure that version > release
# => read-write, >_release
_version = '0.3.5'
_version = '0.3.6-rc'

# the build number will generate conflicts on each PR merge
# just keep yours every time
# => read-only
_build = '086e59f5d9fdf7c643968137b2f1fe3936657d3d'
_build = '97af8f67a2199caf413a8c42793ba06f675c1145'
13 changes: 2 additions & 11 deletions infoscience_exports/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_env_variable(var_name):

# Site
# https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts
ALLOWED_HOSTS = [get_env_variable('ALLOWED_HOST')]
ALLOWED_HOSTS = get_env_variable('ALLOWED_HOSTS').split(',')

INSTALLED_APPS = (
'django.contrib.admin',
Expand Down Expand Up @@ -77,7 +77,6 @@ def get_env_variable(var_name):
# Postgres
parse.uses_netloc.append("postgres")
database_url = parse.urlparse(get_env_variable("DATABASE_URL"))
mocks_url = parse.urlparse(get_env_variable("MOCKS_DATABASE_URL"))

DATABASES = {
'default': {
Expand All @@ -87,14 +86,6 @@ def get_env_variable(var_name):
'PASSWORD': database_url.password,
'HOST': database_url.hostname,
'PORT': database_url.port,
},
'mock': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': mocks_url.path[1:],
'USER': mocks_url.username,
'PASSWORD': mocks_url.password,
'HOST': mocks_url.hostname,
'PORT': mocks_url.port,
}
}

Expand All @@ -106,7 +97,7 @@ def get_env_variable(var_name):
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
Expand Down
17 changes: 8 additions & 9 deletions infoscience_exports/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

TEQUILA_SERVER_URL = "https://test-tequila.epfl.ch"

ALLOWED_HOSTS = [
get_env_variable('ALLOWED_HOST'),
"127.0.0.1",
"localhost",
"idevelopsrv25.epfl.ch",
"test-infoscience.epfl.ch",
"infoscience.epfl.ch",
]
ALLOWED_HOSTS = get_env_variable('ALLOWED_HOSTS').split(',') \
+ [ "127.0.0.1",
"localhost",
"idevelopsrv25.epfl.ch",
"test-infoscience.epfl.ch",
"infoscience.epfl.ch",
]

for config in TEMPLATES:
config['OPTIONS']['debug'] = DEBUG
Expand Down Expand Up @@ -63,4 +62,4 @@ def show_toolbar(request):

DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
}
}
2 changes: 1 addition & 1 deletion nginx/sites-enabled/web.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server {
listen ${DEV_PORT} ssl http2;

server_name ${ALLOWED_HOST};
server_name ${SERVER_HOST};
charset utf-8;

ssl on;
Expand Down
Loading