From bfc3953be000e426551c90d65a0633c62c9fde89 Mon Sep 17 00:00:00 2001 From: Martin Blumenstingl Date: Tue, 7 Jan 2014 22:36:28 +0100 Subject: [PATCH 1/7] Add a testcase for client.save_host_keys. --- tests/test_client.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index e535227..7aaa1bf 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -25,6 +25,8 @@ import threading import time import unittest import weakref +import warnings +import os from binascii import hexlify import paramiko @@ -184,7 +186,29 @@ class SSHClientTest (unittest.TestCase): self.assertEquals(1, len(self.tc.get_host_keys())) self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) - def test_5_cleanup(self): + def test_5_save_host_keys(self): + """ + verify that SSHClient correctly saves a known_hosts file. + """ + warnings.filterwarnings('ignore', 'tempnam.*') + + host_key = paramiko.RSAKey.from_private_key_file('tests/test_rsa.key') + public_host_key = paramiko.RSAKey(data=str(host_key)) + localname = os.tempnam() + + self.tc = paramiko.SSHClient() + self.assertEquals(0, len(self.tc.get_host_keys())) + + self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + self.assertEquals(1, len(self.tc.get_host_keys())) + self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + + self.tc.save_host_keys(localname) + self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) + + os.unlink(localname) + + def test_6_cleanup(self): """ verify that when an SSHClient is collected, its transport (and the transport's packetizer) is closed. From 78d9e4834cb9e4bb0060b419c8677660b016f4d2 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:35:46 -0800 Subject: [PATCH 2/7] No need for 'self.tc' within a single test :) --- tests/test_client.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 7aaa1bf..7be2d73 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -196,14 +196,14 @@ class SSHClientTest (unittest.TestCase): public_host_key = paramiko.RSAKey(data=str(host_key)) localname = os.tempnam() - self.tc = paramiko.SSHClient() - self.assertEquals(0, len(self.tc.get_host_keys())) + client = paramiko.SSHClient() + self.assertEquals(0, len(client.get_host_keys())) - self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) - self.assertEquals(1, len(self.tc.get_host_keys())) - self.assertEquals(public_host_key, self.tc.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + client.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + self.assertEquals(1, len(client.get_host_keys())) + self.assertEquals(public_host_key, client.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) - self.tc.save_host_keys(localname) + client.save_host_keys(localname) self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) os.unlink(localname) From 74e06aff9e1869dece030b61e99365150fb2e315 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:39:11 -0800 Subject: [PATCH 3/7] Small refactor --- tests/test_client.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 7be2d73..ad80e72 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -199,12 +199,14 @@ class SSHClientTest (unittest.TestCase): client = paramiko.SSHClient() self.assertEquals(0, len(client.get_host_keys())) - client.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key) + host_id = '[%s]:%d' % (self.addr, self.port) + + client.get_host_keys().add(host_id, 'ssh-rsa', public_host_key) self.assertEquals(1, len(client.get_host_keys())) - self.assertEquals(public_host_key, client.get_host_keys()['[%s]:%d' % (self.addr, self.port)]['ssh-rsa']) + self.assertEquals(public_host_key, client.get_host_keys()[host_id]['ssh-rsa']) client.save_host_keys(localname) - self.assertEquals(len('[%s]:%d' % (self.addr, self.port)) + 210, os.path.getsize(localname)) + self.assertEquals(len(host_id) + 210, os.path.getsize(localname)) os.unlink(localname) From 6d326fcde20dece926c2cf661991590e20a00eab Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:44:12 -0800 Subject: [PATCH 4/7] Saner (to me) positive assertion --- tests/test_client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index ad80e72..fae1d32 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -20,6 +20,7 @@ Some unit tests for SSHClient. """ +from __future__ import with_statement # Python 2.5 support import socket import threading import time @@ -206,7 +207,9 @@ class SSHClientTest (unittest.TestCase): self.assertEquals(public_host_key, client.get_host_keys()[host_id]['ssh-rsa']) client.save_host_keys(localname) - self.assertEquals(len(host_id) + 210, os.path.getsize(localname)) + + with open(localname) as fd: + assert host_id in fd.read() os.unlink(localname) From a1c1f8f29f3c24e9f34a76e3f5efc57e62396cd8 Mon Sep 17 00:00:00 2001 From: Nathan Scowcroft Date: Thu, 13 Jun 2013 13:05:11 -0400 Subject: [PATCH 5/7] Check correct stored hosts filename. --- paramiko/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paramiko/client.py b/paramiko/client.py index c5a2d1a..4adcfe7 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -189,8 +189,8 @@ class SSHClient (object): # update local host keys from file (in case other SSH clients # have written to the known_hosts file meanwhile. - if self.known_hosts is not None: - self.load_host_keys(self.known_hosts) + if self._host_keys_filename is not None: + self.load_host_keys(self._known_keys_filename) f = open(filename, 'w') for hostname, keys in self._host_keys.iteritems(): From 0fea895cdb1e69737f3526bad02e533e99b0bfe0 Mon Sep 17 00:00:00 2001 From: Nathan Scowcroft Date: Mon, 24 Jun 2013 16:43:12 -0400 Subject: [PATCH 6/7] ditto --- paramiko/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paramiko/client.py b/paramiko/client.py index 4adcfe7..be89609 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -190,7 +190,7 @@ class SSHClient (object): # update local host keys from file (in case other SSH clients # have written to the known_hosts file meanwhile. if self._host_keys_filename is not None: - self.load_host_keys(self._known_keys_filename) + self.load_host_keys(self._host_keys_filename) f = open(filename, 'w') for hostname, keys in self._host_keys.iteritems(): From b57e825f77544a88eea77cf65bbe7d2d0426d4c9 Mon Sep 17 00:00:00 2001 From: Jeff Forcier Date: Wed, 8 Jan 2014 12:49:27 -0800 Subject: [PATCH 7/7] Changelog, fixes #176 --- NEWS | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/NEWS b/NEWS index f3d00dd..2b48ba6 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,13 @@ Issues noted as "Fabric #NN" can be found at https://github.com/fabric/fabric/. Releases ======== +v1.10.5 (8th Jan 2014) +---------------------- + +* #176: Fix AttributeError bugs in known_hosts file (re)loading. Thanks to + Nathan Scowcroft for the patch & Martin Blumenstingl for the initial test + case. + v1.10.4 (27th Sep 2013) -----------------------