From 486f91d1290e1d38860acbd6e017106785a08178 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 13 Jun 2019 12:47:22 -0700 Subject: [PATCH] letsencrypt: Remove old style hooks from all configuration files Signed-off-by: Sunil Mohan Adapa Reviewed-by: Joseph Nuthalapati --- actions/letsencrypt | 49 ++++++++++++++++++++++++++ plinth/modules/letsencrypt/__init__.py | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/actions/letsencrypt b/actions/letsencrypt index dcf09632e..5cef58287 100755 --- a/actions/letsencrypt +++ b/actions/letsencrypt @@ -20,12 +20,15 @@ Configuration helper for Let's Encrypt. """ import argparse +import glob import json import os import re import subprocess import sys +import configobj + from plinth import action_utils from plinth.modules import letsencrypt as le @@ -141,6 +144,10 @@ def subcommand_setup(arguments): Nothing to do for first time setup and for newer versions. """ + if arguments.old_version == 2: + _remove_old_hooks() + return + if arguments.old_version != 1: return @@ -202,6 +209,48 @@ def subcommand_obtain(arguments): setup_webserver_config(domain, webserver_change) +def _remove_old_hooks(): + """Remove old style renewal hooks from individual configuration files. + + This has been replaced with global hooks by adding script files in directory + /etc/letsencrypt/renewal-hooks/{pre,post,deploy}/. + + """ + for file_path in glob.glob(RENEWAL_DIRECTORY + '*.conf'): + try: + _remove_old_hooks_from_file(file_path) + except Exception as exception: + print('Error removing hooks from file:', file_path, exception) + + +def _remove_old_hooks_from_file(file_path): + """Remove old style hooks from a single configuration file.""" + config = configobj.ConfigObj(file_path) + edited = False + for line in config.initial_comment: + if 'edited by plinth' in line.lower(): + edited = True + + if not edited: + return + + config.initial_comment = [ + line for line in config.initial_comment + if 'edited by plinth' not in line.lower() + ] + + if 'pre_hook' in config['renewalparams']: + del config['renewalparams']['pre_hook'] + + if 'renew_hook' in config['renewalparams']: + del config['renewalparams']['renew_hook'] + + if 'post_hook' in config['renewalparams']: + del config['renewalparams']['post_hook'] + + config.write() + + def subcommand_run_pre_hooks(_): """Do nothing, kept for legacy LE configuration. diff --git a/plinth/modules/letsencrypt/__init__.py b/plinth/modules/letsencrypt/__init__.py index 84423cb61..860f0fabc 100644 --- a/plinth/modules/letsencrypt/__init__.py +++ b/plinth/modules/letsencrypt/__init__.py @@ -33,7 +33,7 @@ from plinth.utils import format_lazy from .manifest import backup -version = 2 +version = 3 is_essential = True