paramiko/paramiko — pull request #224 by alanfranz (2013-10-18 15:33:48+00:00)
Title: Refactor key loading logic into its own method for clarity and better er...

Many a time, when trying to login to a server using multiple RSA keys through the key_filename parameter, I got an error like "Not a valid DSA key file". This was puzzling because all keys were in fact RSA, and perfecly valid, and I was able to log in other servers.

In fact the issue was different: the problem lied in the fact that the public key matching the private RSA key was not in the target authorized_keys file, but the connection logic "masked" the true error with the last "saved exception", and since it tried to load DSA keys after RSA keys, the error was always "not a valid DSA key file".

This proposal splits the key loading part into its own method, with its own exception; in this situation, the correct "authentication failed" exception will be propagated. Of course, if an invalid RSA/DSA key file is found after an authentication failure, it's still the latter error that will be propagated, yet I think it's an improvement over the original behaviour.