Skip to content

Refactor SSH stuff into using paramiko. - #124

Closed
aszlig wants to merge 21 commits into
NixOS:masterfrom
aszlig:paramiko
Closed

aszlig wants to merge 21 commits into
NixOS:masterfrom
aszlig:paramiko

Conversation

@aszlig

@aszlig aszlig commented Aug 26, 2013

Copy link
Copy Markdown
Member

I know, this adds an additional dependency, but using Popen to execute OpenSSH directly, really is painful in a lot of cases where we have to implement things using a bunch of temporary files and dirty workarounds. Paramiko on the other side is a pure Python implementation and it's way easier to handle keys, passwords, file uploads, persistent sessions (not to mention host keys) and other stuff without the need to create temporary files or do really ugly and error-prone workarounds.

The only thing I've stumbled on so far which can't be used directly is nix-copy-closure, but even there, we could use nix-store --export/--import directly and connect the file instances.

Things to finish before it's ready for merge:

  • File copying.
  • Flags to ssh-for-each and ssh (what do people want here?)
  • Always allocate a PTY on nixops ssh.
  • Fix buffering issue on nixops ssh.
  • Provide more meaningful error messages.
  • Be more fault-tolerant so that we don't end up being in an unrecoverable state.
  • Properly control agent and X-forwarding.
  • Fix exception if stdin is a pipe/not a TTY.
  • Refactor SSH channel stdin/stdout/stderr processing.

@domenkozar

Copy link
Copy Markdown
Member

+1

Fabric (http://docs.fabfile.org/en/1.7/) uses paramiko extensively and it provides a wide featureset base for openssh interaction.

@aszlig

aszlig commented Aug 25, 2013

Copy link
Copy Markdown
Member Author

@iElectric: Yep, this is already work in progress and works so far for basic deployment (file transfer and more to come). Paramiko itself has support for OpenSSH keys and agent, so I don't see any reason to use Fabric.

@aszlig

aszlig commented Aug 26, 2013

Copy link
Copy Markdown
Member Author

Attached the branch to this issue, although - as said - it isn't finished yet.

So, this marks the start of moving from subprocess based OpenSSH calls
over to a more native approach, namely the Paramiko library.

Signed-off-by: aszlig <[email protected]>
We're going to refactor the class so let's begin by providing some basic
test cases and work on the implementation.

Obviously this makes running the test cases using setup.py fail and thus
the Hydra build product won't succeed as well.

In this new test case we're setting up a small dummp SSH server using
Paramiko, connect to the server and try various commands. Of course at
the moment, we only have two tests, because we need to replace
SSH.run_command().

Signed-off-by: aszlig <[email protected]>
This now should not need the Perl version anymore and will use calls to
nix-store directly instead of passing OpenSSH flags to an external
program.

Of course, this is only a basic version and it won't print the paths as
they're moved onto the target machine, but let's improve that later.

Signed-off-by: aszlig <[email protected]>
The SSH class now only is one main class with a connect() method that's
returning a SSHConnection instance. Of course this looks pointless right
now, but that way we can easily implement connection pooling later,
without those dirty hacks like register_*_fun(), which add unnecessary
references to the SSH instance and thus could prevent it from being
garbage collected.

In addition, we now no longer need the SSHMaster process stuff anymore
(which is one of the primary reasons for switching to Paramiko) and the
SSHConnection stays open for further requests until it is either closed
directly or by the pooling functionality (as soon as it's implemented).

Now the downside is that we need to pass more arguments tu methods of
SSHConnection, but that's actually a good thing because we can clearly
separate it from the rest of the code.

And of course, this implementation is neither correctly working at the
moment, nor is it complete. So we have more work ahead.

Signed-off-by: aszlig <[email protected]>
After reading from the stdin string or file instance, check whether the
send() call to the channel really sent all data. And if not, buffer up
data until we have sent everything.

That way we should no longer have gaps inside the input data and the
unit tests now succeed.

Signed-off-by: aszlig <[email protected]>
This happens only if we have stderr data that's bigger than the buffer
size and there is no data on stdout in-between.

Of course, also add tests to ensure something like this won't happen
again.

Signed-off-by: aszlig <[email protected]>
This methods automatically detects newlines and splits logging output
accordingly, so we don't have to fuzz around with log_start() and
log_end() outside of the Logger.

Signed-off-by: aszlig <[email protected]>
This should make SSH completely decoupled from anything that has to do
with Logger and we now have a more lightweight way of logging _and_
testing because we can even provide a function that just captures stderr
instead of actually logging it.

Signed-off-by: aszlig <[email protected]>
This is something which happens if you - for example - issue a reboot
command on the remote machine. There is no EOF happening and instead the
channel is just closed.

Signed-off-by: aszlig <[email protected]>
This now should get us into a (more or less) working state again and we
can actually deploy machines using the new implementation.

Signed-off-by: aszlig <[email protected]>
This allows for basic uploading and downloading of files, though not
recursively. It also provides an open() method, to manipulate files on
the remote side as if they were local.

Signed-off-by: aszlig <[email protected]>
The previous approach closed the SFTPClient too early. And before
creating hacky solutions for that, let's use a context manager here.

This should finally fix key uploading.

Signed-off-by: aszlig <[email protected]>
We're now setting non-blocking mode for sys.stdin, and resetting it on
cleanup/exit. This especially makes input escape sequences much more
responsive. Also, we're now reading on a per-char basis from the
channel, which works around buffering issues and needs to be properly
fixed later. But for now it does what we want.

Also removing the comment referencing Paramiko's demo code, because
we're already differing from the code and the difference will increase
during the work on the Paramiko integration.

Signed-off-by: aszlig <[email protected]>
This ends up being more code that we wish it to be, because we not only
have to install (and clean up) a handler for SIGWINCH, but also properly
detect interruption of the select system call (thus checking
select.error for errno 4, aka EINTR).

Signed-off-by: aszlig <[email protected]>
The fix is not really very nice, but we're going to refactor it anyway.

But the hardest part here was to properly write a unit test for this,
because we need to send a buffer large enough for the race condition to
happen. Also, we need to shutdown the write end as soon as possible, so
we exactly mimic the behaviour of OpenSSH.

Unfortunately I fear that in some cases (on veeery slow systems) the
test might succeed even without the fix(!), because we are concurrent
here by use of threading for server/client. In any case, I've put my
system under heavy load to make sure this won't happen, so I suppose the
test should be good enough.

Signed-off-by: aszlig <[email protected]>
This should make it way easier to use something like:

nixos ssh machine htop

We always used the logged command execution before which not only
clutters up stuff such as htop, but it also made no sense because the
commend is only to be used for single machines anyway.

Signed-off-by: aszlig <[email protected]>
This uses the pipes.quote() function, that is available and documented
in Python 3.2 as shlex.quote(). Although undocumented in 2.x it already
exists and probably will only vanish in 3.x or later, and that's an
entirely different story.

Signed-off-by: aszlig <[email protected]>
This now makes it possible to use something like:

nixops ssh-for-each ls -l /

Without getting an error about "-l" being an unknown commandline
argument. But of course, arguments _before_ the command are parsed.

Signed-off-by: aszlig <[email protected]>
@aszlig

aszlig commented Sep 17, 2013

Copy link
Copy Markdown
Member Author

Rebased against current master.

It just doesn't make sense to initiate a separate connection for every
single key, so let's do it in one shot. In the long term this probably
should be consolidated even more by caching/pooling SSH connections.

Signed-off-by: aszlig <[email protected]>
aszlig added a commit to NixOS/nixpkgs that referenced this pull request Oct 2, 2013
This patch should be backwards-incompatible and is also submitted
upstream as paramiko/paramiko#218.

The main reason for this patch is that we need it for NixOS/nixops#124
in order to cope with NixOS/nixops@a2718b6, which makes ECDSA private
key the default for new deployments.

Signed-off-by: aszlig <[email protected]>
We can still introduce the use of keys from ~/.ssh or the agent later,
but for now this behaviour is quite annoying (especially when debugging
connection issues), so let's turn it off by default.

Signed-off-by: aszlig <[email protected]>
This overrides the paramiko derivation to include a patch that allowsa
to use ECDSA private keys, which is the default in the current version
of NixOps.

Previously I've added the patch in NixOS/nixpkgs@58fdf34, but as it is
largely NixOps specific, I revented it und thus added it here directly.

Signed-off-by: aszlig <[email protected]>
@zimbatm

zimbatm commented Oct 22, 2013

Copy link
Copy Markdown
Member

Ansible has started with Paramiko and is now using OpenSSH by default. My impression was that it was mainly for compatibility and performance but I might be wrong. Just saying it might be worth having a look at what they're doing: https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/connection_plugins/ssh.py

@aszlig

aszlig commented Oct 22, 2013

Copy link
Copy Markdown
Member Author

By reading the change log and a bit of the history it doesn't look like they're moving away from Paramiko, especially if you look at ansible/ansible#4022. They seem to be using a modular approach and thus can fall back to either SSH/paramiko depending on what is available. I don't know a lot of details about ansible itself, but it looks like they do not need things such as password authentication and most of the keys are provided as files, so they don't need to create temporary files for those keys (and risk keys to be leaked by race conditions).

Of course this is only from my point of view in the NixOps camp, so I'm going to continue working on the branch and if we should need the OpenSSH implementation as a fallback, we can introduce it later anyway. I don't think it's a good idea to throw it away altogether in the first place. Thanks however for the pointer.

@zimbatm

zimbatm commented Oct 22, 2013

Copy link
Copy Markdown
Member

Yeah they're still supporting Paramiko but OpenSSH seems favored because it has more features. Here is an overview of their approach: http://www.ansibleworks.com/docs/intro_getting_started.html#id5

They have another connection type called "accelerate" which is a sort of ZeroMQ socket that they keep open.
Here is what they have to write on the subject:

Accelerated mode can be anywhere from 2-6x faster than SSH with ControlPersist enabled, and 10x faster than paramiko.
So I assume that paramiko is quite slow.

I was reading all their docs these past weeks so I just thought I'd drop some info here when I saw the issue. That's all I have for now :)

@domenkozar

Copy link
Copy Markdown
Member

What's the status of this? Paramiko 2.1 is now the latest version https://github.com/paramiko/paramiko/releases

@aszlig

aszlig commented Jan 25, 2017

Copy link
Copy Markdown
Member Author

@domenkozar: I lost interest in doing this, even though it was usable at the time. Maybe it's worth reinvestigating whether paramiko nowadays could be used as a complete OpenSSH replacement.

@aszlig aszlig closed this Jan 25, 2017
@coretemp

Copy link
Copy Markdown

I prefer to continue to use openssh, because I would test whether paramiko works by using openssh, which means I can just as well use openssh.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants