ejabberd: Use Let's Encrypt certificate, also across renewals.

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Johannes Keyser authored and James Valleroy committed 2017-10-01 10:48:39 -04:00
1 parent 1e57441dec
commit f628eb2cc2
6 files changed
+377 -42

No files matched your search

+28
View File
@@ -23,10 +23,12 @@ from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import action_utils
from plinth import cfg
from plinth.errors import ActionError
from plinth.menu import main_menu
from plinth.modules import names
from plinth.utils import format_lazy
from plinth.signals import domainname_change
from plinth import module_loader
version = 1
@@ -60,6 +62,9 @@ description = [
service = None
MODULES_WITH_HOOKS = ['ejabberd']
LIVE_DIRECTORY = '/etc/letsencrypt/live/'
def init():
"""Intialize the module."""
@@ -94,6 +99,29 @@ def on_domainname_change(sender, old_domainname, new_domainname, **kwargs):
del new_domainname # Unused
del kwargs # Unused
for module in MODULES_WITH_HOOKS:
actions.superuser_run(module, ['letsencrypt', 'drop',
'--domain', old_domainname], async=True)
actions.superuser_run('letsencrypt', ['manage_hooks', 'disable',
'--domain', old_domainname],
async=True)
def get_manage_hooks_status():
"""Return status of hook management for current domain."""
try:
output = actions.superuser_run('letsencrypt',
['manage_hooks', 'status'])
except ActionError:
return False
return output.strip()
def get_installed_modules():
installed_modules = [module_name for module_name, module in
module_loader.loaded_modules.items()
if module_name in MODULES_WITH_HOOKS
and module.setup_helper.get_state() == 'up-to-date']
return installed_modules
@@ -34,6 +34,9 @@
{% endblock %}
{% block configuration %}
<h3>{% trans "Configuration" %}</h3>
{% if status.domains %}
<div class="row">
<div class="col-lg-12">
@@ -134,6 +137,18 @@
</div>
</div>
{% include "diagnostics_button.html" with module="letsencrypt" enabled=True %}
<h4>{% trans "Certificate renewal management and use by other modules" %}</h4>
<p class="help-block">
{% blocktrans trimmed %}
If you have a Let's Encrypt certificate for your current domain, you may
let {{ box_name }} manage its renewal process. This also enables other apps
to use that certificate, so most users would not prompted with security
warnings when using them.
{% endblocktrans %}
</p>
<form class="form" method="post"
action="{% url 'letsencrypt:toggle_hooks' status.current_domain.name %}">
{% csrf_token %}
@@ -141,7 +156,9 @@
<label>
{% if status.current_domain.name and status.current_domain.has_cert %}
<input type="checkbox" name="toggle_hooks" id="id_toggle_hooks"
{% if status.current_domain.manage_hooks_enabled %}checked{% endif %}
{% if 'enabled' in status.current_domain.manage_hooks_status %}
checked
{% endif %}
onchange="this.form.submit();">
</input>
<noscript>
@@ -159,12 +176,12 @@
<span>
{% if status.current_domain.name %}
{% blocktrans with current_domain=status.current_domain.name %}
Let Plinth manage certificate renewal of
<b>{{ current_domain }}</b> (recommended)
Let {{ box_name }} manage certificate renewal of
<b>{{ current_domain }}</b>
{% endblocktrans %}
{% else %}
{% blocktrans with current_domain=status.current_domain.name %}
Let Plinth manage certificate renewal of the current domain (recommended)
Let {{ box_name }} manage certificate renewal of the current domain
{% endblocktrans %}
{% endif %}
</span>
@@ -173,14 +190,13 @@
<p class="help-block">
{% if status.current_domain.name and status.current_domain.has_cert %}
{% blocktrans trimmed %}
If {{ box_name }} manages the certificate renewal for the current domain,
it will ensure that all apps that use the certificate can use it, as soon
as it gets renewed.
If enabled, {{ box_name }} can make sure that all apps can use the
certificate as soon as it is renewed.
{% endblocktrans %}
{% elif not status.current_domain.has_cert %}
{% blocktrans trimmed %}
<b>No certificate available for the current domain.</b>
First obtain a certificate to enable management of its renewal.
First obtain a certificate to enable its management.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
@@ -190,7 +206,66 @@
{% endif %}
</p>
</form>
{% include "diagnostics_button.html" with module="letsencrypt" enabled=True %}
<form class="form" method="post"
action="{% url 'letsencrypt:toggle_module' domain=status.current_domain.name module='ejabberd' %}">
{% csrf_token %}
<div class="checkbox">
<label>
{% if 'ejabberd' in installed_modules and 'enabled' in status.current_domain.manage_hooks_status %}
<input type="checkbox" name="ejabberd" id="ejabberd"
{% if 'ejabberd' in status.current_domain.manage_hooks_status %}
checked
{% endif %}
onchange="this.form.submit();">
</input>
<noscript>
<button class="btn btn-sm btn-default" type="submit">
{% trans "Update config" %}</button>
</noscript>
{% else %}
<input type="checkbox" name="ejabberd" id="ejabberd"
class="disabled"></input>
<noscript>
<button class="btn btn-sm btn-default disabled" type="submit">
{% trans "Update config" %}</button>
</noscript>
{% endif %}
<span>
{% if 'ejabberd' in installed_modules and 'enabled' in status.current_domain.manage_hooks_status %}
{% blocktrans with current_domain=status.current_domain.name %}
Use certificate of {{ current_domain }} for <b>ejabberd</b>
{% endblocktrans %}
{% else %}
{% blocktrans %}
Use certificate of the current domain for <b>ejabberd</b>
{% endblocktrans %}
{% endif %}
</span>
</label>
</div>
<p class="help-block">
{% url 'ejabberd:index' as ejabberd_url %}
{% if 'ejabberd' in installed_modules and 'enabled' in status.current_domain.manage_hooks_status %}
{% blocktrans trimmed %}
If enabled, the app <a href="{{ ejabberd_url }}">ejabberd</a> will also use the
Let's Encrypt certificate.
This will reduce warnings about self-signed certificates in client applications,
and enable more wide-spread federation with other XMPP servers in the Internet.
{% endblocktrans %}
{% elif 'ejabberd' not in installed_modules %}
{% blocktrans trimmed %}
This feature only makes sense if you are using the
<a href="{{ ejabberd_url }}">ejabberd</a> chat server app.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
To use a Let's Encrypt certificate for <a href="{{ ejabberd_url }}">ejabberd</a>
chat server app, you must first enable certificate renewal of the current domain.
{% endblocktrans %}
{% endif %}
</p>
</form>
{% else %}
{% blocktrans trimmed %}
No domains have been configured. Configure domains to be able to
+2
View File
@@ -33,4 +33,6 @@ urlpatterns = [
name='delete'),
url(r'^sys/letsencrypt/toggle_hooks/(?P<domain>[^/]+)/$',
views.toggle_hooks, name='toggle_hooks'),
url(r'^sys/letsencrypt/toggle_module/(?P<domain>[^/]+)/'
'(?P<module>[^/]+)/$', views.toggle_module, name='toggle_module'),
]
+57 -16
View File
@@ -41,11 +41,12 @@ logger = logging.getLogger(__name__)
def index(request):
"""Serve configuration page."""
status = get_status()
return TemplateResponse(request, 'letsencrypt.html',
{'title': letsencrypt.name,
'description': letsencrypt.description,
'status': status})
'status': status,
'installed_modules':
letsencrypt.get_installed_modules()})
@require_POST
@@ -102,8 +103,17 @@ def obtain(request, domain):
@require_POST
def toggle_hooks(request, domain):
"""Toggle pointing of certbot's hooks to Plinth, for the current domain."""
subcommand = 'disable' if _hooks_manage_enabled() else 'enable'
manage_hooks_status = letsencrypt.get_manage_hooks_status()
subcommand = 'disable' if 'enabled' in manage_hooks_status else 'enable'
try:
if subcommand == 'disable':
enabled_modules = [module for module in
letsencrypt.MODULES_WITH_HOOKS
if module in manage_hooks_status]
for module in enabled_modules:
actions.superuser_run(module, ['letsencrypt', 'drop'],
async=True)
actions.superuser_run('letsencrypt', ['manage_hooks', subcommand])
if subcommand == 'enable':
msg = _('Certificate renewal management enabled for {domain}.')\
@@ -122,10 +132,51 @@ def toggle_hooks(request, domain):
return redirect(reverse_lazy('letsencrypt:index'))
@require_POST
def toggle_module(request, domain, module):
"""Toggle usage of LE cert for a module name, for the current domain."""
manage_hooks_status = letsencrypt.get_manage_hooks_status()
enabled_modules = [module for module in letsencrypt.MODULES_WITH_HOOKS
if module in manage_hooks_status]
if module in enabled_modules:
mod_le_arg = 'drop'
enabled_modules.remove(module)
else:
mod_le_arg = 'add'
enabled_modules.append(module)
module_args = ['letsencrypt', mod_le_arg]
le_arguments = ['manage_hooks', 'enable']
if not enabled_modules == []:
le_arguments.extend(['--modules', ' '.join(enabled_modules)])
try:
actions.superuser_run(module, module_args)
actions.superuser_run('letsencrypt', le_arguments)
messages.success(
request, _('Switched use of certificate for app {module}')
.format(module=module))
except ActionError as exception:
messages.error(
request,
_('Failed to switch certificate use for app {module}: {error}')
.format(module=module, error=exception.args[2]))
return redirect(reverse_lazy('letsencrypt:index'))
@require_POST
def delete(request, domain):
"""Delete a certificate for a given domain."""
try: # also delete any potential left-over renewal config backup
"""Delete a certificate for a given domain, and cleanup renewal config."""
manage_hooks_status = letsencrypt.get_manage_hooks_status()
enabled_modules = [module for module in letsencrypt.MODULES_WITH_HOOKS
if module in manage_hooks_status]
try:
for module in enabled_modules:
actions.superuser_run(module, ['letsencrypt', 'drop'], async=True)
actions.superuser_run('letsencrypt', ['manage_hooks', 'disable',
'--domain', domain])
except ActionError as exception:
@@ -158,7 +209,7 @@ def get_status():
'name': curr_dom,
'has_cert': (curr_dom in status['domains'] and
status['domains'][curr_dom]['certificate_available']),
'manage_hooks_enabled': _hooks_manage_enabled()
'manage_hooks_status': letsencrypt.get_manage_hooks_status()
}
status['current_domain'] = current_domain
@@ -171,13 +222,3 @@ def get_status():
status['domains'].setdefault(domain, {})
return status
def _hooks_manage_enabled():
"""Return status of hook management for current domain."""
try:
output = actions.superuser_run('letsencrypt',
['manage_hooks', 'status'])
except ActionError:
return False
return output.strip() == 'enabled'