Introducing a new way to review your PRs.

Read the announcement

hcavarsan / kftray

kubectl port-forward manager and reverse tunnel (ngrok-like) for exposing local services publicly, with TLS termination, HTTP traffic inspection, UDP forwarding, multi-hop proxy routing through k8s clusters, stateful config via filesystem or git - GUI and TUI available

View on GitHub

19 documents · Updated Nov 23, 2026

docs / ssh-forwarding

SSH forwarding

SSH forwarding gives a local TCP client access to a remote TCP service through an SSH route. A saved ssh-port-forward configuration selects the destination and provider, while the connection system builds an authenticated SSH session, checks the host identity, and maps each local connection to a separate SSH channel.

Forwarding model

The SSH path starts at the normal connection-management entry point and ends at a local listener. The listener accepts local clients, the SSH session reaches the destination from the bastion network, and the forward remains part of the same start, stop, retry, and status lifecycle as other connection types. See the connection-forwarding parent page for the wider lifecycle, Kubernetes port forwarding for the cluster-based path, and endpoint exposure for the separate public exposure path.

The path has these main roles:

RoleResponsibility
SSH configurationNames the provider, bastion target, remote host, remote port, and local endpoint.
Provider routeResolves the bastion and prepares transport, login, and host-key rules.
SSH runtimeShares one session for the same route and creates channels for local clients.
Local listenerConverts each accepted TCP connection into one SSH channel and copies bytes in both directions.
Lifecycle managerRegisters the forward, reports session state, and cleans up failed or stopped forwards.

This diagram shows how a local connection becomes traffic to a private destination.

flowchart LR
    C[SSH forward configuration] --> R[Provider route]
    R --> S[Authenticated SSH session]
    S --> L[Local TCP listener]
    L -->|one channel per client| D[Remote host and port]
    S --> M[Connection lifecycle]
    M --> L

References: crates/…/start.rs• L1384-1406 crates/…/mod.rs• L164-234 crates/…/listener.rs• L49-56

SSH configuration

An SSH forward is a TCP workload with workload_type set to ssh-port-forward. It does not use a Kubernetes service, pod, context, or namespace as its destination. The configuration instead supplies a provider object plus remote_address and a non-zero remote_port.

The current provider is gcp-bastion. Its configuration identifies a Compute Engine VM and its network interface. The interface defaults to nic0, so exports can omit that value when it is not changed.

ConfigurationPurpose
workload_typeSelects the SSH forwarding path.
protocolMust be tcp.
ssh_provider.typeSelects the provider, currently gcp-bastion.
project, zone, instanceIdentify the GCP bastion VM.
interfaceSelects the VM network interface for the IAP connection.
remote_address, remote_portIdentify the service that the bastion must reach.
Local address and port fieldsSelect where the local listener accepts clients.
alias, domain_enabledAllow the normal local domain alias feature for the forward.

The configuration boundary rejects an unsupported provider, invalid provider values, a missing destination, or a non-TCP protocol before the SSH session starts. An unknown provider is preserved when configuration is read, but it cannot build a route in the current version.

References: crates/…/config_model.rs• L78-125 crates/…/config_model.rs• L209-225 crates/…/ssh_provider.rs• L10-34 crates/…/mod.rs• L91-120 crates/…/config.rs• L54-129 examples/ssh-gcp-bastion.json• L3-17

Provider route

The provider route is the bridge between a saved configuration and the generic SSH engine. For GCP, route creation checks the caller identity and inspects the VM before it opens the IAP tunnel. It then supplies the engine with the IAP transport, OS Login authentication, and the VM host-key source.

The GCP path uses these Google services:

  • Application Default Credentials provide the caller identity and access token.
  • Token information provides the email used as the OS Login user.
  • Compute Engine inspection confirms the VM state and gathers route details.
  • OS Login supplies the SSH certificate or key material needed for login.
  • IAP TCP forwarding carries the SSH stream to port 22 on the selected VM interface.

The provider context is shared by forwards. This avoids rebuilding credential and HTTP state for every local connection, while failed context creation is not kept so a later attempt can use newly available credentials.

References: crates/…/providers.rs• L14-57 crates/…/mod.rs• L1-3 crates/…/mod.rs• L29-62 crates/…/route.rs• L20-65 crates/…/endpoints.rs• L8-29

Authentication responsibilities

SSH authentication has two separate trust decisions. The provider authenticates the caller to Google and creates the SSH login material. The SSH engine then verifies the VM host key before it accepts that login and opens channels.

The engine keeps one kftray Ed25519 key for the process and store. A provider may use that key directly or return a short-lived SSH certificate for it. The GCP provider uses the caller email as the SSH user and obtains credentials through OS Login. Authentication errors are distinct from transport errors, so a bad login does not trigger the same recovery as a temporary network loss.

Host identity follows this order:

  1. If the provider publishes trusted VM keys, the presented key must match one of them.
  2. If it publishes no key, the first presented key is stored under the provider alias.
  3. Later connections must present the stored key.
  4. A changed key fails the connection until an operator explicitly forgets the stored key.

The host-key store uses the ssh_host_keys database table. The SSH test command can report the known and presented fingerprints, and the forget command removes the stored entry for the route.

References: crates/…/connect.rs• L116-214 crates/…/key.rs• L23-40 crates/…/auth.rs• L25-29 crates/…/route.rs• L68-91 crates/…/host_keys.rs• L37-70 crates/…/host_key_store.rs• L15-61 crates/…/ssh_host_keys.rs• L11-18 crates/…/mod.rs• L501-539 crates/…/ssh.rs• L9-20

Session and channel lifecycle

The SSH runtime pools sessions by route identity. Forwards that use the same bastion, destination route, and login identity share one authenticated SSH session, while each local client receives its own direct-tcpip channel. This keeps session setup out of the per-client path and lets the SSH server resolve private destination names from the bastion network.

When a session ends, active forwards observe a reconnecting state while the runtime retries with backoff. A non-retryable error moves the session to failed, and a later acquire can create a fresh session. When the last forward releases the session, the runtime closes it.

The listener keeps the session lease for the life of the forward. It opens a channel for each accepted client, records channel errors, and stops the forward if the listener itself fails. A destination check opens a temporary channel during startup, so an unreachable remote port fails startup and rolls back local resources instead of failing only the first user connection.

References: crates/…/lib.rs• L77-119 crates/…/route.rs• L24-49 crates/…/mod.rs• L17-64 crates/…/pool.rs• L38-106 crates/…/pool.rs• L110-177 crates/…/opener.rs• L81-129 crates/…/mod.rs• L205-234 crates/…/listener.rs• L123-179

Connection-management integration

SSH forwarding enters through the shared start path. That path locks the configuration lifecycle, chooses the SSH starter for the ssh-port-forward workload, claims local resources, builds the route, checks the destination, binds the listener, and registers the running process. Stop and cancellation release the listener and the session lease through the same lifecycle controls used by other forwards.

Session state is copied into the forward status record. A reconnecting session exposes its retry attempt, a ready session clears the retry state, and a terminal session records its failure. This lets the wider UI and management tools show SSH health without owning SSH details.

The SSH path also reuses shared local behavior. It can create a loopback address, write a domain alias, apply local TLS settings, and report connection or HTTP-log state. The remote hop is SSH-specific, but the local endpoint remains a normal managed forward.

References: crates/…/start.rs• L1367-1406 crates/…/mod.rs• L174-203 crates/…/mod.rs• L236-339 crates/…/mod.rs• L342-410 crates/…/listener.rs• L59-86 crates/…/config_model.rs• L209-225

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep ssh-port-forward validation aligned between the config model, MCP input checks, route validation, and the start dispatcher.
  • Keep provider fields aligned between the serialized SshProvider shape, route construction, provider schema, and exported example configurations.
  • Keep host-key aliases stable across route creation, database storage, forget-host-key actions, and provider-published keys.
  • Keep SessionState changes aligned with forward retry status so reconnect and terminal failure remain visible to connection management.
  • Keep SSH startup rollback aligned with listener registration so a failed destination check does not leave a local address, alias, or process recorded as active.

Important failure modes

TriggerConsequenceGuard
Missing or invalid GCP credentialsRoute creation or token access fails before the SSH session starts.Application Default Credentials and provider route checks.
VM is not reachable through IAPTransport setup fails and startup rolls back local resources.IAP transport errors and the start cancellation/rollback path.
VM host key changesThe handshake fails with a host-key mismatch.Provider trust keys or the stored first-use key; explicit forget action for approved rotation.
OS Login rejects the user or certificateThe session fails authentication and does not serve channels.Caller email and OS Login credential creation.
Remote host or port is closedThe destination check rejects startup, or one later client fails without killing the session.Startup probe and per-channel target errors.
SSH server prohibits forwardingThe affected local connection fails with forwarding prohibited.SSH server forwarding policy and per-connection error reporting.
SSH session dropsActive forwards enter reconnecting state; unrecoverable errors become failed.Session supervisor backoff and lifecycle status updates.

Key files

FileWhy to read it
crates/…/mod.rsConnects configuration, route creation, startup checks, registration, tests, and host-key actions.
crates/…/providers.rsMaps the saved provider type to the generic route and shared provider context.
crates/…/listener.rsShows how local TCP clients become SSH channels and how listener failure affects lifecycle state.
crates/…/lib.rsDefines the runtime that owns the shared key, session pool, and host-key store.
crates/…/pool.rsExplains route sharing, reconnect behavior, and session release.
crates/…/connect.rsShows the transport, host-key handshake, and authentication sequence.
crates/…/opener.rsShows per-client channel creation and target errors.
crates/…/route.rsDescribes the GCP bastion route and its transport, login, and host-key sources.
crates/…/ssh_provider.rsDefines the saved provider contract and validation rules.
crates/…/ssh_host_keys.rsDefines persistent host-key storage and replacement behavior.