SSH Tunnel

An SSH tunnel to a database, for a SqlConnection whose authenticationMethod is tcpip_ssh.

A database that isn’t reachable from the internet is often reachable through an SSH server, a “bastion”, that is. SshTunnel connects to that server, and forwards a port on this machine’s loopback interface to the database’s host and port, as the SSH server reaches them. The database driver then connects to the loopback port, and every byte it sends travels through the SSH connection. This is what MySQL Workbench and DBeaver call “Standard TCP/IP over SSH”.

A SqlConnection describes it with fields it already had:

  • proxyHost and proxyPort (22 when unset): the SSH server;

  • proxyUsername and proxyPassword: the SSH login;

  • hostname and port: the database, as the SSH server reaches it, e.g. 127.0.0.1 and 3306 for a database on the SSH server itself;

  • sshKnownHosts: the SSH server’s host keys, in known_hosts format, e.g. the output of ssh-keyscan -p 22 bastion.example.com.

Host keys. The SSH server’s host key must be one of sshKnownHosts. An unknown or changed key is refused, so that a server in the middle can’t read the database’s traffic or its password. A key is never learned on first use: the connection’s owner adds it, once, to the manifest.

The tunnel needs no database and no Smarter Secret of its own: the SSH password comes with its settings, decrypted by the app, so it runs wherever the query runs (ADR-038).

class smarter.apps.connection.ssh.SshTunnel(ssh_host, ssh_port, username, password, known_hosts, remote_host, remote_port, timeout=30)[source]

Bases: object

An SSH tunnel from a port on this machine’s loopback interface to a remote host and port.

Use it as a context manager. Inside the with block, local_port forwards to the remote host and port, through the SSH server. On exit, the forwarded connections, the listening socket and the SSH connection are closed.

with SshTunnel("bastion.example.com", 22, "deploy", password, known_hosts, "127.0.0.1", 3306) as tunnel:
    connect_to_database(host="127.0.0.1", port=tunnel.local_port)
Parameters:
  • ssh_host (str) – The SSH server.

  • ssh_port (int) – Its port.

  • username (str | None) – The SSH login.

  • password (str | None) – Its password.

  • known_hosts (str | None) – The SSH server’s host keys, in known_hosts format.

  • remote_host (str) – The host to forward to, as the SSH server reaches it.

  • remote_port (int) – The port to forward to.

  • timeout (float) – Seconds to wait for the SSH server.

__init__(ssh_host, ssh_port, username, password, known_hosts, remote_host, remote_port, timeout=30)[source]
accept_connections()[source]

Accept connections to local_port, and forward each through its own SSH channel, until the tunnel closes.

Return type:

None

close()[source]

Stop forwarding, and close the listening socket and the SSH connection.

Return type:

None

forward(local)[source]

Forward one local connection to the remote host and port, through an SSH direct-tcpip channel.

Parameters:

local (socket) – The accepted local connection.

Return type:

None

open()[source]

Connect to the SSH server, and start forwarding local_port.

Return type:

SshTunnel

Returns:

This tunnel.

Raises:

SshTunnelError – If the SSH server’s host key isn’t in known_hosts, the login is refused, or the server can’t be reached.

pump(local, channel)[source]

Copy bytes both ways between a local connection and its SSH channel, until either end closes.

Parameters:
  • local (socket) – The local connection.

  • channel (Channel) – Its SSH channel.

Return type:

None

exception smarter.apps.connection.ssh.SshTunnelError(message='')[source]

Bases: SmarterValueError

The SSH tunnel couldn’t be opened: the server can’t be reached, its host key isn’t known, or the login was refused.

smarter.apps.connection.ssh.host_keys_from_text(known_hosts)[source]

Parse known_hosts text into paramiko’s host keys.

Blank lines, comments and lines that paramiko can’t read are skipped.

Parameters:

known_hosts (str | None) – The text, e.g. the output of ssh-keyscan.

Return type:

HostKeys

Returns:

The host keys.