Compare commits

...

2 Commits

Author SHA1 Message Date
Joona Hoikkala
69364f65d3 Fix linter errors 2018-07-24 16:57:29 +03:00
Joona Hoikkala
948de4a16a Fix pre-existing VirtualHost object paths when adding a new one 2018-07-24 16:34:48 +03:00
3 changed files with 82 additions and 0 deletions

View File

@@ -306,6 +306,7 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
a lack of directives
"""
logger.debug("Processing domain %s...", domain)
vhosts = self.choose_vhosts(domain)
for vhost in vhosts:
self._deploy_cert(vhost, cert_path, key_path, chain_path, fullchain_path)
@@ -1127,6 +1128,7 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
ssl_vhost.ancestor = nonssl_vhost
self.vhosts.append(ssl_vhost)
self._fix_vhost_paths(ssl_vhost)
# NOTE: Searches through Augeas seem to ruin changes to directives
# The configuration must also be saved before being searched
@@ -1139,6 +1141,15 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
return ssl_vhost
def _fix_vhost_paths(self, new_vhost):
"""
Helper method for make_vhost_ssl for fixing paths for pre-existing
VirtualHosts that reside in the same path, and might need indices after
adding a new VirtualHost.
"""
for i, vhost in enumerate(self.vhosts):
self.vhosts[i].path = self.parser.fix_path_indices(vhost.path, new_vhost.path)
def _get_new_vh_path(self, orig_matches, new_matches):
""" Helper method for make_vhost_ssl for matching augeas paths. Returns
VirtualHost path from new_matches that's not present in orig_matches.

View File

@@ -694,6 +694,53 @@ class ApacheParser(object):
remove_old = False
return use_new, remove_old
def fix_path_indices(self, old_path, new_path):
"""Fix Augeas path if required.
This function adds index to Auges path if there's a new node created to
the corresponding path.
:param str old_path: Path to fix
:param str new_path: Newly created path
:returns: A fixed Augeas DOM path
:rtype: str
"""
new_fragments = new_path.split("/")
old_fragments = old_path.split("/")
fixed_path = []
div_path = False
for i, v in enumerate(old_fragments):
if div_path:
# Paths are diverged from here on, so use old_path values
fixed_path.append(v)
else:
try:
fixed_path.append(self._fix_path(v, new_fragments[i]))
except IndexError:
fixed_path.append(v)
except ValueError:
div_path = True
fixed_path.append(v)
return "/".join(fixed_path)
def _fix_path(self, old, new):
"""Helper function to add index to Augeas path if required"""
index_re = r"\[\d*\]"
if old == new:
return old
if re.search(index_re, old):
# Already has an index
return old
if re.sub(index_re, "", new) == old:
# Path matches, but index needs to get added, starts at 1
return old+"[1]"
# Raise ValueError to signal path divergence
raise ValueError
def _remove_httpd_transform(self, filepath):
"""Remove path from Augeas transform

View File

@@ -12,6 +12,7 @@ from certbot_apache.tests import util
class BasicParserTest(util.ParserTest):
# pylint: disable=too-many-public-methods
"""Apache Parser Test."""
def setUp(self): # pylint: disable=arguments-differ
@@ -299,6 +300,29 @@ class BasicParserTest(util.ParserTest):
errors.MisconfigurationError,
self.parser.update_runtime_variables)
def test_fix_path_indices_match(self):
old_path = new_path = "/augeas/dom/path[1]/match"
self.assertEquals(self.parser.fix_path_indices(old_path, new_path),
old_path)
def test_fix_path_indices_needs_index(self):
old_path = "/path/index/needed"
new_path = "/path/index[2]/needed"
fixed_path = self.parser.fix_path_indices(old_path, new_path)
self.assertEquals(fixed_path, "/path/index[1]/needed")
def test_fix_path_indices_shorter_new(self):
old_path = "/path/index/needed"
new_path = "/path/index[2]"
fixed_path = self.parser.fix_path_indices(old_path, new_path)
self.assertEquals(fixed_path, "/path/index[1]/needed")
def test_fix_path_indices_needs_index_divergence(self):
old_path = "/path/index/needed/unique/path"
new_path = "/path/index[2]/needed/another/path"
fixed_path = self.parser.fix_path_indices(old_path, new_path)
self.assertEquals(fixed_path, "/path/index[1]/needed/unique/path")
def test_add_comment(self):
from certbot_apache.parser import get_aug_path
self.parser.add_comment(get_aug_path(self.parser.loc["name"]), "123456")