Exemplo n.º 1
0
def run_scrAPI():
    resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), app)
    site = server.Site(resource)
    http_server = endpoints.TCP4ServerEndpoint(reactor, 5005)
    http_server.listen(site)
    reactor.run()
    return reactor
Exemplo n.º 2
0
 def _run():
     reactor.suggestThreadPoolSize(FLAGS.threadpool_size)
     resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), app)
     site = server.Site(resource)
     endpoint = endpoints.serverFromString(reactor, address)
     endpoint.listen(site).addErrback(err_shutdown)
     reactor.run(installSignalHandlers=int(not debug))
Exemplo n.º 3
0
 def makeService(self, options):
     pool = threadpool.ThreadPool(minthreads=1, maxthreads=100)
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     root = wsgi.WSGIResource(reactor, pool, wsgi_hello.application)
     site = server.Site(root)
     return strports.service('tcp:8000', site)
Exemplo n.º 4
0
def wsgi_resource():
    pool = threadpool.ThreadPool()
    pool.start()
    # Allow Ctrl-C to get you out cleanly:
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    wsgi_resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())
    return wsgi_resource
Exemplo n.º 5
0
def wsgi_resource():
    pool = threadpool.ThreadPool()
    pool.start()
    # Allow Ctrl-C to get you out cleanly:
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    handler = StaticFilesHandler(get_internal_wsgi_application())
    wsgi_resource = wsgi.WSGIResource(reactor, pool, handler)
    return wsgi_resource
Exemplo n.º 6
0
def getRoot(pool):
    application = blog.app.app
    wsgi_resource = wsgi.WSGIResource(reactor, pool, application)
    root = DelegatingResource(wsgi_resource)
    static_resource = static.File('index.html')
    root.putChild(b'', static_resource)
    static_resource = static.File('static')
    root.putChild(b'static', static_resource)
    return root
 def makeService(self, options):
     s = service.MultiService()
     pool = threadpool.ThreadPool()
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     root = wsgi.WSGIResource(reactor, pool, wsgi_param.application)
     site = server.Site(root)
     strports.service('tcp:8000', site).setServiceParent(s)
     ts = internet.TimerService(1, update, wsgi_param.application, reactor)
     ts.setServiceParent(s)
     return s
Exemplo n.º 8
0
def wsgi_resource(multi):

    # make a new ThreadPoolService and add it to the multi service
    tps = ThreadPoolService(threadpool.ThreadPool())
    tps.setServiceParent(multi)

    # Allow Ctrl-C to get you out cleanly:
    reactor.addSystemEventTrigger('after', 'shutdown', tps.pool.stop)

    wsgi_resource = wsgi.WSGIResource(reactor, tps.pool, WSGIHandler())
    return wsgi_resource
Exemplo n.º 9
0
    def __init__(self):
        resource.Resource.__init__(self)

        pool = threadpool.ThreadPool()
        pool.start()
        # Allow Ctrl-C to get you out cleanly:
        reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
        self.wsgi_resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())

        staticsrc = static.File(blog.settings.STATIC_ROOT, blog.settings.STATIC_URL)
        self.putChild('static', staticsrc)
Exemplo n.º 10
0
 def makeService(self, options):
     s = service.MultiService()
     pool = threadpool.ThreadPool()
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     root = wsgi.WSGIResource(reactor, pool, wsgi_param.application)
     site = server.Site(root)
     strports.service('tcp:8000', site).setServiceParent(s)
     factory = protocol.Factory.forProtocol(UpdateMessage)
     factory.application = wsgi_param.application
     strports.service('tcp:8001', factory).setServiceParent(s)
     return s
Exemplo n.º 11
0
 def makeService(self, options):
     application = wsgi_hello.application
     pool = threadpool.ThreadPool(minthreads=1, maxthreads=100)
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     dynamic = wsgi.WSGIResource(reactor, pool, application)
     files = static.File('static')
     root = vhost.NameVirtualHost()
     root.addHost(b'app.example.org', dynamic)
     root.addHost(b'static.example.org', files)
     site = server.Site(root)
     return strports.service(options['port'], site)
Exemplo n.º 12
0
 def _run():
     log.msg("logging run")
     # this is set in an oscar flag in pyg
     reactor.suggestThreadPoolSize(
         30
     )  # this is the default in pyg.  Maybe we use something different.
     resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), app)
     site = server.Site(resource)
     endpoint = endpoints.serverFromString(reactor, address)
     endpoint.listen(site).addErrback(err_shutdown)
     # this suggests that debug is boolean, perhaps
     reactor.run(installSignalHandlers=int(not debug))
Exemplo n.º 13
0
 def opt_wsgi(self, name):
     """
     The FQPN of a WSGI application object to serve as the root resource of
     the webserver.
     """
     pool = threadpool.ThreadPool()
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     try:
         application = reflect.namedAny(name)
     except (AttributeError, ValueError):
         raise usage.UsageError("No such WSGI application: %r" % (name,))
     self['root'] = wsgi.WSGIResource(reactor, pool, application)
Exemplo n.º 14
0
 def makeService(self, options):
     application = pyramid_dynamic.application
     pool = threadpool.ThreadPool(minthreads=1, maxthreads=100)
     reactor.callWhenRunning(pool.start)
     reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
     root = wsgi.WSGIResource(reactor, pool, application)
     site = server.Site(root)
     control = protocol.Factory()
     control.protocol = lambda: amp.AMP(locator=AppConfiguration())
     ret = service.MultiService()
     strports.service('tcp:8000', site).setServiceParent(ret)
     strports.service('tcp:8001', control).setServiceParent(ret)
     return ret
Exemplo n.º 15
0
def twisted_adapter(host, port):
    from twisted.web import server, wsgi
    from twisted.python.threadpool import ThreadPool
    from twisted.internet import reactor

    thread_pool = ThreadPool()
    thread_pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)

    ittyResource = wsgi.WSGIResource(reactor, thread_pool, handle_request)
    site = server.Site(ittyResource)
    reactor.listenTCP(port, site)
    reactor.run()
Exemplo n.º 16
0
    def _build_api_service(self):
        """
        Once called, the resource is initialized. Any calls to self._add_resource() should be done before calling this fn.
        :return:
        """

        wsgi_app = self.anchore_service.get_api_application()
        wsgi_site = wsgi.WSGIResource(reactor, reactor.getThreadPool(), application=wsgi_app)

        self._add_resource(self.anchore_service.__service_api_version__.encode('utf-8'), wsgi_site)
        self.root_resource = web.resource.Resource()

        # Add nodes
        for name, resource in self.resource_nodes.items():
            self.root_resource.putChild(name, resource)

        # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask
        self._api_version_bytes = self.anchore_service.__service_api_version__.encode('utf-8') # This is optimization

        # Handle the auth vs non-auth child resources to not consume a path element
        root = rewrite.RewriterResource(self.root_resource, self._default_version_rewrite)

        # Build the main site server
        site = server.Site(root)
        listen = self.anchore_service.configuration['listen']


        if str(self.anchore_service.configuration.get('ssl_enable', '')).lower() == 'true':
            try:
                ssl_data = {
                    'ssl_cert': _load_ssl_cert(self.anchore_service.configuration['ssl_cert']) if 'ssl_cert' in self.anchore_service.configuration else None,
                    'ssl_chain': _load_ssl_cert(self.anchore_service.configuration['ssl_chain']) if 'ssl_chain' in self.anchore_service.configuration else None,
                    'ssl_key': _load_ssl_key(self.anchore_service.configuration['ssl_key']) if 'ssl_key' in self.anchore_service.configuration else None
                }

                if ssl_data['ssl_chain']:
                    sfact = ssl.CertificateOptions(privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert'],
                                                   extraCertChain=[ssl_data['ssl_chain']])
                else:
                    sfact = ssl.CertificateOptions(privateKey=ssl_data['ssl_key'], certificate=ssl_data['ssl_cert'])

                endpoint = SSL4ServerEndpoint(reactor=reactor, port=int(self.anchore_service.configuration['port']), sslContextFactory=sfact, interface=listen)
            except Exception as err:
                raise err
        else:
            endpoint = TCP4ServerEndpoint(reactor=reactor, port=int(self.anchore_service.configuration['port']), interface=listen)

        ret_svc = StreamServerEndpointService(endpoint=endpoint, factory=site)
        ret_svc.setName(self.anchore_service.name)
        
        return ret_svc
Exemplo n.º 17
0
def getfactory(reactor):
    # Create a WSGI callable from our application
    app = cherrypy.Application(Node(LIBVIRT_URIS), "", APPCONFIG)

    # Twisted needs a threadpool to run this in
    threads = threadpool.ThreadPool(minthreads=1, maxthreads=1, name='virtweb')
    threads.start()
    # Shut it down, too!
    reactor.addSystemEventTrigger('after', 'shutdown', threads.stop)

    # Setup the twisted.web factory
    resource = wsgi.WSGIResource(reactor, threads, app)
    factory = server.Site(resource, 'server.log')
    return factory
Exemplo n.º 18
0
def twisted_adapter(host, port, application):
    from twisted.application import service, strports
    from twisted.web import server, http, wsgi
    from twisted.python.threadpool import ThreadPool
    from twisted.internet import reactor

    thread_pool = ThreadPool()
    thread_pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', thread_pool.stop)

    resource = wsgi.WSGIResource(reactor, thread_pool, application)
    site = server.Site(resource)
    reactor.listenTCP(port, site)
    reactor.run()
Exemplo n.º 19
0
 def do_run(self):
     resource = wsgi.WSGIResource(internet.reactor,
                                  internet.reactor.getThreadPool(),
                                  WSGIHandler())
     if settings.USE_SSL:
         internet.reactor.listenSSL(settings.BLOB_PORT,
                                    server.Site(resource),
                                    ChainedOpenSSLContextFactory(
                                        settings.SSL_CERTIFICATE,
                                        settings.SSL_CERTIFICATE),
                                    interface=settings.BLOB_HOST)
     else:
         internet.reactor.listenTCP(settings.BLOB_PORT,
                                    server.Site(resource),
                                    interface=settings.BLOB_HOST)
     internet.reactor.run()
Exemplo n.º 20
0
def makeService(cliconfig):
    application = service.Application('TSX API')

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    observer = log.PythonLoggingObserver("tsxapi")
    observer.start()
    # config = read_config(cliconfig) # JW - disabling this for now
    # setup python logging

    log_file = tsx.config.get("api", "log_file", default="/var/log/tsxapi.log")

    from logging.handlers import TimedRotatingFileHandler
    fh = TimedRotatingFileHandler(log_file, when='midnight', backupCount=7)
    fh.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger.addHandler(fh)

    # read config file. this could do with some prettifying to give more
    # verbose feedback if a key isn't found.
    port = int(tsx.config.get("api", "port", default=8080))
    logger.info("Using Port %d" % port)

    from tsx.api.api import app  # TODO - make package naming less awkward

    # This bit is a workaround around a bug in Twisted.
    # If the threadpool is initialised before Twisted is daemonised, the
    # whole thing goes up in flames (sort of).
    # We avoid this by putting the multi-service in between.
    multi = service.MultiService()
    pool = threadpool.ThreadPool()
    tps = ThreadPoolService(pool)
    tps.setServiceParent(multi)
    resource = wsgi.WSGIResource(reactor, tps.pool, app)

    # Serve it up:
    main_site = server.Site(resource)
    internet.TCPServer(port, main_site).setServiceParent(multi)
    multi.setServiceParent(application)
    return multi
Exemplo n.º 21
0
 def makeService(self, options):
     s = service.MultiService()
     for name, port in port_assignments.items():
         application = reflect.namedAny(name)
         pool = threadpool.ThreadPool()
         reactor.callWhenRunning(pool.start)
         reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
         root = wsgi.WSGIResource(reactor, pool, application)
         site = server.Site(root)
         strports.service(port, site).setServiceParent(s)
     root = static.File('data')
     site = server.Site(root)
     strports.service('tcp:8085', site).setServiceParent(s)
     fle = filepath.FilePath('data/time.txt')
     ts = internet.TimerService(1, update, fle, reactor)
     ts.setServiceParent(s)
     return s
Exemplo n.º 22
0
def webResourceFactory(staticfiles, datafiles=None):
    """This factory function creates an instance of the front end web
    resource tree containing both the django wsgi and the async
    notebook resources.
    """

    class Root(resource.Resource):

        def __init__(self, wsgi_resource):
            resource.Resource.__init__(self)
            self.wsgi_resource = wsgi_resource

        def getChild(self, path, request):
            path0 = request.prepath.pop(0)
            request.postpath.insert(0, path0)
            return self.wsgi_resource


    # The kernel server does not require django, so this step is not
    # required for every import of service.py (this file)
    from django.core.handlers.wsgi import WSGIHandler
    from twisted.python import threadpool

    pool = threadpool.ThreadPool()
    reactor.callWhenRunning(pool.start)
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)

    django_wsgi_resource = wsgi.WSGIResource(reactor, pool, WSGIHandler())
    resource_root = Root(django_wsgi_resource)

    static_resource = static.File(staticfiles)


    backend_bus = backend.BackendBus()

    resource_root.putChild("asyncnotebook", backend.EngineBusAdapter(backend_bus))
    resource_root.putChild("static", static_resource)
    
    if datafiles: 
        data_resource = static.File(datafiles)
        resource_root.putChild("data", data_resource)
    
    return resource_root
Exemplo n.º 23
0
    def run_twisted_wsgi():
        from sys import stdout
        from twisted.logger import globalLogBeginner, textFileLogObserver
        from twisted.web import server, wsgi
        from twisted.internet import endpoints, reactor

        # start the logger
        globalLogBeginner.beginLoggingTo([textFileLogObserver(stdout)])

        # logger = logging.getLogger(__name__)
        logging.basicConfig(level=logging.INFO)

        # start the WSGI server
        root_resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(),
                                          app)
        factory = server.Site(root_resource)
        http_server = endpoints.TCP4ServerEndpoint(reactor, 5000)
        http_server.listen(factory)

        # start event loop
        reactor.run(**reactor_args)
Exemplo n.º 24
0
    def build_service(reactor):
        multi = MultiService()
        StreamServerEndpointService(
            TCP4ServerEndpoint(reactor, port),
            server.Site(
                wsgi.WSGIResource(reactor, reactor.getThreadPool(),
                                  app), )).setServiceParent(multi)

        logger = Logger()
        TimerService(
            # Run every 10 minutes
            10 * 60,
            lambda: deferToThread(check_for_revocation, cert_db, crtsh_checker)
            .addErrback(lambda f: logger.failure(
                "Error checking for revocation", f))).setServiceParent(multi)

        TimerService(
            60 * 60,
            lambda: deferToThread(raw_app._update_lint_summaries).addErrback(
                lambda f: logger.failure("Error updating cablint summaries", f
                                         ))).setServiceParent(multi)
        return multi
Exemplo n.º 25
0
def makeService(config):
    from django.core.handlers.wsgi import WSGIHandler
    os.environ['DJANGO_SETTINGS_MODULE'] = 'lisa.server.web.weblisa.settings'

    if config['configuration']:
        ConfigManagerSingleton.get().setConfiguration(config['configuration'])

    configuration = ConfigManagerSingleton.get().getConfiguration()
    dir_path = ConfigManagerSingleton.get().getPath()

    from lisa.server import libs

    # Creating MultiService
    multi = service.MultiService()
    pool = threadpool.ThreadPool()
    tps = ThreadPoolService(pool)
    tps.setServiceParent(multi)

    # Creating the web stuff
    resource_wsgi = wsgi.WSGIResource(reactor, tps.pool, WSGIHandler())
    root = static.File('/'.join([dir_path, 'web/frontend/build']))
    backendsrc = libs.Root(resource_wsgi)
    root.putChild("backend", backendsrc)
    staticrsrc = static.File('/'.join([dir_path, 'web/interface/static']))
    root.putChild("static", staticrsrc)

    socketfactory = SockJSFactory(Factory.forProtocol(libs.WebSocketProtocol))
    root.putChild("websocket", socketfactory)

    # Configuring servers to launch
    if configuration['enable_secure_mode'] or configuration[
            'enable_unsecure_mode']:
        if configuration['enable_secure_mode']:
            SSLContextFactoryEngine = ssl.DefaultOpenSSLContextFactory(
                os.path.normpath(dir_path + '/' +
                                 'configuration/ssl/server.key'),
                os.path.normpath(dir_path + '/' +
                                 'configuration/ssl/server.crt'))
            SSLContextFactoryWeb = ssl.DefaultOpenSSLContextFactory(
                os.path.normpath(dir_path + '/' +
                                 'configuration/ssl/server.key'),
                os.path.normpath(dir_path + '/' +
                                 'configuration/ssl/server.crt'))
            ctx = SSLContextFactoryEngine.getContext()
            ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                           libs.verifyCallback)
            # Since we have self-signed certs we have to explicitly
            # tell the server to trust them.
            with open(
                    os.path.normpath(dir_path + '/' +
                                     'configuration/ssl/server.pem'),
                    'w') as outfile:
                for file in os.listdir(
                        os.path.normpath(dir_path + '/' +
                                         'configuration/ssl/public/')):
                    with open(
                            os.path.normpath(dir_path + '/' +
                                             'configuration/ssl/public/' +
                                             file)) as infile:
                        for line in infile:
                            outfile.write(line)

            ctx.load_verify_locations(
                os.path.normpath(dir_path + '/' +
                                 'configuration/ssl/server.pem'))
            internet.SSLServer(configuration['lisa_web_port_ssl'],
                               server.Site(root),
                               SSLContextFactoryWeb).setServiceParent(multi)
            internet.SSLServer(configuration['lisa_engine_port_ssl'],
                               libs.LisaFactorySingleton.get(),
                               SSLContextFactoryEngine).setServiceParent(multi)
        if configuration['enable_unsecure_mode']:
            # Serve it up:
            internet.TCPServer(configuration['lisa_web_port'],
                               server.Site(root)).setServiceParent(multi)
            internet.TCPServer(
                configuration['lisa_engine_port'],
                libs.LisaFactorySingleton.get()).setServiceParent(multi)

        configuration['enable_cloud_mode'] = True
        #if configuration['enable_cloud_mode']:
        #    portforward.ProxyClient.dataReceived = client_dataReceived
        #    portforward.ProxyServer.dataReceived = server_dataReceived
        #    reactor.listenTCP(1080, portforward.ProxyFactory('localhost', 8000))

    else:
        exit(1)
    libs.scheduler.setServiceParent(multi)
    multi.setServiceParent(application)
    libs.Initialize()
    return multi
Exemplo n.º 26
0
def init():
    global _WSGIListener
    global _WSGIPort
    result = Deferred()
    if _Debug:
        lg.out(_DebugLevel, 'control.init')
    request_update()
    if _WSGIListener:
        if _Debug:
            lg.out(_DebugLevel, '    SKIP listener already exist')
        result.callback(0)
        return result

    try:
        import django


#         ver = django.get_version()
#         if not ver.startswith('1.7'):
#             if _Debug:
#                 lg.out(_DebugLevel, '    Django version must be 1.7, skip!')
#             result.callback(0)
#             return result
    except:
        lg.exc()
        result.callback(0)
        return result

    if _Debug:
        lg.out(_DebugLevel + 6, '    \n' + pprint.pformat(sys.path))

    if _Debug:
        lg.out(
            _DebugLevel,
            '    setting environment DJANGO_SETTINGS_MODULE=web.asite.settings'
        )
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.asite.settings")

    from django.core.wsgi import get_wsgi_application
    from django.conf import settings as django_settings
    from django.core import management
    # from django.contrib.auth.management.commands import changepassword

    if _Debug:
        lg.out(_DebugLevel,
               '    configuring WSGI bridge from Twisted to Django')
    wsgi_handler = get_wsgi_application()
    my_wsgi_handler = MyFakedWSGIHandler(wsgi_handler)
    pool = threadpool.ThreadPool()
    pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    resource = wsgi.WSGIResource(reactor, pool, my_wsgi_handler)
    root = DjangoRootResource(resource)
    root_static_dir = os.path.join(bpio.getExecutableDir(), "web")
    for sub in os.listdir(root_static_dir):
        static_path = os.path.join(root_static_dir, sub, 'static')
        if not os.path.isdir(static_path):
            continue
        node = static.File(static_path)
        root.putChild(sub, node)
        if _Debug:
            lg.out(_DebugLevel,
                   '        added static dir: %s->%s' % (sub, static_path))
        if sub == 'asite':
            admin_path = os.path.join(root_static_dir, sub, 'admin', 'static')
            node.putChild('admin', static.File(admin_path))
            if _Debug:
                lg.out(
                    _DebugLevel,
                    '        added ADMIN static dir: admin->%s' % admin_path)
    site = server.Site(root)
    _WSGIPort = 8080  # TODO: read port num from settings
    if _Debug:
        lg.out(_DebugLevel, '        %s' % my_wsgi_handler)
        lg.out(_DebugLevel, '        %s' % resource)
        lg.out(_DebugLevel, '        %s' % site)

    verbosity = 0
    if lg.is_debug(18):
        verbosity = 3
    if lg.is_debug(12):
        verbosity = 2
    if lg.is_debug(8):
        verbosity = 1

    # lg.out(4, '    running django "flush" command')
    # management.call_command('flush', interactive=False, verbosity=verbosity)

    # lg.out(4, '    running django "createsuperuser" command')
    # management.call_command('createsuperuser',
    #     interactive=False, verbosity=verbosity,
    #     username="******", email="admin@localhost")
    # command = changepassword.Command()
    # command._get_pass = lambda *args: 'admin'
    # command.execute("admin")

    if _Debug:
        lg.out(_DebugLevel, '    running django "syncdb" command')
    management.call_command('syncdb',
                            stdout=open(
                                os.path.join(settings.LogsDir(),
                                             'django-syncdb.log'), 'w'),
                            interactive=False,
                            verbosity=verbosity)

    _ShortPoolPort = 8081  # TODO: read port num from settings
    # shortpool.init(get_update_items, set_updated, _ShortPoolPort)

    if _Debug:
        lg.out(_DebugLevel, '    starting listener: %s' % site)
    result = start_listener(site)
    result.addCallback(lambda portnum: post_init(portnum))

    return result
Exemplo n.º 27
0
 def init_protocol(self):
     self.web_factory = server.Site(wsgi.WSGIResource(reactor, reactor.getThreadPool(), self.app))
Exemplo n.º 28
0
    return 'Scrape Still Progress'


def finished_scrape(null):
    """
    A callback that is fired after the scrape has completed.
    Set a flag to allow display the results from /results
    """
    global scrape_complete
    scrape_complete = True


if __name__ == '__main__':
    from sys import stdout

    from twisted.logger import globalLogBeginner, textFileLogObserver
    from twisted.web import server, wsgi
    from twisted.internet import endpoints, reactor

    # start the logger
    globalLogBeginner.beginLoggingTo([textFileLogObserver(stdout)])

    # start the WSGI server
    root_resource = wsgi.WSGIResource(reactor, reactor.getThreadPool(), app)
    factory = server.Site(root_resource)
    http_server = endpoints.TCP4ServerEndpoint(reactor, 9000)
    http_server.listen(factory)

    # start event loop
    reactor.run()
Exemplo n.º 29
0
    def _build_api_service(self):
        """
        Once called, the resource is initialized. Any calls to self._add_resource() should be done before calling this fn.
        :return:
        """

        thread_count = int(
            self.service_config.get('max_request_threads',
                                    localconfig.DEFAULT_SERVICE_THREAD_COUNT))

        wsgi_app = self.anchore_service.get_api_application()
        wsgi_site = wsgi.WSGIResource(reactor,
                                      reactor.getThreadPool(),
                                      application=wsgi_app)
        reactor.getThreadPool().adjustPoolsize(maxthreads=thread_count)
        logger.debug('Thread pool size stats. Min={}, Max={}'.format(
            reactor.getThreadPool().min,
            reactor.getThreadPool().max))

        self._add_resource(
            self.anchore_service.__service_api_version__.encode('utf-8'),
            wsgi_site)

        if enable_thread_dumper:
            logger.warn(
                "Adding thread dump route for debugging since debug flag is set. This is dangerous and should not be done in normal production"
            )
            self._add_resource(b'threads', ThreadDumperResource())

        self.root_resource = web.resource.Resource()

        # Add nodes
        for name, resource in self.resource_nodes.items():
            self.root_resource.putChild(name, resource)

        # this will rewrite any calls that do not have an explicit version to the base path before being processed by flask
        self._api_version_bytes = self.anchore_service.__service_api_version__.encode(
            'utf-8')  # This is optimization

        # Handle the auth vs non-auth child resources to not consume a path element
        root = rewrite.RewriterResource(self.root_resource,
                                        self._default_version_rewrite)

        # Build the main site server
        site = server.Site(root)
        listen = self.anchore_service.configuration['listen']

        # Disable the twisted access logging by overriding the log function as it uses a raw 'write' and cannot otherwise be disabled, iff enable_access_logging is set to False in either the service or global config
        try:
            eal = True
            if "enable_access_logging" in self.anchore_service.configuration:
                eal = self.anchore_service.configuration.get(
                    "enable_access_logging", True)
            elif "enable_access_logging" in self.configuration:
                eal = self.configuration.get("enable_access_logging", True)

            if not eal:

                def _null_logger(request):
                    pass

                site.log = _null_logger

        except:
            pass

        if str(self.anchore_service.configuration.get('ssl_enable',
                                                      '')).lower() == 'true':
            try:
                ssl_data = {
                    'ssl_cert':
                    _load_ssl_cert(
                        self.anchore_service.configuration['ssl_cert']) if
                    'ssl_cert' in self.anchore_service.configuration else None,
                    'ssl_chain':
                    _load_ssl_cert(
                        self.anchore_service.configuration['ssl_chain'])
                    if 'ssl_chain' in self.anchore_service.configuration else
                    None,
                    'ssl_key':
                    _load_ssl_key(
                        self.anchore_service.configuration['ssl_key']) if
                    'ssl_key' in self.anchore_service.configuration else None
                }

                if ssl_data['ssl_chain']:
                    sfact = ssl.CertificateOptions(
                        privateKey=ssl_data['ssl_key'],
                        certificate=ssl_data['ssl_cert'],
                        extraCertChain=[ssl_data['ssl_chain']])
                else:
                    sfact = ssl.CertificateOptions(
                        privateKey=ssl_data['ssl_key'],
                        certificate=ssl_data['ssl_cert'])

                endpoint = SSL4ServerEndpoint(
                    reactor=reactor,
                    port=int(self.anchore_service.configuration['port']),
                    sslContextFactory=sfact,
                    interface=listen)
            except Exception as err:
                raise err
        else:
            endpoint = TCP4ServerEndpoint(
                reactor=reactor,
                port=int(self.anchore_service.configuration['port']),
                interface=listen)

        ret_svc = StreamServerEndpointService(endpoint=endpoint, factory=site)
        ret_svc.setName(self.anchore_service.name)

        return ret_svc
Exemplo n.º 30
0
def makeService(config):
    from django.core.handlers.wsgi import WSGIHandler
    os.environ['DJANGO_SETTINGS_MODULE'] = 'lisa.server.web.weblisa.settings'

    # Get configuration
    if config['configuration']:
        if ConfigManager.setConfiguration(config['configuration']) == False:
            log.err("Error : configuration file invalid")
            return

    configuration = ConfigManager.getConfiguration()
    dir_path = configuration['path']

    from lisa.server import libs

    # Multiservice mode
    multi = service.MultiService()
    multi.setServiceParent(application)
    pool = threadpool.ThreadPool()
    tps = ThreadPoolService(pool)
    tps.setServiceParent(multi)
    libs.scheduler.setServiceParent(multi)

    # Creating the web stuff
    resource_wsgi = wsgi.WSGIResource(reactor, tps.pool, WSGIHandler())
    root = libs.Root(resource_wsgi)
    staticrsrc = static.File('/'.join([dir_path, 'web/interface/static']))
    root.putChild("static", staticrsrc)

    # Start client protocol factory
    if configuration['enable_secure_mode']:
        # Create a SSL context factory for clients
        clientAuthContextFactory = ssl.DefaultOpenSSLContextFactory(
            configuration['lisa_ssl_key'], configuration['lisa_ssl_crt'])

        # Add client authentification to SSL context
        ctx = clientAuthContextFactory.getContext()
        ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                       ClientAuthVerifyCallback)

        # Load client certificates authorized to connect
        cert_path = dir_path + '/' + 'configuration/ssl/public/'
        outfile = os.path.normpath(dir_path + '/' +
                                   'configuration/ssl/public/server.pem')
        with open(outfile, "w") as f:
            wildcard = os.path.normpath("{path}*.crt".format(path=cert_path))
            for infile in glob.glob(wildcard):
                call(['openssl', 'x509', '-in', infile, '-text'], stdout=f)
        ctx.load_verify_locations(outfile)

        # Initialize client factory
        engineService = internet.SSLServer(configuration['lisa_port'],
                                           libs.ClientFactory.get(),
                                           clientAuthContextFactory)

        # Create a SSL context factory for web interface
        webAuthContextFactory = ssl.DefaultOpenSSLContextFactory(
            configuration['lisa_ssl_key'], configuration['lisa_ssl_crt'])

        # Initialize web factory
        webService = internet.SSLServer(configuration['lisa_web_port'],
                                        server.Site(root),
                                        webAuthContextFactory)
    else:
        # Initialize factories
        engineService = internet.TCPServer(configuration['lisa_port'],
                                           libs.ClientFactory.get())
        webService = internet.TCPServer(configuration['lisa_web_port'],
                                        server.Site(root))

    # Create the websocket factory
    if configuration['enable_secure_mode']:
        socketfactory = WebSocketServerFactory(
            "wss://" + configuration['lisa_url'] + ":" +
            str(configuration['lisa_web_port']),
            debug=False)

    else:
        # Initialize factories
        socketfactory = WebSocketServerFactory(
            "ws://" + configuration['lisa_url'] + ":" +
            str(configuration['lisa_web_port']),
            debug=False)
    socketfactory.protocol = libs.WebSocketProtocol
    socketresource = WebSocketResource(socketfactory)
    root.putChild("websocket", socketresource)

    # Add services to application
    engineService.setServiceParent(multi)
    webService.setServiceParent(multi)

    return multi