def test_check_remote_command_fail(self): cmds = [] if tools.checkCommand('nice'): cmds.append('nice') self.cfg.setNiceOnRemote(True) if tools.checkCommand('ionice'): cmds.append('ionice') self.cfg.setIoniceOnRemote(True) if tools.checkCommand('nocache'): cmds.append('nocache') self.cfg.setNocacheOnRemote(True) if tools.checkCommand('screen') and tools.checkCommand('flock'): cmds.extend(('screen', 'flock', 'rmdir', 'mktemp')) self.cfg.setSmartRemoveRunRemoteInBackground(True) # make one after an other command from 'cmds' fail by symlink them # to /bin/false false = tools.which('false') for cmd in cmds: msg = 'current trap: %s' %cmd with self.subTest(cmd = cmd): with TemporaryDirectory() as self.remotePath: self.cfg.setSshSnapshotsPath(self.remotePath) os.symlink(false, os.path.join(self.remotePath, cmd)) self.cfg.setSshPrefix(True, "export PATH=%s:$PATH; " %self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) with self.assertRaisesRegex(MountException, r"Remote host .+ doesn't support '.*?%s.*'" %cmd, msg = msg): ssh.checkRemoteCommands()
def test_checkLogin_fail_wrong_user(self): self.cfg.setSshUser('non_existing_user') ssh = sshtools.SSH(cfg=self.cfg) with self.assertRaisesRegex( MountException, r"Password-less authentication for .+ failed.+"): ssh.checkLogin()
def test_check_remote_command_with_spaces(self): self.cfg.setSmartRemoveRunRemoteInBackground(tools.checkCommand('screen') and tools.checkCommand('flock')) self.remotePath = os.path.join(self.tmpDir.name, 'foo bar') self.cfg.setSshSnapshotsPath(self.remotePath) os.mkdir(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) ssh.checkRemoteCommands()
def __init__(self, cfg=None, profile_id=None, mode=None, parent=None, *args, **kwargs): self.config = cfg if self.config is None: self.config = config.Config() self.profile_id = profile_id if self.profile_id is None: self.profile_id = self.config.currentProfile() self.mode = mode if self.mode is None: self.mode = self.config.snapshotsMode(self.profile_id) self.parent = parent self.args = args self.kwargs = kwargs self.ssh = sshtools.SSH(*self.args, symlink=False, **self.splitKwargs('ssh')) self.rev_root = EncFS_mount(*self.args, symlink=False, **self.splitKwargs('encfs_reverse')) super(EncFS_SSH, self).__init__(*self.args, **self.splitKwargs('encfs'))
def test_checkPingHost_fail(self): self.cfg.setSshHost('non_existing_host') ssh = sshtools.SSH(cfg=self.cfg) with self.assertRaisesRegex( MountException, r'Ping .+ failed\. Host is down or wrong address\.'): ssh.checkPingHost()
def test_check_remote_command_hard_link_fail(self): # let hard-link check fail by manipulate one of the files os.mkdir(self.remotePath) self.cfg.setSshPrefix(True, 'TRAP=$(ls -1d %s/tmp_* | tail -n1)/a; rm $TRAP; echo bar > $TRAP; ' %self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) with self.assertRaisesRegex(MountException, r"Remote host .+ doesn't support hardlinks"): ssh.checkRemoteCommands()
def test_check_remote_command(self): self.cfg.setNiceOnRemote(tools.checkCommand('nice')) self.cfg.setIoniceOnRemote(tools.checkCommand('ionice')) self.cfg.setNocacheOnRemote(tools.checkCommand('nocache')) self.cfg.setSmartRemoveRunRemoteInBackground(tools.checkCommand('screen') and tools.checkCommand('flock')) os.mkdir(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) ssh.checkRemoteCommands()
def test_checkRemoteFolder_with_spaces(self): self.remotePath = os.path.join(self.tmpDir.name, 'foo bar') self.cfg.setSshSnapshotsPath(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) #create new directories ssh.checkRemoteFolder() self.assertTrue(os.path.isdir(self.remotePath))
def test_unlockSshAgent_fail(self): subprocess.Popen(['ssh-add', '-D'], stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL).communicate() ssh = sshtools.SSH(cfg = self.cfg) ssh.private_key_fingerprint = 'wrong fingerprint' with self.assertRaisesRegex(MountException, r"Could not unlock ssh private key\. Wrong password or password not available for cron\."): ssh.unlockSshAgent(force = True)
def test_checkRemoteFolder_fail_can_not_create(self): ssh = sshtools.SSH(cfg = self.cfg) #can not create path os.chmod(self.tmpDir.name, stat.S_IRUSR | stat.S_IXUSR) with self.assertRaisesRegex(MountException, r"Couldn't create remote path.+"): ssh.checkRemoteFolder() #make it writeable again otherwise cleanup will fail os.chmod(self.tmpDir.name, stat.S_IRWXU)
def test_checkRemoteFolder_fail_not_a_folder(self): with open(self.remotePath, 'wt') as f: f.write('foo') self.cfg.setSshSnapshotsPath(self.remotePath) ssh = sshtools.SSH(cfg = self.cfg) #path already exist but is not a folder with self.assertRaisesRegex(MountException, r"Remote path exists but is not a directory.+"): ssh.checkRemoteFolder()
def test_checkRemoteFolder_fail_can_not_create(self): ssh = sshtools.SSH(cfg=self.cfg) #can not create path with generic.mockPermissions(self.tmpDir.name, stat.S_IRUSR | stat.S_IXUSR): with self.assertRaisesRegex(MountException, r"Couldn't create remote path.+"): ssh.checkRemoteFolder()
def test_unlockSshAgent(self): subprocess.Popen(['ssh-add', '-D'], stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL).communicate() ssh = sshtools.SSH(cfg = self.cfg) ssh.unlockSshAgent(force = True) out = subprocess.Popen(['ssh-add', '-l'], stdout = subprocess.PIPE, universal_newlines = True).communicate()[0] self.assertIn(ssh.private_key_fingerprint, out)
def benchmarkCipher(args): """ Command for transfering a file with scp to remote host with all available ciphers and print its speed and time. args: argparse.ArgumentParser instance """ setQuiet(args) printHeader() cfg = getConfig(args) if cfg.get_snapshots_mode() in ('ssh', 'ssh_encfs'): ssh = sshtools.SSH(cfg) ssh.benchmark_cipher(args.FILE_SIZE) sys.exit(RETURN_OK) else: logger.error("SSH is not configured for profile '%s'!" % cfg.get_profile_name()) sys.exit(RETURN_ERR)
def test_checkRemoteFolder(self): ssh = sshtools.SSH(cfg = self.cfg) #create new directories ssh.checkRemoteFolder() self.assertTrue(os.path.isdir(self.remotePath)) #rerun check to test if it correctly recognize previous created folders ssh.checkRemoteFolder() #make folder read-only os.chmod(self.remotePath, stat.S_IRUSR | stat.S_IXUSR) with self.assertRaisesRegex(MountException, r"Remote path is not writeable.+"): ssh.checkRemoteFolder() #make folder not executable os.chmod(self.remotePath, stat.S_IRUSR | stat.S_IWUSR) with self.assertRaisesRegex(MountException, r"Remote path is not executable.+"): ssh.checkRemoteFolder() #make it writeable again otherwise cleanup will fail os.chmod(self.remotePath, stat.S_IRWXU)
def test_checkRemoteFolder(self): ssh = sshtools.SSH(cfg=self.cfg) #create new directories ssh.checkRemoteFolder() self.assertIsDir(self.remotePath) #rerun check to test if it correctly recognize previous created folders ssh.checkRemoteFolder() #make folder read-only with generic.mockPermissions(self.remotePath, stat.S_IRUSR | stat.S_IXUSR): with self.assertRaisesRegex(MountException, r"Remote path is not writable.+"): ssh.checkRemoteFolder() #make folder not executable with generic.mockPermissions(self.remotePath, stat.S_IRUSR | stat.S_IWUSR): with self.assertRaisesRegex(MountException, r"Remote path is not executable.+"): ssh.checkRemoteFolder()
def benchmarkCipher(args): """ Command for transferring a file with scp to remote host with all available ciphers and print its speed and time. Args: args (argparse.Namespace): previously parsed arguments Raises: SystemExit: 0 """ setQuiet(args) printHeader() cfg = getConfig(args) if cfg.snapshotsMode() in ('ssh', 'ssh_encfs'): ssh = sshtools.SSH(cfg) ssh.benchmarkCipher(args.FILE_SIZE) sys.exit(RETURN_OK) else: logger.error("SSH is not configured for profile '%s'!" % cfg.profileName()) sys.exit(RETURN_ERR)
def test_checkCipher_fail(self): # fix debug log self.cfg.SSH_CIPHERS['non_existing_cipher'] = 'non_existing_cipher' ssh = sshtools.SSH(cfg = self.cfg, cipher = 'non_existing_cipher') with self.assertRaisesRegex(MountException, r"Cipher .+ failed for.+"): ssh.checkCipher()
def test_checkPingHost(self): ssh = sshtools.SSH(cfg = self.cfg) ssh.checkPingHost()
def test_checkCipher_default(self): ssh = sshtools.SSH(cfg = self.cfg, cipher = 'default') ssh.checkCipher()
def test_checkCipher_specific(self): ssh = sshtools.SSH(cfg = self.cfg, cipher = 'aes128-ctr') ssh.checkCipher()
def test_checkLogin(self): ssh = sshtools.SSH(cfg = self.cfg) ssh.checkLogin()
def setUp(self): super(TestStartSshAgent, self).setUp() self.ssh = sshtools.SSH(cfg=self.cfg) self.currentSock = os.environ.pop(self.SOCK, '') self.currentPid = os.environ.pop(self.PID, '')
def test_checkKnownHosts(self): ssh = sshtools.SSH(cfg = self.cfg) ssh.checkKnownHosts()
def test_randomId(self): ssh = sshtools.SSH(cfg = self.cfg) self.assertRegex(ssh.randomId(size = 6), r'[A-Z0-9]{6}')
def test_checkKnownHosts_fail(self): self.cfg.setSshHost('non_existing_host') ssh = sshtools.SSH(cfg = self.cfg) with self.assertRaisesRegex(MountException, r".+ not found in ssh_known_hosts\."): ssh.checkKnownHosts()