def testParseFiles(self):
        samples_dir = os.path.join(os.path.dirname(__file__), 'samples',
                                   'perl-config-general')

        errors = []

        with make_loader() as loader:

            for filename in os.listdir(samples_dir):

                filepath = os.path.join(samples_dir, filename)

                if os.path.isdir(filepath):
                    continue

                try:
                    config = loader.load(filepath)

                except ApacheConfigError as ex:
                    errors.append('failed to parse %s: %s' % (filepath, ex))
                    continue

                if filename in TEST_CONFIGS:
                    self.assertEqual(config, TEST_CONFIGS[filename])

        self.assertEqual(len(errors), 2)
Beispiel #2
0
    def get_apache2_documentroots(self):
        documentroots = []
        # apacheconfig options
        options = {
            'includerelative': True,
            'lowercasenames': True
        }

        for configpath in self.configpaths:
            options['configroot'] = os.path.dirname(configpath)
            try:
                if self.verbose:
                    printerr('Processing {}...'.format(configpath))
                with make_loader(**options) as loader:
                    config = loader.load(configpath)
            except Exception as exc:
                msg = 'Issue loading Apache config {}: {}'.format(configpath,
                                                                  exc)
                printerr(msg)
                if self.hume:
                    self.Hume({'level': 'critical',
                               'msg': msg,
                               'task': 'WPUPDATER'})
            documentroots.extend(self._extract_documentroots(config))
        documentroots = list(set(documentroots))
        return(documentroots)
    def testIncludeOptional(self):
        samples_file = os.path.join(os.path.dirname(__file__), 'samples',
                                    'perl-config-general',
                                    'include-optional-test.conf')

        with make_loader() as loader:
            config = loader.load(samples_file)

        self.assertFalse(config)
    def _load_cfg(self):
        with apacheconfig.make_loader(**self.options) as loader:
            config = loader.load(self.path)

        if self.env_var_file:
            self.env_vars = self._parse_env_vars()
            for key, value in config.items():
                config[key] = self._process_vars(value)

        return config
    def __load_apache_conf(self, file: Path) -> dict:
        """
        Internal method to load the apache configuration file.

        :param file: path to the configuration file
        :type file: str
        :return: loaded configuration
        :rtype: dict
        """
        with make_loader() as loader:
            return loader.load(str(file.absolute()))
    def testIncludeGlob(self):
        samples_file = os.path.join(os.path.dirname(__file__), 'samples',
                                    'perl-config-general',
                                    'include-glob-test.conf')

        options = {'includeglob': True}

        with make_loader(**options) as loader:
            config = loader.load(samples_file)

        self.assertTrue(config)
    def save(self, file_name: str = None):
        """
        Saves the configuration.

        :param file_name: file name to save, if None, the input file name is used
        :type file_name: str
        :default file_name: None
        """
        self.__logging.info("Saving config file...")
        if not file_name:
            path = self.__path
        else:
            path = file_name
        file = Path(path)
        file.touch()
        with make_loader() as loader:
            loader.dump(filepath=str(file.absolute()), dct=self.__loaded_conf)
        self.__logging.info(f"Saved configuration in file {file.absolute()}")
Beispiel #8
0
    def testIncludeDirectories(self):
        text = """\
<<include xxx>>
"""

        options = {'includedirectories': True}

        with make_loader(**options) as loader:
            with mock.patch('os.path.exists') as path_exists_mock:
                with mock.patch('os.path.isdir') as path_isdir_mock:
                    with mock.patch('os.listdir') as listdir_mock:
                        path_exists_mock.side_effect = lambda x: [True, False]
                        path_isdir_mock.side_effect = lambda x: [True, False]
                        listdir_mock.return_value = []

                        config = loader.loads(text)

                        self.assertEqual(config, {})
    def testIncludeAgainDisabled(self):
        samples_file = os.path.join(os.path.dirname(__file__), 'samples',
                                    'perl-config-general',
                                    'include-again-test.conf')

        options = {'includeagain': False}

        with make_loader(**options) as loader:
            config = loader.load(samples_file)

        self.assertEqual(
            config, {
                'seen_second_config': 'true',
                'inner': {
                    'final_include': 'true',
                    'seen_third_config': 'true'
                }
            })
Beispiel #10
0
    def testIncludeRelative(self, path_exists_mock):
        text = """\
<<include t.conf>>
"""

        options = {'includerelative': True, 'configroot': 'a'}

        path_exists_mock.return_value = False

        with make_loader(**options) as loader:
            self.assertRaises(ApacheConfigError, loader.loads, text)

        expected_probes = ['a/t.conf']
        actual_probes = [
            x[1][0] for x in path_exists_mock.mock_calls
            if len(x[1]) and x[1][0] in expected_probes
        ]

        self.assertEqual(expected_probes, actual_probes)
Beispiel #11
0
    def testConfigPath(self, path_exists_mock):
        text = """\
<<include t.conf>>
"""

        options = {'configpath': ['a', 'b']}

        path_exists_mock.return_value = False

        with make_loader(**options) as loader:
            self.assertRaises(ApacheConfigError, loader.loads, text)

        expected_probes = ['a/t.conf', 'b/t.conf', './t.conf']
        actual_probes = [
            x[1][0] for x in path_exists_mock.mock_calls
            if len(x[1]) and x[1][0] in expected_probes
        ]

        self.assertEqual(expected_probes, actual_probes)
def test_backuppc_conf_file(host, pytestconfig):
    apache_require = test_utils.get_parameter_value(
        host=host,
        ansible_var_name="backuppc_server_apache_require",
        param_value=pytestconfig.getoption("apache_require"),
        default_value="Require all granted").lower().replace("require ", "")

    with apacheconfig.make_loader() as loader:
        config = loader.loads(
            host.file(
                "/etc/apache2/conf-available/backuppc.conf").content_string)
    # BackupPC source code has this trailing space
    cgi_bin_dir = pytestconfig.getoption("cgi_bin_dir") + " "
    module_2_4_plus = next(
        item for item in config["Directory"][cgi_bin_dir]["IfModule"]
        if "authz_core_module" in item)
    assert {
        "Require": [apache_require, "valid-user"]
    } in module_2_4_plus["authz_core_module"]["RequireAll"]
Beispiel #13
0
 def setUp(self):
     context = make_loader(writable=True, **flavors.NATIVE_APACHE)
     self.loader = context.__enter__()
     self.addCleanup(context.__exit__, None, None, None)
Beispiel #14
0
 def setUp(self):
     self._context = make_loader(writable=True, **flavors.NATIVE_APACHE)
     self.loader = self._context.__enter__()
def main():

    parser = argparse.ArgumentParser(
        description='Dump Apache config files into JSON')

    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='%(prog)s ' + __version__)

    parser.add_argument(
        '--json-input',
        action='store_true',
        help='Expect JSON file(s) on input, produce Apache configuration')

    parser.add_argument('file',
                        nargs='+',
                        help='Path to the configuration file to dump')

    options = parser.add_argument_group('parsing options')

    options.add_argument('--allowmultioptions',
                         action='store_false',
                         help='Collect multiple identical options into a list')

    options.add_argument(
        '--forcearray',
        action='store_true',
        help=('Force a single config line to get parsed into a list by '
              'turning on this option and by surrounding the value of the '
              'config entry by []'))

    options.add_argument(
        '--lowercasenames',
        action='store_true',
        help='All options found in the config will be converted to lowercase')

    options.add_argument(
        '--nostripvalues',
        action='store_true',
        help='All values found in the config will not be right-stripped')

    options.add_argument(
        '--useapacheinclude',
        action='store_true',
        help='Consider "include ..." as valid include statement')

    options.add_argument('--includeagain',
                         action='store_true',
                         help='Allow including sub-configfiles multiple times')

    options.add_argument(
        '--includerelative',
        action='store_true',
        help=('Open included config files from within the location of the '
              'configfile instead from within the location of the script'))

    options.add_argument(
        '--includedirectories',
        action='store_true',
        help=('Include statement may point to a directory, in which case '
              'all files inside the directory will be loaded in ASCII order'))

    options.add_argument(
        '--includeglob',
        action='store_true',
        help=('Include statement may point to a glob pattern, in which case '
              'all files matching the pattern will be loaded in ASCII order'))

    options.add_argument(
        '--mergeduplicateblocks',
        action='store_true',
        help=('Duplicate blocks (blocks and named blocks), will be merged '
              'into a single one'))

    options.add_argument(
        '--mergeduplicateoptions',
        action='store_true',
        help=('If the same option occurs more than once, the last one will '
              'be used in the resulting config dictionary'))

    options.add_argument(
        '--namedblocks',
        action='store_false',
        help='Do not split tags by the first whitespace and turn the trailing '
        'part into a nested block')

    options.add_argument(
        '--autotrue',
        action='store_true',
        help='Turn various forms of binary values in config into "1" and "0"')

    options.add_argument('--interpolatevars',
                         action='store_true',
                         help='Enable variable interpolation')

    options.add_argument(
        '--interpolateenv',
        action='store_true',
        help='Enable process environment variable interpolation')

    options.add_argument(
        '--allowsinglequoteinterpolation',
        action='store_true',
        help='Perform variable interpolation even when being in single quotes')

    options.add_argument(
        '--strictvars',
        action='store_false',
        help=('Do not fail on an undefined variable when performing '
              'interpolation'))

    options.add_argument(
        '--noescape',
        action='store_true',
        help=('Preserve special escape characters left outs in the '
              'configuration values'))

    options.add_argument(
        '--preservewhitespace',
        action='store_true',
        help=('Preserve insignificant whitespaces, dump them them as is '
              'on code generation'))

    options.add_argument(
        '--disableemptyelementtags',
        action='store_true',
        help=('Disables the parsing of empty element tags like <block/>'))

    options.add_argument('--ccomments',
                         action='store_false',
                         help='Do not parse C-style comments')

    options.add_argument('--multilinehashcomments',
                         action='store_true',
                         help='Enable multi-line hash comments.')

    options.add_argument(
        '--configpath',
        action='append',
        default=[],
        help=('Search path for the configuration files being included. Can '
              'repeat.'))

    options.add_argument(
        '--flagbits',
        metavar='<JSON>',
        type=str,
        help=('Named bits for an option in form of a JSON object of the '
              'following structure {"OPTION": {"NAME": "VALUE"}}'))

    options.add_argument(
        '--defaultconfig',
        metavar='<JSON>',
        type=str,
        help='Default values for parsed configuration in form of a JSON object'
    )

    args = parser.parse_args()

    options = dict([
        (option, getattr(args, option)) for option in dir(args)
        if not option.startswith('_') and getattr(args, option) is not None
    ])

    options['programpath'] = os.path.dirname(sys.argv[0])

    if 'flagbits' in options:
        try:
            options['flagbits'] = json.loads(options['flagbits'])

        except Exception as ex:
            sys.stderr.write('Malformed flagbits %s: %s\n' %
                             (options['flagbits'], ex))
            return 1

    if 'defaultconfig' in options:
        try:
            options['defaultconfig'] = json.loads(options['defaultconfig'])

        except Exception as ex:
            sys.stderr.write('Malformed defaultconfig %s: %s\n' %
                             (options['defaultconfig'], ex))
            return 1

    if args.json_input:

        with make_loader(**options) as loader:

            for json_file in args.file:
                try:
                    with open(json_file) as f:
                        sys.stdout.write(loader.dumps(json.load(f)))

                except Exception as ex:
                    sys.stderr.write(('Failed to dump JSON document %s into '
                                      'Apache config: %s\n') % (json_file, ex))
                    return 1

    else:

        for config_file in args.file:

            options['configroot'] = os.path.dirname(config_file)

            with make_loader(**options) as loader:

                try:
                    config = loader.load(config_file)

                except ApacheConfigError as ex:
                    sys.stderr.write('Failed to parse Apache config %s: %s\n' %
                                     (config_file, ex))
                    return 1

                sys.stdout.write(json.dumps(config, indent=2) + '\n')
        default=os.path.join("/etc", "clamav", "safebrowsing.conf"))
    cli_parser.add_argument("-d",
                            "--debug",
                            dest="debug",
                            action="store_true",
                            help=argparse.SUPPRESS)
    cli_parser.add_argument("-v",
                            "--verbose",
                            dest="verbose",
                            action="store_true",
                            help="be verbose")
    cli_parser.add_argument("--logfile",
                            dest="logfile",
                            action="store",
                            nargs="?",
                            help="Specify a logfile destination")
    opts = cli_parser.parse_args(sys.argv[1:])

    log = utils.setup_logging(opts.debug, opts.verbose, logfile=opts.logfile)
    log.info("Running gdbwrite.py CLI")

    with apacheconfig.make_loader() as loader:
        config = loader.load(opts.configfile)['safebrowsing']
        db_host, db_user, db_pw, db_name = [
            config.get(key)
            for key in ["db_host", "db_user", "db_pw", "db_name"]
        ]

    g = GDBWriter(db_host, db_user, db_pw, db_name)
    g.writegdb('safebrowsing.gdb')