Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): explain an SSH tunnel that opens no forwarded connection (#108974)

Tom Owers4d3a13c23a22
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
index 6c4d249a4d41..e3d3a9243d78 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py
@@ -328,6 +328,16 @@ _SSH_GATEWAY_UNREACHABLE_MESSAGE = f"{_SSH_GATEWAY_UNREACHABLE_GUIDANCE}."
# Mirrors `_SSH_HANDSHAKE_EOF_ERROR` below, the same gateway-configuration class.
_SSH_GATEWAY_UNREACHABLE_SYNC_MESSAGE = f"{_SSH_GATEWAY_UNREACHABLE_GUIDANCE}, then re-enable the sync."
+# sshtunnel raises this once the SSH session is up but no forwarded tunnel came up with it: the
+# bastion could not open a channel to the database host and port, or it refuses port forwarding
+# altogether. Its own wording names neither, so it leaves the user nothing to act on.
+_SSH_FORWARD_FAILED_ERROR = "An error occurred while opening tunnels."
+_SSH_FORWARD_FAILED_MESSAGE = (
+ "PostHog signed in to your SSH server but couldn't reach the database through it. Check that "
+ "the database host and port are reachable from the SSH server, and that the server allows port "
+ "forwarding."
+)
+
# A source past the SSL cutoff connects with sslmode=require, so a server built without SSL support
# fails the moment the sync — or a direct query — opens its connection. An SSH tunnel with
# `require_tls` off is the supported way to reach such a server.
@@ -1688,10 +1698,14 @@ class PostgresSource(
raw = e.value or ""
if _SSH_GATEWAY_SESSION_ERROR in raw:
return False, _SSH_GATEWAY_UNREACHABLE_MESSAGE
+ if _SSH_FORWARD_FAILED_ERROR in raw:
+ return False, _SSH_FORWARD_FAILED_MESSAGE
+ # sshtunnel writes its messages for whoever is reading a traceback, so an unmapped one
+ # gives the user nothing and can carry the host it was dialing. Keep it for triage.
+ capture_exception(e)
return (
False,
- raw
- or f"Could not connect to {self.source_name} via the SSH tunnel. Please check all connection details are valid.",
+ f"Could not connect to {self.source_name} via the SSH tunnel. Please check all connection details are valid.",
)
except Exception as e:
capture_exception(e)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
index 289d697e4cf7..52ead070b831 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py
@@ -5285,6 +5285,40 @@ class TestValidateCredentialsErrorMapping:
assert "Could not establish session to SSH gateway" not in error
assert "SSH gateway" in error and "firewall" in error
+ def test_ssh_forward_failure_maps_to_actionable_message(self, source, config):
+ err = BaseSSHTunnelForwarderError("An error occurred while opening tunnels.")
+ with (
+ mock.patch.object(source, "ssh_tunnel_is_valid", return_value=(True, None)),
+ mock.patch.object(source, "is_database_host_valid", return_value=(True, None)),
+ mock.patch.object(source, "get_schemas", side_effect=err),
+ ):
+ valid, error = source.validate_credentials(config, team_id=1)
+
+ assert valid is False
+ assert error is not None
+ assert "opening tunnels" not in error
+ assert "port forwarding" in error
+
+ def test_unmapped_ssh_tunnel_error_is_not_echoed_and_is_captured(self, source, config):
+ # An unmapped sshtunnel message can name the host it was dialing, so it must not reach the
+ # wizard. It still has to reach error tracking, or the condition goes unnoticed.
+ err = BaseSSHTunnelForwarderError("Problem setting SSH Forwarder up: some internal detail")
+ with (
+ mock.patch.object(source, "ssh_tunnel_is_valid", return_value=(True, None)),
+ mock.patch.object(source, "is_database_host_valid", return_value=(True, None)),
+ mock.patch.object(source, "get_schemas", side_effect=err),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source.capture_exception"
+ ) as mock_capture,
+ ):
+ valid, error = source.validate_credentials(config, team_id=1)
+
+ assert valid is False
+ assert (
+ error == "Could not connect to Postgres via the SSH tunnel. Please check all connection details are valid."
+ )
+ mock_capture.assert_called_once()
+
@pytest.mark.parametrize(
"host",
[