示例#1
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = load(handle)
        parsed[-1][-1].append(UnspacedList([['server'],
                               [['listen', ' ', '443 ssl'],
                                ['server_name', ' ', 'localhost'],
                                ['ssl_certificate', ' ', 'cert.pem'],
                                ['ssl_certificate_key', ' ', 'cert.key'],
                                ['ssl_session_cache', ' ', 'shared:SSL:1m'],
                                ['ssl_session_timeout', ' ', '5m'],
                                ['ssl_ciphers', ' ', 'HIGH:!aNULL:!MD5'],
                                [['location', ' ', '/'],
                                 [['root', ' ', 'html'],
                                  ['index', ' ', 'index.html index.htm']]]]]))

        with open(util.get_data_filename('nginx.new.conf'), 'w') as handle:
            dump(parsed, handle)
        with open(util.get_data_filename('nginx.new.conf')) as handle:
            parsed_new = load(handle)
        try:
            self.maxDiff = None
            self.assertEqual(parsed[0], parsed_new[0])
            self.assertEqual(parsed[1:], parsed_new[1:])
        finally:
            os.unlink(util.get_data_filename('nginx.new.conf'))
示例#2
0
    def test_comments(self):
        with open(util.get_data_filename('minimalistic_comments.conf')) as handle:
            parsed = load(handle)

        with open(util.get_data_filename('minimalistic_comments.new.conf'), 'w') as handle:
            dump(parsed, handle)

        with open(util.get_data_filename('minimalistic_comments.new.conf')) as handle:
            parsed_new = load(handle)

        try:
            self.assertEqual(parsed, parsed_new)

            self.assertEqual(parsed_new, [
                ['#', " Use bar.conf when it's a full moon!"],
                ['include', 'foo.conf'],
                ['#', ' Kilroy was here'],
                ['check_status'],
                [['server'],
                 [['#', ''],
                  ['#', " Don't forget to open up your firewall!"],
                  ['#', ''],
                  ['listen', '1234'],
                  ['#', ' listen 80;']]],
            ])
        finally:
            os.unlink(util.get_data_filename('minimalistic_comments.new.conf'))
示例#3
0
    def _parse_files(self, filepath, override=False):
        """Parse files from a glob

        :param str filepath: Nginx config file path
        :param bool override: Whether to parse a file that has been parsed
        :returns: list of parsed tree structures
        :rtype: list

        """
        files = glob.glob(filepath) # nginx on unix calls glob(3) for this
                                    # XXX Windows nginx uses FindFirstFile, and
                                    # should have a narrower call here
        trees = []
        for item in files:
            if item in self.parsed and not override:
                continue
            try:
                with open(item) as _file:
                    parsed = nginxparser.load(_file)
                    self.parsed[item] = parsed
                    trees.append(parsed)
            except IOError:
                logger.warn("Could not open file: %s", item)
            except pyparsing.ParseException:
                logger.debug("Could not parse file: %s", item)
        return trees
示例#4
0
    def _parse_files(self, filepath, override=False):
        """Parse files from a glob

        :param str filepath: Nginx config file path
        :param bool override: Whether to parse a file that has been parsed
        :returns: list of parsed tree structures
        :rtype: list

        """
        files = glob.glob(filepath)  # nginx on unix calls glob(3) for this
        # XXX Windows nginx uses FindFirstFile, and
        # should have a narrower call here
        trees = []
        for item in files:
            if item in self.parsed and not override:
                continue
            try:
                with open(item) as _file:
                    parsed = nginxparser.load(_file)
                    self.parsed[item] = parsed
                    trees.append(parsed)
            except IOError:
                logger.warning("Could not open file: %s", item)
            except pyparsing.ParseException as err:
                logger.debug("Could not parse file: %s due to %s", item, err)
        return trees
示例#5
0
def _parse_ssl_options(ssl_options):
    if ssl_options is not None:
        try:
            with open(ssl_options) as _file:
                return nginxparser.load(_file)
        except IOError:
            logger.warn("Missing NGINX TLS options file: %s", ssl_options)
        except pyparsing.ParseBaseException as err:
            logger.debug("Could not parse file: %s due to %s", ssl_options, err)
    return []
示例#6
0
def _parse_ssl_options(ssl_options):
    if ssl_options is not None:
        try:
            with open(ssl_options) as _file:
                return nginxparser.load(_file)
        except IOError:
            logger.warn("Missing NGINX TLS options file: %s", ssl_options)
        except pyparsing.ParseBaseException as err:
            logger.debug("Could not parse file: %s due to %s", ssl_options, err)
    return []
示例#7
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = load(handle)
        parsed[-1][-1].append(UnspacedList([['server'],
                               [['listen', ' ', '443 ssl'],
                                ['server_name', ' ', 'localhost'],
                                ['ssl_certificate', ' ', 'cert.pem'],
                                ['ssl_certificate_key', ' ', 'cert.key'],
                                ['ssl_session_cache', ' ', 'shared:SSL:1m'],
                                ['ssl_session_timeout', ' ', '5m'],
                                ['ssl_ciphers', ' ', 'HIGH:!aNULL:!MD5'],
                                [['location', ' ', '/'],
                                 [['root', ' ', 'html'],
                                  ['index', ' ', 'index.html index.htm']]]]]))

        with tempfile.TemporaryFile() as f:
            dump(parsed, f)
            f.seek(0)
            parsed_new = load(f)
        self.assertEqual(parsed, parsed_new)
示例#8
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = load(handle)
        parsed[-1][-1].append(UnspacedList([['server'],
                               [['listen', ' ', '443', ' ', 'ssl'],
                                ['server_name', ' ', 'localhost'],
                                ['ssl_certificate', ' ', 'cert.pem'],
                                ['ssl_certificate_key', ' ', 'cert.key'],
                                ['ssl_session_cache', ' ', 'shared:SSL:1m'],
                                ['ssl_session_timeout', ' ', '5m'],
                                ['ssl_ciphers', ' ', 'HIGH:!aNULL:!MD5'],
                                [['location', ' ', '/'],
                                 [['root', ' ', 'html'],
                                  ['index', ' ', 'index.html', ' ', 'index.htm']]]]]))

        with tempfile.TemporaryFile(mode='w+t') as f:
            dump(parsed, f)
            f.seek(0)
            parsed_new = load(f)
        self.assertEqual(parsed, parsed_new)
示例#9
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = util.filter_comments(load(handle))
        parsed[-1][-1].append([['server'],
                               [['listen', '443 ssl'],
                                ['server_name', 'localhost'],
                                ['ssl_certificate', 'cert.pem'],
                                ['ssl_certificate_key', 'cert.key'],
                                ['ssl_session_cache', 'shared:SSL:1m'],
                                ['ssl_session_timeout', '5m'],
                                ['ssl_ciphers', 'HIGH:!aNULL:!MD5'],
                                [['location', '/'],
                                 [['root', 'html'],
                                  ['index', 'index.html index.htm']]]]])

        with open(util.get_data_filename('nginx.new.conf'), 'w') as handle:
            dump(parsed, handle)
        with open(util.get_data_filename('nginx.new.conf')) as handle:
            parsed_new = util.filter_comments(load(handle))
        self.assertEquals(parsed, parsed_new)
示例#10
0
    def test_comments(self):
        with open(util.get_data_filename(
                'minimalistic_comments.conf')) as handle:
            parsed = load(handle)

        with tempfile.TemporaryFile() as f:
            dump(parsed, f)
            f.seek(0)
            parsed_new = load(f)

        self.assertEqual(parsed, parsed_new)
        self.assertEqual(parsed_new, [
            ['#', " Use bar.conf when it's a full moon!"],
            ['include', 'foo.conf'],
            ['#', ' Kilroy was here'],
            ['check_status'],
            [['server'],
             [['#', ''], ['#', " Don't forget to open up your firewall!"],
              ['#', ''], ['listen', '1234'], ['#', ' listen 80;']]],
        ])
示例#11
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = util.filter_comments(load(handle))
        parsed[-1][-1].append([['server'],
                               [['listen', '443 ssl'],
                                ['server_name', 'localhost'],
                                ['ssl_certificate', 'cert.pem'],
                                ['ssl_certificate_key', 'cert.key'],
                                ['ssl_session_cache', 'shared:SSL:1m'],
                                ['ssl_session_timeout', '5m'],
                                ['ssl_ciphers', 'HIGH:!aNULL:!MD5'],
                                [['location', '/'],
                                 [['root', 'html'],
                                  ['index', 'index.html index.htm']]]]])

        with open(util.get_data_filename('nginx.new.conf'), 'w') as handle:
            dump(parsed, handle)
        with open(util.get_data_filename('nginx.new.conf')) as handle:
            parsed_new = util.filter_comments(load(handle))
        self.assertEquals(parsed, parsed_new)
示例#12
0
    def test_comments(self):
        with open(util.get_data_filename('minimalistic_comments.conf')) as handle:
            parsed = load(handle)

        with tempfile.TemporaryFile() as f:
            dump(parsed, f)
            f.seek(0)
            parsed_new = load(f)

        self.assertEqual(parsed, parsed_new)
        self.assertEqual(parsed_new, [
            ['#', " Use bar.conf when it's a full moon!"],
            ['include', 'foo.conf'],
            ['#', ' Kilroy was here'],
            ['check_status'],
            [['server'],
             [['#', ''],
              ['#', " Don't forget to open up your firewall!"],
              ['#', ''],
              ['listen', '1234'],
              ['#', ' listen 80;']]],
        ])
示例#13
0
 def test_parse_from_file3(self):
     with open(util.get_data_filename('multiline_quotes.conf')) as handle:
         parsed = util.filter_comments(load(handle))
     self.assertEqual(
         parsed,
         [[['http'],
             [[['server'],
                 [['listen', '*:443'],
                 [['location', '/'],
                     [['body_filter_by_lua',
                       '\'ngx.ctx.buffered = (ngx.ctx.buffered or "")'
                       ' .. string.sub(ngx.arg[1], 1, 1000)\n'
                       '                            '
                       'if ngx.arg[2] then\n'
                       '                              '
                       'ngx.var.resp_body = ngx.ctx.buffered\n'
                       '                            end\'']]]]]]]])
示例#14
0
 def test_parse_from_file2(self):
     with open(util.get_data_filename('edge_cases.conf')) as handle:
         parsed = util.filter_comments(load(handle))
     self.assertEqual(
         parsed,
         [[['server'], [['server_name', 'simple']]],
          [['server'],
           [['server_name', 'with.if'],
            [['location', '~', '^/services/.+$'],
             [[['if', '($request_filename ~* \\.(ttf|woff)$)'],
               [['add_header', 'Access-Control-Allow-Origin "*"']]]]]]],
          [['server'],
           [['server_name', 'with.complicated.headers'],
            [['location', '~*', '\\.(?:gif|jpe?g|png)$'],
             [['add_header', 'Pragma public'],
              ['add_header',
               'Cache-Control  \'public, must-revalidate, proxy-revalidate\''
               ' "test,;{}" foo'],
              ['blah', '"hello;world"'],
              ['try_files', '$uri @rewrites']]]]]])
示例#15
0
 def test_parse_from_file(self):
     with open(util.get_data_filename('foo.conf')) as handle:
         parsed = util.filter_comments(load(handle))
     self.assertEqual(
         parsed,
         [['user', 'www-data'],
          [['http'],
           [[['server'],
             [['listen', '*:80 default_server ssl'],
              ['server_name', '*.www.foo.com *.www.example.com'],
              ['root', '/home/ubuntu/sites/foo/'],
              [['location', '/status'],
               [
                   [['types'], [['image/jpeg', 'jpg']]],
               ]],
              [['location', '~', r'case_sensitive\.php$'],
               [
                   ['index', 'index.php'],
                   ['root', '/var/root'],
               ]], [['location', '~*', r'case_insensitive\.php$'], []],
              [['location', '=', r'exact_match\.php$'], []],
              [['location', '^~', r'ignore_regex\.php$'], []]]]]]])
示例#16
0
 def test_parse_from_file(self):
     with open(util.get_data_filename('foo.conf')) as handle:
         parsed = util.filter_comments(load(handle))
     self.assertEqual(
         parsed,
         [['user', 'www-data'],
          [['http'],
           [[['server'], [
               ['listen', '*:80 default_server ssl'],
               ['server_name', '*.www.foo.com *.www.example.com'],
               ['root', '/home/ubuntu/sites/foo/'],
               [['location', '/status'], [
                   [['types'], [['image/jpeg', 'jpg']]],
               ]],
               [['location', '~', r'case_sensitive\.php$'], [
                   ['index', 'index.php'],
                   ['root', '/var/root'],
               ]],
               [['location', '~*', r'case_insensitive\.php$'], []],
               [['location', '=', r'exact_match\.php$'], []],
               [['location', '^~', r'ignore_regex\.php$'], []]
           ]]]]]
     )
示例#17
0
    def _parse_files(self, filepath, override=False):
        """Parse files from a glob

        :param str filepath: Nginx config file path
        :param bool override: Whether to parse a file that has been parsed
        :returns: list of parsed tree structures
        :rtype: list

        """
        files = glob.glob(filepath)
        trees = []
        for item in files:
            if item in self.parsed and not override:
                continue
            try:
                with open(item) as _file:
                    parsed = nginxparser.load(_file)
                    self.parsed[item] = parsed
                    trees.append(parsed)
            except IOError:
                logger.warn("Could not open file: %s", item)
            except pyparsing.ParseException:
                logger.debug("Could not parse file: %s", item)
        return trees