Beispiel #1
0
    def test_run(self):
        pack = 'dummy_pack_1'
        # Verify all the resources are there

        pack_dbs = Pack.query(ref=pack)
        action_dbs = Action.query(pack=pack)
        alias_dbs = ActionAlias.query(pack=pack)
        rule_dbs = Rule.query(pack=pack)
        sensor_dbs = Sensor.query(pack=pack)
        trigger_type_dbs = TriggerType.query(pack=pack)
        policy_dbs = Policy.query(pack=pack)

        config_schema_dbs = ConfigSchema.query(pack=pack)
        config_dbs = Config.query(pack=pack)

        self.assertEqual(len(pack_dbs), 1)
        self.assertEqual(len(action_dbs), 1)
        self.assertEqual(len(alias_dbs), 2)
        self.assertEqual(len(rule_dbs), 1)
        self.assertEqual(len(sensor_dbs), 3)
        self.assertEqual(len(trigger_type_dbs), 4)
        self.assertEqual(len(policy_dbs), 2)

        self.assertEqual(len(config_schema_dbs), 1)
        self.assertEqual(len(config_dbs), 1)

        # Run action
        action = self.get_action_instance()
        action.run(packs=[pack])

        # Make sure all resources have been removed from the db
        pack_dbs = Pack.query(ref=pack)
        action_dbs = Action.query(pack=pack)
        alias_dbs = ActionAlias.query(pack=pack)
        rule_dbs = Rule.query(pack=pack)
        sensor_dbs = Sensor.query(pack=pack)
        trigger_type_dbs = TriggerType.query(pack=pack)
        policy_dbs = Policy.query(pack=pack)

        config_schema_dbs = ConfigSchema.query(pack=pack)
        config_dbs = Config.query(pack=pack)

        self.assertEqual(len(pack_dbs), 0)
        self.assertEqual(len(action_dbs), 0)
        self.assertEqual(len(alias_dbs), 0)
        self.assertEqual(len(rule_dbs), 0)
        self.assertEqual(len(sensor_dbs), 0)
        self.assertEqual(len(trigger_type_dbs), 0)
        self.assertEqual(len(policy_dbs), 0)

        self.assertEqual(len(config_schema_dbs), 0)
        self.assertEqual(len(config_dbs), 0)
Beispiel #2
0
    def test_run(self):
        pack = 'dummy_pack_1'
        # Verify all the resources are there

        pack_dbs = Pack.query(ref=pack)
        action_dbs = Action.query(pack=pack)
        alias_dbs = ActionAlias.query(pack=pack)
        rule_dbs = Rule.query(pack=pack)
        sensor_dbs = Sensor.query(pack=pack)
        trigger_type_dbs = TriggerType.query(pack=pack)
        policy_dbs = Policy.query(pack=pack)

        config_schema_dbs = ConfigSchema.query(pack=pack)
        config_dbs = Config.query(pack=pack)

        self.assertEqual(len(pack_dbs), 1)
        self.assertEqual(len(action_dbs), 1)
        self.assertEqual(len(alias_dbs), 3)
        self.assertEqual(len(rule_dbs), 1)
        self.assertEqual(len(sensor_dbs), 3)
        self.assertEqual(len(trigger_type_dbs), 4)
        self.assertEqual(len(policy_dbs), 2)

        self.assertEqual(len(config_schema_dbs), 1)
        self.assertEqual(len(config_dbs), 1)

        # Run action
        action = self.get_action_instance()
        action.run(packs=[pack])

        # Make sure all resources have been removed from the db
        pack_dbs = Pack.query(ref=pack)
        action_dbs = Action.query(pack=pack)
        alias_dbs = ActionAlias.query(pack=pack)
        rule_dbs = Rule.query(pack=pack)
        sensor_dbs = Sensor.query(pack=pack)
        trigger_type_dbs = TriggerType.query(pack=pack)
        policy_dbs = Policy.query(pack=pack)

        config_schema_dbs = ConfigSchema.query(pack=pack)
        config_dbs = Config.query(pack=pack)

        self.assertEqual(len(pack_dbs), 0)
        self.assertEqual(len(action_dbs), 0)
        self.assertEqual(len(alias_dbs), 0)
        self.assertEqual(len(rule_dbs), 0)
        self.assertEqual(len(sensor_dbs), 0)
        self.assertEqual(len(trigger_type_dbs), 0)
        self.assertEqual(len(policy_dbs), 0)

        self.assertEqual(len(config_schema_dbs), 0)
        self.assertEqual(len(config_dbs), 0)
Beispiel #3
0
 def test_rule_lookup(self):
     triggertype = ReactorModelTest._create_save_triggertype()
     trigger = ReactorModelTest._create_save_trigger(triggertype)
     runnertype = ActionModelTest._create_save_runnertype()
     action = ActionModelTest._create_save_action(runnertype)
     saved = ReactorModelTest._create_save_rule(trigger, action)
     retrievedrules = Rule.query(trigger=reference.get_str_resource_ref_from_model(trigger))
     self.assertEqual(1, len(retrievedrules), 'No rules found.')
     for retrievedrule in retrievedrules:
         self.assertEqual(saved.id, retrievedrule.id, 'Incorrect rule returned.')
     ReactorModelTest._delete([saved, trigger, action, runnertype, triggertype])
Beispiel #4
0
 def test_rule_lookup(self):
     triggertype = ReactorModelTest._create_save_triggertype()
     trigger = ReactorModelTest._create_save_trigger(triggertype)
     runnertype = ActionModelTest._create_save_runnertype()
     action = ActionModelTest._create_save_action(runnertype)
     saved = ReactorModelTest._create_save_rule(trigger, action)
     retrievedrules = Rule.query(trigger=reference.get_str_resource_ref_from_model(trigger))
     self.assertEqual(1, len(retrievedrules), 'No rules found.')
     for retrievedrule in retrievedrules:
         self.assertEqual(saved.id, retrievedrule.id, 'Incorrect rule returned.')
     ReactorModelTest._delete([saved, trigger, action, runnertype, triggertype])
Beispiel #5
0
 def test_rule_lookup_disabled(self):
     triggertype = ReactorModelTest._create_save_triggertype()
     trigger = ReactorModelTest._create_save_trigger(triggertype)
     runnertype = ActionModelTest._create_save_runnertype()
     action = ActionModelTest._create_save_action(runnertype)
     saved = ReactorModelTest._create_save_rule(trigger, action, False)
     retrievedrules = Rule.query(trigger=reference.get_str_resource_ref_from_model(trigger), enabled=False)
     self.assertEqual(1, len(retrievedrules), "Error looking up enabled rules.")
     for retrievedrule in retrievedrules:
         self.assertEqual(saved.id, retrievedrule.id, "Incorrect rule returned.")
     ReactorModelTest._delete([saved, trigger, action, runnertype, triggertype])
Beispiel #6
0
    def get_matching_rules_for_trigger(self, trigger_instance):
        trigger = trigger_instance.trigger
        trigger = get_trigger_db_by_ref(trigger_instance.trigger)
        rules = Rule.query(trigger=trigger_instance.trigger, enabled=True)
        LOG.info('Found %d rules defined for trigger %s (type=%s)', len(rules), trigger['name'],
                 trigger['type'])
        matcher = RulesMatcher(trigger_instance=trigger_instance,
                               trigger=trigger, rules=rules)

        matching_rules = matcher.get_matching_rules()
        LOG.info('Matched %s rule(s) for trigger_instance %s (type=%s)', len(matching_rules),
                 trigger['name'], trigger['type'])
        return matching_rules
    def get_matching_rules_for_trigger(self, trigger_instance):
        trigger = trigger_instance.trigger
        trigger = get_trigger_db_by_ref(trigger_instance.trigger)
        rules = Rule.query(trigger=trigger_instance.trigger, enabled=True)
        LOG.info('Found %d rules defined for trigger %s (type=%s)', len(rules),
                 trigger['name'], trigger['type'])
        matcher = RulesMatcher(trigger_instance=trigger_instance,
                               trigger=trigger,
                               rules=rules)

        matching_rules = matcher.get_matching_rules()
        LOG.info('Matched %s rule(s) for trigger_instance %s (type=%s)',
                 len(matching_rules), trigger['name'], trigger['type'])
        return matching_rules
Beispiel #8
0
def get_rules_with_trigger_ref(trigger_ref=None, enabled=True):
    """
    Get rules in DB corresponding to given trigger_ref as a string reference.

    :param trigger_ref: Reference to trigger.
    :type trigger_ref: ``str``

    :rtype: ``list`` of ``RuleDB``
    """

    if not trigger_ref:
        return None

    LOG.debug('Querying rules with trigger %s', trigger_ref)
    return Rule.query(trigger=trigger_ref, enabled=enabled)
Beispiel #9
0
def get_rules_with_trigger_ref(trigger_ref=None, enabled=True):
    """
    Get rules in DB corresponding to given trigger_ref as a string reference.

    :param trigger_ref: Reference to trigger.
    :type trigger_ref: ``str``

    :rtype: ``list`` of ``RuleDB``
    """

    if not trigger_ref:
        return None

    LOG.debug('Querying rules with trigger %s', trigger_ref)
    return Rule.query(trigger=trigger_ref, enabled=enabled)
Beispiel #10
0
 def test_rule_lookup_disabled(self):
     triggertype = ReactorModelTestCase._create_save_triggertype()
     trigger = ReactorModelTestCase._create_save_trigger(triggertype)
     runnertype = ActionModelTestCase._create_save_runnertype()
     action = ActionModelTestCase._create_save_action(runnertype)
     saved = ReactorModelTestCase._create_save_rule(trigger, action, False)
     retrievedrules = Rule.query(
         trigger=reference.get_str_resource_ref_from_model(trigger),
         enabled=False)
     self.assertEqual(1, len(retrievedrules),
                      'Error looking up enabled rules.')
     for retrievedrule in retrievedrules:
         self.assertEqual(saved.id, retrievedrule.id,
                          'Incorrect rule returned.')
     ReactorModelTestCase._delete(
         [saved, trigger, action, runnertype, triggertype])
Beispiel #11
0
 def analyze(self, root_action_ref, link_tigger_ref):
     rules = Rule.query(trigger=link_tigger_ref, enabled=True)
     # pprint.pprint([rule.ref for rule in rules])
     for rule in rules:
         source_action_ref = self._get_source_action_ref(rule)
         if not source_action_ref:
             print('No source_action_ref for rule %s' % rule.ref)
             continue
         rule_links = self._rules.get(source_action_ref, None)
         if rule_links is None:
             rule_links = []
             self._rules[source_action_ref] = rule_links
         rule_links.append(RuleLink(source_action_ref=source_action_ref, rule_ref=rule.ref,
                                    dest_action_ref=rule.action.ref))
     analyzed = self._do_analyze(action_ref=root_action_ref)
     for (depth, rule_link) in analyzed:
         print('%s%s' % ('  ' * depth, rule_link))
     return analyzed
Beispiel #12
0
 def analyze(self, root_action_ref, link_tigger_ref):
     rules = Rule.query(trigger=link_tigger_ref, enabled=True)
     # pprint.pprint([rule.ref for rule in rules])
     for rule in rules:
         source_action_ref = self._get_source_action_ref(rule)
         if not source_action_ref:
             print 'No source_action_ref for rule %s' % rule.ref
             continue
         rule_links = self._rules.get(source_action_ref, None)
         if rule_links is None:
             rule_links = []
             self._rules[source_action_ref] = rule_links
         rule_links.append(RuleLink(source_action_ref=source_action_ref, rule_ref=rule.ref,
                                    dest_action_ref=rule.action.ref))
     analyzed = self._do_analyze(action_ref=root_action_ref)
     for (depth, rule_link) in analyzed:
         print '%s%s' % ('  ' * depth, rule_link)
     return analyzed