: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(postgres): honor SSH tunnel TLS opt-out for CDC connections (#63440)
Tom Owers0569698ed80b
diff --git a/posthog/temporal/data_imports/sources/postgres/cdc/slot_manager.py b/posthog/temporal/data_imports/sources/postgres/cdc/slot_manager.pyindex 7d6b44c65ed3..ea8b4aaeaf00 100644--- a/posthog/temporal/data_imports/sources/postgres/cdc/slot_manager.py+++ b/posthog/temporal/data_imports/sources/postgres/cdc/slot_manager.py@@ -28,7 +28,10 @@ def cdc_pg_connection(source: ExternalDataSource, connect_timeout: int = 15) ->source_impl = PostgresSource()config = source_impl.parse_config(source.job_inputs or {})- require_ssl = source_requires_ssl(source)+ # Pass `config` so the SSH-tunnel `require_tls` opt-out is honored, matching the main+ # pipeline path. Without it SSL is forced on, and a database reached over an SSH tunnel+ # that doesn't speak SSL fails with "server does not support SSL, but SSL was required".+ require_ssl = source_requires_ssl(source, config)with source_impl.with_ssh_tunnel(config) as (host, port):conn = _connect_to_postgres(diff --git a/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_slot_manager.py b/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_slot_manager.pyindex f6f05c61f9e2..c9164ce5a233 100644--- a/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_slot_manager.py+++ b/posthog/temporal/data_imports/sources/postgres/cdc/tests/test_slot_manager.py@@ -1,11 +1,22 @@+import datetime as dt+from contextlib import contextmanager+from types import SimpleNamespace+from typing import cast++import pytest+from unittest import mockfrom unittest.mock import MagicMockimport psycopg.errorsfrom posthog.temporal.data_imports.sources.postgres.cdc.slot_manager import (add_table_to_publication,+ cdc_pg_connection,remove_table_from_publication,)+from posthog.temporal.data_imports.sources.postgres.postgres import SSL_REQUIRED_AFTER_DATE++from products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcedef _mock_conn(execute_side_effect=None):@@ -65,3 +76,63 @@ class TestRemoveTableFromPublication:conn.rollback.assert_called_once()conn.commit.assert_not_called()+++class TestCdcPgConnectionSsl:+ @pytest.mark.parametrize(+ "is_new_source,ssh_tunnel_enabled,require_tls,expected_require_ssl",+ [+ (True, False, True, True),+ (True, True, True, True),+ (True, True, False, False),+ (False, False, True, False),+ ],+ )+ def test_honors_ssh_tunnel_tls_optout(self, is_new_source, ssh_tunnel_enabled, require_tls, expected_require_ssl):+ # CDC must derive `require_ssl` from the full config like the main pipeline path, so an+ # SSH-tunnel `require_tls` opt-out is honored. Otherwise SSL is forced on and a database+ # reached over an SSH tunnel that doesn't speak SSL fails with "server does not support SSL".+ from posthog.temporal.data_imports.sources.postgres.source import PostgresSource++ config = PostgresSource().parse_config(+ {+ "host": "db.example.com",+ "port": 5432,+ "database": "postgres",+ "user": "postgres",+ "password": "postgres",+ "schema": "public",+ }+ )+ if ssh_tunnel_enabled:+ config.ssh_tunnel = mock.MagicMock(enabled=True, require_tls=mock.MagicMock(enabled=require_tls))+ else:+ config.ssh_tunnel = None++ created_at = (+ SSL_REQUIRED_AFTER_DATE + dt.timedelta(days=1)+ if is_new_source+ else SSL_REQUIRED_AFTER_DATE - dt.timedelta(days=1)+ )+ source = SimpleNamespace(job_inputs={}, created_at=created_at)++ @contextmanager+ def fake_tunnel(self, config):+ yield ("127.0.0.1", 44549)++ with (+ mock.patch(+ "posthog.temporal.data_imports.sources.postgres.source.PostgresSource.parse_config",+ return_value=config,+ ),+ mock.patch(+ "posthog.temporal.data_imports.sources.postgres.source.PostgresSource.with_ssh_tunnel",+ fake_tunnel,+ ),+ mock.patch("posthog.temporal.data_imports.sources.postgres.postgres._connect_to_postgres") as mock_connect,+ ):+ with cdc_pg_connection(cast(ExternalDataSource, source)):+ pass++ mock_connect.assert_called_once()+ assert mock_connect.call_args.kwargs["require_ssl"] is expected_require_ssl