airflow.providers.ssh.tunnel

SSH tunnel implementations for the Airflow SSH provider.

This module provides SSHTunnel (sync, paramiko-based) and AsyncSSHTunnel (async, asyncssh-based) as replacements for the removed sshtunnel.SSHTunnelForwarder.

SSHTunnel reuses an already-connected paramiko.SSHClient from SSHHook.get_conn(), so all authentication and proxy configuration is inherited automatically. It binds a local TCP socket and forwards connections to a remote host/port through the SSH transport using open_channel('direct-tcpip', ...).

AsyncSSHTunnel wraps asyncssh.forward_local_port() and is intended for use with SSHHookAsync.

Migration from sshtunnel.SSHTunnelForwarder

Before:

from sshtunnel import SSHTunnelForwarder

tunnel = hook.get_tunnel(remote_port=5432)
tunnel.start()
# use tunnel.local_bind_port
tunnel.stop()

After:

with hook.get_tunnel(remote_port=5432) as tunnel:
    # use tunnel.local_bind_port

The .start() / .stop() methods still exist but emit deprecation warnings. Use the context manager interface instead.

Attributes

log

Classes

SSHTunnel

Local port-forwarding tunnel over an existing paramiko SSH connection.

AsyncSSHTunnel

Async local port-forwarding tunnel over an asyncssh SSH connection.

Module Contents

airflow.providers.ssh.tunnel.log[source]
class airflow.providers.ssh.tunnel.SSHTunnel(ssh_client, remote_host, remote_port, local_port=None, logger=None)[source]

Local port-forwarding tunnel over an existing paramiko SSH connection.

This replaces sshtunnel.SSHTunnelForwarder by using the SSH client’s transport directly via open_channel('direct-tcpip', ...).

The recommended usage is as a context manager:

client = hook.get_conn()
with SSHTunnel(client, "dbhost", 5432) as tunnel:
    connect_to_db("localhost", tunnel.local_bind_port)
Parameters:
  • ssh_client (paramiko.SSHClient) – An already-connected paramiko.SSHClient.

  • remote_host (str) – The destination host to forward to (from the SSH server’s perspective).

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

  • local_port (int | None) – Local port to bind. None means an OS-assigned ephemeral port.

  • logger (logging.Logger | airflow.sdk.types.Logger | None) – Optional logger instance. Falls back to the module logger.

property local_bind_port: int[source]

Return the local port the tunnel is listening on.

property local_bind_address: tuple[str, int][source]

Return ('localhost', <port>) — the local address the tunnel is listening on.

__enter__()[source]
__exit__(exc_type, exc_val, exc_tb)[source]
start()[source]

Start the tunnel. Deprecated — use the context manager interface instead.

stop()[source]

Stop the tunnel. Deprecated — use the context manager interface instead.

__getattr__(name)[source]
class airflow.providers.ssh.tunnel.AsyncSSHTunnel(ssh_conn, remote_host, remote_port, local_port=None)[source]

Async local port-forwarding tunnel over an asyncssh SSH connection.

This wraps asyncssh.SSHClientConnection.forward_local_port() and is intended for use with SSHHookAsync.

Usage:

async with await hook.get_tunnel(remote_port=5432) as tunnel:
    connect_to_db("localhost", tunnel.local_bind_port)

On exit, both the forwarding listener and the underlying SSH connection are closed.

Parameters:
  • ssh_conn (asyncssh.SSHClientConnection) – An asyncssh.SSHClientConnection.

  • remote_host (str) – The destination host to forward to.

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

  • local_port (int | None) – Local port to bind. None means an OS-assigned ephemeral port.

property local_bind_port: int[source]

Return the local port the tunnel is listening on.

async __aenter__()[source]
async __aexit__(exc_type, exc_val, exc_tb)[source]

Was this entry helpful?