Ejemplo n.º 1
0
    def __init__(self):

        super(RawReportProcessor, self).__init__()
        from faraday.client.start_client import setupPlugins
        setupPlugins()

        self.pending_actions = Queue()

        try:
            plugin_manager = PluginManager(
                os.path.join(CONF.getConfigPath(), "plugins"))
        except AttributeError:
            get_logger().warning(
                "Upload reports in WEB-UI not configurated, run Faraday client and try again..."
            )
            self._stop = True
            return

        mappers_manager = MapperManager()

        self.model_controller = ModelController(mappers_manager,
                                                self.pending_actions)
        self.model_controller.start()
        self.end_event = Event()

        plugin_controller = PluginController('PluginController',
                                             plugin_manager, mappers_manager,
                                             self.pending_actions,
                                             self.end_event)

        self.processor = ReportProcessor(plugin_controller, None)
        self._stop = False
Ejemplo n.º 2
0
    def __init__(self, args):
        self._original_excepthook = sys.excepthook

        self.args = args

        if args.creds_file:
            try:
                with open(args.creds_file, 'r') as fp:
                    creds = json.loads(fp.read())
                    username = creds.get('username')
                    password = creds.get('password')
                    session_cookie = login_user(CONF.getServerURI(),
                                                username, password)
                    if session_cookie:
                        logger.info('Login successful')
                        CONF.setDBUser(username)
                        CONF.setDBSessionCookies(session_cookie)
                    else:
                        logger.error('Login failed')
            except (IOError, ValueError):
                logger.error("Credentials file couldn't be loaded")

        self._mappers_manager = MapperManager()
        pending_actions = Queue()
        self._model_controller = ModelController(self._mappers_manager, pending_actions)

        self._plugin_manager = PluginManager(
            os.path.join(CONF.getConfigPath(), "plugins"),
            pending_actions=pending_actions,
        )

        self._workspace_manager = WorkspaceManager(
            self._mappers_manager)

        # Create a PluginController and send this to UI selected.
        self._plugin_controller = PluginController(
            'PluginController',
            self._plugin_manager,
            self._mappers_manager,
            pending_actions
        )

        if self.args.cli:

            self.app = CliApp(self._workspace_manager, self._plugin_controller)

            if self.args.keep_old:
                CONF.setMergeStrategy("old")
            else:
                CONF.setMergeStrategy("new")

        else:
            self.app = UiFactory.create(self._model_controller,
                                        self._plugin_manager,
                                        self._workspace_manager,
                                        self._plugin_controller,
                                        self.args.gui)

        self.timer = TimerClass()
        self.timer.start()
Ejemplo n.º 3
0
def test_end_pluging_multiple_times():
    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    controller._pluginEnd('test', None)
    controller._pluginEnd('test', None)
    assert controller.active_plugins_count == 0
    assert controller.processing is False
Ejemplo n.º 4
0
def test_controller_stop_when_is_not_processing():
    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    assert controller.processing is False
    assert controller._must_stop is False
    controller.start()
    assert controller.is_alive()
    controller.stop()
    controller.join()
    assert controller._must_stop is True
    assert controller.is_alive() is False
Ejemplo n.º 5
0
def test_find(get, url_endpoint, test_data, session):
    if 'api_result' in test_data:
        get.return_value = test_data['api_result']
    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    workspace = WorkspaceFactory.create()
    mappers_manager.createMappers(workspace.name)
    obj = test_data['factory'].create(workspace=workspace)
    session.add(obj)
    session.commit()
    result = controller.find(test_data['class_signature'], obj.id)
    assert get.called
    print(get.mock_calls[0][1][0])
    assert get.mock_calls[0][1][0].endswith('/_api/v2/ws/{0}/{1}/{2}/'.format(
        workspace.name, url_endpoint, obj.id))
Ejemplo n.º 6
0
def process_workspaces(mappers_manager, plugin_manager, query, disable_polling):
    report_managers = []
    controllers = []
    for workspace in query.all():
        pending_actions = Queue()
        plugin_controller = PluginController(
            'PluginController',
            plugin_manager,
            mappers_manager,
            pending_actions
        )
        mappers_manager.createMappers(workspace.name)
        controller = ModelController(mappers_manager, pending_actions)
        workspace_manager = WorkspaceManager(mappers_manager)
        setUpAPIs(controller, workspace_manager, hostname=None, port=None)
        controller.start()
        controllers.append(controller)
        report_manager = ReportManager(
            0.1,
            workspace.name,
            plugin_controller,
            polling=not disable_polling
        )
        report_managers.append(report_manager)
        report_manager.start()

    for report_manager in report_managers:
        report_manager.stop()

    for controller in controllers:
        controller.stop()
Ejemplo n.º 7
0
def test_only_start_plugin():
    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    controller._pluginStart('test', None)
    assert controller.active_plugins_count == 1
    assert controller.processing
    controller._pluginStart('test', None)
    assert controller.active_plugins_count == 2
Ejemplo n.º 8
0
class RawReportProcessor(Thread):
    def __init__(self):

        super(RawReportProcessor, self).__init__()
        from faraday.client.start_client import setupPlugins
        setupPlugins()

        self.pending_actions = Queue()

        try:
            plugin_manager = PluginManager(
                os.path.join(CONF.getConfigPath(), "plugins"))
        except AttributeError:
            get_logger().warning(
                "Upload reports in WEB-UI not configurated, run Faraday client and try again..."
            )
            self._stop = True
            return

        mappers_manager = MapperManager()

        self.model_controller = ModelController(mappers_manager,
                                                self.pending_actions)
        self.model_controller.start()
        self.end_event = Event()

        plugin_controller = PluginController('PluginController',
                                             plugin_manager, mappers_manager,
                                             self.pending_actions,
                                             self.end_event)

        self.processor = ReportProcessor(plugin_controller, None)
        self._stop = False

    def stop(self):
        self.model_controller.stop()
        self._stop = True

    def run(self):
        logger.info('Tool report processor started')
        while not self._stop:
            try:

                workspace, file_path, cookie = UPLOAD_REPORTS_QUEUE.get(
                    False, timeout=0.1)
                get_logger().info(
                    'Processing raw report {0}'.format(file_path))

                # Cookie of user, used to create objects in server with the right owner.
                server.FARADAY_UPLOAD_REPORTS_WEB_COOKIE = cookie
                server.FARADAY_UPLOAD_REPORTS_OVERWRITE_SERVER_URL = "http://{0}:{1}".format(
                    FaradayServerConfig.faraday_server.bind_address,
                    FaradayServerConfig.faraday_server.port)

                self.processor.ws_name = workspace

                command_id = self.processor.processReport(file_path)
                UPLOAD_REPORTS_CMD_QUEUE.put(command_id)
                if not command_id:
                    continue

                self.end_event.wait()
                get_logger().info(
                    'Report processing of report {0} finished'.format(
                        file_path))
                self.end_event.clear()

            except Empty:
                time.sleep(0.1)

            except KeyboardInterrupt as ex:
                get_logger().info(
                    'Keyboard interrupt, stopping report processing thread')
                self.stop()

            except Exception as ex:
                get_logger().exception(ex)
                continue
Ejemplo n.º 9
0
def test_controller_cant_be_stopped_when_is_processing():
    """
        If someone tells the controller to stop and it is processing then it
        will stop when the processing finishes
    """

    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    assert controller.processing is False
    assert controller._must_stop is False
    controller.start()
    controller.processing = True
    controller.active_plugins_count = 1
    assert controller.is_alive()
    controller.stop()
    assert controller._must_stop is True
    assert controller.processing
    controller.join(timeout=2)
    assert controller.is_alive()
    controller.processing = False
    controller.join()
    assert controller.is_alive() is False
Ejemplo n.º 10
0
def test_controller_plugin_start_action_updates_internal_state():
    mappers_manager = MapperManager()
    pending_actions = Queue()
    controller = ModelController(mappers_manager, pending_actions)
    controller.start()
    controller.add_action((Modelactions.PLUGINSTART, "test", None))
    time.sleep(1)
    assert controller.active_plugins_count == 1
    assert controller.processing
    controller.add_action((Modelactions.PLUGINEND, "test", None))
    time.sleep(1)
    assert controller.active_plugins_count == 0
    assert controller.processing is False
    controller.stop()
    controller.join()
    assert controller.is_alive() is False
Ejemplo n.º 11
0
def dispatch(args, unknown, user_help, username, password):
    session_cookie = login_user(args.url, username, password)
    if not session_cookie:
        raise UserWarning('Invalid credentials!')

    CONF.setDBUser(username)
    CONF.setDBSessionCookies(session_cookie)

    if '--' in unknown:
        unknown.remove('--')

    # We need the ModelController to register all available models
    mappers_manager = MapperManager()
    pending_actions = Queue()
    model_controller = ModelController(mappers_manager, pending_actions)

    if not args.command:
        print(user_help)
        if not args.interactive:
            sys.exit(1)

    if args.command not in list(plugins.keys()):
        sys.stderr.write(Fore.RED +
                         ("ERROR: Plugin %s not found.\n" % args.command) +
                         Fore.RESET)
        if args.interactive:
            return None
        else:
            sys.exit(1)

    from faraday import client  # pylint:disable=import-outside-toplevel
    faraday_directory = os.path.dirname(
        os.path.realpath(os.path.join(client.__file__)))

    plugin_path = os.path.join(faraday_directory, "bin/", args.command + '.py')
    # Get filename and import this
    module_fplugin = imp.load_source('module_fplugin', plugin_path)
    module_fplugin.models.server.FARADAY_UP = False
    module_fplugin.models.server.SERVER_URL = args.url
    module_fplugin.models.server.AUTH_USER = username
    module_fplugin.models.server.AUTH_PASS = password

    call_main = getattr(module_fplugin, 'main', None)

    if call_main is None:
        sys.stderr.write(Fore.RED + "ERROR: Main function not found.\n" +
                         Fore.RESET)
        if args.interactive:
            return None
        else:
            sys.exit(1)

    # Inspect the main function imported from the plugin and decide the best calling option
    main_argspec = inspect.getargspec(call_main)

    if main_argspec != CURRENT_MAIN_ARGSPEC:
        # Function argspec does not match current API.
        # Warn the user and call with original parameteres.
        sys.stderr.write(
            Fore.YELLOW +
            "WARNING: Plugin does not follow current call signature. Please update it! [%s.py]\n"
            % args.command + Fore.RESET)

    obj_id = None

    if {'args', 'parser'} <= set(main_argspec.args):
        # Function accepts args and parser arguments

        new_parser = argparse.ArgumentParser(
            description=plugins[args.command]['description'],
            prog="fplugin %s" % args.command,
            formatter_class=RawDescriptionAndDefaultsHelpFormatter)

        ret, obj_id = call_main(workspace=args.workspace,
                                args=unknown,
                                parser=new_parser)

        if obj_id is not None:
            print(obj_id)
    else:
        # Use old API to call plugin
        sys.stderr.write(
            Fore.YELLOW +
            "WARNING: Call with arguments and parser not supported.\n" +
            Fore.RESET)
        ret = call_main(workspace=args.workspace)

    if ret is None:
        ret = 0

    if args.interactive:
        # print ('code = %d' % ret)
        return obj_id
    else:
        sys.exit(ret)
Ejemplo n.º 12
0
class MainApplication(object):

    def __init__(self, args):
        self._original_excepthook = sys.excepthook

        self.args = args

        if args.creds_file:
            try:
                with open(args.creds_file, 'r') as fp:
                    creds = json.loads(fp.read())
                    username = creds.get('username')
                    password = creds.get('password')
                    session_cookie = login_user(CONF.getServerURI(),
                                                username, password)
                    if session_cookie:
                        logger.info('Login successful')
                        CONF.setDBUser(username)
                        CONF.setDBSessionCookies(session_cookie)
                    else:
                        logger.error('Login failed')
            except (IOError, ValueError):
                logger.error("Credentials file couldn't be loaded")

        self._mappers_manager = MapperManager()
        pending_actions = Queue()
        self._model_controller = ModelController(self._mappers_manager, pending_actions)

        self._plugin_manager = PluginManager(
            os.path.join(CONF.getConfigPath(), "plugins"),
            pending_actions=pending_actions,
        )

        self._workspace_manager = WorkspaceManager(
            self._mappers_manager)

        # Create a PluginController and send this to UI selected.
        self._plugin_controller = PluginController(
            'PluginController',
            self._plugin_manager,
            self._mappers_manager,
            pending_actions
        )

        if self.args.cli:

            self.app = CliApp(self._workspace_manager, self._plugin_controller)

            if self.args.keep_old:
                CONF.setMergeStrategy("old")
            else:
                CONF.setMergeStrategy("new")

        else:
            self.app = UiFactory.create(self._model_controller,
                                        self._plugin_manager,
                                        self._workspace_manager,
                                        self._plugin_controller,
                                        self.args.gui)

        self.timer = TimerClass()
        self.timer.start()

    def on_connection_lost(self):
        """All it does is send a notification to the notification center"""
        faraday.client.model.guiapi.notification_center.DBConnectionProblem()

    def enableExceptHook(self):
        sys.excepthook = exception_handler
        installThreadExcepthook()

    def start(self):
        try:
            signal.signal(signal.SIGINT, self.ctrlC)

            faraday.client.model.api.devlog("Starting application...")
            faraday.client.model.api.devlog("Setting up remote API's...")

            if not self.args.workspace:
                workspace = CONF.getLastWorkspace()
                self.args.workspace = workspace

            faraday.client.model.api.setUpAPIs(
                self._model_controller,
                self._workspace_manager,
                CONF.getApiConInfoHost(),
                CONF.getApiConInfoPort())
            faraday.client.model.guiapi.setUpGUIAPIs(self._model_controller)

            faraday.client.model.api.devlog("Starting model controller daemon...")

            self._model_controller.start()
            faraday.client.model.api.startAPIServer()
            restapi.startAPIs(
                self._plugin_controller,
                self._model_controller,
                CONF.getApiConInfoHost(),
                CONF.getApiRestfulConInfoPort()
            )

            faraday.client.model.api.devlog("Faraday ready...")

            exit_code = self.app.run(self.args)

        except Exception as exception:
            print("There was an error while starting Faraday:")
            print("*" * 3)
            print(exception) # instead of traceback.print_exc()
            print("*" * 3)
            exit_code = -1

        finally:
            return self.__exit(exit_code)

    def __exit(self, exit_code=0):
        """
        Exits the application with the provided code.
        It also waits until all app threads end.
        """
        faraday.client.model.api.log("Closing Faraday...")
        faraday.client.model.api.devlog("stopping model controller thread...")
        faraday.client.model.api.stopAPIServer()
        restapi.stopServer()
        self._model_controller.stop()
        if self._model_controller.isAlive():
            # runs only if thread has started, i.e. self._model_controller.start() is run first
            self._model_controller.join()
        self.timer.stop()
        faraday.client.model.api.devlog("Waiting for controller threads to end...")
        return exit_code

    def quit(self):
        """
        Redefined quit handler to nicely end up things
        """
        self.app.quit()

    def ctrlC(self, signal, frame):
        logger.info("Exiting...")
        self.app.quit()