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(clickhouse): map raw ssh tunnel errors to actionable setup messages (#114866)

Tom Owers1b14803a7735
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
index 9929b11f53a2..7d40ebf662f7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py
@@ -91,6 +91,17 @@ _INVALID_CREDENTIALS = (
"The database rejected the username or password. Check the user and password for this source and try again."
)
+# sshtunnel reports every failure to reach the gateway with this fixed wording, which names no fix.
+_SSH_GATEWAY_SESSION_ERROR = "Could not establish session to SSH gateway"
+_SSH_GATEWAY_UNREACHABLE_ERROR = (
+ "Could not connect to your SSH tunnel. Check that the SSH host, port, and credentials are correct, the bastion "
+ "host is running and reachable, and that PostHog's IP addresses are allowed through its firewall."
+)
+_SSH_TUNNEL_GENERIC_ERROR = (
+ "Could not connect to ClickHouse through your SSH tunnel. Check the SSH tunnel settings for this source, then "
+ "try again."
+)
+
_HOST_NOT_RESOLVED = "Host could not be resolved. Check the host is spelled correctly and reachable from PostHog."
# Error message → user-friendly translation. Matched as a substring of the
@@ -290,7 +301,7 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val
# this per-source dict, so without the entry a down tunnel retries to the maximum and
# reports the customer's gateway misconfig as error-tracking noise. Postgres, MySQL, and
# MSSQL already treat this identical error as non-retryable.
- "Could not establish session to SSH gateway": "Could not connect to your SSH tunnel. Check that the SSH host, port, and credentials are correct, the bastion host is running and reachable, and that PostHog's IP addresses are allowed through its firewall.",
+ _SSH_GATEWAY_SESSION_ERROR: _SSH_GATEWAY_UNREACHABLE_ERROR,
"Could not resolve the ClickHouse host": None,
"nodename nor servname provided": None,
"Name or service not known": None,
@@ -580,11 +591,11 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val
# user-facing wording and neither is a PostHog defect, so they are not captured.
return False, str(e)
except BaseSSHTunnelForwarderError as e:
- return (
- False,
- e.value
- or "Could not connect to ClickHouse via the SSH tunnel. Please check all connection details are valid.",
- )
+ if _SSH_GATEWAY_SESSION_ERROR in (e.value or ""):
+ return False, _SSH_GATEWAY_UNREACHABLE_ERROR
+ # Other sshtunnel messages are written for a traceback reader and can carry the host it dialed.
+ capture_exception(e)
+ return False, _SSH_TUNNEL_GENERIC_ERROR
except ClickHouseConnectionError as e:
message = self._translate_error(str(e))
return False, message or GENERIC_CONNECTION_ERROR
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
index 7c711ebc11ac..1bf0f4ce9143 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py
@@ -18,6 +18,7 @@ import pyarrow as pa
import clickhouse_connect
from clickhouse_connect.driver.exceptions import ClickHouseError, OperationalError, ProgrammingError
from parameterized import parameterized
+from sshtunnel import BaseSSHTunnelForwarderError
from products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchema
from products.warehouse_sources.backend.models.external_data_source import ExternalDataSource
@@ -646,6 +647,40 @@ class TestSourceClassValidateCredentials:
assert valid is False
assert msg == GENERIC_CONNECTION_ERROR
+ @pytest.mark.parametrize(
+ "raw_error,expected",
+ [
+ ("Could not establish session to SSH gateway", "Could not connect to your SSH tunnel"),
+ (
+ "Could not establish session to SSH gateway: the SSH server did not accept a connection within 15 seconds",
+ "Could not connect to your SSH tunnel",
+ ),
+ (
+ "Problem setting SSH Forwarder up: Couldn't open tunnel 127.0.0.1:0 <> bastion.example.com:9000",
+ "SSH tunnel settings",
+ ),
+ ],
+ )
+ def test_ssh_tunnel_errors_return_user_facing_message(self, raw_error, expected):
+ from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse import source as source_module
+
+ source = source_module.ClickHouseSource()
+
+ config = MagicMock()
+ config.host = "play.clickhouse.com"
+ config.ssh_tunnel = None
+
+ with patch.object(source, "ssh_tunnel_is_valid", return_value=(True, None)):
+ with patch.object(source, "is_database_host_valid", return_value=(True, None)):
+ with patch.object(source, "get_schemas", side_effect=BaseSSHTunnelForwarderError(raw_error)):
+ with patch.object(source_module, "capture_exception"):
+ valid, msg = source.validate_credentials(config, team_id=1)
+
+ assert valid is False
+ assert msg is not None and expected in msg
+ assert "SSH gateway" not in msg
+ assert "example.com" not in msg
+
class TestHasDuplicatePrimaryKeys:
def _logger(self):