: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(clickhouse): make default_client token-aware (#117937)
Tommy Gilmoredf27e4e159ed
diff --git a/posthog/clickhouse/client/AGENTS.md b/posthog/clickhouse/client/AGENTS.mdindex 60837a30f667..42e84b36ecdb 100644--- a/posthog/clickhouse/client/AGENTS.md+++ b/posthog/clickhouse/client/AGENTS.md@@ -21,7 +21,10 @@ Pass `ch_user` to connect as a dedicated user, the way `get_migrations_cluster`A caller that connects as a different user through `connection_overrides` owns its own credential, and its discovery still runs as the pod default user.The backups and part_breaker Dagster resources do this: each sets `credential_provider` itself when its user is file-backed, the way `get_pool` does.-Token-awareness is lost when you pass your own `sync_client` to `sync_execute`, or build your own pool or client.+`default_client()` without `user` and `password` reads the token for the pod's `CLICKHOUSE_USER` when it builds the client.+It is a one-shot client: close it after one short task, because a retained client that reconnects after the token expires is rejected.++Token-awareness is lost when you pass any other client as `sync_client` to `sync_execute`, or build your own pool or client.## A custom pool or client must stay token-awarediff --git a/posthog/clickhouse/client/connection.py b/posthog/clickhouse/client/connection.pyindex c212fbd8cc3d..26b134944bd1 100644--- a/posthog/clickhouse/client/connection.py+++ b/posthog/clickhouse/client/connection.py@@ -543,10 +543,18 @@ def default_client(Return a bare bones client for use in places where we are only interested in general ClickHouse stateDO NOT USE THIS FOR QUERYING DATA- user and password override CLICKHOUSE_USER and the static CLICKHOUSE_PASSWORD, for example with a- dedicated user's resolved file-backed token.+ When the caller passes neither user nor password, the client authenticates as CLICKHOUSE_USER with the+ token from CLICKHOUSE_PASSWORD_FILE, and falls back to the static CLICKHOUSE_PASSWORD the way the pool does.+ The token is read once, so close the client after one short task: a retained client that reconnects+ after the token expires is rejected.+ user and password override these, for example with a dedicated user's resolved file-backed token.send_receive_timeout bounds each socket read and write; None keeps the driver default."""+ if user is None and password is None:+ user = settings.CLICKHOUSE_USER+ password = ClickHouseCredentials(+ user=user, password=settings.CLICKHOUSE_PASSWORD, password_file=settings.CLICKHOUSE_PASSWORD_FILE+ ).read_password()return ClickHouseClient(host=host,**({"send_receive_timeout": send_receive_timeout} if send_receive_timeout is not None else {}),diff --git a/posthog/clickhouse/client/test/test_connection.py b/posthog/clickhouse/client/test/test_connection.pyindex c00d712b6d30..12b0e466badd 100644--- a/posthog/clickhouse/client/test/test_connection.py+++ b/posthog/clickhouse/client/test/test_connection.py@@ -13,6 +13,7 @@ from posthog.clickhouse.client.connection import (ClickHouseUser,RefreshingChPool,Workload,+ default_client,get_clickhouse_creds,get_http_client,get_http_kwargs,@@ -267,6 +268,28 @@ def test_get_client_from_pool_http_branch_sends_fresh_token(settings, monkeypatcassert mock_http_client.call_args.kwargs["password"] == "tok-0"+ "kwargs,expected_user,expected_password",+ [+ pytest.param({}, "pod-user", "live-token", id="no-credentials-uses-the-pod-token"),+ pytest.param({"password": "explicit"}, "pod-user", "explicit", id="explicit-password-is-kept"),+ pytest.param({"user": "other"}, "other", "pod-static", id="other-user-never-gets-the-pod-token"),+ ],+)+def test_default_client_sends_the_pod_token_only_without_explicit_credentials(+ settings, tmp_path, kwargs, expected_user, expected_password+):+ token = tmp_path / "token"+ token.write_text("live-token")+ settings.CLICKHOUSE_USER = "pod-user"+ settings.CLICKHOUSE_PASSWORD = "pod-static"+ settings.CLICKHOUSE_PASSWORD_FILE = str(token)++ client = default_client(**kwargs)++ assert (client.connection.user, client.connection.password) == (expected_user, expected_password)++def test_refreshing_pool_stamps_current_credential(tmp_path):token = tmp_path / "token"token.write_text("tok-0")diff --git a/posthog/clickhouse/cluster.py b/posthog/clickhouse/cluster.pyindex fda9aafa61b6..ebef6ae08753 100644--- a/posthog/clickhouse/cluster.py+++ b/posthog/clickhouse/cluster.py@@ -659,7 +659,7 @@ def get_cluster(if not caller_owns_credential:overrides.update(user=creds.user, password=creds.password)else:- bootstrap_client = default_client(host=host)+ bootstrap_client = default_client(host=host, user=creds.user, password=creds.password)return ClickhouseCluster(bootstrap_client,diff --git a/posthog/clickhouse/test/test_cluster.py b/posthog/clickhouse/test/test_cluster.pyindex 16cc5e62a008..8e476e883196 100644--- a/posthog/clickhouse/test/test_cluster.py+++ b/posthog/clickhouse/test/test_cluster.py@@ -65,7 +65,14 @@ def cluster(django_db_setup) -> Iterator[ClickhouseCluster]:id="token_first_default_bootstraps_on_token",),pytest.param(- ClickHouseUser.DEFAULT, "static-only", False, None, {}, {}, False, id="static_default_uses_password"+ ClickHouseUser.DEFAULT,+ "static-only",+ False,+ None,+ {"user": "default", "password": "static-only"},+ {},+ False,+ id="static_default_uses_password",),pytest.param(ClickHouseUser.DEFAULT,