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.

chore(clickhouse): add token-auth skill and AGENTS.md for new services (#103207)

Tommy Gilmore7c96ec9752da
diff --git a/.agents/skills/authenticating-to-clickhouse/SKILL.md b/.agents/skills/authenticating-to-clickhouse/SKILL.md
new file mode 100644
index 000000000000..b95b9f25b92c
--- /dev/null
+++ b/.agents/skills/authenticating-to-clickhouse/SKILL.md
@@ -0,0 +1,21 @@
+---
+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.
+---
+
+# Authenticating to ClickHouse
+
+A service authenticates to ClickHouse with a short-lived ServiceAccount token, validated by the ch-podauth bridge, not a static password. The token rotates, so the client reads it on each use. Read `posthog/clickhouse/client/AGENTS.md` for the full recipe and the traps. This skill is the entry point.
+
+Use it when adding a new service or ClickHouse user, or when building or changing a ClickHouse pool or client by hand. Converting an already-deployed user off a static password is a ClickHouse-team operation and is out of scope.
+
+## 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.
+- Wire the deploy side in the charts repo. The `connect-app-to-clickhouse` skill mounts the token and sets `CLICKHOUSE_<USER>_PASSWORD_FILE`.
+
+## Do not
+
+- Pass your own `sync_client` to `sync_execute`, or build a pool or client by hand, without keeping it token-aware. A native pool must carry `credential_provider`; an HTTP or one-shot client resolves the token per call instead. A pool that skips this silently stays on the static password. `posthog/clickhouse/client/AGENTS.md` has both shapes.
+- Read the credential once at startup, in any language. The token rotates, so read the token file on each connection or request.
diff --git a/AGENTS.md b/AGENTS.md
index e782967ea8ca..2ea8decbfcc1 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -299,6 +299,7 @@ ALWAYS invoke the matching skill **first** — do not skip it, and do not attemp
- `/integrating-with-posthog-ai` — making a product surface work with PostHog AI: injecting scene context or custom instructions, reacting to the agent's tool calls, or rendering your product's tool cards in a thread
- `/sending-notifications` — adding notification support
- `/routing-outbound-api-calls` — adding or changing Python code that calls a third-party HTTP API, or changing any domain under `posthog/egress/` (budget, lanes, identity, metrics, headers)
+- `/authenticating-to-clickhouse` — adding a service, sidecar, or container that connects to ClickHouse, adding a `ClickHouseUser`, or building or changing a ClickHouse pool or client by hand; it must stay token-aware (a native pool carries the credential provider, an HTTP client resolves the token per call) or it silently uses the static password
- `/adding-activity-logging` — adding activity logging (the audit trail) to a model, writing or changing a `model_activity_signal` receiver or an activity describer, auditing which write paths of a model are logged, or debugging a change that is missing from the activity log
- `/adding-inbound-webhooks` — adding a webhook endpoint for a third party that sends to PostHog, adding a consumer for a provider that already has an endpoint, or migrating a verifier the `inbound-webhooks-go-through-ingress` rule flags
- `/writing-skills` — creating or updating skills in `.agents/skills/`
diff --git a/posthog/clickhouse/client/AGENTS.md b/posthog/clickhouse/client/AGENTS.md
new file mode 100644
index 000000000000..aa0f516dba33
--- /dev/null
+++ b/posthog/clickhouse/client/AGENTS.md
@@ -0,0 +1,63 @@
+# ClickHouse client authentication
+
+How code authenticates to ClickHouse.
+For migrations, see `posthog/clickhouse/migrations/AGENTS.md`.
+
+A service authenticates to ClickHouse with a short-lived Kubernetes ServiceAccount token.
+The on-node ch-podauth LDAP bridge validates the token.
+The token file rotates on a short TTL.
+The client reads the file on each use and does not cache the value.
+
+## The default path is already token-aware
+
+`sync_execute(..., ch_user=...)` reads the token on each use.
+It routes through `get_client_from_pool`, which uses the token-aware `get_pool` for native connections and `get_http_kwargs` for HTTP.
+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.
+
+Token-awareness is lost when you pass your own `sync_client` to `sync_execute`, or build your own pool or client.
+
+## A custom pool or client must stay token-aware
+
+`credential_provider` is a native-pool mechanism. An HTTP or one-shot client does not use it.
+
+Build a custom native pool for a timeout or a setting only by replicating the file-backed branch of `get_pool`.
+A pool that skips this branch stays on the static password with no error.
+Resolve the credentials with `get_clickhouse_creds`.
+When `_is_file_backed_user` is true, pop the static `password` and pass `credential_provider=creds.read_password` to `make_ch_pool`.
+`read_password` reads the token file on each call.
+It falls back to the static password only when the file is unreadable or empty.
+`RefreshingChPool` re-stamps the live token on each checkout, so one pool survives a rotation.
+The canonical native implementation is `get_pool`.
+
+An HTTP or one-shot client is rebuilt on each call, so it needs no credential provider.
+Resolve the token once with `creds.read_password()` and pass it as the password, the way `get_http_kwargs` does.
+
+## Wire the username, or the user falls back to the default 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.
+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.
+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.
+
+## Other languages
+
+A non-Python service has no shared helper.
+The rule is the same in any language: read the token file on each connection or request.
+A client that reads the password once at startup breaks after the first token rotation.
+
+## Deploy side
+
+The token mount and the `CLICKHOUSE_<USER>_PASSWORD_FILE` env are wired in the charts repo.
+See its `connect-app-to-clickhouse` skill and `docs/claude/clickhouse-connections.md` when you wire the deployment.
diff --git a/posthog/clickhouse/client/CLAUDE.md b/posthog/clickhouse/client/CLAUDE.md
new file mode 120000
index 000000000000..47dc3e3d863c
--- /dev/null
+++ b/posthog/clickhouse/client/CLAUDE.md
@@ -0,0 +1 @@
+AGENTS.md
\ No newline at end of file