letsencrypt: Add option to let Plinth "manage" certbot's renewal hooks

- add action commands for managing and running hooks

- adding template, view and url for managing and running hooks

- disable Plinth hook management on domain change
This commit is contained in:
Johannes Keyser authored and James Valleroy committed 2017-07-19 21:47:00 -04:00
1 parent 1d4289068a
commit 1069f84154
6 files changed
+334 -6

No files matched your search

+14 -1
View File
@@ -20,12 +20,13 @@ Plinth module for using Let's Encrypt.
"""
from django.utils.translation import ugettext_lazy as _
from plinth import actions
from plinth import action_utils
from plinth import cfg
from plinth.menu import main_menu
from plinth.modules import names
from plinth.utils import format_lazy
from plinth.signals import domainname_change
version = 1
@@ -63,6 +64,7 @@ def init():
menu = main_menu.get('system')
menu.add_urlname(_('Certificates (Let\'s Encrypt)'),
'glyphicon-lock', 'letsencrypt:index')
domainname_change.connect(on_domainname_change)
def setup(helper, old_version=None):
@@ -82,3 +84,14 @@ def diagnose():
results.append(action_utils.diagnose_url('https://' + domain))
return results
def on_domainname_change(sender, old_domainname, new_domainname, **kwargs):
"""Disable renewal hook management after a domain name change."""
del sender # Unused
del new_domainname # Unused
del kwargs # Unused
actions.superuser_run('letsencrypt', ['manage_hooks', 'disable',
'--domain', old_domainname],
async=True)
@@ -49,7 +49,13 @@
<tbody>
{% for domain, domain_status in status.domains.items %}
<tr>
<td>{{ domain }}</td>
<td>
{% if domain == status.current_domain.name %}
<b>{{ domain }}</b>
{% else %}
{{ domain }}
{% endif %}
</td>
<td>
{% if domain_status.certificate_available and domain_status.validity == "valid" %}
<span class="label label-success">
@@ -128,6 +134,62 @@
</div>
</div>
<form class="form" method="post"
action="{% url 'letsencrypt:toggle_hooks' status.current_domain.name %}">
{% csrf_token %}
<div class="checkbox">
<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 %}
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="toggle_hooks" id="id_toggle_hooks"
class="disabled"></input>
<noscript>
<button class="btn btn-sm btn-default disabled" type="submit">
{% trans "Update config" %}</button>
</noscript>
{% endif %}
<span>
{% if status.current_domain.name %}
{% blocktrans with current_domain=status.current_domain.name %}
Let Plinth manage certificate renewal of
<b>{{ current_domain }}</b> (recommmended)
{% endblocktrans %}
{% else %}
{% blocktrans with current_domain=status.current_domain.name %}
Let Plinth manage certificate renewal of the current domain (recommmended)
{% endblocktrans %}
{% endif %}
</span>
</label>
</div>
<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.
{% 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.
{% endblocktrans %}
{% else %}
{% blocktrans trimmed %}
<b>No current domain is configured.</b>
First configure a domain to enable management of its certificates.
{% endblocktrans %}
{% endif %}
</p>
</form>
{% include "diagnostics_button.html" with module="letsencrypt" enabled=True %}
{% else %}
{% blocktrans trimmed %}
+2
View File
@@ -31,4 +31,6 @@ urlpatterns = [
name='obtain'),
url(r'^sys/letsencrypt/delete/(?P<domain>[^/]+)/$', views.delete,
name='delete'),
url(r'^sys/letsencrypt/toggle_hooks/(?P<domain>[^/]+)/$',
views.toggle_hooks, name='toggle_hooks'),
]
+51
View File
@@ -32,6 +32,7 @@ from plinth import actions
from plinth.errors import ActionError
from plinth.modules import letsencrypt
from plinth.modules import names
from plinth.modules.config import config
logger = logging.getLogger(__name__)
@@ -69,14 +70,48 @@ def obtain(request, domain):
"""Obtain and install a certificate for a given domain."""
try:
actions.superuser_run('letsencrypt', ['obtain', '--domain', domain])
actions.superuser_run('letsencrypt', ['manage_hooks', 'enable'])
messages.success(
request, _('Certificate successfully obtained for domain {domain}')
.format(domain=domain))
successful_obtain = True
except ActionError as exception:
messages.error(
request,
_('Failed to obtain certificate for domain {domain}: {error}')
.format(domain=domain, error=exception.args[2]))
successful_obtain = False
if domain == config.get_domainname() and successful_obtain:
try:
actions.superuser_run('letsencrypt', ['manage_hooks', 'enable'])
messages.success(
request, _('Certificate management enabled for {domain}.')
.format(domain=domain))
except ActionError as exception:
messages.error(
request,
_('Failed to enable certificate management for {domain}: '
'{error}')
.format(domain=domain, error=exception.args[2]))
return redirect(reverse_lazy('letsencrypt:index'))
@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'
try:
actions.superuser_run('letsencrypt', ['manage_hooks', subcommand])
messages.success(
request, _('Certificate management changed for domain {domain}')
.format(domain=domain))
except ActionError as exception:
messages.error(
request,
_('Failed to switch certificate management for {domain}: {error}')
.format(domain=domain, error=exception.args[2]))
return redirect(reverse_lazy('letsencrypt:index'))
@@ -102,6 +137,12 @@ def get_status():
"""Get the current settings."""
status = actions.superuser_run('letsencrypt', ['get-status'])
status = json.loads(status)
curr_dom = config.get_domainname()
current_domain = {'name': curr_dom,
'has_cert': curr_dom in status['domains'] and
status['domains'][curr_dom]['certificate_available'],
'manage_hooks_enabled': _hooks_manage_enabled()}
status['current_domain'] = current_domain
for domain_type, domains in names.domains.items():
# XXX: Remove when Let's Encrypt supports .onion addresses
@@ -112,3 +153,13 @@ 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'