Skip to content

Smartcard pkcs11 support - #827

Closed
thedavidwhiteside wants to merge 38 commits into
paramiko:final-827-intfrom
thedavidwhiteside:smartcard_pkcs11
Closed

thedavidwhiteside wants to merge 38 commits into
paramiko:final-827-intfrom
thedavidwhiteside:smartcard_pkcs11

Conversation

@thedavidwhiteside

@thedavidwhiteside thedavidwhiteside commented Oct 13, 2016 •

Copy link
Copy Markdown

This adds pkcs11 support to enable using paramiko with smartcards. I have a forked version of Ansible that uses this feature and its working great.

I have tested on python 2.7 and python 3.6.2

Multithreading Example:

import paramiko
from multiprocessing import Queue
from threading import Thread

pkcs11provider="/usr/local/lib/opensc-pkcs11.so"
smartcard_pin="123456"

def do_it(q):
    session = q.get()
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect("HOSTNAME", username="USERNAME", pkcs11_session=session)
    stdin, stdout, stderr = ssh.exec_command("uname -a")
    for line in stdout:
        print(line)

q = Queue()
session = paramiko.pkcs11.open_session(pkcs11provider, smartcard_pin)
mythread = Thread(target=do_it, args=(q,))
q.put(session)
mythread.start()
mythread.join()
paramiko.pkcs11.close_session(session)

Basic Example:

import paramiko

pkcs11provider="/usr/local/lib/opensc-pkcs11.so"
smartcard_pin="123456"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = paramiko.pkcs11.open_session(pkcs11provider, smartcard_pin)
ssh.connect("HOSTNAME", username="USERNAME", pkcs11_session=session)
paramiko.pkcs11.close_session(session)
stdin, stdout, stderr = ssh.exec_command("uname -a")
for line in stdout:
    print(line)

@coveralls

coveralls commented Oct 13, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-2.5%) to 71.737% when pulling 5555a02 on starboarder2001:smartcard_pkcs11 into 7366f96 on paramiko:master.

@coveralls

coveralls commented Oct 13, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.6%) to 72.571% when pulling 42f51ef on starboarder2001:smartcard_pkcs11 into 7366f96 on paramiko:master.

@coveralls

coveralls commented Oct 13, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.6%) to 72.57% when pulling 4a6489c on starboarder2001:smartcard_pkcs11 into 7366f96 on paramiko:master.

@coveralls

coveralls commented Oct 13, 2016 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-1.6%) to 72.584% when pulling 902215f on starboarder2001:smartcard_pkcs11 into 7366f96 on paramiko:master.

@Ovomaltine85

Copy link
Copy Markdown

Hi, great work! I'm actually waiting for this feature.
Can you tell me when it becomes part of a new release of paramiko?
Thank you!

@thedavidwhiteside

thedavidwhiteside commented Jun 2, 2017 •

Copy link
Copy Markdown
Author

@bitprophet is there anything I can do to help with the "needs tests" and "needs docs"? Are you looking for more unit tests? Where would you like this feature documented, is this where it should be documented: sites/docs/api?

Thanks!

@sethmlarson

Copy link
Copy Markdown

You should probably add an entry to the change log. :)

@bitprophet

Copy link
Copy Markdown
Member

@starboarder2001 Yea, something in the tests/ folder would be grand (hopefully there are existing tests in roughly the same area, re: Client.connect parameters), and docs, I think for now it'd be sufficient to update the docstring for Client.connect so it mentions that the new params exist and what they're for. (Typically, anytime you modify a function's API, you should be modifying the nearby docstring as well :))

Also what @SethMichaelLarson said, tho changelog is always something I can add myself (I am picky about language...)

Thanks!

@bitprophet

Copy link
Copy Markdown
Member

Also, you'll want to make sure the merge conflicts are fixed, we pep8'd the crap out of the codebase recently which may be what caused those.

@thedavidwhiteside

thedavidwhiteside commented Jun 6, 2017 •

Copy link
Copy Markdown
Author

I think its in pretty good shape. I updated the changelog (feel free to reword as needed), docstrings for Client.connect, and made sure the code passed all the pep8 checks.

@thedavidwhiteside

thedavidwhiteside commented Jun 20, 2017 •

Copy link
Copy Markdown
Author

Trying to help out the best I can on this one, is there anything I can do to help this along? I have been using this feature along w/ Ansible for about 9 months.

@ploxiln

ploxiln commented Jun 20, 2017

Copy link
Copy Markdown
Contributor

There was a big round of merging and releasing about a week ago (as you probably noticed), and it reduced the backlog noticeably. The maintainer will probably do another around in a few weeks. I think there is a decent chance this PR will eventually get in, if you keep it up-to-date :)

I think it would help a bit to squash down to just one commit (or a few). This will require a careful interactive rebase, re-resolving some merge conflicts, and a force-push. Since the "flake8 re-style" is behind us, I don't expect this much tedium being needed again.

Comment thread paramiko/auth_handler.py Outdated
def _pkcs11_get_public_key(self):
public_key = ""
if self.pkcs11session is None:
if which("pkcs15-tool") is 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.

I think you should remove the which() function you added. Just catch the subprocess.Popen() exception for this case (an OSError with .errno equal to https://docs.python.org/2/library/errno.html#errno.ENOENT). Then you can re-throw your more specific Exception("pkcs15-tool not found in PATH") (I wouldn't include the extended instructions in the exception text).

Comment thread paramiko/client.py Outdated
:param str pkcs11provider: If using PKCS11, this will be the provider
for the PKCS11 interface. Example: /usr/local/lib/opensc-pkcs11.so.
:param str pkcs11session: If using PKCS11 in a multithreaded
application you can share the session between threads.

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.

Can this be reduced to just one additional argument to SSHClient.connect()? Perhaps the user could be required to call pkcs11_open_session() themselves, then pass just the session to connect()?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

For most cases the pkcs11provider is all thats needed, very few users will need to use the pkcs11session argument. pkcs11session is optional and only needed when used in a multithreaded application.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I second @ploxiln's question - I think what he's getting at is it's unclear what the relationship is between these arguments & the data stored in the session - which, w/o doing a deep analysis, seems to largely be a 3-tuple whose members include the pin and provider anyways.

So if we can simplify the API on our end to "you should have called pkcs11_open_session() yourself, please hand me the resulting session obj/tuple" that cleans up a lot of intermediate code in an already-very-crowded space.

@ploxiln

ploxiln commented Jun 20, 2017 •

Copy link
Copy Markdown
Contributor

In some cases, where you raise a Exception (with explanation text), it might be better to use a more specific type from paramiko.ssh_exception e.g. AuthenticationException. (Each case would require some thought, which I haven't done yet :)

Comment thread paramiko/pkcs11.py Outdated
public_key = out
except OSError as error:
if error.errno == errno.ENOENT:
raise SSHException("Cannot find pkcs15-tool in PATH.")

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.

If not translating to this exception, you still need to re-raise the original exception, e.g. just

else:
    raise

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, good point! I'll do more review and testing tomorrow.

@thedavidwhiteside
thedavidwhiteside force-pushed the smartcard_pkcs11 branch 3 times, most recently from 5af5cde to 73ef6e1 Compare June 21, 2017 22:14
@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #827 into master will decrease coverage by 1.51%.
The diff coverage is 18.62%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #827      +/-   ##
==========================================
- Coverage   75.25%   73.74%   -1.52%     
==========================================
  Files          42       43       +1     
  Lines        7449     7645     +196     
==========================================
+ Hits         5606     5638      +32     
- Misses       1843     2007     +164
Impacted Files Coverage Δ
paramiko/__init__.py 97.29% <100%> (+0.07%) ⬆️
paramiko/client.py 66.17% <14.28%> (-1.38%) ⬇️
paramiko/auth_handler.py 50.92% <26%> (-6.1%) ⬇️
paramiko/transport.py 79.61% <9.09%> (-0.58%) ⬇️
paramiko/pkcs11.py 9.52% <9.52%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 842caba...73ef6e1. Read the comment docs.

@ploxiln

ploxiln commented Jun 21, 2017

Copy link
Copy Markdown
Contributor

73ef6e1 contains a bunch of changes from master. Merging with master hides this from the diff in the "Files Changed" tab, but does not fix the problem.

(When you squashed, you needed to ensure you did not include commits you did not author.)

@thedavidwhiteside
thedavidwhiteside force-pushed the smartcard_pkcs11 branch 2 times, most recently from 46b8278 to e07a4e2 Compare June 21, 2017 23:07
@thedavidwhiteside

Copy link
Copy Markdown
Author

I think the last rebase worked ok.

@ploxiln

ploxiln commented Jun 22, 2017

Copy link
Copy Markdown
Contributor

yup, looks good, thanks

@thedavidwhiteside
thedavidwhiteside force-pushed the smartcard_pkcs11 branch 4 times, most recently from 8618efa to 8f58137 Compare September 14, 2017 21:34
@thedavidwhiteside

thedavidwhiteside commented Sep 14, 2017 •

Copy link
Copy Markdown
Author

I think its all squared away this time. Unit tests passed after the refactor, I have also tested using a SC.

  • used final-827-int as a reference, hopefully I got most of what was pkcs11 specific in there.
  • switched to new .format()
  • avoided using \ to break strings, used # noqa
  • removed any pkcs11 prepends to the name of methods, etc, this was in there from where I developed some of this separate from paramiko and in my lazyness I just left it. Its cleaned up now.
  • fixed docstrings in pkcs11.py, added sphinx documentation for pkcs11 (hopefully its in the right place :/) so the links would work, pulled most of the documentation from final-827-int

@thedavidwhiteside
thedavidwhiteside force-pushed the smartcard_pkcs11 branch 6 times, most recently from 67d6631 to b49ce80 Compare September 15, 2017 16:30
@bitprophet

Copy link
Copy Markdown
Member

Hm, it doesn't seem like you actually based the new work on my branch, so it undoes a bunch of the refactoring & other modifications I made in said branch :( Really what I was asking as for you to checkout that branch of mine (final-827-int) & make more modifications directly on top of it.

That may involve you having to make a new PR, but if so, that's totally fine - I'd rather be able to just click merge than have to resolve all these conflicts myself =/

@bitprophet bitprophet modified the milestones: 2.3.0, 2.4.0 Sep 18, 2017
@thedavidwhiteside

Copy link
Copy Markdown
Author

I can use the same PR. I'll see if I can get to it after work sometime this week, shouldn't be much trouble.

  • David

@bitprophet

Copy link
Copy Markdown
Member

Yea, again don't care too much about how it looks at the Github level, so long as when I pull some branch from you to check it over, it includes final-827-int such that I don't have merge conflicts or removals of the stuff I committed. Thanks! (And no rush on my part, it'll be a bit before I get to working on Paramiko 2.4.)

@thedavidwhiteside
thedavidwhiteside changed the base branch from master to final-827-int September 23, 2017 01:48
@thedavidwhiteside
thedavidwhiteside deleted the smartcard_pkcs11 branch September 23, 2017 01:48
@thedavidwhiteside

Copy link
Copy Markdown
Author

Trying to determine how to re-open it...

@thedavidwhiteside

Copy link
Copy Markdown
Author

I ended up just creating another pull request...#1073

@thedavidwhiteside
thedavidwhiteside changed the base branch from final-827-int to master September 23, 2017 02:04
@thedavidwhiteside
thedavidwhiteside changed the base branch from master to final-827-int September 23, 2017 02:04
@bitprophet bitprophet removed this from the 2.4.0 milestone Sep 25, 2017
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.

8 participants