Пример #1
0
    def __init__(self, token_values=None, replacer_function=None, html_escape_function=None):
        if token_values is None:
            token_values = {}
        self.token_values = {}
        self.token_used = set()
        for token in token_values:
            self.token_values[token] = token_values[token].decode('UTF-8').strip()

        if not replacer_function:
            def replacer_function(token, replacement):
                return replacement
        else:
            verbose(LOGGER).debug("Using custom replacer_function %s", replacer_function.__name__)

        if not html_escape_function:
            def html_escape_function(filename, content):
                try:
                    content = cgi.escape(content, quote=True)
                    return u"<!DOCTYPE html><html><head><title>%s</title></head><body><pre>%s</pre></body></html>" % (filename, content)
                except Exception as e:
                    raise CouldNotEscapeHtmlException("Could not html escape file: " + filename + '\n\n' + str(e))

        self.replacer_function = replacer_function
        self.html_escape_function = html_escape_function

        self.token_values = self._replace_tokens_in_token_values(self.token_values)
        self.magic_mime_encoding = None
Пример #2
0
    def resolve(self, hostname):
        dns_searchlist = get_custom_dns_search_list()

        if dns_searchlist:
            for dns_suffix in dns_searchlist:
                fqdn_hostname = hostname + '.' + dns_suffix
                try:
                    result = self._resolve(fqdn_hostname)
                    verbose(LOGGER).debug('Host name "%s" resolved to %s', fqdn_hostname, result)
                    return result

                except Exception as exception:
                    verbose(LOGGER).debug('Resolving "%s" resulted in error: %s', fqdn_hostname, str(exception))

        else:
            try:
                result = self._resolve(hostname)
                verbose(LOGGER).debug('Host name "%s" resolved to %s', hostname, result)
                return result
            except Exception as exception:
                verbose(LOGGER).debug('Resolving "%s" resulted in error: %s', hostname, str(exception))

        if not unknown_hosts_are_allowed():
            raise Exception("Could not lookup '%s' with 'getent hosts'" % hostname)

        ip = "127.0.0.1"
        fqdn = "localhost.localdomain"
        aliases = ""
        verbose(LOGGER).debug('Could not resolve "%s" using default values %s', hostname, (ip, fqdn, aliases))
        return ip, fqdn, aliases
    def filter_file(self, filename, html_escape=False):
        try:
            self.file_size_limit = get_max_file_size()

            if getsize(filename) > self.file_size_limit:
                raise FileLimitExceededException(filename,
                                                 self.file_size_limit)

            file_content = self._read_content_from_file(filename)

            file_encoding = self._get_file_encoding(file_content)
            if file_encoding:
                if file_encoding != 'binary' and file_encoding != 'unknown-8bit':
                    self._perform_filtering_on_file(filename, file_content,
                                                    file_encoding, html_escape)
                else:
                    verbose(LOGGER).warn(
                        'Not filtering file "%s" since it has encoding "%s".',
                        filename, file_encoding)

        except MissingTokenException as exception:
            raise MissingTokenException(exception.token, filename)

        except Exception as e:
            raise CannotFilterFileException(
                'Cannot filter file %s.\n%s' %
                (os.path.basename(filename), str(e)))
Пример #4
0
    def test_should_return_given_logger_when_configuration_value_for_verbose_is_true(self, mock_get):

        mock_logger = Mock(Logger)
        mock_get.return_value = True

        verbose(mock_logger).info("Hello")

        mock_logger.info.assert_called_with("Hello")
        mock_get.assert_called_with()
Пример #5
0
def _delete_host_directory(host_name):
    """ deletes the config viewer data for the given host name """

    host_directory = build_config_viewer_host_directory(host_name)
    if exists(host_directory):
        LOGGER.info('Deleting config viewer data for host "%s"', host_name)
        rmtree(host_directory)
    else:
        verbose(LOGGER).debug('Wanted to delete host directory "%s", but it did not exist.' % host_directory)
    def test_should_not_return_muted_logger_when_configuration_value_for_verbose_is_false(
            self, mock_get, mock_muted_logger):

        mock_logger = Mock(Logger)
        mock_get.return_value = False

        verbose(mock_logger).info("Hello")

        mock_muted_logger.info.assert_called_with("Hello")
        mock_get.assert_called_with()
    def test_should_return_given_logger_when_configuration_value_for_verbose_is_true(
            self, mock_get):

        mock_logger = Mock(Logger)
        mock_get.return_value = True

        verbose(mock_logger).info("Hello")

        mock_logger.info.assert_called_with("Hello")
        mock_get.assert_called_with()
Пример #8
0
    def _clean_up(self):
        if is_no_clean_up_enabled():
            verbose(LOGGER).debug('Not cleaning up anything for host "%s"', self.hostname)
            return

        LOGGER.debug('Cleaning up temporary files for host "%s"', self.hostname)

        rmtree(self.variables_dir)
        rmtree(self.host_config_dir)
        remove(self.output_file_path)
        remove(self.error_file_path)
Пример #9
0
def _delete_host_directory(host_name):
    """ deletes the config viewer data for the given host name """

    host_directory = build_config_viewer_host_directory(host_name)
    if exists(host_directory):
        LOGGER.info('Deleting config viewer data for host "%s"', host_name)
        rmtree(host_directory)
    else:
        verbose(LOGGER).debug(
            'Wanted to delete host directory "%s", but it did not exist.' %
            host_directory)
Пример #10
0
def clean_up_deleted_hosts_data(svn_service, revision):
    """ Deletes host directories within config viewer data
        when the svn change set contains a delete of the host directory """

    deleted_paths = svn_service.get_deleted_paths(revision)

    if deleted_paths:
        LOGGER.debug("Change set contains %d deleted path(s).", len(deleted_paths))
        _delete_host_directories(deleted_paths)
    else:
        verbose(LOGGER).debug("Change set did not contain any deleted paths.")
Пример #11
0
    def test_should_not_return_muted_logger_when_configuration_value_for_verbose_is_false(
        self, mock_get, mock_muted_logger
    ):

        mock_logger = Mock(Logger)
        mock_get.return_value = False

        verbose(mock_logger).info("Hello")

        mock_muted_logger.info.assert_called_with("Hello")
        mock_get.assert_called_with()
Пример #12
0
    def _perform_filtering_on_file(self, filename, file_content, file_encoding, html_escape):

        verbose(LOGGER).debug('Filtering file "%s" using encoding "%s"', filename, file_encoding)
        file_content = file_content.decode(file_encoding)

        if html_escape:
            file_content = self.html_escape_function(os.path.basename(filename), file_content)

        file_content_filtered = self.filter(file_content)

        with open(filename, "w") as output_file:
            output_file.write(file_content_filtered.encode(file_encoding))
Пример #13
0
def clean_up_deleted_hosts_data(svn_service, revision):
    """ Deletes host directories within config viewer data
        when the svn change set contains a delete of the host directory """

    deleted_paths = svn_service.get_deleted_paths(revision)

    if deleted_paths:
        LOGGER.debug("Change set contains %d deleted path(s).",
                     len(deleted_paths))
        _delete_host_directories(deleted_paths)
    else:
        verbose(LOGGER).debug("Change set did not contain any deleted paths.")
    def _perform_filtering_on_file(self, filename, file_content, file_encoding,
                                   html_escape):

        verbose(LOGGER).debug('Filtering file "%s" using encoding "%s"',
                              filename, file_encoding)
        file_content = file_content.decode(file_encoding)

        if html_escape:
            file_content = self.html_escape_function(
                os.path.basename(filename), file_content)

        file_content_filtered = self.filter(file_content)

        with open(filename, "w") as output_file:
            output_file.write(file_content_filtered.encode(file_encoding))
Пример #15
0
    def assert_no_cycles_present(self):

        verbose(LOGGER).debug('Checking that graph %s has no cycles.', self.edges)

        cycles = []
        components = tarjan_scc(self.edges)
        for component in components:
            if len(component) > 1:
                cycles.append(component)
                verbose(LOGGER).debug("Found cycle %s in graph %s", component, self.edges)
                # every nontrivial strongly connected component
                # contains at least one directed cycle, so len()>1 is a showstopper

        if len(cycles) > 0:
            error_message = "Found cycle(s) in variable declarations :\n"
            for cycle in cycles:
                error_message += "These variables form a cycle : " + str(cycle) + "\n"
            raise ContainsCyclesException(error_message)
Пример #16
0
    def assert_no_cycles_present(self):

        verbose(LOGGER).debug('Checking that graph %s has no cycles.', self.edges)

        cycles = []
        components = tarjan_scc(self.edges)
        for component in components:
            if len(component) > 1:
                cycles.append(component)
                verbose(LOGGER).debug("Found cycle %s in graph %s", component, self.edges)
                # every nontrivial strongly connected component
                # contains at least one directed cycle, so len()>1 is a showstopper

        if len(cycles) > 0:
            error_message = "Found cycle(s) in variable declarations :\n"
            for cycle in cycles:
                error_message += "These variables form a cycle : " + str(cycle) + "\n"
            raise ContainsCyclesException(error_message)
Пример #17
0
    def filter_file(self, filename, html_escape=False):
        try:
            self.file_size_limit = get_max_file_size()

            if getsize(filename) > self.file_size_limit:
                raise FileLimitExceededException(filename, self.file_size_limit)

            file_content = self._read_content_from_file(filename)

            file_encoding = self._get_file_encoding(file_content)
            if file_encoding:
                if file_encoding != 'binary' and file_encoding != 'unknown-8bit':
                    self._perform_filtering_on_file(filename, file_content, file_encoding, html_escape)
                else:
                    verbose(LOGGER).warn('Not filtering file "%s" since it has encoding "%s".', filename, file_encoding)

        except MissingTokenException as exception:
            raise MissingTokenException(exception.token, filename)

        except Exception as e:
            raise CannotFilterFileException('Cannot filter file %s.\n%s' % (os.path.basename(filename), str(e)))
Пример #18
0
    def resolve(self, hostname):
        dns_searchlist = get_custom_dns_search_list()

        if dns_searchlist:
            for dns_suffix in dns_searchlist:
                fqdn_hostname = hostname + '.' + dns_suffix
                try:
                    result = self._resolve(fqdn_hostname)
                    verbose(LOGGER).debug('Host name "%s" resolved to %s',
                                          fqdn_hostname, result)
                    return result

                except Exception as exception:
                    verbose(LOGGER).debug(
                        'Resolving "%s" resulted in error: %s', fqdn_hostname,
                        str(exception))

        else:
            try:
                result = self._resolve(hostname)
                verbose(LOGGER).debug('Host name "%s" resolved to %s',
                                      hostname, result)
                return result
            except Exception as exception:
                verbose(LOGGER).debug('Resolving "%s" resulted in error: %s',
                                      hostname, str(exception))

        if not unknown_hosts_are_allowed():
            raise Exception("Could not lookup '%s' with 'getent hosts'" %
                            hostname)

        ip = "127.0.0.1"
        fqdn = "localhost.localdomain"
        aliases = ""
        verbose(LOGGER).debug('Could not resolve "%s" using default values %s',
                              hostname, (ip, fqdn, aliases))
        return ip, fqdn, aliases
    def __init__(self,
                 token_values={},
                 replacer_function=None,
                 html_escape_function=None):
        self.token_values = {}
        self.token_used = set()
        for token in token_values:
            self.token_values[token] = token_values[token].decode(
                'UTF-8').strip()

        if not replacer_function:

            def replacer_function(token, replacement):
                return replacement
        else:
            verbose(LOGGER).debug("Using custom replacer_function %s",
                                  replacer_function.__name__)

        if not html_escape_function:

            def html_escape_function(filename, content):
                try:
                    content = cgi.escape(content, quote=True)
                    return u"<!DOCTYPE html><html><head><title>%s</title></head><body><pre>%s</pre></body></html>" % (
                        filename, content)
                except Exception as e:
                    raise CouldNotEscapeHtmlException(
                        "Could not html escape file: " + filename + '\n\n' +
                        str(e))

        self.replacer_function = replacer_function
        self.html_escape_function = html_escape_function

        self.token_values = self._replace_tokens_in_token_values(
            self.token_values)
        self.magic_mime_encoding = None