Allow enforcing HTTPS for custom domains

azure
David Fischer 2018-08-06 15:15:00 -07:00
parent ae9a6d3c5a
commit a0a2b26c74
No known key found for this signature in database
GPG Key ID: F0C9B0ADA737AB60
5 changed files with 28 additions and 14 deletions

View File

@ -129,7 +129,7 @@ class ResolverBase(object):
def resolve_domain(self, project, private=None):
# pylint: disable=unused-argument
canonical_project = self._get_canonical_project(project)
domain = canonical_project.domains.filter(canonical=True).first()
domain = self._get_project_custom_domain(canonical_project)
if domain:
return domain.domain
elif self._use_subdomain():
@ -144,13 +144,24 @@ class ResolverBase(object):
version_slug = project.get_default_version()
private = self._get_private(project, version_slug)
domain = self.resolve_domain(project, private=private)
canonical_project = self._get_canonical_project(project)
custom_domain = self._get_project_custom_domain(canonical_project)
# Use HTTPS if settings specify
public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
use_https = getattr(settings, 'PUBLIC_DOMAIN_USES_HTTPS', False)
if use_https and public_domain and public_domain in domain:
protocol = 'https'
if custom_domain:
domain = custom_domain.domain
elif self._use_subdomain():
domain = self._get_project_subdomain(canonical_project)
else:
domain = getattr(settings, 'PRODUCTION_DOMAIN')
if custom_domain:
protocol = 'https' if custom_domain.https else 'http'
else:
# Use HTTPS if settings specify
public_domain = getattr(settings, 'PUBLIC_DOMAIN', None)
use_https = getattr(settings, 'PUBLIC_DOMAIN_USES_HTTPS', False)
if use_https and public_domain and public_domain in domain:
protocol = 'https'
return '{protocol}://{domain}{path}'.format(
protocol=protocol,
@ -196,6 +207,9 @@ class ResolverBase(object):
subdomain_slug = project.slug.replace('_', '-')
return "%s.%s" % (subdomain_slug, public_domain)
def _get_project_custom_domain(self, project):
return project.domains.filter(canonical=True).first()
def _get_private(self, project, version_slug):
from readthedocs.builds.models import Version
try:

View File

@ -184,7 +184,7 @@ class DomainAdmin(admin.ModelAdmin):
list_display = ('domain', 'project', 'https', 'count')
search_fields = ('domain', 'project__slug')
raw_id_fields = ('project',)
list_filter = ('canonical',)
list_filter = ('canonical', 'https')
model = Domain

View File

@ -634,7 +634,7 @@ class DomainForm(forms.ModelForm):
class Meta(object):
model = Domain
exclude = ['machine', 'cname', 'count', 'https']
exclude = ['machine', 'cname', 'count']
def __init__(self, *args, **kwargs):
self.project = kwargs.pop('project', None)

View File

@ -985,7 +985,7 @@ class Domain(models.Model):
https = models.BooleanField(
_('Use HTTPS'),
default=False,
help_text=_('SSL is enabled for this domain')
help_text=_('Always use HTTPS for this domain')
)
count = models.IntegerField(default=0, help_text=_(
'Number of times this domain has been hit'),)

View File

@ -745,16 +745,16 @@ class TestSubprojectsWithTranslations(TestCase):
)
url = resolve(self.superproject_en, filename='')
self.assertEqual(url, 'http://docs.example.com/en/latest/')
self.assertEqual(url, 'https://docs.example.com/en/latest/')
url = resolve(self.superproject_es, filename='')
self.assertEqual(url, 'http://docs.example.com/es/latest/')
self.assertEqual(url, 'https://docs.example.com/es/latest/')
# yapf: disable
url = resolve(self.subproject_en, filename='')
self.assertEqual(
url,
('http://docs.example.com/projects/'
('https://docs.example.com/projects/'
'{subproject.slug}/en/latest/').format(
subproject=self.subproject_en,
),
@ -763,7 +763,7 @@ class TestSubprojectsWithTranslations(TestCase):
url = resolve(self.subproject_es, filename='')
self.assertEqual(
url,
('http://docs.example.com/projects/'
('https://docs.example.com/projects/'
'{subproject.slug}/es/latest/').format(
subproject=self.subproject_en,
),