Example #1
0
    def test_library_restrictions(self):  # {{{
        from calibre.srv.opts import Options
        from calibre.srv.handler import Handler
        from calibre.db.legacy import create_backend
        opts = Options(userdb=':memory:')
        Data = namedtuple('Data', 'username')
        with TemporaryDirectory() as base:
            l1, l2, l3 = map(lambda x: os.path.join(base, 'l' + x), '123')
            for l in (l1, l2, l3):
                create_backend(l).close()
            ctx = Handler((l1, l2, l3), opts).router.ctx
            um = ctx.user_manager

            def get_library(username=None, library_id=None):
                ans = ctx.get_library(Data(username), library_id=library_id)
                return os.path.basename(ans.backend.library_path)

            def library_info(username=None):
                lmap, defaultlib = ctx.library_info(Data(username))
                lmap = {k:os.path.basename(v) for k, v in iteritems(lmap)}
                return lmap, defaultlib

            self.assertEqual(get_library(), 'l1')
            self.assertEqual(library_info()[0], {'l%d'%i:'l%d'%i for i in range(1, 4)})
            self.assertEqual(library_info()[1], 'l1')
            self.assertRaises(HTTPForbidden, get_library, 'xxx')
            um.add_user('a', 'a')
            self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4)})
            um.update_user_restrictions('a', {'blocked_library_names': ['L2']})
            self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i != 2})
            um.update_user_restrictions('a', {'allowed_library_names': ['l3']})
            self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i == 3})
            self.assertEqual(library_info('a')[1], 'l3')
            self.assertRaises(HTTPForbidden, get_library, 'a', 'l1')
            self.assertRaises(HTTPForbidden, get_library, 'xxx')
Example #2
0
 def __init__(self, listen_on):
     Thread.__init__(self, name='ReloadServer')
     self.reload_handler = ReloadHandler()
     self.loop = ServerLoop(
         create_http_handler(websocket_handler=self.reload_handler),
         opts=Options(shutdown_timeout=0.1, listen_on=(listen_on or '127.0.0.1'), port=0))
     self.loop.LISTENING_MSG = None
     self.notify_reload = self.reload_handler.notify_reload
     self.ping = self.reload_handler.ping
     self.start()
Example #3
0
 def __init__(self, handler, plugins=(), **kwargs):
     Thread.__init__(self, name='ServerMain')
     from calibre.srv.opts import Options
     from calibre.srv.loop import ServerLoop
     from calibre.srv.http_response import create_http_handler
     self.setup_defaults(kwargs)
     self.loop = ServerLoop(
         create_http_handler(handler),
         opts=Options(**kwargs),
         plugins=plugins,
         log=ServerLog(level=ServerLog.DEBUG),
     )
     self.log = self.loop.log
Example #4
0
    def __init__(
        self,
        handler,
        opts=None,
        plugins=(),
        # A calibre logging object. If None, a default log that logs to
        # stdout is used
        log=None,
        # A calibre logging object for access logging, by default no access
        # logging is performed
        access_log=None):
        self.ready = False
        self.handler = handler
        self.opts = opts or Options()
        if self.opts.trusted_ips:
            self.opts.trusted_ips = tuple(
                parse_trusted_ips(self.opts.trusted_ips))
        self.log = log or ThreadSafeLog(level=ThreadSafeLog.DEBUG)
        self.jobs_manager = JobsManager(self.opts, self.log)
        self.access_log = access_log

        ba = (self.opts.listen_on, int(self.opts.port))
        if not ba[0]:
            # AI_PASSIVE does not work with host of '' or None
            ba = ('0.0.0.0', ba[1])
        self.bind_address = ba
        self.bound_address = None
        self.connection_map = {}

        self.ssl_context = None
        if self.opts.ssl_certfile is not None and self.opts.ssl_keyfile is not None:
            self.ssl_context = ssl.create_default_context(
                ssl.Purpose.CLIENT_AUTH)
            self.ssl_context.load_cert_chain(certfile=self.opts.ssl_certfile,
                                             keyfile=self.opts.ssl_keyfile)
            self.ssl_context.set_servername_callback(self.on_ssl_servername)

        self.pre_activated_socket = None
        if self.opts.allow_socket_preallocation:
            from calibre.srv.pre_activated import pre_activated_socket
            self.pre_activated_socket = pre_activated_socket()
            if self.pre_activated_socket is not None:
                set_socket_inherit(self.pre_activated_socket, False)
                self.bind_address = self.pre_activated_socket.getsockname()

        self.create_control_connection()
        self.pool = ThreadPool(self.log,
                               self.job_completed,
                               count=self.opts.worker_count)
        self.plugin_pool = PluginPool(self, plugins)
Example #5
0
 def __init__(self, handler, plugins=(), specialize=lambda srv:None, **kwargs):
     Thread.__init__(self, name='ServerMain')
     from calibre.srv.opts import Options
     from calibre.srv.loop import ServerLoop
     from calibre.srv.http_response import create_http_handler
     self.setup_defaults(kwargs)
     self.loop = ServerLoop(
         create_http_handler(handler),
         opts=Options(**kwargs),
         plugins=plugins,
         log=ServerLog(level=ServerLog.WARN),
     )
     self.log = self.loop.log
     self.silence_log = self.log
     specialize(self)
Example #6
0
 def __init__(self, handler, plugins=(), specialize=lambda srv:None, **kwargs):
     Thread.__init__(self, name='ServerMain')
     from calibre.srv.opts import Options
     from calibre.srv.loop import ServerLoop
     from calibre.srv.http_response import create_http_handler
     kwargs['shutdown_timeout'] = kwargs.get('shutdown_timeout', 0.1)
     kwargs['listen_on'] = kwargs.get('listen_on', 'localhost')
     kwargs['port'] = kwargs.get('port', 0)
     self.loop = ServerLoop(
         create_http_handler(handler),
         opts=Options(**kwargs),
         plugins=plugins,
         log=ServerLog(level=ServerLog.WARN),
     )
     self.log = self.loop.log
     specialize(self)
Example #7
0
 def __init__(self, library_path, libraries=(), plugins=(), **kwargs):
     Thread.__init__(self, name='ServerMain')
     from calibre.srv.opts import Options
     from calibre.srv.loop import ServerLoop
     from calibre.srv.handler import Handler
     from calibre.srv.http_response import create_http_handler
     self.setup_defaults(kwargs)
     opts = Options(**kwargs)
     self.libraries = libraries or (library_path, )
     self.handler = Handler(self.libraries, opts, testing=True)
     self.loop = ServerLoop(
         create_http_handler(self.handler.dispatch),
         opts=opts,
         plugins=plugins,
         log=ServerLog(level=ServerLog.DEBUG),
     )
     self.log = self.loop.log
     self.handler.set_log(self.log)