diff --git a/src/borgstore/backends/sftp.py b/src/borgstore/backends/sftp.py index 1d25e0c..18a6ccb 100644 --- a/src/borgstore/backends/sftp.py +++ b/src/borgstore/backends/sftp.py @@ -40,7 +40,18 @@ def __init__(self, hostname: str, path: str, port: int = 22, username: Optional[ def _connect(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(hostname=self.hostname, username=self.username, port=self.port, allow_agent=True) + config_path = Path("~/.ssh/config").expanduser() + if config_path.exists(): + sc = paramiko.SSHConfig.from_path(str(config_path)) + scd = sc.lookup(self.hostname) + else: + scd = paramiko.SSHConfigDict() # empty dict + ssh.connect( + hostname=scd.get("hostname", self.hostname), + username=scd.get("user", self.username), + port=scd.as_int("port") if "port" in scd else self.port, + allow_agent=True, + ) self.client = ssh.open_sftp() def _disconnect(self): diff --git a/tests/test_backends.py b/tests/test_backends.py index 7a15ed4..b0e0136 100644 --- a/tests/test_backends.py +++ b/tests/test_backends.py @@ -31,13 +31,23 @@ def posixfs_backend_created(tmp_path): be.destroy() -def _get_sftp_backend(): +def _get_sftp_backend(local=False, remote_url=False, remote_config=True): # needs an authorized key loaded into the ssh agent. pytest works, tox doesn't: - # return Sftp(username="tw", hostname="localhost", path="/Users/tw/w/borgstore/temp-store") + if local: + return Sftp(username="tw", hostname="localhost", path="/Users/tw/w/borgstore/temp-store") # for tests with higher latency: - return Sftp( - username="twaldmann", hostname="shell.ipv4.thinkmo.de", port=2222, path="/home/twaldmann/borgstore/temp-store" - ) + if remote_url: + return Sftp( + username="twaldmann", + hostname="shell.ipv4.thinkmo.de", + port=2222, + path="/home/twaldmann/borgstore/temp-store", + ) + # same as previous, but loads config for host "shell" from ~/.ssh/config: + if remote_config: + return Sftp(hostname="shell", path="/home/twaldmann/borgstore/temp-store") + + raise ValueError("check _get_sftp_backend() parameter defaults!") def check_sftp_available():