From fc9e406bf8285d8ff0194dde61131348b2a426b0 Mon Sep 17 00:00:00 2001 From: Michael Pimmer Date: Mon, 10 Dec 2018 18:31:09 +0100 Subject: [PATCH] Backups: allow using keyfile as credentials for sshfs mounts Reviewed-by: James Valleroy --- actions/sshfs | 12 +++------- plinth/modules/backups/tests/test_backups.py | 24 +++++++++++++++++--- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/actions/sshfs b/actions/sshfs index 11077b584..f308cbcc6 100755 --- a/actions/sshfs +++ b/actions/sshfs @@ -58,7 +58,7 @@ def parse_arguments(): def subcommand_mount(arguments): - """Show repository information.""" + """Mount a remote ssh path via sshfs.""" try: validate_mountpoint(arguments.mountpoint) except AlreadyMountedError: @@ -72,7 +72,7 @@ def subcommand_mount(arguments): 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no'] if arguments.ssh_keyfile: - cmd += ['-o', 'IdentityFile=$SSHKEY'] + cmd += ['-o', 'IdentityFile=' + arguments.ssh_keyfile] else: password = read_password() if not password: @@ -84,7 +84,7 @@ def subcommand_mount(arguments): def subcommand_umount(arguments): """Unmount a mountpoint.""" - run(['umount', arguments.mountpoint]) + subprocess.run(['umount', arguments.mountpoint], check=True) def validate_mountpoint(mountpoint): @@ -123,12 +123,6 @@ def read_password(): return ''.join(sys.stdin) -def run(cmd, env=None, check=True): - """Wrap the command with ssh password or keyfile authentication""" - # Set a timeout to not get stuck if the remote server asks for a password. - subprocess.run(cmd, check=check, env=env, timeout=TIMEOUT) - - def main(): """Parse arguments and perform all duties.""" arguments = parse_arguments() diff --git a/plinth/modules/backups/tests/test_backups.py b/plinth/modules/backups/tests/test_backups.py index 4af583b8f..518720fa2 100644 --- a/plinth/modules/backups/tests/test_backups.py +++ b/plinth/modules/backups/tests/test_backups.py @@ -116,9 +116,27 @@ class TestBackups(unittest.TestCase): self.assertEquals(len(content), 0) @unittest.skipUnless(euid == 0 and test_config.backups_ssh_path, - 'Needs to be root and ssh credentials provided') - def test_ssh_mount(self): - """Test (un)mounting if credentials for a remote location are given""" + 'Needs to be root and ssh password provided') + def test_sshfs_mount_password(self): + """Test (un)mounting if password for a remote location is given""" + credentials = self.get_credentials() + if not credentials: + return + ssh_path = test_config.backups_ssh_path + + repository = SshBorgRepository(uuid=str(uuid.uuid1()), + path=ssh_path, + credentials=credentials, + automount=False) + repository.mount() + self.assertTrue(repository.is_mounted) + repository.umount() + self.assertFalse(repository.is_mounted) + + @unittest.skipUnless(euid == 0 and test_config.backups_ssh_keyfile, + 'Needs to be root and ssh keyfile provided') + def test_sshfs_mount_keyfile(self): + """Test (un)mounting if keyfile for a remote location is given""" credentials = self.get_credentials() if not credentials: return