Skip to content

connect() fails once with invalid hosts file, then works #625

Open

Description

I'm running into this issue with Paramiko 1.15.1 on CentOS 7.1. This is a simple example of the issue I'm encountering:

>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.load_host_keys("/root/.ssh/known_hosts")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 121, in load_host_keys
    self._host_keys.load(filename)
  File "/usr/lib/python2.7/site-packages/paramiko/hostkeys.py", line 94, in load
    with open(filename, 'r') as f:
IOError: [Errno 2] No such file or directory: '/root/.ssh/known_hosts'
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 288, in connect
    server_key)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 556, in missing_host_key
    client.save_host_keys(client._host_keys_filename)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 137, in save_host_keys
    self.load_host_keys(self._host_keys_filename)
  File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 121, in load_host_keys
    self._host_keys.load(filename)
  File "/usr/lib/python2.7/site-packages/paramiko/hostkeys.py", line 94, in load
    with open(filename, 'r') as f:
IOError: [Errno 2] No such file or directory: '/root/.ssh/known_hosts'
>>> ssh.connect('foo')
>>> ssh.close()

Activity

  1. bitprophet commented on Dec 2, 2015

    @bitprophet
    Member

    Can't think offhand why this would be happening, there's probably some state in SSHClient which is getting filled in prior to the IOError line which is causing the behavior to (right or wrong) be non-idempotent. Needs someone to poke.

  2. zsjohny commented on Jan 28, 2016

    @zsjohny

    @bitprophet same to me

  3. radssh commented on Jan 28, 2016

    @radssh
    Contributor

    Poke results:

    The known_hosts file does not exist, so the initial load raises an exception, and the SSHClient has an empty KnownHosts object. When the key is added by the AutoAddPolicy, it initially triggers a save call for the file, which attempts to reload the known_hosts file contents, just in case it was externally changed since it was loaded (https://github.com/paramiko/paramiko/blob/master/paramiko/client.py#L137). Since the file didn't exist at original load time (and still doesn't exist), an exception is raised here as well, bypassing the save code that would normally rewrite/create the file.

    The second time the connect() call is made, the host entry is in the HostEntry object (just not saved to disk) so it is found, and not re-added, which would trigger a call to save_host_keys(). The connect succeeds on subsequent calls once the host key is present in the HostEntry memory dict.

    Not sure what the most appropriate handling of this ought to be: Intiial load() call could create an empty file if it doesn't already exist although it might be better to defer that until save_host_keys() is called anyway. Potentially more complicated if the path to the file requires parent directories to be created. Might also make sense to translate AutoAdd/AutoSave exceptions to warnings(?).

  4. added a commit that references this issue on Oct 27, 2016
    bbbff69
  5. bitprophet commented on Oct 28, 2016

    @bitprophet
    Member

    To rephrase/summarize the previous comment:

    • SSHClient caches known_hosts data in memory in addition to persisting to disk
    • updates to the cache trigger disk write
    • disk write performs disk read to ensure no OOB updates are stomped
    • thus, the first call to connect() for a given host in a given session will except (given an invalid/nonexistent known hosts file setting), but subsequent calls will not (as the cache is now warm)

    What I think ought to happen:

    • First, if the file does not exist, it should be created; this is what the OpenSSH client does. Doing so early enough would prevent most instances of this issue.
    • In the sub-case where the file cannot be created (due to missing intermediate directories, or permissions issues, etc) OpenSSH emits a warning to stderr (Failed to add the host to the list of known hosts (/path/to/file)) but continues operation.
      • I reckon us doing sys.stderr.write() would be reasonable here too, though a log entry or using the warning module are other options.
  6. added this to the milestone on Oct 28, 2016
  7. modified the milestones: 1.17.3 et al, on Dec 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions