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(warehouse-sources): pass source type on postgres source connects (#123219)

James Greenhille64793a07ec5
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
index 4e254d31fe5f..73d03212aba3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
@@ -901,7 +901,7 @@ def open_ssh_tunnel(config, team_id: int | None = None, source_type: str | None
def make_ssh_tunnel_factory(
- config, team_id: int | None = None
+ config, team_id: int | None = None, source_type: str | None = None
) -> Callable[[], _GeneratorContextManager[tuple[str, int]]]:
"""Return a zero-arg factory that opens a fresh `open_ssh_tunnel(config)` context each call.
@@ -931,7 +931,7 @@ def make_ssh_tunnel_factory(
with _logged_connection(config, team_id):
# Checked per reopen, not once when the factory is built, so a long-running
# sync that reconnects re-checks the host each time.
- _check_direct_host(config, team_id)
+ _check_direct_host(config, team_id, source_type)
yield config.host, config.port
return without_ssh_func
@@ -946,9 +946,9 @@ class SSHTunnelMixin:
return open_ssh_tunnel(config, team_id, source_type)
def make_ssh_tunnel_func(
- self, config, team_id: int | None = None
+ self, config, team_id: int | None = None, source_type: str | None = None
) -> Callable[[], _GeneratorContextManager[tuple[str, int]]]:
- return make_ssh_tunnel_factory(config, team_id)
+ return make_ssh_tunnel_factory(config, team_id, source_type)
def ssh_tunnel_enabled(self, config) -> bool:
"""Whether the tunnel helpers above will tunnel rather than connect directly.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_managed_warehouse_exemption.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_managed_warehouse_exemption.py
index a8e219560dcd..75e4d738a992 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_managed_warehouse_exemption.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_managed_warehouse_exemption.py
@@ -635,6 +635,41 @@ class TestTrinoConnectPathAgreesWithValidation(SimpleTestCase):
assert kwargs.get("source_type") == "trino"
+class TestPostgresConnectPathsAgreeWithValidation(SimpleTestCase):
+ """A managed Postgres source must not validate and then refuse to connect.
+
+ Validation names the source type, so it approves the managed host. Every later connect of the
+ source must name it too, or saving the source and every sync of it fail with a host refusal.
+ """
+
+ @parameterized.expand([("schema_listing",), ("sync_tunnel",)])
+ @override_settings(CLOUD_DEPLOYMENT="US", MANAGED_WAREHOUSE_INGRESS_IPS=_NLB_A)
+ def test_a_managed_warehouse_host_is_admitted(self, path: str) -> None:
+ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.postgres import (
+ PostgresSourceConfig,
+ )
+ from products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source import PostgresSource
+
+ source = PostgresSource()
+ config = PostgresSourceConfig(host=_TENANT_HOST, port=5432, database="db", user="root", password="secret")
+ with (
+ patch(f"{_MIXINS_MODULE}.socket.getaddrinfo", return_value=[(None, None, None, None, (_NLB_A, 0))]),
+ patch(f"{_MIXINS_MODULE}._managed_warehouse_organization_id", return_value="org-1"),
+ patch(f"{_MIXINS_MODULE}.logger"),
+ patch(f"{_INGRESS_MODULE}.get_managed_warehouse_target", return_value=_target()),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.postgres.source.get_postgres_schemas",
+ side_effect=_no_connection,
+ ),
+ ):
+ with pytest.raises(_Stop):
+ if path == "schema_listing":
+ source.get_schemas(config, team_id=999)
+ else:
+ with source.make_ssh_tunnel_func(config, 999, "postgres")():
+ raise _Stop()
+
+
class _Stop(Exception):
pass
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 176a4faa6789..83a5a004c0af 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
@@ -1736,7 +1736,7 @@ class PostgresSource(
) -> list[SourceSchema]:
schemas = []
- with self.with_ssh_tunnel(config, team_id) as (host, port):
+ with self.with_ssh_tunnel(config, team_id, POSTGRES_HOST_CHECK_SOURCE_TYPE) as (host, port):
db_schemas = get_postgres_schemas(
host=host,
port=port,
@@ -2062,7 +2062,7 @@ class PostgresSource(
def get_connection_metadata(
self, config: PostgresSourceConfig, team_id: int, require_ssl: bool = False
) -> dict[str, object]:
- with self.with_ssh_tunnel(config, team_id) as (host, port):
+ with self.with_ssh_tunnel(config, team_id, POSTGRES_HOST_CHECK_SOURCE_TYPE) as (host, port):
return get_postgres_connection_metadata(
host=host,
port=port,
@@ -2095,7 +2095,7 @@ class PostgresSource(
_connect_to_postgres,
)
- with self.with_ssh_tunnel(config, team_id) as (host, port):
+ with self.with_ssh_tunnel(config, team_id, POSTGRES_HOST_CHECK_SOURCE_TYPE) as (host, port):
conn = _connect_to_postgres(
host=host,
port=port,
@@ -2291,7 +2291,7 @@ class PostgresSource(
ForeignServerUnreachableError,
)
- ssh_tunnel = self.make_ssh_tunnel_func(config, inputs.team_id)
+ ssh_tunnel = self.make_ssh_tunnel_func(config, inputs.team_id, POSTGRES_HOST_CHECK_SOURCE_TYPE)
# This reads sync metadata from PostHog's own database, not the customer's Postgres — a
# transient failure reaching it (e.g. a DNS blip resolving our host) is a plain Django