mirror of
https://github.com/certbot/certbot.git
synced 2026-08-03 03:41:53 +02:00
Add method and test for dumping nginx configs
This commit is contained in:
@@ -105,6 +105,4 @@ def dumps(blocks, indentation=4):
|
|||||||
|
|
||||||
|
|
||||||
def dump(blocks, _file, indentation=4):
|
def dump(blocks, _file, indentation=4):
|
||||||
_file.write(dumps(blocks, indentation))
|
return _file.write(dumps(blocks, indentation))
|
||||||
_file.close()
|
|
||||||
return _file
|
|
||||||
|
|||||||
@@ -129,13 +129,15 @@ class NginxParser(object):
|
|||||||
"""
|
"""
|
||||||
enabled = True # We only look at enabled vhosts for now
|
enabled = True # We only look at enabled vhosts for now
|
||||||
vhosts = []
|
vhosts = []
|
||||||
for filename, tree in self.parsed:
|
for filename in self.parsed:
|
||||||
|
tree = self.parsed[filename]
|
||||||
vhost = obj.VirtulHost(filename,
|
vhost = obj.VirtulHost(filename,
|
||||||
self._get_addrs(tree),
|
self._get_addrs(tree),
|
||||||
self._get_ssl(tree),
|
self._get_ssl(tree),
|
||||||
enabled,
|
enabled,
|
||||||
self._get_names(tree))
|
self._get_names(tree))
|
||||||
vhosts.append(vhost)
|
vhosts.append(vhost)
|
||||||
|
return vhosts
|
||||||
|
|
||||||
def add_dir_to_ifmodssl(self, aug_conf_path, directive, val):
|
def add_dir_to_ifmodssl(self, aug_conf_path, directive, val):
|
||||||
"""Adds directive and value to IfMod ssl block.
|
"""Adds directive and value to IfMod ssl block.
|
||||||
@@ -369,9 +371,10 @@ class NginxParser(object):
|
|||||||
if f in self.parsed:
|
if f in self.parsed:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
parsed = load(open(f))
|
with open(f) as fo:
|
||||||
self.parsed[f] = parsed
|
parsed = load(fo)
|
||||||
trees.append(parsed)
|
self.parsed[f] = parsed
|
||||||
|
trees.append(parsed)
|
||||||
except IOError:
|
except IOError:
|
||||||
logging.warn("Could not open file: %s" % f)
|
logging.warn("Could not open file: %s" % f)
|
||||||
except pyparsing.ParseException:
|
except pyparsing.ParseException:
|
||||||
@@ -431,6 +434,23 @@ class NginxParser(object):
|
|||||||
raise errors.LetsEncryptNoInstallationError(
|
raise errors.LetsEncryptNoInstallationError(
|
||||||
"Could not find configuration root")
|
"Could not find configuration root")
|
||||||
|
|
||||||
|
def filedump(self, ext='tmp'):
|
||||||
|
"""Dumps parsed configurations into files.
|
||||||
|
|
||||||
|
:param str ext: The file extension to use for the dumped files. If
|
||||||
|
empty, this overrides the existing conf files.
|
||||||
|
|
||||||
|
"""
|
||||||
|
for filename in self.parsed:
|
||||||
|
tree = self.parsed[filename]
|
||||||
|
if ext:
|
||||||
|
filename = filename + os.path.extsep + ext
|
||||||
|
try:
|
||||||
|
with open(filename, 'w') as f:
|
||||||
|
dump(tree, f)
|
||||||
|
except IOError:
|
||||||
|
logging.error("Could not open file for writing: %s" % filename)
|
||||||
|
|
||||||
|
|
||||||
def case_i(string):
|
def case_i(string):
|
||||||
"""Returns case insensitive regex.
|
"""Returns case insensitive regex.
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ class TestRawNginxParser(unittest.TestCase):
|
|||||||
['index', 'index.html index.htm']]]]])
|
['index', 'index.html index.htm']]]]])
|
||||||
f = open(util.get_data_filename('nginx.new.conf'), 'w')
|
f = open(util.get_data_filename('nginx.new.conf'), 'w')
|
||||||
dump(parsed, f)
|
dump(parsed, f)
|
||||||
|
f.close()
|
||||||
parsed_new = load(open(util.get_data_filename('nginx.new.conf')))
|
parsed_new = load(open(util.get_data_filename('nginx.new.conf')))
|
||||||
self.assertEquals(parsed, parsed_new)
|
self.assertEquals(parsed, parsed_new)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
"""Tests for letsencrypt.client.plugins.nginx.parser."""
|
"""Tests for letsencrypt.client.plugins.nginx.parser."""
|
||||||
|
import glob
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
@@ -29,8 +30,8 @@ class NginxParserTest(util.NginxTest):
|
|||||||
shutil.rmtree(self.work_dir)
|
shutil.rmtree(self.work_dir)
|
||||||
|
|
||||||
def test_root_normalized(self):
|
def test_root_normalized(self):
|
||||||
path = os.path.join(self.temp_dir, "debian_nginx_2_4/////"
|
path = os.path.join(self.temp_dir, "foo/////"
|
||||||
"two_vhost_80/../../testdata")
|
"bar/../../testdata")
|
||||||
parser = NginxParser(path, None)
|
parser = NginxParser(path, None)
|
||||||
self.assertEqual(parser.root, self.config_path)
|
self.assertEqual(parser.root, self.config_path)
|
||||||
|
|
||||||
@@ -46,20 +47,38 @@ class NginxParserTest(util.NginxTest):
|
|||||||
"""Test recursive conf file parsing.
|
"""Test recursive conf file parsing.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.parser = NginxParser(self.config_path, self.ssl_options)
|
parser = NginxParser(self.config_path, self.ssl_options)
|
||||||
self.assertEqual(set(map(self.parser.abs_path,
|
self.assertEqual(set(map(parser.abs_path,
|
||||||
['foo.conf', 'nginx.conf', 'server.conf',
|
['foo.conf', 'nginx.conf', 'server.conf',
|
||||||
'sites-enabled/default',
|
'sites-enabled/default',
|
||||||
'sites-enabled/example.com'])),
|
'sites-enabled/example.com'])),
|
||||||
set(self.parser.parsed.keys()))
|
set(parser.parsed.keys()))
|
||||||
self.assertEqual([['server_name', 'somename alias another.alias']],
|
self.assertEqual([['server_name', 'somename alias another.alias']],
|
||||||
self.parser.parsed[self.parser.abs_path(
|
parser.parsed[parser.abs_path('server.conf')])
|
||||||
'server.conf')])
|
|
||||||
self.assertEqual([[['server'], [['listen', '9000'],
|
self.assertEqual([[['server'], [['listen', '9000'],
|
||||||
['server_name', 'example.com']]]],
|
['server_name', 'example.com']]]],
|
||||||
self.parser.parsed[self.parser.abs_path(
|
parser.parsed[parser.abs_path(
|
||||||
'sites-enabled/example.com')])
|
'sites-enabled/example.com')])
|
||||||
|
|
||||||
|
def test_abs_path(self):
|
||||||
|
parser = NginxParser(self.config_path, self.ssl_options)
|
||||||
|
self.assertEqual('/etc/nginx/*', parser.abs_path('/etc/nginx/*'))
|
||||||
|
self.assertEqual(os.path.join(self.config_path, 'foo/bar/'),
|
||||||
|
parser.abs_path('foo/bar/'))
|
||||||
|
|
||||||
|
def test_filedump(self):
|
||||||
|
parser = NginxParser(self.config_path, self.ssl_options)
|
||||||
|
parser.filedump('test')
|
||||||
|
# pylint: disable=protected-access
|
||||||
|
parsed = parser._parse_files(parser.abs_path(
|
||||||
|
'sites-enabled/example.com.test'))
|
||||||
|
self.assertEqual(3, len(glob.glob(parser.abs_path('*.test'))))
|
||||||
|
self.assertEqual(2, len(
|
||||||
|
glob.glob(parser.abs_path('sites-enabled/*.test'))))
|
||||||
|
self.assertEqual([[['server'], [['listen', '9000'],
|
||||||
|
['server_name', 'example.com']]]],
|
||||||
|
parsed[0])
|
||||||
|
|
||||||
# def test_find_dir(self):
|
# def test_find_dir(self):
|
||||||
# from letsencrypt.client.plugins.nginx.parser import case_i
|
# from letsencrypt.client.plugins.nginx.parser import case_i
|
||||||
# test = self.parser.find_dir(case_i("Listen"), "443")
|
# test = self.parser.find_dir(case_i("Listen"), "443")
|
||||||
|
|||||||
Reference in New Issue
Block a user