Skip to content

Speed up loading of HostKeys - #2254

Open
jack-stanek-hpe wants to merge 1 commit into
paramiko:mainfrom
jack-stanek-hpe:faster-hostkeys-loading
Open

jack-stanek-hpe wants to merge 1 commit into
paramiko:mainfrom
jack-stanek-hpe:faster-hostkeys-loading

Conversation

@jack-stanek-hpe

Copy link
Copy Markdown

Use a dict to handle non-hashed keys in the known_hosts file to speed up loading and access. Addresses #2251.

@bskinn bskinn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handful of questions/change requests.

Also, this should be pretty easy to add/augment tests for.

And, this will definitely need a CHANGELOG entry in /sites/www/changelog.rst.

Thanks for the PR!

Comment thread paramiko/hostkeys.py
Comment on lines +76 to +77
return self._hashed_entries
return self._entries[hostname]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this return the entirety of self._hashed_entries when hostname is hashed, instead of a specific subset of _hashed_entries related to hostname?

As opposed to the non-hashed case, where you do return a subset, per self._entries[hostname].

Is it because when things are hashed they are lumped into ._hashed_entries in a way that they can't be separated out by the hostname they relate to?

Adding a comment here on why this works this way would be good.


Additionally, I think you should edit into the HostKeys class docstring some of the description in the long paragraph of your #2251 (comment), to explain the paradigm of how the data is handled/stored.

That would then provide context for inline code comments here and elsewhere.

I'm guessing the class docstring will also need a stub .. versionchanged:: x.y directive added, since this is a substantial change in implementation. bitprophet would update to the correct version if/when he merges.

Comment thread paramiko/hostkeys.py
"""

def __init__(self, hostnames=None, key=None):
def __init__(self, hostnames=None, key=None, lineno=None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to document this addition of a lineno parameter, probably in the HostKeyEntry class docstring: What is it for? What is the meaning of the default value of None? Etc.

lineno will also need a .. versionadded:: x.y stub for bitprophet to complete.


Also, with this addition of lineno to the HostKeyEntry class, what's to stop a user from adding an entry programmatically and specifying lineno when they do so? Seems like this would inject the programmatic entry into the middle of the list of entries when exported with save().

Now -- that could be a useful feature! But it needs to be thought about; and, if determined to be desirable, documented and its implications thought through.


This class could also in general use better documentation -- what are the types of hostnames and key. hostnames looks like a list of str; I don't know the guts of paramiko well enough to know for sure what type key should be.... based on some of the code above, looks like a PKey?

Comment thread paramiko/hostkeys.py

entries = []
for e in self._entries:
for e in chain(self._entries[hostname], self._hashed_entries):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for e in chain(self._entries[hostname], self._hashed_entries):
# We have to search for both hashed and unhashed entries
for e in chain(self._entries[hostname], self._hashed_entries):

Comment thread paramiko/hostkeys.py
with open(filename, "w") as f:
for e in self._entries:
line = e.to_line()
all_entries = set(chain(*self._entries.values(), self._hashed_entries))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some risk of loss of information here? E.g., a collision between an otherwise identical hashed and unhashed value (this might not be possible); or, duplicate entries for a hostname (this might also not be possible)?

I think there's not any concern here, but my grip on the data structures is still a bit hazy so I want to raise the question.

@bskinn

bskinn commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

Also, looks like the CI errors were due to formatting issues. Blacken and those should go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants