Skip to content

SSH

The daemon exposes SSH as an API: a request on the socket turns its connection into a tunnel to the SSH server of a sandbox. No port is published and no key is needed. This page covers the tunnel of the library and the SSH client of the system on top of it.

The socket is the boundary

The SSH server of a sandbox accepts any user, root included, without credentials: whoever holds a tunnel is root in the sandbox, and whoever reaches the socket can open one. A program that relays a tunnel further guards it as it guards the socket.

The tunnel

sandbox.ssh_tunnel() opens an SshTunnel, a stream of bytes to the SSH server; the sandbox must run. The server greets first, with a line starting with SSH-2.0-:

docs_src/client/ssh.py
import fastsandbox


async def main() -> None:
    client = await fastsandbox.connect()
    async with client.create(kit="shell") as sandbox, sandbox.running():
        # A byte tunnel to the SSH server of the sandbox, through the socket.
        async with await sandbox.ssh_tunnel() as tunnel:
            greeting = b""
            while b"\n" not in greeting and (data := await tunnel.read()):
                greeting += data
        print(greeting.decode().strip())


if __name__ == "__main__":
    fastsandbox.run(main())
Call What it does
read(size=65536) reads at most size bytes; b"" is the end of the stream
write(data) writes bytes
close() closes the tunnel, as leaving its async with block does, without masking the error of the block (see Leaving a block whose cleanup fails)
proxy_stdio() relays the tunnel to the standard input and output of the process

The SSH client of the system

fastsandbox ssh-proxy SANDBOX relays its standard input and output to the tunnel of a sandbox, which it keeps running meanwhile: the job of an OpenSSH ProxyCommand. The SSH client of the system logs in through it, as agent or as root for instance, the users the shell-ssh example logs in as:

ssh -o ProxyCommand="fastsandbox ssh-proxy my-sandbox" agent@my-sandbox uname -a

Or once for all, in ~/.ssh/config:

Host my-sandbox
    User agent
    ProxyCommand fastsandbox ssh-proxy %h

The SSH server of a sandbox presents a host key: the client asks to accept it, and refuses a key that changed under a name it knows, as it may when a sandbox of the same name is created again. sbx setup ssh writes a ProxyCommand of the sbx CLI into the SSH configuration of the user; fastsandbox ssh-proxy writes nothing.

Testing

The fake daemon of fastsandbox.testing runs no SSH server: it answers a tunnel with 501, unless a test adds the route and gives the bytes the tunnel reads (see Routes and limits). An SSH session with a real server is tested against the real daemon.