Beispiel #1
0
def _update_hash(checksum, rule):
    """
    Update the given checksum with all required fields of the given rule.
    """
    checksum.update(to_str_from_unicode(rule.ruleset, should_be_uni=True))
    checksum.update(to_str_from_unicode(rule.name, should_be_uni=True))
    for type_ in rule.types:
        checksum.update(to_str_from_unicode(type_, should_be_uni=True))
    # Do not take filter into account
    # ruleset_h.update(str(rule.filter))
    checksum.update(to_str_from_unicode(replace_if_none_by_uni(rule.action), \
                    should_be_uni=True))
    checksum.update(to_str_from_unicode(replace_if_none_by_uni(rule.depsfinder), \
                    should_be_uni=True))
    checksum.update(to_str_from_unicode(replace_if_none_by_uni(rule.help), \
                    should_be_uni=True))
    for dep in rule.dependson:
        checksum.update(to_str_from_unicode(replace_if_none_by_uni(dep), \
                    should_be_uni=True))
Beispiel #2
0
    def add_rule(self, rule, commit=True):
        """
        Create a single entry in the DB.
        """
        # NB: it is OK to remove the conversion to str because all the args used
        # to create a Rule are unicode-typed (done in the main).
        # As create_rule_from_string_array converts the string "None" to 
        # the value None, we have to convert it back.
        config = self.config_for_ruleset.setdefault(rule.ruleset,
                                                    UnicodeConfigParser())
        _LOGGER.info("Adding rule: %s to %s", rule, str(config.sections()))
        try:
            config.add_section(rule.name)
        except DuplicateSectionError as dse:
            _LOGGER.debug("DuplicateSectionError catched: %s"
                          " -> raising DuplicateRuleError", dse)
            raise DuplicateRuleError(rule.ruleset, rule.name)
        config.set(rule.name, 'types', ",".join(rule.types))

        config.set(rule.name, 'filter', replace_if_none_by_uni(rule.filter))

        # was "... str(rule.action))"
        config.set(rule.name, 'action', replace_if_none_by_uni(rule.action))

        # was "... str(rule.depsfinder))"
        config.set(rule.name, 'depsfinder', 
                    replace_if_none_by_uni(rule.depsfinder))

        config.set(rule.name, 'dependson',
                   NONE_VALUE if len(rule.dependson) == 0 else u",".join(rule.dependson))

        # was "... str(rule.comments))"
        config.set(rule.name, 'comments', replace_if_none_by_uni(rule.comments))

        # was "... str(rule.help))"
        config.set(rule.name, 'help', replace_if_none_by_uni(rule.help))

        if commit:
            self._commit_all_changes([rule.ruleset])
Beispiel #3
0
    def update_rule(self, ruleset, name, update_set, nodeps=False, commit=True):
        """
        Update the column 'col' of the given rule ('ruleset', 'name')
        with the given value 'val'. Return true Iff the given rule has
        been successfully updated. False otherwise.
        """
        config = self.config_for_ruleset.get(ruleset)
        if config is None:
            raise UnknownRuleSet(ruleset)
        if not config.has_section(name):
            raise NoSuchRuleError(ruleset, name)

        new_ruleset = None
        new_name = None
        new_config = None
        _LOGGER.info("Updating rule (%s, %s) with %s",
                     ruleset, name, str(update_set))

        record_set = []
        # Remove ruleset and name from the record as they are not
        # present in the config file (config file name is the ruleset,
        # and section is the rule name)
        for record in update_set:
            if record[0].upper() == 'NAME':
                if not nodeps:
                    new_name = record[1]
                continue
            if record[0].upper() == 'RULESET':
                new_ruleset = record[1]
                continue
            record_set.append(record)

        if _LOGGER.isEnabledFor(logging.DEBUG):
            _LOGGER.debug("Original config: %s", str(config.items(name)))
            _LOGGER.debug("Config change: %s", str(record_set))
        section_name = new_name if new_name is not None else name
        # Copy and remove rule from previous config if a new ruleset
        # has been specified
        if new_ruleset is not None:
            new_config = self.config_for_ruleset.setdefault(new_ruleset,
                                                         UnicodeConfigParser())
            if new_config.has_section(section_name):
                raise ValueError("Cannot move (%s, %s)" % (ruleset, name) + \
                                     " to (%s, %s):" % (new_ruleset,
                                                       section_name) + \
                                     " destination already exists")
            # Copy
            _LOGGER.debug("Copying (%s, %s) section to (%s, %s)",
                          ruleset, name, new_ruleset, section_name)
            new_config.add_section(section_name)
            for (option, value) in config.items(name):
                new_config.set(section_name, option, value)
            # Remove
            _LOGGER.debug("Removing section (%s, %s)", ruleset, name)
            config.remove_section(name)

        # Copy rule to a new section if new name has been specified
        elif new_name is not None:
            items = list(config.items(name))
            # Remove
            _LOGGER.debug("Removing section (%s, %s)", ruleset, name)
            config.remove_section(name)
            # Copy
            if config.has_section(section_name):
                raise ValueError("Cannot move (%s, %s)" % (ruleset, name) + \
                                     " to (%s, %s):" % (ruleset,
                                                       section_name) + \
                                     " destination already exists")
            _LOGGER.debug("Copying (%s, %s) section to (%s, %s)",
                          ruleset, name, ruleset, section_name)
            config.add_section(section_name)
            for (option, value) in items:
                config.set(section_name, option, value)

        # Update
        final_config = config if new_ruleset is None else new_config
        for record in record_set:
            # Was : str(record[0]), str(record[1]). Unnecessary because all the
            # args used are unicode-typed (done in the main).
            final_config.set(section_name, record[0],
                                replace_if_none_by_uni(record[1]))

        _LOGGER.debug("Final config: %s",
                      final_config.items(section_name))

        # Update dependencies
        if not nodeps and (new_name is not None or new_ruleset is not None):
            try:
                rules = self.get_rules_for(ruleset)
                for rule in rules.values():
                    deps = rule.dependson
                    if deps is not None and name in deps:
                        deps.remove(name)
                        if new_ruleset is None:
                            deps.add(new_name)
                        update_set = set()
                        update_set.add(("dependson", ",".join(deps)))
                        _LOGGER.info("Updating deps of %s: ", rule)
                        self.update_rule(ruleset, rule.name, update_set,
                                         nodeps=False, commit=False)
            except UnknownRuleSet:
                pass
        if commit:
            self._commit_all_changes()
        return True
Beispiel #4
0
 def test_replace_if_none_by_uni(self):
     noneval = None
     noneuni = lib.replace_if_none_by_uni(noneval)
     assert type(noneuni) == unicode
     assert noneuni == u"None"