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

+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'