Beispiel #1
0
    def test_get_config_existing_config(self):
        pack_name = 'dummy_pack_2'
        parser = ContentPackConfigParser(pack_name=pack_name)

        config = parser.get_config()
        self.assertEqual(config.config['section1']['key1'], 'value1')
        self.assertEqual(config.config['section2']['key10'], 'value10')
Beispiel #2
0
    def test_get_config_existing_config(self):
        pack_name = "dummy_pack_2"
        parser = ContentPackConfigParser(pack_name=pack_name)

        config = parser.get_config()
        self.assertEqual(config.config["section1"]["key1"], "value1")
        self.assertEqual(config.config["section2"]["key10"], "value10")
Beispiel #3
0
    def _get_action_instance(self):
        actions_cls = action_loader.register_plugin(Action, self._file_path)
        action_cls = actions_cls[0] if actions_cls and len(
            actions_cls) > 0 else None

        if not action_cls:
            raise Exception(
                'File "%s" has no action or the file doesn\'t exist.' %
                (self._file_path))

        config_parser = ContentPackConfigParser(pack_name=self._pack)
        config = config_parser.get_action_config(
            action_file_path=self._file_path)

        if config:
            LOG.info('Using config "%s" for action "%s"' %
                     (config.file_path, self._file_path))

            action_instance = action_cls(config=config.config)
        else:
            LOG.info('No config found for action "%s"' % (self._file_path))
            action_instance = action_cls(config={})

        # Setup action_instance proeprties
        action_instance.logger = self._set_up_logger(action_cls.__name__)
        action_instance.datastore = DatastoreService(
            logger=action_instance.logger,
            pack_name=self._pack,
            class_name=action_cls.__name__,
            api_username="******")

        return action_instance
Beispiel #4
0
    def _get_action_instance(self):
        actions_cls = action_loader.register_plugin(Action, self._file_path)
        action_cls = actions_cls[0] if actions_cls and len(
            actions_cls) > 0 else None

        if not action_cls:
            raise Exception(
                'File "%s" has no action or the file doesn\'t exist.' %
                (self._file_path))

        config_parser = ContentPackConfigParser(pack_name=self._pack)
        config = config_parser.get_action_config(
            action_file_path=self._file_path)

        if config:
            LOG.info('Using config "%s" for action "%s"' %
                     (config.file_path, self._file_path))
            config = config.config
        else:
            LOG.info('No config found for action "%s"' % (self._file_path))
            config = None

        action_service = ActionService(action_wrapper=self)
        action_instance = get_action_class_instance(
            action_cls=action_cls,
            config=config,
            action_service=action_service)
        return action_instance
    def test_get_action_and_sensor_config_no_config(self):
        pack_name = 'dummy_pack_1'
        parser = ContentPackConfigParser(pack_name=pack_name)

        config = parser.get_action_config(action_file_path='my_action.py')
        self.assertEqual(config, None)

        config = parser.get_sensor_config(sensor_file_path='my_sensor.py')
        self.assertEqual(config, None)
Beispiel #6
0
    def _get_sensor_config(self):
        config_parser = ContentPackConfigParser(pack_name=self._pack)
        config = config_parser.get_sensor_config(sensor_file_path=self._file_path)

        if config:
            self._logger.info('Using config "%s" for sensor "%s"' % (config.file_path,
                                                                     self._class_name))
            return config.config
        else:
            self._logger.info('No config found for sensor "%s"' % (self._class_name))
            return {}
    def test_get_action_and_sensor_config_existing_config(self):
        pack_name = 'dummy_pack_2'
        parser = ContentPackConfigParser(pack_name=pack_name)

        config = parser.get_action_config(action_file_path='my_action.py')
        self.assertEqual(config.config['section1']['key1'], 'value1')
        self.assertEqual(config.config['section2']['key10'], 'value10')

        config = parser.get_sensor_config(sensor_file_path='my_sensor.py')
        self.assertEqual(config.config['section1']['key1'], 'value1')
        self.assertEqual(config.config['section2']['key10'], 'value10')
Beispiel #8
0
    def _load_action(self):
        actions_kls = action_loader.register_plugin(Action, self.entry_point)
        action_kls = actions_kls[0] if actions_kls and len(actions_kls) > 0 else None

        if not action_kls:
            raise Exception('%s has no action.' % self.entry_point)

        config_parser = ContentPackConfigParser(pack_name=self.pack)
        config = config_parser.get_action_config(action_file_path=self.entry_point)

        if config:
            LOG.info('Using config "%s" for action "%s"' % (config.file_path,
                                                            self.entry_point))

            return action_kls(config=config.config)
        else:
            LOG.info('No config found for action "%s"' % (self.entry_point))
            return action_kls(config={})
Beispiel #9
0
 def test_get_config_for_unicode_char(self):
     pack_name = 'dummy_pack_18'
     parser = ContentPackConfigParser(pack_name=pack_name)
     config = parser.get_config()
     self.assertEqual(config.config['section1']['key1'], u'测试')
Beispiel #10
0
    def test_get_config_no_config(self):
        pack_name = 'dummy_pack_1'
        parser = ContentPackConfigParser(pack_name=pack_name)

        config = parser.get_config()
        self.assertEqual(config, None)
Beispiel #11
0
 def test_get_config_inexistent_pack(self):
     parser = ContentPackConfigParser(pack_name='inexistent')
     config = parser.get_config()
     self.assertEqual(config, None)
Beispiel #12
0
    def __init__(self, pack_name, user=None):
        self.pack_name = pack_name
        self.user = user or cfg.CONF.system_user.user

        self.pack_path = content_utils.get_pack_base_path(pack_name=pack_name)
        self._config_parser = ContentPackConfigParser(pack_name=pack_name)
Beispiel #13
0
 def test_get_action_config_inexistent_pack(self):
     parser = ContentPackConfigParser(pack_name='inexistent')
     config = parser.get_action_config(action_file_path='test.py')
     self.assertEqual(config, None)
Beispiel #14
0
 def test_get_config_for_unicode_char(self):
     pack_name = "dummy_pack_18"
     parser = ContentPackConfigParser(pack_name=pack_name)
     config = parser.get_config()
     self.assertEqual(config.config["section1"]["key1"], "测试")
Beispiel #15
0
    def run_sensors(self, sensors_dict):
        LOG.info('Setting up container to run %d sensors.', len(sensors_dict))
        container_service = ContainerService()
        sensors_to_run = []
        # TODO: Once the API registration is in place, query DB for available
        # sensors here
        # TODO: Use trigger_types and description from sensors metadata
        for filename, sensors in six.iteritems(sensors_dict):
            for sensor_class in sensors:
                sensor_class_kwargs = {}
                class_name = sensor_class.__name__

                # System sensors which are not located inside a content pack
                # don't and can't have custom config associated with them
                pack = getattr(sensor_class, 'pack', None)
                if pack:
                    # TODO: Don't parse the same config multiple times when we
                    # are referring to sensors from the same pack
                    pack = validate_pack_name(name=pack)
                    config_parser = ContentPackConfigParser(pack_name=pack)
                    config = config_parser.get_sensor_config(sensor_file_path=filename)

                    if config:
                        sensor_class_kwargs['config'] = config.config
                        LOG.info('Using config "%s" for sensor "%s"' % (config.file_path,
                                                                        class_name))
                    else:
                        LOG.info('No config found for sensor "%s"' % (class_name))
                        sensor_class_kwargs['config'] = {}
                else:
                    pack = SYSTEM_PACK_NAME

                try:
                    sensor = sensor_class(container_service=container_service,
                                          **sensor_class_kwargs)
                except Exception as e:
                    LOG.warning('Unable to create instance for sensor %s in file %s. Exception: %s',
                                sensor_class, filename, e, exc_info=True)
                    continue

                try:
                    trigger_types = sensor.get_trigger_types()
                    if not trigger_types:
                        trigger_type_dbs = []
                        LOG.warning('No trigger type registered by sensor %s in file %s',
                                    sensor_class, filename)
                    else:
                        assert isinstance(trigger_types, (list, tuple))
                        trigger_type_dbs = container_utils.add_trigger_models(
                            pack=pack,
                            trigger_types=trigger_types)
                except TriggerTypeRegistrationException as e:
                    LOG.warning('Unable to register trigger type for sensor %s in file %s.'
                                + ' Exception: %s', sensor_class, filename, e, exc_info=True)
                    continue

                # Populate sensors dict
                trigger_type_refs = []
                for trigger_type_db, _ in trigger_type_dbs:
                    ref_obj = trigger_type_db.get_reference()
                    trigger_type_ref = ref_obj.ref
                    self._trigger_sensors[trigger_type_ref] = sensor
                    trigger_type_refs.append(trigger_type_ref)

                # Register sensor type in the DB
                sensor_obj = {
                    'filename': os.path.abspath(filename),
                    'name': class_name,
                    'class_name': class_name,
                    'trigger_types': trigger_type_refs
                }
                container_utils.add_sensor_model(pack=pack,
                                                 sensor=sensor_obj)

                # Add good sensor to the run list
                sensors_to_run.append(sensor)

        for trigger in Trigger.get_all():
            self._create_handler(trigger=trigger)

        self._trigger_watcher.start()
        LOG.info('Watcher started.')

        LOG.info('(PID:%s) SensorContainer started.', os.getpid())
        sensor_container = SensorContainer(sensor_instances=sensors_to_run)
        try:
            exit_code = sensor_container.run()
            LOG.info('(PID:%s) SensorContainer stopped. Reason - run ended.', os.getpid())
            return exit_code
        except (KeyboardInterrupt, SystemExit):
            LOG.info('(PID:%s) SensorContainer stopped. Reason - %s', os.getpid(),
                     sys.exc_info()[0].__name__)
            return 0
        finally:
            self._trigger_watcher.stop()