Merge pull request #3385 from certbot/filepath_refactor

Refactored get_file_path
This commit is contained in:
Noah Swartz
2016-08-10 13:11:17 -07:00
committed by GitHub
2 changed files with 28 additions and 19 deletions
+22 -19
View File
@@ -538,6 +538,9 @@ class ApacheConfigurator(augeas_configurator.AugeasConfigurator):
is_ssl = True is_ssl = True
filename = get_file_path(self.aug.get("/augeas/files%s/path" % get_file_path(path))) filename = get_file_path(self.aug.get("/augeas/files%s/path" % get_file_path(path)))
if filename is None:
return None
if self.conf("handle-sites"): if self.conf("handle-sites"):
is_enabled = self.is_site_enabled(filename) is_enabled = self.is_site_enabled(filename)
else: else:
@@ -1801,25 +1804,25 @@ def get_file_path(vhost_path):
:rtype: str :rtype: str
""" """
# Strip off /files # Strip off /files/
avail_fp = vhost_path[6:] try:
# This can be optimized... if vhost_path.startswith("/files/"):
while True: avail_fp = vhost_path[7:].split("/")
# Cast all to lowercase to be case insensitive else:
find_if = avail_fp.lower().find("/ifmodule") return None
if find_if != -1: except AttributeError:
avail_fp = avail_fp[:find_if] # If we recieved a None path
continue return None
find_vh = avail_fp.lower().find("/virtualhost")
if find_vh != -1: last_good = ""
avail_fp = avail_fp[:find_vh] # Loop through the path parts and validate after every addition
continue for p in avail_fp:
find_macro = avail_fp.lower().find("/macro") cur_path = last_good+"/"+p
if find_macro != -1: if os.path.exists(cur_path):
avail_fp = avail_fp[:find_macro] last_good = cur_path
continue else:
break break
return avail_fp return last_good
def install_ssl_options_conf(options_ssl): def install_ssl_options_conf(options_ssl):
@@ -125,6 +125,12 @@ class MultipleVhostsTest(util.ApacheTest):
self.assertTrue("google.com" in names) self.assertTrue("google.com" in names)
self.assertTrue("certbot.demo" in names) self.assertTrue("certbot.demo" in names)
def test_get_bad_path(self):
from certbot_apache.configurator import get_file_path
self.assertEqual(get_file_path(None), None)
self.assertEqual(get_file_path("nonexistent"), None)
self.assertEqual(self.config._create_vhost("nonexistent"), None) # pylint: disable=protected-access
def test_bad_servername_alias(self): def test_bad_servername_alias(self):
ssl_vh1 = obj.VirtualHost( ssl_vh1 = obj.VirtualHost(
"fp1", "ap1", set([obj.Addr(("*", "443"))]), "fp1", "ap1", set([obj.Addr(("*", "443"))]),