Skip to content

Mux transport for client - #1341

Closed
radssh wants to merge 23 commits into
paramiko:mainfrom
radssh:Mux-transport-for-client
Closed

radssh wants to merge 23 commits into
paramiko:mainfrom
radssh:Mux-transport-for-client

Conversation

@radssh

@radssh radssh commented Nov 25, 2018

Copy link
Copy Markdown
Contributor

Draft implementation for #852

Seems to work fine for normal open_session management, some quirks with port forwarding and global request/response handling, and catastrophic behavior if renegotiate_keys is called when in proxy mode :)

Some guesswork needed, as the OpenSSH client uses "passenger mode" instead of "proxy mode", but proxy mode seems a much better fit for paramiko, since the SSH protocol messages are already implemented, and the proxy mode only required a small set of changes to the packetizing (zero padding failed to work due to modulo calculation)

Supply a (new) controlpath parameter to Transport() or SSHClient(), and domain socket connection is attempted. If successful, it confirms that is is talking to a ControlMaster server, and switches the ControlMaster session into Proxy mode. The Transport then behaves as an authenticated transport, skipping the initial hostkey validation, key exchange, and user auth.

radssh added 22 commits August 21, 2015 19:18
Basic support - Transport connect via Unix domain socket, and bypass the protocol/kex/userauth stages, and just use proxy. Some jiggling of the packetizer pad byte calculations
ControlMaster proxy seems to forward global_request messages, but swallows the global_response, so the proxied client should never wait for the response. For tcpip-forward, the client can't distinguish between success and failure.
kex_init is even more borky, it seems. The proxy forwards the initial kex_init, but fields the server reply as a server initiated kex_init, so the ContolMaster sends another kex_init in response. The server, upon getting a kex_init message while already processing the proxied one, boots the connection altogether (per RFC4253 7.1)
Rather than complicate the retry logic, have Transport raise exception, and allow retry only within SSHClient.
Change mux_path to controlpath, to be more consistent with OpenSSH terminology
flake8 & black format issues
@krsfrodaslz

krsfrodaslz commented Dec 14, 2018 •

Copy link
Copy Markdown

For anyone who got "unsupported request" error, you need openssh-client 7.4p1 or newer.

plus a typo I'm just going to attribute to spellcheck
@radssh

radssh commented Dec 14, 2018

Copy link
Copy Markdown
Contributor Author

For anyone who got "unsupported request" error, you need openssh-client 7.4p1 or newer to work.

Good catch - I did not realize this was a relatively recent addition to the mix protocol, and a bit surprised that the protocol version number didn't get bumped along with that change so it could be caught cleaner than that. Added that detail to the exception message.

@mjtravers

Copy link
Copy Markdown

This works great. Any thought to adding the initialization/management of the ControlMaster connection and ControlPath file via paramiko rather than relying one that is already up and running?

@radssh

radssh commented Dec 21, 2018

Copy link
Copy Markdown
Contributor Author

The mux server side implementation can be done, but is going to be a significant amount of work. It probably needs to be a full implementation covering both proxy mode (which I used in the client implementation here) as well as passenger mode, that makes use of directly passing file handles, that is supported by the Python3 standard library, but would need another dependency added for Python2 support.

@t2y

t2y commented Feb 7, 2019

Copy link
Copy Markdown

I confirmed this patch works for me. It saved me from annoyance. Great! 👍

@krsfrodaslz

Copy link
Copy Markdown

The shared connection closed periodically when I was using this PR. It does not happen every time but if you keep trying (for about 5 times or more), ssh will disconnect itself from the server. Here is the error message,

debug3: mux_client_read_packet: read header failed: Broken pipe

I tried to find the cause but with no luck. My program exited normally and no exception was thrown.

FYI, I created a context manager to close the client automatically.

def ssh_to(host):
    import paramiko
    ssh_config = paramiko.config.SSHConfig()
    with open(osp.join(HOME, '.ssh', 'config')) as f:
        ssh_config.parse(f)
    host_config = ssh_config.lookup(host)

    controlpath = osp.expanduser(host_config['controlpath'])
    if not osp.exists(controlpath):
        controlpath = None

    ssh_client = paramiko.client.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy)
    ssh_client.connect(
            host_config['hostname'],
            port=host_config['port'],
            username=host_config['user'],
            controlpath=controlpath
    )
    yield ssh_client
    ssh_client.close()

# then use it
with ssh_to('x.x.x.x') as ssh_client:
    ssh_client.exec_command('ls')

@krsfrodaslz

Copy link
Copy Markdown

@radssh Please take a look at the log.
paramiko.log

@radssh

radssh commented Mar 13, 2019

Copy link
Copy Markdown
Contributor Author

At a glance, the paramiko log (and the ssh client log for the ControlMaster) seem to indicate that the connection is getting dropped. I will do some code testing, based on the log, using the sftp for file transfers.

I tried your earlier code sample, and it looks like your context manager isn't really a context manager. I'm getting AttributeError about missing __enter__(Python3) or __exit__ (Python2) methods. Since your log shows you have something working with the SSHClient, I'll skip trying to test with your context manager code for now.

@krsfrodaslz

krsfrodaslz commented Mar 13, 2019 •

Copy link
Copy Markdown

At a glance, the paramiko log (and the ssh client log for the ControlMaster) seem to indicate that the connection is getting dropped. I will do some code testing, based on the log, using the sftp for file transfers.

I tried your earlier code sample, and it looks like your context manager isn't really a context manager. I'm getting AttributeError about missing __enter__(Python3) or __exit__ (Python2) methods. Since your log shows you have something working with the SSHClient, I'll skip trying to test with your context manager code for now.

My bad, I forgot to paste some import statements. Yeah, I'm using sftp to transfer files because the bastion host says ssh2. I tried doing the same task using command line tools and the error never showed. So I guess there might be something wrong with paramiko. I've uploaded my real program. My shared connection gets closed each time I run it. I changed its extension since Github does not accept .py. You have to modify some variables' values in the main function, otherwise the program won't work. Thanks for your help. I really appreciate it.

sync.txt

@bauen1

bauen1 commented Aug 7, 2020

Copy link
Copy Markdown

I would love to see this merged, even if only the ability to piggyback onto an existing socket.
This would make using pyinfra with ssh jump hosts that e.g. require multi factor authentication a lot easier and potentially faster.

@rupertnash

Copy link
Copy Markdown

I just tried this and it works fine to connect, however when Paramiko disconnects it also hangs up the master.

Afraid I know little about ssh and networking so can't contribute to the patch

@takluyver

Copy link
Copy Markdown

I have rebased this as #1857.

What do people think about having a separate method for connecting to a multiplexing socket, rather than an extra parameter to the connect() method? This would mean a couple of extra lines of code if you want to fallback to opening a new connection, but leaves the caller the choice of not falling back like that.

@bitprophet

Copy link
Copy Markdown
Member

Rolling into #1931.

@bitprophet bitprophet closed this Mar 12, 2022
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.

8 participants