From 7dc090014a0090d4167e83ca6a2842d5b5c55712 Mon Sep 17 00:00:00 2001 From: Justin Littman Date: Mon, 7 Aug 2017 12:49:24 -0400 Subject: [PATCH] refs #361. Moves creating url into utility function. Adds HTTPS config to docs. --- docs/install.rst | 16 ++++++++++++++++ sfm/message_consumer/sfm_ui_consumer.py | 9 +++------ sfm/sfm/settings/docker_settings.py | 6 +++++- sfm/ui/management/commands/addsocialapp.py | 7 +++---- sfm/ui/notifications.py | 5 ++--- sfm/ui/utils.py | 7 +++++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/docs/install.rst b/docs/install.rst index f33eab88..0e577d3b 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -183,6 +183,22 @@ the change only applies to a single container, then you can stop the container w the change applies to multiple containers (or you're not sure), you can stop all containers with ``docker-compose stop``. Containers can then be brought back up with ``docker-compose up -d`` and the configuration change will take effect. +------- + HTTPS +------- +To run SFM with HTTPS: + +1. Create or acquire a valid certificate and private key. +2. In ``docker-compose.yml`` uncomment the nginx-proxy container and set the paths under ``volumes`` to point to your certificate and key. +3. In ``.env`` change ``USE_HTTPS`` to True and ``SFM_PORT`` to 8080. Make sure that ``SFM_HOSTNAME`` matches your certificate. +4. Start up SFM. + +Note: + +* HTTPS will run on 443. Port 80 will redirect to 443. +* For more information on nginx-proxy, including advanced configuration see https://github.com/jwilder/nginx-proxy. +* If you receive a 502 (bad gateway), wait until SFM UI has completely started. If the 502 continues, troubleshoot SFM UI. + ---------- Stopping ---------- diff --git a/sfm/message_consumer/sfm_ui_consumer.py b/sfm/message_consumer/sfm_ui_consumer.py index bd5d9d56..5639a0fa 100644 --- a/sfm/message_consumer/sfm_ui_consumer.py +++ b/sfm/message_consumer/sfm_ui_consumer.py @@ -3,7 +3,7 @@ from sfmutils.harvester import CODE_UNKNOWN_ERROR from ui.models import User, Harvest, Collection, Seed, Warc, Export, HarvestStat from ui.jobs import collection_stop -from ui.utils import get_email_addresses_for_collection_set +from ui.utils import get_email_addresses_for_collection_set, get_site_url from ui.export import create_readme_for_export import json @@ -11,7 +11,6 @@ from django.conf import settings from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse -from django.contrib.sites.models import Site import iso8601 import time from smtplib import SMTPException @@ -148,8 +147,7 @@ def _on_harvest_status_message(self): break if receiver_emails: - harvest_url = 'http://{}{}'.format(Site.objects.get_current().domain, - reverse('harvest_detail', args=(harvest.id,))) + harvest_url = get_site_url() + reverse('harvest_detail', args=(harvest.id,)) # Send Status mail if settings.PERFORM_EMAILS: @@ -253,8 +251,7 @@ def _on_export_status_message(self): # Get receiver's email address receiver_email = export.user.email if receiver_email: - export_url = 'http://{}{}'.format(Site.objects.get_current().domain, - reverse('export_detail', args=(export.id,))) + export_url = get_site_url() + reverse('export_detail', args=(export.id,)) # Send Status mail if settings.PERFORM_EMAILS: diff --git a/sfm/sfm/settings/docker_settings.py b/sfm/sfm/settings/docker_settings.py index 4c934f70..8b5b8f72 100644 --- a/sfm/sfm/settings/docker_settings.py +++ b/sfm/sfm/settings/docker_settings.py @@ -21,9 +21,13 @@ '1': { 'name': 'SFM', 'domain': env.get('SFM_HOST', 'localhost') + }, + '2': { + 'name': 'SFM-80', + 'domain': env.get('SFM_HOSTNAME', 'localhost') } } -SITE_ID = 1 +SITE_ID = 2 if env.get('SFM_USE_HTTPS', 'False').lower() == 'true' else 1 if 'SFM_SITE_ADMIN_EMAIL' in env: ADMINS = ((env.get('SFM_SITE_ADMIN_NAME', 'sfmadmin'), env.get('SFM_SITE_ADMIN_EMAIL')),) diff --git a/sfm/ui/management/commands/addsocialapp.py b/sfm/ui/management/commands/addsocialapp.py index 50001a8f..ad0bd4de 100644 --- a/sfm/ui/management/commands/addsocialapp.py +++ b/sfm/ui/management/commands/addsocialapp.py @@ -18,10 +18,9 @@ def handle(self, *args, **options): client_id=options['client_id'], secret=options['secret'], name=options['provider']) - site = Site.objects.all()[0] - assert site - social_app.sites.add(site) - site.save() + for site in Site.objects.all(): + social_app.sites.add(site) + site.save() self.stdout.write('Created {} social app.'.format(options['provider'])) else: self.stdout.write( diff --git a/sfm/ui/notifications.py b/sfm/ui/notifications.py index 63306a62..d8175854 100644 --- a/sfm/ui/notifications.py +++ b/sfm/ui/notifications.py @@ -10,12 +10,11 @@ from django.core.mail import EmailMultiAlternatives from django.db.models import Sum, Q from django.conf import settings -from django.contrib.sites.models import Site from django.core.urlresolvers import reverse from .models import User, CollectionSet, Collection, HarvestStat, Harvest from .sched import next_run_time -from .utils import get_admin_email_addresses +from .utils import get_admin_email_addresses, get_site_url import ui.monitoring log = logging.getLogger(__name__) @@ -387,4 +386,4 @@ def _was_harvest_in_range(range_start, range_end, collection): def _create_url(path): - return 'http://{}{}'.format(Site.objects.get_current().domain, path) + return get_site_url() + path diff --git a/sfm/ui/utils.py b/sfm/ui/utils.py index e4a86fad..99e0b739 100644 --- a/sfm/ui/utils.py +++ b/sfm/ui/utils.py @@ -1,7 +1,9 @@ from django.conf import settings from django.core.exceptions import ObjectDoesNotExist +from django.contrib.sites.models import Site import os +from os import environ as env from itertools import islice, chain import logging @@ -211,3 +213,8 @@ def get_admin_email_addresses(): for _, email_address in settings.ADMINS: email_addresses.append(email_address) return email_addresses + + +def get_site_url(): + return '{}://{}'.format("https" if env.get('SFM_USE_HTTPS', 'False').lower() == 'true' else "http", + Site.objects.get_current().domain)