Пример #1
0
def _comment_out_directive(block, location, include_location):
    """Comment out the line at location, with a note of explanation."""
    comment_message = ' duplicated in {0}'.format(include_location)
    # add the end comment
    # create a dumpable object out of block[location] (so it includes the ;)
    directive = block[location]
    new_dir_block = nginxparser.UnspacedList([])  # just a wrapper
    new_dir_block.append(directive)
    dumped = nginxparser.dumps(new_dir_block)
    commented = dumped + ' #' + comment_message  # add the comment directly to the one-line string
    new_dir = nginxparser.loads(commented)  # reload into UnspacedList

    # add the beginning comment
    insert_location = 0
    if new_dir[0].spaced[0] != new_dir[0][
            0]:  # if there's whitespace at the beginning
        insert_location = 1
    new_dir[0].spaced.insert(insert_location, "# ")  # comment out the line
    new_dir[0].spaced.append(
        ";")  # directly add in the ;, because now dumping won't work properly
    dumped = nginxparser.dumps(new_dir)
    new_dir = nginxparser.loads(dumped)  # reload into an UnspacedList

    block[location] = new_dir[
        0]  # set the now-single-line-comment directive back in place
Пример #2
0
    def test_dump_as_string(self):
        dumped = dumps(
            UnspacedList(
                [['user', ' ', 'www-data'],
                 [['\n', 'server', ' '],
                  [['\n    ', 'listen', ' ', '80'],
                   ['\n    ', 'server_name', ' ', 'foo.com'],
                   ['\n    ', 'root', ' ', '/home/ubuntu/sites/foo/'],
                   [['\n\n    ', 'location', ' ', '/status', ' '],
                    [
                        ['\n        ', 'check_status', ''],
                        [['\n\n        ', 'types', ' '],
                         [['\n            ', 'image/jpeg', ' ', 'jpg']]],
                    ]]]]]))

        self.assertEqual(
            dumped.split('\n'), 'user www-data;\n'
            'server {\n'
            '    listen 80;\n'
            '    server_name foo.com;\n'
            '    root /home/ubuntu/sites/foo/;\n'
            '\n'
            '    location /status {\n'
            '        check_status;\n'
            '\n'
            '        types {\n'
            '            image/jpeg jpg;}}}'.split('\n'))
Пример #3
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        nparser.add_server_directives(
            nparser.abs_path("nginx.conf"),
            set(["localhost", r"~^(www\.)?(example|bar)\."]),
            [["foo", "bar"], ["\n ", "ssl_certificate", " ", "/etc/ssl/cert.pem"]],
            replace=False,
        )
        ssl_re = re.compile(r"\n\s+ssl_certificate /etc/ssl/cert.pem")
        dump = nginxparser.dumps(nparser.parsed[nparser.abs_path("nginx.conf")])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        server_conf = nparser.abs_path("server.conf")
        names = set(["alias", "another.alias", "somename"])
        nparser.add_server_directives(
            server_conf, names, [["foo", "bar"], ["ssl_certificate", "/etc/ssl/cert2.pem"]], replace=False
        )
        nparser.add_server_directives(server_conf, names, [["foo", "bar"]], replace=False)
        from certbot_nginx.parser import COMMENT

        self.assertEqual(
            nparser.parsed[server_conf],
            [
                ["server_name", "somename  alias  another.alias"],
                ["foo", "bar"],
                ["#", COMMENT],
                ["ssl_certificate", "/etc/ssl/cert2.pem"],
                ["#", COMMENT],
                [],
                [],
            ],
        )
Пример #4
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        nparser.add_server_directives(
            nparser.abs_path('nginx.conf'),
            set(['localhost', r'~^(www\.)?(example|bar)\.']),
            [['foo', 'bar'],
             ['\n ', 'ssl_certificate', ' ', '/etc/ssl/cert.pem']],
            replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(
            nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        nparser.add_server_directives(
            server_conf,
            names, [['foo', 'bar'], ['ssl_certificate', '/etc/ssl/cert2.pem']],
            replace=False)
        nparser.add_server_directives(server_conf,
                                      names, [['foo', 'bar']],
                                      replace=False)
        self.assertEqual(
            nparser.parsed[server_conf],
            [['server_name', 'somename  alias  another.alias'], ['foo', 'bar'],
             ['ssl_certificate', '/etc/ssl/cert2.pem']])
Пример #5
0
    def test_dump_as_string(self):
        dumped = dumps(UnspacedList([
            ['user', ' ', 'www-data'],
            [['\n', 'server', ' '], [
                ['\n    ', 'listen', ' ', '80'],
                ['\n    ', 'server_name', ' ', 'foo.com'],
                ['\n    ', 'root', ' ', '/home/ubuntu/sites/foo/'],
                [['\n\n    ', 'location', ' ', '/status', ' '], [
                    ['\n        ', 'check_status', ''],
                    [['\n\n        ', 'types', ' '],
                    [['\n            ', 'image/jpeg', ' ', 'jpg']]],
                ]]
            ]]]))

        self.assertEqual(dumped.split('\n'),
                         'user www-data;\n'
                         'server {\n'
                         '    listen 80;\n'
                         '    server_name foo.com;\n'
                         '    root /home/ubuntu/sites/foo/;\n'
                         '\n'
                         '    location /status {\n'
                         '        check_status;\n'
                         '\n'
                         '        types {\n'
                         '            image/jpeg jpg;}}}'.split('\n'))
Пример #6
0
    def test_dump_as_string(self):
        dumped = dumps([['user', 'www-data'],
                        [['server'],
                         [['listen', '80'], ['server_name', 'foo.com'],
                          ['root', '/home/ubuntu/sites/foo/'],
                          [['location', '/status'],
                           [
                               ['check_status', None],
                               [['types'], [['image/jpeg', 'jpg']]],
                           ]]]]])

        self.assertEqual(
            dumped, 'user www-data;\n'
            'server {\n'
            '    listen 80;\n'
            '    server_name foo.com;\n'
            '    root /home/ubuntu/sites/foo/;\n'
            '\n'
            '    location /status {\n'
            '        check_status;\n'
            '\n'
            '        types {\n'
            '            image/jpeg jpg;\n'
            '        }\n'
            '    }\n'
            '}\n')
Пример #7
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        nparser.add_server_directives(nparser.abs_path('nginx.conf'),
                                      set(['localhost',
                                           r'~^(www\.)?(example|bar)\.']),
                                      [['foo', 'bar'], ['\n ', 'ssl_certificate', ' ',
                                                        '/etc/ssl/cert.pem']],
                                      replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        nparser.add_server_directives(server_conf, names,
                                      [['foo', 'bar'], ['ssl_certificate',
                                                        '/etc/ssl/cert2.pem']],
                                      replace=False)
        nparser.add_server_directives(server_conf, names, [['foo', 'bar']],
                                      replace=False)
        self.assertEqual(nparser.parsed[server_conf],
                         [['server_name', 'somename  alias  another.alias'],
                          ['foo', 'bar'],
                          ['ssl_certificate', '/etc/ssl/cert2.pem']
                          ])
Пример #8
0
    def test_dump_as_string(self):
        dumped = dumps([
            ['user', 'www-data'],
            [['server'], [
                ['listen', '80'],
                ['server_name', 'foo.com'],
                ['root', '/home/ubuntu/sites/foo/'],
                [['location', '/status'], [
                    ['check_status', None],
                    [['types'], [['image/jpeg', 'jpg']]],
                ]]
            ]]])

        self.assertEqual(dumped,
                         'user www-data;\n'
                         'server {\n'
                         '    listen 80;\n'
                         '    server_name foo.com;\n'
                         '    root /home/ubuntu/sites/foo/;\n'
                         '\n'
                         '    location /status {\n'
                         '        check_status;\n'
                         '\n'
                         '        types {\n'
                         '            image/jpeg jpg;\n'
                         '        }\n'
                         '    }\n'
                         '}\n')
Пример #9
0
def _comment_out_directive(block, location, include_location):
    """Comment out the line at location, with a note of explanation."""
    comment_message = ' duplicated in {0}'.format(include_location)
    # add the end comment
    # create a dumpable object out of block[location] (so it includes the ;)
    directive = block[location]
    new_dir_block = nginxparser.UnspacedList([]) # just a wrapper
    new_dir_block.append(directive)
    dumped = nginxparser.dumps(new_dir_block)
    commented = dumped + ' #' + comment_message # add the comment directly to the one-line string
    new_dir = nginxparser.loads(commented) # reload into UnspacedList

    # add the beginning comment
    insert_location = 0
    if new_dir[0].spaced[0] != new_dir[0][0]: # if there's whitespace at the beginning
        insert_location = 1
    new_dir[0].spaced.insert(insert_location, "# ") # comment out the line
    new_dir[0].spaced.append(";") # directly add in the ;, because now dumping won't work properly
    dumped = nginxparser.dumps(new_dir)
    new_dir = nginxparser.loads(dumped) # reload into an UnspacedList

    block[location] = new_dir[0] # set the now-single-line-comment directive back in place
Пример #10
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'),
                                     None, None, None,
                                     set(['localhost',
                                           r'~^(www\.)?(example|bar)\.']),
                                     None, [10, 1, 9])
        nparser.add_server_directives(mock_vhost,
                                      [['foo', 'bar'], ['\n ', 'ssl_certificate', ' ',
                                                        '/etc/ssl/cert.pem']],
                                      replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        example_com = nparser.abs_path('sites-enabled/example.com')
        names = set(['.example.com', 'example.*'])
        mock_vhost.filep = example_com
        mock_vhost.names = names
        mock_vhost.path = [0]
        nparser.add_server_directives(mock_vhost,
                                      [['foo', 'bar'], ['ssl_certificate',
                                                        '/etc/ssl/cert2.pem']],
                                      replace=False)
        nparser.add_server_directives(mock_vhost, [['foo', 'bar']],
                                      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.*'],
                           ['foo', 'bar'],
                           ['#', COMMENT],
                           ['ssl_certificate', '/etc/ssl/cert2.pem'],
                           ['#', COMMENT], [], []
                           ]]])

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        mock_vhost.filep = server_conf
        mock_vhost.names = names
        mock_vhost.path = []
        self.assertRaises(errors.MisconfigurationError,
                          nparser.add_server_directives,
                          mock_vhost,
                          [['foo', 'bar'],
                           ['ssl_certificate', '/etc/ssl/cert2.pem']],
                          replace=False)
Пример #11
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        mock_vhost = obj.VirtualHost(nparser.abs_path('nginx.conf'),
                                     None, None, None,
                                     set(['localhost',
                                           r'~^(www\.)?(example|bar)\.']),
                                     None, [9, 1, 9])
        nparser.add_server_directives(mock_vhost,
                                      [['foo', 'bar'], ['\n ', 'ssl_certificate', ' ',
                                                        '/etc/ssl/cert.pem']],
                                      replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        example_com = nparser.abs_path('sites-enabled/example.com')
        names = set(['.example.com', 'example.*'])
        mock_vhost.filep = example_com
        mock_vhost.names = names
        mock_vhost.path = [0]
        nparser.add_server_directives(mock_vhost,
                                      [['foo', 'bar'], ['ssl_certificate',
                                                        '/etc/ssl/cert2.pem']],
                                      replace=False)
        nparser.add_server_directives(mock_vhost, [['foo', 'bar']],
                                      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.*'],
                           ['foo', 'bar'],
                           ['#', COMMENT],
                           ['ssl_certificate', '/etc/ssl/cert2.pem'],
                           ['#', COMMENT], [], []
                           ]]])

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        mock_vhost.filep = server_conf
        mock_vhost.names = names
        mock_vhost.path = []
        self.assertRaises(errors.MisconfigurationError,
                          nparser.add_server_directives,
                          mock_vhost,
                          [['foo', 'bar'],
                           ['ssl_certificate', '/etc/ssl/cert2.pem']],
                          replace=False)
Пример #12
0
def roundtrip(stuff):
    success = True
    for t in stuff:
        print t
        if not os.path.isfile(t):
            continue
        with open(t, "r") as f:
            config = f.read()
            try:
                if nginxparser.dumps(nginxparser.loads(config)) != config:
                    print("Failed parsing round-trip for {0}".format(t))
                    success = False
            except Exception as e:
                print("Failed parsing {0} ({1})".format(t, e))
                success = False
    return success
Пример #13
0
def roundtrip(stuff):
    success = True
    for t in stuff:
        print t
        if not os.path.isfile(t):
            continue
        with open(t, "r") as f:
            config = f.read()
            try:
                if nginxparser.dumps(nginxparser.loads(config)) != config:
                    print("Failed parsing round-trip for {0}".format(t))
                    success = False
            except Exception as e:
                print("Failed parsing {0} ({1})".format(t, e))
                success = False
    return success
Пример #14
0
    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:
                logger.debug('Dumping to %s:\n%s', filename, nginxparser.dumps(tree))
                with open(filename, 'w') as _file:
                    nginxparser.dump(tree, _file)
            except IOError:
                logger.error("Could not open file for writing: %s", filename)
Пример #15
0
    def filedump(self, ext='tmp', lazy=True):
        """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.
        :param bool lazy: Only write files that have been modified

        """
        # Best-effort atomicity is enforced above us by reverter.py
        for filename in self.parsed:
            tree = self.parsed[filename]
            if ext:
                filename = filename + os.path.extsep + ext
            try:
                if lazy and not tree.is_dirty():
                    continue
                out = nginxparser.dumps(tree)
                logger.debug('Writing nginx conf tree to %s:\n%s', filename, out)
                with open(filename, 'w') as _file:
                    _file.write(out)

            except IOError:
                logger.error("Could not open file for writing: %s", filename)
Пример #16
0
    def filedump(self, ext='tmp', lazy=True):
        """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.
        :param bool lazy: Only write files that have been modified

        """
        # Best-effort atomicity is enforced above us by reverter.py
        for filename in self.parsed:
            tree = self.parsed[filename]
            if ext:
                filename = filename + os.path.extsep + ext
            try:
                if lazy and not tree.is_dirty():
                    continue
                out = nginxparser.dumps(tree)
                logger.debug('Writing nginx conf tree to %s:\n%s', filename, out)
                with open(filename, 'w') as _file:
                    _file.write(out)

            except IOError:
                logger.error("Could not open file for writing: %s", filename)