Skip to content

Commit

Permalink
sftp: add support for ~/.ssh/config, fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Sep 15, 2024
1 parent 024a91d commit c7b98b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/borgstore/backends/sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 15 additions & 5 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit c7b98b4

Please sign in to comment.