Example #1
0
 def setup_class(cls):
     super(TestLocator, cls).setup_class()
     sources = platform.get_location_sources()
     if not sources:
         raise SkipTest('No location sources avaialble')
     cls.locator = Locator()
     cls.locator.add_source(sources[0]())
     cls.crypto = CryptoProvider()
Example #2
0
    def run(self):
        """Initialize the backend and run its main loop."""
        self.logger.debug('initializing backend components')

        self.logger.debug('initializing cryto provider')
        crypto = singleton(CryptoProvider)
        pwgen = singleton(PasswordGenerator)

        self.logger.debug('initializing database')
        fname = os.path.join(self.data_dir, 'bluepass.db')
        database = singleton(Database, fname)
        database.lock()

        self.logger.debug('initializing model')
        model = singleton(Model, database)

        self.logger.debug('initializing locator')
        locator = singleton(Locator)
        for ls in platform.get_location_sources():
            self.logger.debug('adding location source: {}'.format(ls.name))
            locator.add_source(ls())

        self.logger.debug('initializing sync API')
        listener = util.create_listener(('0.0.0.0', 0))
        listener.setblocking(False)
        app = singleton(SyncAPIApplication)
        syncapi = singleton(SyncAPIServer, listener, app)
        syncapi.start()

        self.logger.debug('initializing sync API publisher')
        publisher = singleton(SyncAPIPublisher, syncapi)
        publisher.start()

        # The syncer is started at +10 seconds so that the locator will have
        # hopefully located all neighbors by then and we can do a once-off
        # sync at startup. This is just a heuristic for optimization, the
        # correctness of our sync algorithm does not depend on this.
        if locator.sources:
            self.logger.debug('initializing background sync worker')
            syncer = singleton(Syncer)
            syncer.start_later(10)
        else:
            self.logger.warning('no location sources available')
            self.logger.warning('network synchronization is disabled')

        self.logger.debug('initializing control API')
        listener = util.create_listener(self.listen_address)
        listener.setblocking(False)
        fname = os.path.join(self.data_dir, 'backend.addr')
        addr = util.unparse_address(listener.getsockname())
        with open(fname, 'w') as fout:
            fout.write('{}\n'.format(addr))
        self.logger.info('listening on: {}'.format(addr))
        handler = SocketAPIHandler()
        messagebus = singleton(MessageBusServer, listener, self.auth_token,
                               handler)
        if self.options.get('trace'):
            fname = os.path.join(self.data_dir, 'backend.trace')
            messagebus.set_trace(fname)
        if not self.options.get('daemon'):
            def messagebus_event(event, *args):
                if event == 'LastConnectionClosed':
                    self.stop()
            messagebus.add_callback(messagebus_event)
        messagebus.start()

        self.logger.debug('installing signal handlers')
        on_signal = get_hub().loop.signal(signal.SIGTERM)
        on_signal.start(self.stop)

        monkey.patch_time() # Make Thread.join() cooperative.

        self.logger.debug('all backend components succesfully initialized')

        # This is where the backend runs (until stopped).
        self._stop_event.wait()

        self.logger.debug('backend event loop terminated')

        self.logger.debug('shutting down control API')
        messagebus.stop()

        self.logger.debug('shutting down database')
        database.close()

        self.logger.debug('stopped all backend components')
Example #3
0
    def run(self):
        """Initialize the backend and run its main loop."""
        self._log.debug('initializing backend components')

        self._log.debug('initializing password generator')
        pwgen = singleton(PasswordGenerator)

        self._log.debug('initializing document store')
        fname = os.path.join(self.options.data_dir, 'bluepass.db')
        store = singleton(Store, fname)

        self._log.debug('initializing model')
        model = singleton(Model, store)
        token = {'id': self.options.auth_token, 'expires': 0,
                 'allow': {'control_api': True}}
        model.create_token(token)

        self._log.debug('initializing locator')
        locator = singleton(Locator)
        for ls in platform.get_location_sources():
            self._log.debug('adding location source: {}', ls.name)
            locator.add_source(ls())

        self._log.debug('initializing sync API')
        syncapi = singleton(SyncApiServer)
        syncapi.listen(('0.0.0.0', 0))

        self._log.debug('initializing sync API publisher')
        publisher = singleton(SyncApiPublisher, syncapi)
        publisher.start()

        if locator.sources:
            self._log.debug('initializing background sync worker')
            syncer = singleton(Syncer)
            syncer.start()
        else:
            self._log.warning('no location sources available')
            self._log.warning('network synchronization is disabled')

        self._log.debug('initializing control API')
        ctrlapi = singleton(ControlApiServer)
        if self.options.trace:
            tracename = os.path.join(self.options.data_dir, 'backend.trace')
            tracefile = open(tracename, 'w')
            ctrlapi._set_tracefile(tracefile)
        addr = gruvi.paddr(self.options.listen)
        ctrlapi.listen(addr)

        fname = os.path.join(self.options.data_dir, 'backend.run')
        addr = ctrlapi.addresses[0]
        runinfo = { 'listen': gruvi.saddr(addr), 'pid': os.getpid() }
        util.write_atomic(fname, json.dumps(runinfo))

        #self._log.debug('initializing client API')
        #clientapi = singleton(ClientApiServer)
        #clientapi.listen()

        # This is where the backend runs (until stop_event is raised or CTRL-C
        # is pressed).
        self._stop_event.wait()

        self._log.debug('backend event loop terminated')

        self._log.debug('shutting down control API')
        ctrlapi.close()

        self._log.debug('shutting down document store')
        store.close()

        self._log.debug('stopped all backend components')

        return 0