mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-29 10:10:19 +00:00
Backups: allow using keyfile as credentials for sshfs mounts
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
b2f5d68ca6
commit
fc9e406bf8
@ -58,7 +58,7 @@ def parse_arguments():
|
|||||||
|
|
||||||
|
|
||||||
def subcommand_mount(arguments):
|
def subcommand_mount(arguments):
|
||||||
"""Show repository information."""
|
"""Mount a remote ssh path via sshfs."""
|
||||||
try:
|
try:
|
||||||
validate_mountpoint(arguments.mountpoint)
|
validate_mountpoint(arguments.mountpoint)
|
||||||
except AlreadyMountedError:
|
except AlreadyMountedError:
|
||||||
@ -72,7 +72,7 @@ def subcommand_mount(arguments):
|
|||||||
'UserKnownHostsFile=/dev/null', '-o',
|
'UserKnownHostsFile=/dev/null', '-o',
|
||||||
'StrictHostKeyChecking=no']
|
'StrictHostKeyChecking=no']
|
||||||
if arguments.ssh_keyfile:
|
if arguments.ssh_keyfile:
|
||||||
cmd += ['-o', 'IdentityFile=$SSHKEY']
|
cmd += ['-o', 'IdentityFile=' + arguments.ssh_keyfile]
|
||||||
else:
|
else:
|
||||||
password = read_password()
|
password = read_password()
|
||||||
if not password:
|
if not password:
|
||||||
@ -84,7 +84,7 @@ def subcommand_mount(arguments):
|
|||||||
|
|
||||||
def subcommand_umount(arguments):
|
def subcommand_umount(arguments):
|
||||||
"""Unmount a mountpoint."""
|
"""Unmount a mountpoint."""
|
||||||
run(['umount', arguments.mountpoint])
|
subprocess.run(['umount', arguments.mountpoint], check=True)
|
||||||
|
|
||||||
|
|
||||||
def validate_mountpoint(mountpoint):
|
def validate_mountpoint(mountpoint):
|
||||||
@ -123,12 +123,6 @@ def read_password():
|
|||||||
return ''.join(sys.stdin)
|
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():
|
def main():
|
||||||
"""Parse arguments and perform all duties."""
|
"""Parse arguments and perform all duties."""
|
||||||
arguments = parse_arguments()
|
arguments = parse_arguments()
|
||||||
|
|||||||
@ -116,9 +116,27 @@ class TestBackups(unittest.TestCase):
|
|||||||
self.assertEquals(len(content), 0)
|
self.assertEquals(len(content), 0)
|
||||||
|
|
||||||
@unittest.skipUnless(euid == 0 and test_config.backups_ssh_path,
|
@unittest.skipUnless(euid == 0 and test_config.backups_ssh_path,
|
||||||
'Needs to be root and ssh credentials provided')
|
'Needs to be root and ssh password provided')
|
||||||
def test_ssh_mount(self):
|
def test_sshfs_mount_password(self):
|
||||||
"""Test (un)mounting if credentials for a remote location are given"""
|
"""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()
|
credentials = self.get_credentials()
|
||||||
if not credentials:
|
if not credentials:
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user