This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
/
utils.py
198 lines (165 loc) · 6.58 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
#
# OpenCraft -- tools to aid developing and hosting free software projects
# Copyright (C) 2015-2019 OpenCraft <xavier@opencraft.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Utility functions related to registration.
"""
import binascii
import logging
import os
import dns.resolver as dns_resolver
from django.conf import settings
from django.contrib.auth.models import User
from simple_email_confirmation.models import EmailAddress
from email_verification import send_email_verification
from opencraft.utils import html_email_helper
from registration.models import BetaTestApplication
# Logger ######################################################################
logger = logging.getLogger(__name__)
# Utilities ###################################################################
logger = logging.getLogger(__name__)
# Functions #####################################################################
def verify_user_emails(user: User, *email_addresses: str):
"""
Start email verification process for specified email addresses.
This should ignore already-verified email addresses.
"""
for email_address in email_addresses:
if not EmailAddress.objects.filter(email=email_address).exists():
email = EmailAddress.objects.create_unconfirmed(email_address, user)
send_email_verification(email)
def send_welcome_email(application: BetaTestApplication) -> None:
"""
Send a welcome email to a new user after they have confirmed their email
addresses.
"""
user = application.user
context = dict(
user_name=user.profile.full_name,
lms_link=application.instance.get_domain('lms'),
studio_link=application.instance.get_domain('studio'),
customise_link=settings.USER_CONSOLE_FRONTEND_URL,
)
html_email_helper(
template_base_name='emails/welcome_email',
context=context,
subject=settings.WELCOME_EMAIL_SUBJECT,
recipient_list=(user.email,)
)
def send_account_info_email(application: BetaTestApplication) -> None:
"""
Send an email with account information to a new user after they have
confirmed their email addresses and their instance is set up.
"""
user = application.user
context = dict(
user_name=user.profile.full_name,
instance_url=application.instance.get_domain('lms'),
instance_name=application.instance_name,
full_name=user.profile.full_name,
email=user.email,
public_contact_email=application.public_contact_email,
)
html_email_helper(
template_base_name='emails/account_info_email',
context=context,
subject=settings.ACCOUNT_INFO_EMAIL_SUBJECT,
recipient_list=(user.email,)
)
def send_changes_deployed_success_email(application: BetaTestApplication) -> None:
"""
Send an email to user after successful redeployment of the application.
"""
instance_name = application.instance.name
user = application.user
context = dict(
see_update_url=f"{settings.USER_CONSOLE_FRONTEND_URL}/console/notice"
)
recipients = [user.email]
logger.warning(
"Sending notification e-mail to %s after instance %s was redeployed",
recipients,
instance_name,
)
html_email_helper(
template_base_name='emails/redeployment_success_email',
context=context,
subject='Open edX instance deployment: Success',
recipient_list=recipients
)
def send_dns_not_configured_email(application: BetaTestApplication) -> None:
"""
Send an email with DNS configuration details to the user after detecting
that the DNS is not configured properly
"""
user = application.user
context = dict(
cname_value=settings.EXTERNAL_DOMAIN_CNAME_VALUE,
external_domain=application.external_domain
)
logger.info("Sending email to %s for DNS Configuration for external domain", user.email)
html_email_helper(
template_base_name='emails/dns_not_configured',
context=context,
subject="OpenCraft domain verification failed!",
recipient_list=(user.email,)
)
def is_external_domain_dns_configured(domain: str) -> bool:
"""
Checks that there is proper configuration for the external_domain.
This will check for both domain as well as for the widcard subdomain
"""
return is_dns_configured(domain) and is_subdomain_dns_configured(domain)
def is_subdomain_dns_configured(domain: str) -> bool:
"""
Checks that the provided domain has a proper DNS configuration
for wildcard subdomain i.e. *.{domain}.
We can use a randomly generated long subdomain to confirm wildcard
configuration.
"""
subdomain = '{}.{}'.format(
binascii.hexlify(os.urandom(20)).decode(),
domain
)
return is_dns_configured(subdomain)
def is_dns_configured(domain: str) -> bool:
"""
Checks that the provided domain has a proper DNS configuration.
There should be a CNAME record present with value haproxy.opencraft.hosting
"""
def get_records(domain_name, record_type):
try:
return [str(rdata) for rdata in dns_resolver.resolve(domain_name, record_type)]
except (dns_resolver.NXDOMAIN, dns_resolver.NoAnswer, dns_resolver.NoNameservers):
logger.info(
"DNS not configured for external domain %s in %s",
domain_name, record_type,
)
return []
cname_records = get_records(domain, 'CNAME')
if cname_records:
matching_records = any([
record == settings.EXTERNAL_DOMAIN_CNAME_VALUE
for record in cname_records
])
if matching_records:
return True
# In case of CloudFlare proxied domains, the CNAME records are flattened and therefore,
# converted to A records. Since the A records are pointing to CloudFlare name servers,
# we cannot check them properly. Let's assume that the A records are
return bool(get_records(domain, 'A'))