Backups: allow using keyfile as credentials for sshfs mounts

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Michael Pimmer 2018-12-10 18:31:09 +01:00 committed by James Valleroy
parent b2f5d68ca6
commit fc9e406bf8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 24 additions and 12 deletions

View File

@ -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()

View File

@ -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