: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
chore(clickhouse): correct the token-auth agent docs (#118078)
Tommy Gilmore24c5ac0b9206
diff --git a/.agents/skills/authenticating-to-clickhouse/SKILL.md b/.agents/skills/authenticating-to-clickhouse/SKILL.mdindex b95b9f25b92c..8c45403e03a5 100644--- a/.agents/skills/authenticating-to-clickhouse/SKILL.md+++ b/.agents/skills/authenticating-to-clickhouse/SKILL.md@@ -1,6 +1,6 @@---name: authenticating-to-clickhouse-description: Authenticate a new or changed service to ClickHouse with the rotating ch-podauth ServiceAccount token instead of a static password. Use when adding a service, sidecar, or container that reads or writes ClickHouse, adding a new ClickHouseUser, or hand-building a ClickHouse pool or client for a custom timeout or setting. Covers the token-aware default path (sync_execute, get_client), the rule that a custom pool must pass credential_provider or it silently stays on the static password, wiring the username so the user does not fall back to the default user, and the deploy-side companion in the charts repo. Not for converting an already-deployed user off a static password.+description: Authenticate a new or changed service to ClickHouse with the rotating ch-podauth ServiceAccount token instead of a static password. Use when adding a service, sidecar, or container that reads or writes ClickHouse, adding a new ClickHouseUser, or hand-building a ClickHouse pool or client for a custom timeout or setting. Covers the token-aware default path (sync_execute, get_client), the rule that a custom pool must pass credential_provider or it silently stays on the static password, wiring the username so the user does not fall back to the pod's CLICKHOUSE_USER, and the deploy-side companion in the charts repo. Not for converting an already-deployed user off a static password.---# Authenticating to ClickHouse@@ -12,7 +12,7 @@ Use it when adding a new service or ClickHouse user, or when building or changin## Do- Use the token-aware default paths: `sync_execute(..., ch_user=...)` for pooled queries, or the async `get_client` in `posthog/temporal/common/clickhouse.py` for HTTP. Both read the token on each use.-- Choose the user shape: deployment-default (the pod's default user is the service) or a new `ClickHouseUser` enum member. Wire the username env, or the user silently authenticates as the default user.+- Choose the user shape: deployment-default (the pod's `CLICKHOUSE_USER` is the service's own user) or a new `ClickHouseUser` enum member. Wire the username env, or the queries silently authenticate as the pod's `CLICKHOUSE_USER`.- Wire the deploy side in the charts repo. The `connect-app-to-clickhouse` skill mounts the token and sets `CLICKHOUSE_<USER>_PASSWORD_FILE`.## Do notdiff --git a/posthog/clickhouse/client/AGENTS.md b/posthog/clickhouse/client/AGENTS.mdindex 42e84b36ecdb..0d731fec2dcd 100644--- a/posthog/clickhouse/client/AGENTS.md+++ b/posthog/clickhouse/client/AGENTS.md@@ -15,11 +15,11 @@ It routes through `get_client_from_pool`, which uses the token-aware `get_pool`The async temporal client `get_client` in `posthog/temporal/common/clickhouse.py` is token-aware too, because `_request_headers` calls `read_password` on each request.Use these paths and token auth is automatic.-`get_cluster` in `posthog/clickhouse/cluster.py` is token-aware for the user that its `ch_user` argument resolves to, which is the pod default user unless you set it: the per-host pools carry a credential provider that re-stamps the live token on each checkout.+`get_cluster` in `posthog/clickhouse/cluster.py` is token-aware for the user that its `ch_user` argument resolves to, which is the pod's `CLICKHOUSE_USER` unless you set it: the per-host pools carry a credential provider that re-stamps the live token on each checkout.Its bootstrap discovery client is long-lived, so each discovery attempt stamps the live token onto it first, including the discovery that `sibling()` runs hours into a job.Pass `ch_user` to connect as a dedicated user, the way `get_migrations_cluster` does, because discovery and the pools then both use that user.-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.+A caller that connects as a different user through `connection_overrides` owns its own credential, and its discovery still runs as the pod's `CLICKHOUSE_USER`.+The part_breaker Dagster resource does this: it sets `credential_provider` itself when its user is file-backed, the way `get_pool` does.`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.@@ -45,20 +45,21 @@ Resolve the token once with `creds.read_password()` and pass it as the password,A client that is retained and reconnects is not one-shot: a token baked into it expires with no recovery, because `read_password`'s expired-token fallback only fires when it is called again.Stamp `read_password()` onto `client.connection.password` before each use, the way the `get_cluster` bootstrap does, or use a refreshing pool.-## Wire the username, or the user falls back to the default user+## Wire the username, or the user falls back to the pod's `CLICKHOUSE_USER`A dedicated user registers only when both its username env `CLICKHOUSE_<USER>_USER` and a secret are set.The secret is `CLICKHOUSE_<USER>_PASSWORD` or `CLICKHOUSE_<USER>_PASSWORD_FILE`.-If the username env is missing, `get_clickhouse_creds` returns the default user's credentials.-The query then runs as the pod default with no error.+If the username env is missing, `get_clickhouse_creds` returns the credentials of the pod's `CLICKHOUSE_USER`.+The query then runs as that user with no error.+On many pods, including every Django app, `CLICKHOUSE_USER` is the literal `default` user.A new `ClickHouseUser` member needs its username wired, not only its token file.`get_clickhouse_creds` decides the missing-credential behavior per user, and some users raise instead of falling back.## Deployment-default or enum user-Deployment-default: the pod's default ClickHouse user is the service.-Set the default `CLICKHOUSE_USER` and `CLICKHOUSE_PASSWORD_FILE`.-The default user becomes token-aware because the resolved user matches.+Deployment-default: the pod's `CLICKHOUSE_USER` is the service's own user.+Set `CLICKHOUSE_USER` and `CLICKHOUSE_PASSWORD_FILE`.+`ClickHouseUser.DEFAULT` then resolves to that user and reads its token.Enum user: add a `ClickHouseUser` member and wire its `CLICKHOUSE_<USER>_USER` and `CLICKHOUSE_<USER>_PASSWORD_FILE`.A token-first user needs no static password.The static fallback resolves to an empty string when none is set, so the token is the only working credential.diff --git a/posthog/clickhouse/client/connection.py b/posthog/clickhouse/client/connection.pyindex 26b134944bd1..e57bfbf29e07 100644--- a/posthog/clickhouse/client/connection.py+++ b/posthog/clickhouse/client/connection.py@@ -241,13 +241,14 @@ def get_clickhouse_creds(user: ClickHouseUser) -> ClickHouseCredentials:Retrieve ClickHouse credentials for the specified user.This function retrieves the credentials associated with a given ClickHouse- user. Most missing dedicated users fall back to the default user credentials.+ user. Most missing dedicated users fall back to the ClickHouseUser.DEFAULT+ credentials, which belong to the pod's CLICKHOUSE_USER.Business Knowledge fails closed so a deployment cannot silently bypass itsisolated query limit.- The user and password must be properly passed as ENVs:+ The user and a password or token file must be properly passed as ENVs:CLICKHOUSE_<USER_NAME>_USER- CLICKHOUSE_<USER_NAME>_PASSWORD+ CLICKHOUSE_<USER_NAME>_PASSWORD or CLICKHOUSE_<USER_NAME>_PASSWORD_FILEArgs:user (ClickHouseUser): The user whose ClickHouse credentials need