: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: Don't require connection_string when changing host (#62435)
Luke Beltonab0cfa6cc1e0
diff --git a/products/data_warehouse/backend/api/external_data_source.py b/products/data_warehouse/backend/api/external_data_source.pyindex 4e623c598383..4f2752bb8537 100644--- a/products/data_warehouse/backend/api/external_data_source.py+++ b/products/data_warehouse/backend/api/external_data_source.py@@ -293,6 +293,12 @@ def ssh_tunnel_connection_changed(existing: Any, incoming: Any) -> bool:# ServiceNow `auth_method`) keep their secrets one level down, not at the top level._NESTED_AUTH_CONTAINERS = ("auth_method", "auth_type")+# Secrets the edit form can never re-supply (parsed into the individual fields on create, then+# stripped from API reads and hidden in the edit form), so gating credential re-entry on them would+# permanently block host changes. Excluded from the gate but still preserved by the merge: MongoDB+# connects via `connection_string`, while SQL sources use the individual fields and gate `password`.+_CREATION_ONLY_SECRET_FIELDS = frozenset({"connection_string"})+def has_preserved_credentials(existing: dict[str, Any], incoming: dict[str, Any], sensitive_fields: set[str]) -> bool:"""True if any stored secret would be reused because the update didn't re-supply it.@@ -796,7 +802,8 @@ class ExternalDataSourceSerializers(UserAccessControlSerializerMixin, serializermanifest_host_added = bool(new_hosts - existing_hosts)if connection_host_changed or ssh_tunnel_changed or manifest_host_added:- if has_preserved_credentials(existing_job_inputs, incoming_job_inputs, sensitive_fields):+ gate_sensitive_fields = sensitive_fields - _CREATION_ONLY_SECRET_FIELDS+ if has_preserved_credentials(existing_job_inputs, incoming_job_inputs, gate_sensitive_fields):if ssh_tunnel_changed:raise ValidationError("Changing the SSH tunnel requires re-entering your database credentials.")if manifest_host_added:diff --git a/products/data_warehouse/backend/api/test/test_external_data_source.py b/products/data_warehouse/backend/api/test/test_external_data_source.pyindex 5c1702a94e00..c01a532bcc76 100644--- a/products/data_warehouse/backend/api/test/test_external_data_source.py+++ b/products/data_warehouse/backend/api/test/test_external_data_source.py@@ -4082,6 +4082,57 @@ class TestExternalDataSource(APIBaseTest):assert source.job_inputs["password"] == "new_password"mock_validate_credentials.assert_called_once()+ @parameterized.expand([("with_password", {"password": "new_password"}, 200), ("without_password", {}, 400)])+ @patch(+ "posthog.temporal.data_imports.sources.postgres.source.PostgresSource.validate_credentials",+ return_value=(True, None),+ )+ def test_update_host_change_with_stored_connection_string(+ self, _name, extra_creds, expected_status, mock_validate_credentials+ ):+ # A stored `connection_string` (never re-suppliable via the edit form) must not block a host+ # change, while `password` stays gated: re-entering it succeeds, omitting it is still rejected.+ source = ExternalDataSource.objects.create(+ team_id=self.team.pk,+ source_id=str(uuid.uuid4()),+ connection_id=str(uuid.uuid4()),+ destination_id=str(uuid.uuid4()),+ source_type="Postgres",+ created_by=self.user,+ prefix="test_host_conn_string",+ job_inputs={+ "source_type": "Postgres",+ "connection_string": "postgresql://dbuser:[email protected]:5432/mydb",+ "host": "db.example.com",+ "port": "5432",+ "database": "mydb",+ "user": "dbuser",+ "password": "original_password",+ "schema": "public",+ },+ )++ response = self.client.patch(+ f"/api/environments/{self.team.pk}/external_data_sources/{source.pk}/",+ data={"job_inputs": {"host": "new-host.example.com", **extra_creds}},+ )++ assert response.status_code == expected_status, response.json()+ assert ("re-entering your credentials" in str(response.json())) == (expected_status == 400)+ source.refresh_from_db()+ # Preserved by the merge regardless of outcome — connection-string-based sources rely on this.+ assert (+ source.job_inputs["connection_string"] == "postgresql://dbuser:[email protected]:5432/mydb"+ )+ if expected_status == 200:+ assert source.job_inputs["host"] == "new-host.example.com"+ assert source.job_inputs["password"] == "new_password"+ mock_validate_credentials.assert_called_once()+ else:+ assert source.job_inputs["host"] == "db.example.com"+ assert source.job_inputs["password"] == "original_password"+ mock_validate_credentials.assert_not_called()+@patch("posthog.temporal.data_imports.sources.freshdesk.source.FreshdeskSource.validate_credentials",return_value=(True, None),