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): give actionable error for unreachable SSH gateway (#72124)

Tom Owers5472bc3db0db
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 80e8481f756e..690eee55be12 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
@@ -192,6 +192,17 @@ _FOREIGN_SERVER_UNREACHABLE_ERROR = (
"persists, check that the foreign server is running and reachable from your source database."
)
+# sshtunnel raises BaseSSHTunnelForwarderError("Could not establish session to SSH gateway") when it
+# can't open a session to the bastion — the SSH host/port is wrong or unreachable, the bastion is
+# down, or its firewall blocks PostHog's IPs. The raw message tells the user nothing actionable, so
+# replace it with concrete guidance on both the validate and sync paths.
+_SSH_GATEWAY_SESSION_ERROR = "Could not establish session to SSH gateway"
+_SSH_GATEWAY_UNREACHABLE_MESSAGE = (
+ "Could not connect to your SSH tunnel — PostHog couldn't open a session to the SSH gateway. "
+ "Check that the SSH host and port point to a reachable SSH server (not the database port), that "
+ "the bastion is running, and that PostHog's IP addresses are allowed through its firewall."
+)
+
@SourceRegistry.register
class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDatabaseHostMixin):
@@ -490,7 +501,7 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
),
"SSLRequiredError": None,
"SSL/TLS connection is required": None,
- "Could not establish session to SSH gateway": None,
+ _SSH_GATEWAY_SESSION_ERROR: _SSH_GATEWAY_UNREACHABLE_MESSAGE,
# paramiko raises a bare, message-less EOFError when the SSH gateway accepts the TCP
# connection but drops it mid-handshake (a non-SSH service on the port, the bastion
# refusing PostHog's IPs, a proxy resetting the stream). sshtunnel doesn't wrap it, so
@@ -937,9 +948,12 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDa
capture_exception(e)
return False, f"Could not connect to {self.source_name}. Please check all connection details are valid."
except BaseSSHTunnelForwarderError as e:
+ raw = e.value or ""
+ if _SSH_GATEWAY_SESSION_ERROR in raw:
+ return False, _SSH_GATEWAY_UNREACHABLE_MESSAGE
return (
False,
- e.value
+ raw
or f"Could not connect to {self.source_name} via the SSH tunnel. Please check all connection details are valid.",
)
except Exception as 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 21b9211ebf72..72e3e0cebc7a 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
@@ -20,6 +20,7 @@ import pyarrow as pa
import structlog
from parameterized import parameterized
from psycopg import sql
+from sshtunnel import BaseSSHTunnelForwarderError
import products.warehouse_sources.backend.temporal.data_imports.sources.postgres.partitioned_tables as partitioned_tables_pkg
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.consts import DEFAULT_CHUNK_SIZE
@@ -2978,6 +2979,22 @@ class TestValidateCredentialsErrorMapping:
assert valid is False
assert error == expected
+ def test_ssh_gateway_session_error_maps_to_actionable_message(self, source, config):
+ # sshtunnel's raw "Could not establish session to SSH gateway" is meaningless to the user;
+ # it must be replaced with concrete guidance rather than surfaced verbatim.
+ err = BaseSSHTunnelForwarderError("Could not establish session to SSH gateway")
+ 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 "Could not establish session to SSH gateway" not in error
+ assert "SSH gateway" in error and "firewall" in error
+
@pytest.mark.parametrize(
"host",
[