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:
proxyHostandproxyPort(22 when unset): the SSH server;proxyUsernameandproxyPassword: the SSH login;hostnameandport: the database, as the SSH server reaches it, e.g.127.0.0.1and3306for a database on the SSH server itself;sshKnownHosts: the SSH server’s host keys, inknown_hostsformat, e.g. the output ofssh-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:
objectAn 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
withblock,local_portforwards 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.known_hosts (
str|None) – The SSH server’s host keys, inknown_hostsformat.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:
- close()[source]
Stop forwarding, and close the listening socket and the SSH connection.
- Return type:
- forward(local)[source]
Forward one local connection to the remote host and port, through an SSH
direct-tcpipchannel.
- open()[source]
Connect to the SSH server, and start forwarding
local_port.- Return type:
- 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.
- exception smarter.apps.connection.ssh.SshTunnelError(message='')[source]
Bases:
SmarterValueErrorThe SSH tunnel couldn’t be opened: the server can’t be reached, its host key isn’t known, or the login was refused.