Files
rhema-together/deploy/tunnel
vhaudiquet d85f68a176 feat: add reverse ssh sandbox targets
A sandbox behind NAT could not reach the room. A reverse target dials a tunnel-only sshd on the server, forwards localhost:22 to a loopback port, and the worker connects there.

The server assigns the port and writes one authorized_keys line per tunnel target. The key carries permitlisten for that port; the sshd allows remote forwarding only, with no shell or local forwarding. The tunnel private key is shown once and never stored; rotating it rewrites the file. The target list and setup dialog carry the command, and the docs include the sshd config and sidecar requirements.
2026-11-29 19:25:14 +01:00
..

Reverse SSH targets

A reverse SSH target is for a machine that cannot accept an inbound connection. The sandbox dials a tunnel-only sshd on the server, forwards its own localhost:22 to a loopback port, and the worker connects to that port as if the sandbox were local.

The app is the only writer of the tunnel sshd's authorized_keys file. For every reverse target it writes one line:

restrict,port-forwarding,permitlisten="127.0.0.1:22000" ssh-ed25519 AAAA... <target-id>

The key can bind that one port and nothing else. The sshd in this directory allows only remote forwarding, no shell, no local forwarding, and no non-loopback binds.

Host setup

  1. Create the tunnel account. It needs no shell:

    useradd --system --create-home --home-dir /var/lib/rhema-tunnel \
      --shell /usr/sbin/nologin tunnel
    # The app writes authorized_keys, so its uid owns the directory. The
    # sshd runs as root and can read the 0640 file.
    install -d -o 10001 -g 10001 -m 0750 /data/tunnel
    ssh-keygen -t ed25519 -N '' -f /data/tunnel/host_ed25519
    
  2. Point the app at the file and the public endpoint:

    TOGETHER_TUNNEL_AUTHORIZED_KEYS=/data/tunnel/authorized_keys
    TOGETHER_TUNNEL_SSH_HOST=together.example.com
    TOGETHER_TUNNEL_SSH_PORT=2222
    TOGETHER_TUNNEL_USER=tunnel
    TOGETHER_TUNNEL_PORT_START=22000
    TOGETHER_TUNNEL_PORT_END=22999
    

    The app creates the file on startup and rewrites it when a reverse target is added, deleted, or has its key rotated. The file is mode 0640; the tunnel sshd must be able to read it.

  3. Run the sshd in the same network namespace as the app:

    /usr/sbin/sshd -D -f /etc/ssh/sshd_config.tunnel
    

    In Kubernetes this is a sidecar container in the app pod, not a separate deployment and not the node's sshd. A separate pod has a different loopback, so the worker would connect to nothing. The app image does not ship sshd; use an sshd image and mount the data volume at /data read-only. The host key can live in an emptyDir at /data/tunnel-host with HostKey /data/tunnel-host/host_ed25519; authorized_keys comes from the app's volume.

  4. Expose port 2222 with a TCP load balancer, NodePort, or a separate TCP ingress. Do not route it through the HTTP ingress, and do not expose the host's port 22 in its place.

Adding a target

  1. In Settings, choose Reverse SSH, name the target, and enter the sandbox's ssh user and workspace root. The server assigns the loopback port.

  2. Copy the target's public key into the sandbox's ~/.ssh/authorized_keys. This is the key the worker uses after the tunnel is up.

  3. Save the tunnel private key shown once as rhema-tunnel-<target-id> in the directory where you run the command, then run chmod 600 on it.

  4. Run the command shown in the target's Setup dialog:

    autossh -M 0 -N -R 127.0.0.1:22000:localhost:22 -p 2222 \
      tunnel@together.example.com -i ./rhema-tunnel-<target-id> \
      -o ExitOnForwardFailure=yes -o ServerAliveInterval=30
    

    If autossh is not installed, ssh -N with the same options works; use a service manager or a reconnect loop to keep it alive.

  5. Press Test. The worker reaches 127.0.0.1:<port> and logs in with the target's stored key.

Limits

  • The sandbox must run an sshd on localhost:22. A different sshd port needs a different tunnel command; the target form does not model it.
  • The tunnel key is a credential. Delete the target or rotate the key if it leaks; both rewrite the authorized_keys file.
  • The tunnel sshd is internet-facing. Keep it patched, disable password auth, and rate-limit at the network edge. A successful login can still consume a port in the configured range, so alert on repeated failures.
  • The worker's normal host-key pinning still applies after the tunnel. The tunnel sshd has its own host key; ssh asks once and remembers it.