예제 #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), 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)
예제 #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), 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)
예제 #3
0
    def _register_sensor_from_pack(self, pack, sensor):
        sensor_metadata_file_path = sensor

        LOG.debug("Loading sensor from %s.", sensor_metadata_file_path)
        content = self._meta_loader.load(file_path=sensor_metadata_file_path)

        pack_field = content.get("pack", None)
        if not pack_field:
            content["pack"] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception(
                'Model is in pack "%s" but field "pack" is different: %s'
                % (pack, pack_field)
            )

        entry_point = content.get("entry_point", None)
        if not entry_point:
            raise ValueError("Sensor definition missing entry_point")

        # Add in "metadata_file" attribute which stores path to the pack metadata file relative to
        # the pack directory
        metadata_file = content_utils.get_relative_path_to_pack_file(
            pack_ref=pack, file_path=sensor, use_pack_cache=True
        )
        content["metadata_file"] = metadata_file

        # Pass override information
        altered = self._override_loader.override(pack, "sensors", content)

        sensors_dir = os.path.dirname(sensor_metadata_file_path)
        sensor_file_path = os.path.join(sensors_dir, entry_point)
        artifact_uri = "file://%s" % (sensor_file_path)
        content["artifact_uri"] = artifact_uri
        content["entry_point"] = entry_point

        sensor_api = SensorTypeAPI(**content)
        sensor_model = SensorTypeAPI.to_model(sensor_api)

        sensor_types = SensorType.query(pack=sensor_model.pack, name=sensor_model.name)
        if len(sensor_types) >= 1:
            sensor_type = sensor_types[0]
            LOG.debug(
                "Found existing sensor id:%s with name:%s. Will update it.",
                sensor_type.id,
                sensor_type.name,
            )
            sensor_model.id = sensor_type.id

        try:
            sensor_model = SensorType.add_or_update(sensor_model)
        except:
            LOG.exception("Failed creating sensor model for %s", sensor)

        return sensor_model, altered
예제 #4
0
    def _register_sensor_from_pack(self, pack, sensor):
        sensor_metadata_file_path = sensor

        LOG.debug('Loading sensor from %s.', sensor_metadata_file_path)
        content = self._meta_loader.load(file_path=sensor_metadata_file_path)

        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception('Model is in pack "%s" but field "pack" is different: %s' %
                            (pack, pack_field))

        entry_point = content.get('entry_point', None)
        if not entry_point:
            raise ValueError('Sensor definition missing entry_point')

        # Add in "metadata_file" attribute which stores path to the pack metadata file relative to
        # the pack directory
        metadata_file = content_utils.get_relative_path_to_pack_file(pack_ref=pack,
                                                                     file_path=sensor,
                                                                     use_pack_cache=True)
        content['metadata_file'] = metadata_file

        sensors_dir = os.path.dirname(sensor_metadata_file_path)
        sensor_file_path = os.path.join(sensors_dir, entry_point)
        artifact_uri = 'file://%s' % (sensor_file_path)
        content['artifact_uri'] = artifact_uri
        content['entry_point'] = entry_point

        sensor_api = SensorTypeAPI(**content)
        sensor_model = SensorTypeAPI.to_model(sensor_api)

        sensor_types = SensorType.query(pack=sensor_model.pack, name=sensor_model.name)
        if len(sensor_types) >= 1:
            sensor_type = sensor_types[0]
            LOG.debug('Found existing sensor id:%s with name:%s. Will update it.',
                      sensor_type.id, sensor_type.name)
            sensor_model.id = sensor_type.id

        try:
            sensor_model = SensorType.add_or_update(sensor_model)
        except:
            LOG.exception('Failed creating sensor model for %s', sensor)

        return sensor_model
예제 #5
0
    def _register_sensor_from_pack(self, pack, sensor):
        sensor_metadata_file_path = sensor

        LOG.debug('Loading sensor from %s.', sensor_metadata_file_path)
        content = self._meta_loader.load(file_path=sensor_metadata_file_path)

        pack_field = content.get('pack', None)
        if not pack_field:
            content['pack'] = pack
            pack_field = pack
        if pack_field != pack:
            raise Exception(
                'Model is in pack "%s" but field "pack" is different: %s' %
                (pack, pack_field))

        entry_point = content.get('entry_point', None)
        if not entry_point:
            raise ValueError('Sensor definition missing entry_point')

        sensors_dir = os.path.dirname(sensor_metadata_file_path)
        sensor_file_path = os.path.join(sensors_dir, entry_point)
        artifact_uri = 'file://%s' % (sensor_file_path)
        content['artifact_uri'] = artifact_uri
        content['entry_point'] = entry_point

        sensor_api = SensorTypeAPI(**content)
        sensor_model = SensorTypeAPI.to_model(sensor_api)

        sensor_types = SensorType.query(pack=sensor_model.pack,
                                        name=sensor_model.name)
        if len(sensor_types) >= 1:
            sensor_type = sensor_types[0]
            LOG.debug(
                'Found existing sensor id:%s with name:%s. Will update it.',
                sensor_type.id, sensor_type.name)
            sensor_model.id = sensor_type.id

        try:
            sensor_model = SensorType.add_or_update(sensor_model)
        except:
            LOG.exception('Failed creating sensor model for %s', sensor)

        return sensor_model
예제 #6
0
파일: partitioners.py 프로젝트: lyandut/st2
def get_all_enabled_sensors():
    # only query for enabled sensors.
    sensors = SensorType.query(enabled=True)
    LOG.info('Found %d registered sensors in db scan.', len(sensors))
    return sensors
예제 #7
0
def get_all_enabled_sensors():
    # only query for enabled sensors.
    sensors = SensorType.query(enabled=True)
    LOG.info('Found %d registered sensors in db scan.', len(sensors))
    return sensors