diff options
author | ohemorange <ebportnoy@gmail.com> | 2017-05-16 01:30:50 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-16 01:30:50 +0300 |
commit | 23e6c28d802648944638d8b0c054543f72d1d5e6 (patch) | |
tree | a03eb320ab39a7d33979fe6c3974b75388273f70 /certbot-nginx | |
parent | 6723b15f786c9ac25bc8d7aefd051da1a6d9fdb2 (diff) |
Allow Nginx to insert include files with comments inside (#4666)
* add failing test case
* allow include files to insert comments
* lint
Diffstat (limited to 'certbot-nginx')
-rw-r--r-- | certbot-nginx/certbot_nginx/parser.py | 8 | ||||
-rw-r--r-- | certbot-nginx/certbot_nginx/tests/parser_test.py | 29 | ||||
-rw-r--r-- | certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf | 1 |
3 files changed, 35 insertions, 3 deletions
diff --git a/certbot-nginx/certbot_nginx/parser.py b/certbot-nginx/certbot_nginx/parser.py index 558275b0d..4e4aa36ca 100644 --- a/certbot-nginx/certbot_nginx/parser.py +++ b/certbot-nginx/certbot_nginx/parser.py @@ -529,7 +529,10 @@ def _add_directive(block, directive, replace): """ directive = nginxparser.UnspacedList(directive) - if len(directive) == 0 or directive[0] == '#': + def is_whitespace_or_comment(directive): + """Is this directive either a whitespace or comment directive?""" + return len(directive) == 0 or directive[0] == '#' + if is_whitespace_or_comment(directive): # whitespace or comment block.append(directive) return @@ -574,7 +577,8 @@ def _add_directive(block, directive, replace): for included_directive in included_directives: included_dir_loc = find_location(included_directive) included_dir_name = included_directive[0] - if not can_append(included_dir_loc, included_dir_name): + if not is_whitespace_or_comment(included_directive) \ + and not can_append(included_dir_loc, included_dir_name): if block[included_dir_loc] != included_directive: raise errors.MisconfigurationError(err_fmt.format(included_directive, block[included_dir_loc])) diff --git a/certbot-nginx/certbot_nginx/tests/parser_test.py b/certbot-nginx/certbot_nginx/tests/parser_test.py index 7b33b1075..3877bf5d4 100644 --- a/certbot-nginx/certbot_nginx/tests/parser_test.py +++ b/certbot-nginx/certbot_nginx/tests/parser_test.py @@ -13,7 +13,7 @@ from certbot_nginx import parser from certbot_nginx.tests import util -class NginxParserTest(util.NginxTest): +class NginxParserTest(util.NginxTest): #pylint: disable=too-many-public-methods """Nginx Parser Test.""" def setUp(self): @@ -230,6 +230,33 @@ class NginxParserTest(util.NginxTest): ['ssl_certificate', '/etc/ssl/cert2.pem']], replace=False) + def test_comment_is_repeatable(self): + nparser = parser.NginxParser(self.config_path) + example_com = nparser.abs_path('sites-enabled/example.com') + mock_vhost = obj.VirtualHost(example_com, + None, None, None, + set(['.example.com', 'example.*']), + None, [0]) + nparser.add_server_directives(mock_vhost, + [['\n ', '#', ' ', 'what a nice comment']], + replace=False) + nparser.add_server_directives(mock_vhost, + [['\n ', 'include', ' ', + nparser.abs_path('comment_in_file.conf')]], + replace=False) + from certbot_nginx.parser import COMMENT + self.assertEqual(nparser.parsed[example_com], + [[['server'], [['listen', '69.50.225.155:9000'], + ['listen', '127.0.0.1'], + ['server_name', '.example.com'], + ['server_name', 'example.*'], + ['#', ' ', 'what a nice comment'], + [], + ['include', nparser.abs_path('comment_in_file.conf')], + ['#', COMMENT], + []]]] +) + def test_replace_server_directives(self): nparser = parser.NginxParser(self.config_path) target = set(['.example.com', 'example.*']) diff --git a/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf b/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf new file mode 100644 index 000000000..f761079fa --- /dev/null +++ b/certbot-nginx/certbot_nginx/tests/testdata/etc_nginx/comment_in_file.conf @@ -0,0 +1 @@ +# a comment inside a file
\ No newline at end of file |