Skip to content

Commit

Permalink
Fix issue jschneier#118
Browse files Browse the repository at this point in the history
  • Loading branch information
sallyruthstruik committed Jun 2, 2017
1 parent ddcdf9e commit d241378
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ setuptools*
__pycache__
.coverage
.cache
.idea
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ By order of apparition, thanks:
* Matt Braymer-Hayes (S3 with Boto3)
* Eirik Martiniussen Sylliaas (Google Cloud Storage native support)
* Jody McIntyre (Google Cloud Storage native support)
* Stanislav Kaledin (Bug fixes in SFTPStorage)

Extra thanks to Marty for adding this in Django,
you can buy his very interesting book (Pro Django).
10 changes: 6 additions & 4 deletions storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ def __init__(self, host=None, params=None, interactive=None, file_mode=None,
def _connect(self):
self._ssh = paramiko.SSHClient()

if self._known_host_file is not None:
known_host_file = self._known_host_file or os.path.expanduser(
os.path.join("~", ".ssh", "known_hosts")
)

if os.path.exists(known_host_file):
self._ssh.load_host_keys(self._known_host_file)
else:
# automatically add host keys from current user.
self._ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))

# and automatically add new host keys for hosts we haven't seen before.
self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Expand Down Expand Up @@ -152,6 +153,7 @@ def delete(self, name):
def exists(self, name):
# Try to retrieve file info. Return true on success, false on failure.
remote_path = self._remote_path(name)

try:
self.sftp.stat(remote_path)
return True
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def setUp(self):
def test_init(self):
pass

@patch('paramiko.SSHClient')
def test_no_known_hosts_file(self, mock_ssh):
self.storage._known_host_file = "not_existed_file"
self.storage._connect()
self.assertEqual('foo', mock_ssh.return_value.connect.call_args[0][0])

@patch('paramiko.SSHClient')
def test_connect(self, mock_ssh):
self.storage._connect()
Expand Down

0 comments on commit d241378

Please sign in to comment.