def update_host(self, host_id, hostname, port, username, password): """Update the supplied host id with new connection details. Args: host_id (str): The host id to update. hostname (str): The new IP or hostname of the deluge daemon. port (int): The new port of the deluge daemon. username (str): The new username to login to the daemon with. password (str): The new password to login to the daemon with. """ validate_host_info(hostname, port) self.check_info_exists(hostname, port, username, skip_host_id=host_id) if (not password and not username or username == 'localclient') and hostname in LOCALHOST: username, password = get_localhost_auth() for idx, host_entry in enumerate(self.config['hosts']): if host_id == host_entry[0]: self.config['hosts'][ idx] = host_id, hostname, port, username, password self.config.save() return True return False
def connect(self, host="127.0.0.1", port=58846, username="", password=""): """ Connects to a daemon process. :param host: str, the hostname of the daemon :param port: int, the port of the daemon :param username: str, the username to login with :param password: str, the password to login with :returns: a Deferred object that will be called once the connection has been established or fails """ if not username and host in ("127.0.0.1", "localhost"): # No username was provided and it's the localhost, so we can try # to grab the credentials from the auth file. import common username, password = common.get_localhost_auth() self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers)) self._daemon_proxy.set_disconnect_callback(self.__on_disconnect) d = self._daemon_proxy.connect(host, port, username, password) def on_connect_fail(result): log.debug("on_connect_fail: %s", result) self.disconnect() return result d.addErrback(on_connect_fail) return d
def default_hostlist(): """Create a new hosts key for hostlist with a localhost entry""" host_id = uuid.uuid4().hex username, password = get_localhost_auth() return { 'hosts': [(host_id, DEFAULT_HOST, DEFAULT_PORT, username, password)] }
def connect(self, host="127.0.0.1", port=58846, username="", password=""): """ Connects to a daemon process. :param host: str, the hostname of the daemon :param port: int, the port of the daemon :param username: str, the username to login with :param password: str, the password to login with :returns: a Deferred object that will be called once the connection has been established or fails """ if not username and host in ("127.0.0.1", "localhost"): # No username was provided and it's the localhost, so we can try # to grab the credentials from the auth file. import common username, password = common.get_localhost_auth() self._daemon_proxy = DaemonSSLProxy(dict(self.__event_handlers)) self._daemon_proxy.set_disconnect_callback(self.__on_disconnect) d = self._daemon_proxy.connect(host, port, username, password) def on_connect_fail(result): log.debug("on_connect_fail: %s", result) self.disconnect() return result d.addErrback(on_connect_fail) return d
def default_hostlist(): """Create a new hosts key for hostlist with a localhost entry""" host_id = sha1(str(time.time()).encode('utf8')).hexdigest() username, password = get_localhost_auth() return { 'hosts': [(host_id, DEFAULT_HOST, DEFAULT_PORT, username, password)] }
def test_valid_client_login(self): self.authmanager = AuthManager() auth = get_localhost_auth() self.protocol.dispatch(self.request_id, 'daemon.login', auth, {'client_version': 'Test'}) msg = self.protocol.messages.pop() self.assertEqual(msg[0], rpcserver.RPC_RESPONSE, str(msg)) self.assertEqual(msg[1], self.request_id, str(msg)) self.assertEqual(msg[2], rpcserver.AUTH_LEVEL_ADMIN, str(msg))
def test_connect_with_password(self): username, password = get_localhost_auth() yield client.connect('localhost', self.listen_port, username=username, password=password) yield client.core.create_account('testuser', 'testpw', 'DEFAULT') yield client.disconnect() ret = yield client.connect('localhost', self.listen_port, username='******', password='******') self.assertEqual(ret, AUTH_LEVEL_NORMAL) yield
def test_client_invalid_method_call(self): self.authmanager = AuthManager() auth = get_localhost_auth() self.protocol.dispatch(self.request_id, 'invalid_function', auth, {}) msg = self.protocol.messages.pop() self.assertEqual(msg[0], rpcserver.RPC_ERROR) self.assertEqual(msg[1], self.request_id) self.assertEqual(msg[2], 'WrappedException') self.assertEqual(msg[3][1], 'AttributeError')
def from_file(klass, config_dir: Optional[str]) -> AuthProvider: if config_dir: configmanager.set_config_dir(config_dir) (username, password) = common.get_localhost_auth() return klass( host="localhost", username=username, password=password, )
def test_client_invalid_method_call(self): self.authmanager = AuthManager() auth = get_localhost_auth() self.protocol.dispatch(self.request_id, 'invalid_function', auth, {}) msg = self.protocol.messages.pop() self.assertEqual(msg[0], rpcserver.RPC_ERROR) self.assertEqual(msg[1], self.request_id) self.assertEqual(msg[2], 'WrappedException') self.assertEqual(msg[3][1], 'AttributeError')
def authenticate(daemon_version, username, password): if not username and host in ('127.0.0.1', 'localhost'): # No username provided and it's localhost, so attempt to get credentials from auth file. username, password = get_localhost_auth() d = self._daemon_proxy.authenticate(username, password) d.addCallback(on_authenticate, daemon_version) d.addErrback(on_authenticate_fail) return d
def authenticate(daemon_version, username, password): if not username and host in ('127.0.0.1', 'localhost'): # No username provided and it's localhost, so attempt to get credentials from auth file. username, password = get_localhost_auth() d = self._daemon_proxy.authenticate(username, password) d.addCallback(on_authenticate, daemon_version) d.addErrback(on_authenticate_fail) return d
def test_console_command_status(self): username, password = get_localhost_auth() self.patch(sys, 'argv', self.var['sys_arg_cmd'] + ['--port'] + ['58900'] + ['--username'] + [username] + ['--password'] + [password] + ['status']) fd = StringFileDescriptor(sys.stdout) self.patch(sys, 'stdout', fd) yield self.exec_command() std_output = fd.out.getvalue() self.assertTrue(std_output.startswith('Total upload: ') and std_output.endswith(' Moving: 0\n'))
def migrate_config_2_to_3(config): """Mirgrates old hostlist config files to new file version""" localclient_username, localclient_password = get_localhost_auth() if not localclient_username: # Nothing to do here, there's no auth file return for idx, (__, host, __, username, __) in enumerate(config['hosts'][:]): if host in LOCALHOST and not username: config['hosts'][idx][3] = localclient_username config['hosts'][idx][4] = localclient_password return config
def test_connect_invalid_user(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username='******') def on_failure(failure): self.assertEqual(failure.trap(error.BadLoginError), error.BadLoginError) self.assertEqual(failure.value.message, 'Username does not exist') self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_client_login_error(self): # This test causes error log prints while running the test... self.protocol.transport = None # This should cause AttributeError self.authmanager = AuthManager() auth = get_localhost_auth() self.protocol.dispatch(self.request_id, 'daemon.login', auth, {'client_version': 'Test'}) msg = self.protocol.messages.pop() self.assertEqual(msg[0], rpcserver.RPC_ERROR) self.assertEqual(msg[1], self.request_id) self.assertEqual(msg[2], 'WrappedException') self.assertEqual(msg[3][1], 'AttributeError')
def test_connect_localclient(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username=username, password=password) def on_connect(result): self.assertEqual(client.get_auth_level(), AUTH_LEVEL_ADMIN) self.addCleanup(client.disconnect) return result d.addCallbacks(on_connect, self.fail) return d
def patch_arg_command(self, command): if type(command) == str: command = [command] username, password = get_localhost_auth() self.patch( sys, 'argv', self.var['sys_arg_cmd'] + ['--port'] + [str(self.listen_port)] + ['--username'] + [username] + ['--password'] + [password] + command, )
def test_connect_with_password(self): username, password = get_localhost_auth() yield client.connect( 'localhost', self.listen_port, username=username, password=password ) yield client.core.create_account('testuser', 'testpw', 'DEFAULT') yield client.disconnect() ret = yield client.connect( 'localhost', self.listen_port, username='******', password='******' ) self.assertEqual(ret, AUTH_LEVEL_NORMAL) yield
def test_connect_without_password(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username=username) def on_failure(failure): self.assertEqual(failure.trap(error.AuthenticationRequired), error.AuthenticationRequired) self.assertEqual(failure.value.username, username) self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_localclient(self): username, password = get_localhost_auth() d = client.connect( 'localhost', self.listen_port, username=username, password=password ) def on_connect(result): self.assertEqual(client.get_auth_level(), AUTH_LEVEL_ADMIN) self.addCleanup(client.disconnect) return result d.addCallbacks(on_connect, self.fail) return d
def test_connect_bad_password(self): username, password = get_localhost_auth() d = client.connect( 'localhost', self.listen_port, username=username, password=password + '1' ) def on_failure(failure): self.assertEqual(failure.trap(error.BadLoginError), error.BadLoginError) self.assertEqual(failure.value.message, 'Password does not match') self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_without_password(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username=username) def on_failure(failure): self.assertEqual( failure.trap(error.AuthenticationRequired), error.AuthenticationRequired ) self.assertEqual(failure.value.username, username) self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_bad_password(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username=username, password=password + '1') def on_failure(failure): self.assertEqual( failure.trap(error.BadLoginError), error.BadLoginError ) self.assertEqual(failure.value.message, 'Password does not match') self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_invalid_user(self): username, password = get_localhost_auth() d = client.connect('localhost', self.listen_port, username='******') def on_failure(failure): self.assertEqual( failure.trap(error.BadLoginError), error.BadLoginError ) self.assertEqual(failure.value.message, 'Username does not exist') self.addCleanup(client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_without_sending_client_version_fails(self): username, password = get_localhost_auth() no_version_sending_client = NoVersionSendingClient() d = no_version_sending_client.connect( 'localhost', self.listen_port, username=username, password=password ) def on_failure(failure): self.assertEqual( failure.trap(error.IncompatibleClient), error.IncompatibleClient ) self.addCleanup(no_version_sending_client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def test_connect_without_sending_client_version_fails(self): username, password = get_localhost_auth() no_version_sending_client = NoVersionSendingClient() d = no_version_sending_client.connect( 'localhost', self.listen_port, username=username, password=password ) def on_failure(failure): self.assertEqual( failure.trap(error.IncompatibleClient), error.IncompatibleClient ) self.addCleanup(no_version_sending_client.disconnect) d.addCallbacks(self.fail, on_failure) return d
def add_host(self, hostname, port, username, password): """Add a new host to hostlist. Args: hostname (str): The IP or hostname of the deluge daemon. port (int): The port of the deluge daemon. username (str): The username to login to the daemon with. password (str): The password to login to the daemon with. Returns: str: The new host id. """ if (not password and not username or username == 'localclient') and hostname in LOCALHOST: username, password = get_localhost_auth() validate_host_info(hostname, port) self.check_info_exists(hostname, port, username) host_id = uuid.uuid4().hex self.config['hosts'].append( (host_id, hostname, port, username, password)) self.config.save() return host_id
def test_console_command_status(self): username, password = get_localhost_auth() self.patch( sys, 'argv', self.var['sys_arg_cmd'] + ['--port'] + ['58900'] + ['--username'] + [username] + ['--password'] + [password] + ['status'], ) fd = StringFileDescriptor(sys.stdout) self.patch(sys, 'stdout', fd) yield self.exec_command() std_output = fd.out.getvalue() self.assertTrue( std_output.startswith('Total upload: ') and std_output.endswith(' Moving: 0\n') )
def test_authorize(self): self.assertEqual( self.auth.authorize(*get_localhost_auth()), AUTH_LEVEL_ADMIN )
def test_authorize(self): self.assertEqual(self.auth.authorize(*get_localhost_auth()), AUTH_LEVEL_ADMIN)
def add_default_host(self): self.add_host(DEFAULT_HOST, DEFAULT_PORT, *get_localhost_auth())