Skip to content

Add functions to auto-detect and load any private key type - #1558

Open
intgr wants to merge 2 commits into
paramiko:mainfrom
intgr:auto-detect-key-type
Open

intgr wants to merge 2 commits into
paramiko:mainfrom
intgr:auto-detect-key-type

Conversation

@intgr

@intgr intgr commented Nov 22, 2019 •

Copy link
Copy Markdown

Previously there was no way to auto-load any key type. Many downstream
users re-implemented this logic themselves, which is pointless
duplication and could easily become outdated when new key types are
added.

It's also likely that they will implement it badly, for example: https://github.com/tokoyami/pySCP/blob/c4c3ac7ec39965e0fe4274e8e43428a9210988c7/connector.py#L13-L29 :)


Two new functions were created:

  • load_private_key(key_str, password=None)
  • load_private_key_file(filename, password=None)

This functionality required a significant refactoring of key creation
code paths, but none of the public interfaces have changed.

@intgr
intgr force-pushed the auto-detect-key-type branch 2 times, most recently from acd67d6 to 8d69824 Compare November 25, 2019 09:41
@intgr

intgr commented Nov 25, 2019

Copy link
Copy Markdown
Author

Fixed line length and code style issues using black. Sorry, didn't notice those errors earlier.

@intgr
intgr force-pushed the auto-detect-key-type branch 2 times, most recently from ec836bd to de56ba9 Compare November 25, 2019 09:55
Comment thread paramiko/pkey.py Resolved
@intgr
intgr force-pushed the auto-detect-key-type branch 2 times, most recently from eb55e4d to 5c8feb8 Compare November 25, 2019 23:29
@intgr

intgr commented Nov 26, 2019 •

Copy link
Copy Markdown
Author

@ploxiln Is this feature something you would be interested in merging into paramiko-ng?

@ploxiln

ploxiln commented Nov 26, 2019

Copy link
Copy Markdown
Contributor

Yes ... but my fork has new-openssh-format support, and that new format has the key type string sort of in the middle. I may want more refactoring in order to avoid copying a significant chunk of the new-format parsing code.

(There is also a bit of round-about-ness converting between a file-object and a list of lines, which I might prefer some refactoring of... for example, load_private_key() wraps the key_str into a StringIO, and then load_private_key_from_file() calls .read() to get the whole thing as a str in memory again for type detection, and then the first thing _read_private_key() does is .readlines() which gets another full read in memory but into a list of line strings ... seems like the the list-of-lines-in-memory should be the native/internal type passed around and used directly in most places. Or something like that.)

@intgr

intgr commented Nov 27, 2019

Copy link
Copy Markdown
Author

It seems that for this feature to work without doing redundant work (doing multiple passes over the file content), it's necessary to split out significant parts of logic from the PKey class into stand-alone functions.

While reading the code, I didn't much like the ping-ponging of logic between PKey base class and its subclasses, so I think that's an improvement in general. Just making sure that you agree that's the way to go?

@ploxiln

ploxiln commented Nov 27, 2019

Copy link
Copy Markdown
Contributor

The helper methods on the PKey class are very similar to just having helper functions in pkey.py ... but they also organize and reference constants like _CIPHER_TABLE on self ... so to reduce the amount of churn, I think you could continue to use pretty-much-static methods. I don't think a big refactor is needed, just the bits at the beginnings of the functions, but the details are already different in the other fork so maybe the discussion should be there :)

@intgr
intgr force-pushed the auto-detect-key-type branch from 5c8feb8 to 5cfe07e Compare December 8, 2019 22:52
@intgr

intgr commented Dec 8, 2019 •

Copy link
Copy Markdown
Author

I rebased this on paramiko master; merging #1343 invalidated the old patch.

The new work is mostly based on ploxiln#57 but still has some rough edges. In particular, #1567 should be merged before this. Until then, some tests are broken.

@bitprophet

bitprophet commented Dec 9, 2019 •

Copy link
Copy Markdown
Member

Thanks for this, I like the overall idea here, though I haven't yet read the entire patch in depth. Basic thoughts:

  • I've become a fan of classmethod alt constructors, so we might want to make the module level functions into those (alongside the newly classmethod-ized subroutines). Eg PKey.autoload().
    • and maybe combine the 'string vs filepath' versions into a single call w/ kwargs, though I'm less sure about that as it's a tradeoff (more complexity in arg handling).
    • but I've never been a fan of the original codebase's naming here (why is one method implicit about what type it takes, and why is the other ambiguous about file paths vs FLOs?) so I figure we should take this opportunity to not perpetuate it.
  • I'd like to make it clear to readers of the API docs (and changelog) that the autoload does incur the cost of parsing the key twice (even though it does not load from disk twice).
    • The majority of users won't care but there will always be some that do, especially with this particular library...
  • Might be nice to have some basic design-level docs about the key classes and this autoloading, probably as prose at the top of the API doc stub that currently just collates the key class' module autodocs.
    • This is something I can definitely do as I'm super picky about prose 😩
    • But this can probably wait for Key handling is terribad #387 so we'll see.
  • Does need a changelog entry, but again, I can handle that. Just noting it for future PRs :)

@intgr

intgr commented Dec 10, 2019

Copy link
Copy Markdown
Author

My initial thoughts:

I've become a fan of classmethod alt constructors, so we might want to make the module level functions into those (alongside the newly classmethod-ized subroutines). Eg PKey.autoload().

The reason I didn't want to do it like that is because that would create a circular dependency between PKey and each of its subclasses. That just seems like a bad design.

I'm open to renaming this API but I don't particularly like the name autoload, since that puts emphasis on the "auto". I think this new API should be the one and only right way of loading private keys going forward. There is little reason for a user to instantiate a key class directly. Even if they want to apply some policy (e.g. only allow RSA and ECDSA keys), that can be just as nicely done after the key is loaded.

(I also dislike how paramiko mixes private and public keys in one class. Lumping more stuff into one class only makes that worse. Although the breakage from "fixing" that is probably not worth it)

the autoload does incur the cost of parsing the key twice (even though it does not load from disk twice).

It shouldn't. The load_private_key function does the parsing and then passes the parsed data to the correct class via the newly-introduced _raw= argument. Can you point out anything that's done twice?

@intgr

intgr commented Dec 11, 2019

Copy link
Copy Markdown
Author

I created a separate PR #1571 to unify duplicated logic in Ed25519Key, that refactor doesn't really belong in this PR.

The Ed25519Key class contained key parsing and decryption logic for
OPENSSH-format keys before that code became generic. Now that paramiko#1343 is
merged, this logic becomes redundant and can be removed.

This cleanup was extracted from
ploxiln#13 and the original author
@ploxiln is credited as commit author even though I (@intgr) am
submitting this pull request.

Signed-off-by: Marti Raudsepp <[email protected]>
@intgr
intgr force-pushed the auto-detect-key-type branch from 255b95a to 82e69f0 Compare December 11, 2019 22:21
Two new functions were created:
* load_private_key(key_str, password=None)
* load_private_key_file(filename, password=None)

Previously there was no way to auto-load any key type. Many downstream
users re-implemented this logic themselves, which is pointless
duplication and could easily become outdated when new key types are
added.

This functionality required a significant refactoring of key creation
code paths, but none of the public interfaces have changed.

Signed-off-by: Marti Raudsepp <[email protected]>
@intgr
intgr force-pushed the auto-detect-key-type branch from 82e69f0 to 15522b9 Compare December 11, 2019 22:24
@Apreche

Apreche commented May 12, 2020

Copy link
Copy Markdown

I see this hasn't been worked on in a few months. Wondering what can be done to move it along. Right now I'm going to have to ask users of our application to manually input the type of key they are using. Not an ideal solution.

@intgr

intgr commented May 13, 2020

Copy link
Copy Markdown
Author

This PR depends on the refactoring in #1571 -- getting that merged would be the first step. @bitprophet Have you had a chance to look at it?

@intgr

intgr commented Aug 3, 2020

Copy link
Copy Markdown
Author

@Apreche I was recently made aware of this post by bitprophet (maintainer of Paramiko): http://bitprophet.org/blog/2020/07/02/help-wanted/

So maybe if you or someone else steps up to review #1571 and this PR, it can be sped up. Although few people are familiar with the project's coding style, I'm not sure how helpful code review by independent developers could be.

@Apreche

Apreche commented Aug 3, 2020

Copy link
Copy Markdown

@intgr I'd love to help with this, but I'm not sure how much help I can be. The PRs look fine to me from a Python perspective, but I have no familiarity with the projects themselves or their architecture to make any further judgements.

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.

4 participants