Exemple #1
0
def is_server_running(port):
    pid = daemonize.is_server_running(port)
    if pid is not None:
        logger.warn("Faraday Server is already running. PID: {}".format(pid))
        return True
    else:
        return False
Exemple #2
0
def check_postgresql():
    with app.app_context():
        try:
            if not db.session.query(Workspace).count():
                logger.warn(
                    'No workspaces found. Remember to execute CouchDB importer'
                )
        except sqlalchemy.exc.ArgumentError:
            logger.error(
                '\n\b{RED}Please check your PostgreSQL connection string in the file ~/.faraday/config/server.ini on your home directory.{WHITE} \n'
                .format(RED=Fore.RED, WHITE=Fore.WHITE))
            sys.exit(1)
        except sqlalchemy.exc.OperationalError:
            logger.error(
                '\n\n{RED}Could not connect to PostgreSQL.\n{WHITE}Please check: \n{YELLOW}  * if database is running \n  * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} faraday-manage initdb\n\n'
                .format(GREEN=Fore.GREEN,
                        YELLOW=Fore.YELLOW,
                        WHITE=Fore.WHITE,
                        RED=Fore.RED))
            sys.exit(1)
        except sqlalchemy.exc.ProgrammingError as e:
            logger.error(
                '\n\nn{WHITE}Missing migrations, please execute: \n\nfaraday-manage migrate'
                .format(WHITE=Fore.WHITE, RED=Fore.RED))
            sys.exit(1)
Exemple #3
0
    def run(self):
        print self.WEB_UI_LOCAL_PATH

        def signal_handler(*args):
            logger.info('Received SIGTERM, shutting down.')
            logger.info("Stopping threads, please wait...")
            # teardown()
            self.raw_report_processor.stop()

        site = twisted.web.server.Site(self.__root_resource)
        site.displayTracebacks = False
        if self.__ssl_enabled:
            ssl_context = self.__load_ssl_certs()
            self.__listen_func = functools.partial(reactor.listenSSL,
                                                   contextFactory=ssl_context)
        else:
            self.__listen_func = reactor.listenTCP

        try:
            self.install_signal()
            # start threads and processes
            self.raw_report_processor = RawReportProcessor()
            self.raw_report_processor.start()
            # web and static content
            self.__listen_func(self.__listen_port,
                               site,
                               interface=self.__bind_address)
            # websockets
            try:
                listenWS(self.__build_websockets_resource(),
                         interface=self.__bind_address)
            except:
                logger.warn(
                    'Could not start websockets, address already open. This is ok is you wan to run multiple instances.'
                )
            logger.info('Faraday Server is ready')
            reactor.addSystemEventTrigger('before', 'shutdown', signal_handler)
            reactor.run()

        except error.CannotListenError as e:
            logger.error(str(e))
            sys.exit(1)
        except Exception as e:
            logger.error(
                'Something went wrong when trying to setup the Web UI')
            logger.exception(e)
            sys.exit(1)
Exemple #4
0
    def run(self):
        def signal_handler(*args):
            logger.info('Received SIGTERM, shutting down.')
            logger.info("Stopping threads, please wait...")
            self.__stop_all_threads()

        log_path = CONST_FARADAY_HOME_PATH / 'logs' / 'access-logging.log'
        site = twisted.web.server.Site(self.__root_resource,
                                       logPath=log_path,
                                       logFormatter=proxiedLogFormatter)
        site.displayTracebacks = False
        if self.__ssl_enabled:
            ssl_context = self.__load_ssl_certs()
            self.__listen_func = functools.partial(reactor.listenSSL,
                                                   contextFactory=ssl_context)
        else:
            self.__listen_func = reactor.listenTCP

        try:
            self.install_signal()
            # start threads and processes
            self.raw_report_processor = ReportsManager(
                REPORTS_QUEUE, name="ReportsManager-Thread", daemon=True)
            self.raw_report_processor.start()
            self.ping_home_thread = PingHomeThread()
            self.ping_home_thread.start()
            # web and static content
            self.__listen_func(self.__listen_port,
                               site,
                               interface=self.__bind_address)
            # websockets
            if faraday.server.config.websocket_ssl.enabled:

                try:
                    contextFactory = ssl.DefaultOpenSSLContextFactory(
                        faraday.server.config.websocket_ssl.keyfile.strip(
                            '\''),
                        faraday.server.config.websocket_ssl.certificate.strip(
                            '\''))

                    listenWS(self.__build_websockets_resource(),
                             interface=self.__bind_address,
                             contextFactory=contextFactory)

                except SSLError as e:
                    logger.error(
                        'Could not start websockets due to a SSL Config error. Some web functionality will not be available'
                    )
                except error.CannotListenError:
                    logger.warn(
                        'Could not start websockets, address already open. This is ok is you wan to run multiple instances.'
                    )
                except Exception as ex:
                    logger.warn(f'Could not start websocket, error: {ex}')
            else:
                try:
                    listenWS(self.__build_websockets_resource(),
                             interface=self.__bind_address)
                except error.CannotListenError:
                    logger.warn(
                        'Could not start websockets, address already open. This is ok is you wan to run multiple instances.'
                    )
                except Exception as ex:
                    logger.warn(f'Could not start websocket, error: {ex}')
            logger.info('Faraday Server is ready')
            reactor.addSystemEventTrigger('before', 'shutdown', signal_handler)
            reactor.run()

        except error.CannotListenError as e:
            logger.error(e)
            self.__stop_all_threads()
            sys.exit(1)

        except Exception as e:
            logger.exception(
                'Something went wrong when trying to setup the Web UI')
            logger.exception(e)
            self.__stop_all_threads()
            sys.exit(1)
Exemple #5
0
    def run(self):
        def signal_handler(*args):
            logger.info('Received SIGTERM, shutting down.')
            logger.info("Stopping threads, please wait...")
            # teardown()
            self.raw_report_processor.stop()
            self.timer.stop()

        log_path = os.path.join(CONST_FARADAY_HOME_PATH, 'logs',
                                'access-logging.log')
        site = twisted.web.server.Site(self.__root_resource,
                                       logPath=log_path,
                                       logFormatter=proxiedLogFormatter)
        site.displayTracebacks = False
        if self.__ssl_enabled:
            ssl_context = self.__load_ssl_certs()
            self.__listen_func = functools.partial(reactor.listenSSL,
                                                   contextFactory=ssl_context)
        else:
            self.__listen_func = reactor.listenTCP

        try:
            self.install_signal()
            # start threads and processes
            self.raw_report_processor = RawReportProcessor()
            self.raw_report_processor.start()
            self.timer = TimerClass()
            self.timer.start()
            # web and static content
            self.__listen_func(self.__listen_port,
                               site,
                               interface=self.__bind_address)
            # websockets
            if faraday.server.config.websocket_ssl.enabled:
                contextFactory = ssl.DefaultOpenSSLContextFactory(
                    faraday.server.config.websocket_ssl.keyfile.strip('\''),
                    faraday.server.config.websocket_ssl.certificate.strip(
                        '\''))
                try:
                    listenWS(self.__build_websockets_resource(),
                             interface=self.__bind_address,
                             contextFactory=contextFactory)
                except error.CannotListenError:
                    logger.warn(
                        'Could not start websockets, address already open. This is ok is you wan to run multiple instances.'
                    )
                except Exception as ex:
                    logger.warn(
                        'Could not start websocket, error: {}'.format(ex))
            else:
                try:
                    listenWS(self.__build_websockets_resource(),
                             interface=self.__bind_address)
                except error.CannotListenError:
                    logger.warn(
                        'Could not start websockets, address already open. This is ok is you wan to run multiple instances.'
                    )
                except Exception as ex:
                    logger.warn(
                        'Could not start websocket, error: {}'.format(ex))
            logger.info('Faraday Server is ready')
            reactor.addSystemEventTrigger('before', 'shutdown', signal_handler)
            reactor.run()

        except error.CannotListenError as e:
            logger.error(str(e))
            sys.exit(1)
        except Exception as e:
            logger.error(
                'Something went wrong when trying to setup the Web UI')
            logger.exception(e)
            sys.exit(1)
Exemple #6
0
    def _get_order_field(self, **kwargs):
        try:
            order_field = flask.request.args[self.sort_field_paremeter_name]
        except KeyError:
            # Sort field not specified, return the default
            return self.order_field
        # Check that the field is in the schema to prevent unwanted fields
        # value leaking
        schema = self._get_schema_instance(kwargs)

        # Add metadata nested field
        try:
            metadata_field = schema.fields.pop('metadata')
        except KeyError:
            pass
        else:
            for (key, value) in metadata_field.target_schema.fields.items():
                schema.fields['metadata.' + key] = value
                schema.fields[key] = value

        try:
            field_instance = schema.fields[order_field]
        except KeyError:
            if self.sort_pass_silently:
                logger.warn("Unknown field: %s" % order_field)
                return self.order_field
            raise InvalidUsage("Unknown field: %s" % order_field)

        # Translate from the field name in the schema to the database field
        # name
        order_field = field_instance.attribute or order_field

        # TODO migration: improve this checking or use a whitelist.
        # Handle PrimaryKeyRelatedField
        model_class = self.sort_model_class or self.model_class
        if order_field not in inspect(model_class).attrs:
            if self.sort_pass_silently:
                logger.warn("Field not in the DB: %s" % order_field)
                return self.order_field
            # It could be something like fields.Method
            raise InvalidUsage("Field not in the DB: %s" % order_field)

        if hasattr(model_class, order_field + '_id'):
            # Ugly hack to allow sorting by a parent
            field = getattr(model_class, order_field + '_id')
        else:
            field = getattr(model_class, order_field)
        sort_dir = flask.request.args.get(self.sort_direction_paremeter_name,
                                          self.default_sort_direction)
        if sort_dir not in ('asc', 'desc'):
            if self.sort_pass_silently:
                logger.warn("Invalid value for sorting direction: %s" %
                            sort_dir)
                return self.order_field
            raise InvalidUsage("Invalid value for sorting direction: %s" %
                               sort_dir)
        try:
            return getattr(field, sort_dir)()
        except NotImplementedError:
            if self.sort_pass_silently:
                logger.warn("field {} doesn't support sorting".format(
                    order_field
                ))
                return self.order_field
            # There are some fields that can't be used for sorting
            raise InvalidUsage("field {} doesn't support sorting".format(
                order_field
            ))