diff --git a/lib/ssh.py b/lib/ssh.py index 7b2e7edd3836a64de79e4e1a9f0b497b5f3b6f42..cb7aa186a31f085abd320416e9cf6b1cea671a8f 100644 --- a/lib/ssh.py +++ b/lib/ssh.py @@ -26,12 +26,25 @@ import os import logging +import re from ganeti import utils from ganeti import errors from ganeti import constants +def FormatParamikoFingerprint(fingerprint): + """Formats the fingerprint of L{paramiko.PKey.get_fingerprint()} + + @type fingerprint: str + @param fingerprint: PKey fingerprint + @return The string hex representation of the fingerprint + + """ + assert len(fingerprint) % 2 == 0 + return ":".join(re.findall(r"..", fingerprint.lower())) + + def GetUserFiles(user, mkdir=False): """Return the paths of a user's ssh files. diff --git a/test/ganeti.ssh_unittest.py b/test/ganeti.ssh_unittest.py index 5afd59052c74278feef480dab55b38a9f678990e..bd6c951efd742518112e37c3b5740f8c33d07b32 100755 --- a/test/ganeti.ssh_unittest.py +++ b/test/ganeti.ssh_unittest.py @@ -47,6 +47,12 @@ class TestKnownHosts(testutils.GanetiTestCase): "%s ssh-rsa %s\n" % (cfg.GetClusterName(), mocks.FAKE_CLUSTER_KEY)) + def testFormatParamikoFingerprintCorrect(self): + self.assertEqual(ssh.FormatParamikoFingerprint("C0Ffee"), "c0:ff:ee") + + def testFormatParamikoFingerprintNotDividableByTwo(self): + self.assertRaises(AssertionError, ssh.FormatParamikoFingerprint, "C0Ffe") + if __name__ == '__main__': testutils.GanetiTestProgram()