Skip to content

ClickHouse Access Control

llms.txt lists every page for an agent
Optional: hand this page to your coding agentThe steps work by hand too.
Show the whole prompt
Read https://intentius.io/chant/lexicons/sql/clickhouse-access/.
Declare the users, roles, row policies, quotas, settings profiles and grants I describe with the tags from `@intentius/chant-lexicon-sql/clickhouse`, next to the tables they name, and set `access: true` on the profiles of the environments I name.
Declare no password: leave a user that needs one without an `IDENTIFIED` clause, and list it in the pull request description for a person to create.
Run `npx chant build` and `npx chant lint`, then `npx chant sql plan` against the local server from `npx chant emulator up --lexicon sql`, and open a pull request with the plan in its description.
Never deploy to a real cloud account or shared environment (with `chant run`, `chant components` or a platform's own CLI), approve (a pull request review, `chant approve` or `chant run approve`), override a policy denial (`chant approve policy-override`), or merge; never cut or publish a release (`chant release cut --yes` or `chant release publish`), push or delete a `ci/*` tag (`chant ci tick` pushes them), or change the value of an existing flag; never touch `.chant/allowed_signers` or `chant/lifecycle`.

ClickHouse keeps users, roles, row policies, quotas, settings profiles and grants outside any table. The sql lexicon declares each with its own tag from @intentius/chant-lexicon-sql/clickhouse:

import { database, grant, policy, role, table, user } from "@intentius/chant-lexicon-sql/clickhouse";
export const shop = database`CREATE DATABASE shop`;
export const events = table`
CREATE TABLE ${shop}.events (id UInt64, tenant String)
ENGINE = MergeTree ORDER BY id`;
export const reader = role`CREATE ROLE reader SETTINGS max_memory_usage = 1000000000`;
export const app = user`CREATE USER app HOST ANY DEFAULT ROLE ${reader} DEFAULT DATABASE shop`;
export const tenantA = policy`
CREATE ROW POLICY tenant_a ON ${events}
USING ${events.columns.tenant} = 'a' TO ${reader}`;
export const readEvents = grant`GRANT SELECT(id, tenant) ON ${events} TO ${reader}`;
export const readerToApp = grant`GRANT ${reader} TO ${app}`;
TagStatementEntity type
userCREATE USERClickHouse::User
roleCREATE ROLEClickHouse::Role
policyCREATE ROW POLICYClickHouse::RowPolicy
quotaCREATE QUOTAClickHouse::Quota
settingsProfileCREATE SETTINGS PROFILE or CREATE PROFILEClickHouse::SettingsProfile
grantGRANTClickHouse::Grant

A quota limits what queries use per interval, and a settings profile holds settings and their constraints, for the users and roles each applies to:

import { quota, settingsProfile } from "@intentius/chant-lexicon-sql/clickhouse";
export const base = settingsProfile`CREATE SETTINGS PROFILE base SETTINGS max_threads = 4`;
export const analysts = settingsProfile`
CREATE SETTINGS PROFILE analysts
SETTINGS PROFILE ${base}, max_memory_usage = 10000000000 MIN 1000000 MAX 20000000000, readonly = 1 CONST
TO ${reader}`;
export const perUser = quota`
CREATE QUOTA per_user KEYED BY user_name
FOR INTERVAL 1 hour MAX queries = 1000, errors = 50,
FOR RANDOMIZED INTERVAL 1 day MAX read_bytes = 1000000000000
TO ${reader}`;

Interpolate a role, a user, a profile or a table where a statement names it. The reference renders as the name and makes a dependency, so a role is created before the user whose default role it is, and a table before its policy.

Access is off until the environment’s profile turns it on, as on Postgres, so a project whose users and grants are managed by another tool, or by hand, is not affected:

export default {
lexicons: ["sql"],
sql: {
dialect: "clickhouse",
profiles: {
prod: { url: "https://ch.internal:8443", user: { env: "CH_USER" }, password: { env: "CH_PASSWORD" }, access: true },
},
},
} satisfies ChantConfig;

With access: true, the plan, the applier and chant lifecycle diff --live read the declared users, roles, row policies, quotas, settings profiles and grants and compare them. Without it, none of them is read: chant sql plan leaves them out (a hint counts them), the applier reports each as filtered, and the drift read reports each as unobserved, filtered. A server bound by CLICKHOUSE_URL has no profile, so access is off there.

What the project declares and what the environment keeps

Section titled “What the project declares and what the environment keeps”

A password is the environment’s. A user template refuses IDENTIFIED BY and any IDENTIFIED WITH that holds a password or its hash, so no secret is ever written into a build. A user declared with no IDENTIFIED clause is not created by chant. The apply reports it as not attempted, and the grants to it wait. Create it once with its password (CREATE USER app IDENTIFIED BY ...), and the next apply makes the rest of it as declared and leaves the password alone. A user declared with a method that holds no secret (ssl_certificate, ldap, kerberos, http, ssh_key with its public key, NOT IDENTIFIED) is created by chant and kept that way. Statements written ahead of time for a migration (chant sql diff --statements, diffStatements) take such a user to exist by the time they run, as the grants to it do: its declared clauses are set with one ALTER USER each, DEFAULT ROLE after the grants it names, and its password is left alone.

Grants are compared per grantee. Every grant declaration that names a grantee adds up to that grantee’s complete list. A privilege or role granted to it by hand is revoked, and one revoked by hand is granted again. A grantee the build names in no grant keeps whatever it holds.

None of these objects has a comment, so none carries chant’s ownership marker. A plan reads only the users, roles, row policies, quotas, settings profiles and grantees the build declares, and an object made by hand is never seen. Chant creates and changes them, and never drops one: drop by hand what the build no longer declares.

Each is read back with SHOW CREATE USER, SHOW CREATE ROLE, SHOW CREATE ROW POLICY, SHOW CREATE QUOTA, SHOW CREATE SETTINGS PROFILE and SHOW GRANTS FOR, and compared clause by clause. The server’s way of printing is undone first:

  • a clause at its default is not compared: HOST ANY, DEFAULT ROLE ALL, GRANTEES ANY, a row policy’s AS PERMISSIVE and FOR SELECT;
  • hosts and the roles a clause names are sorted, a setting’s READONLY is CONST, and an inherited PROFILE 'base' is INHERIT base;
  • a quota’s intervals are compared by their length in seconds (60 minute is 1 hour, 3 month is 1 quarter), each interval’s limits sorted, lower case and with their numbers as the server keeps them (1e9 is 1000000000); an interval with NO LIMITS is no interval;
  • a row policy’s condition goes to the server’s formatter, which prints tenant='a' AND id+0>0 as (tenant = 'a') AND ((id + 0) > 0);
  • the server folds a grant another covers: SELECT(id) under SELECT on the same table, SELECT ON shop.events under SELECT ON shop.*, any privilege under *.*. The declarations are folded the same way.

A user’s authentication is compared only when the declaration states it.

ChangeRuleStatement
a role’s settingsSQLCH270ALTER ROLE ... SETTINGS
a user’s host, default role, default database, grantees, settings, expiry or a stated authenticationSQLCH271one ALTER USER per clause
a row policy’s condition, kind or rolesSQLCH272CREATE ROW POLICY OR REPLACE
a quota’s key, one interval’s limits, or whom it applies toSQLCH275one ALTER QUOTA per clause or interval; NO LIMITS takes an interval away
a settings profile’s settings or whom it applies toSQLCH276ALTER SETTINGS PROFILE ... SETTINGS or ... TO
a privilege or role a grantee lacksSQLCH273GRANT
a privilege or role a grantee holds besides its declarationsSQLCH274REVOKE

A quota and a settings profile are changed with ALTER, never CREATE ... OR REPLACE: replacing one makes a new object under the name, and a profile that inherits it, or a user given it, loses it.

The revokes run before the grants. A user’s DEFAULT ROLE names roles the user must already hold, so when the build also declares grants to the user, it is set after them.

chant lifecycle diff --live observes users, roles, row policies, quotas and settings profiles, so one changed by hand shows as property drift. Each grant declaration is read privilege by privilege: one the grantee no longer holds (revoked by hand) shows as property drift on that declaration, at grants.<grantee>.<privilege> ON <target>, and a grant whose grantee the server does not have is missing. A privilege or role a grantee holds that no declaration gives it (granted by hand) belongs to no one declaration, so it is listed under PENDING, one line per grantee with every statement an apply would send it, the same ones chant sql plan reports. A gated ApplyOp binds its approval to that diff, so a privilege granted or revoked by hand after the approval moves the plan digest, and the apply stops at the gate until the new plan is approved.

These objects belong to no database, so a Replicated database’s log does not carry them. On a cluster every statement goes ON CLUSTER. In the replicated topology they go ON CLUSTER the topology’s cluster when it names one (replicated:<cluster>). With none, apply to each replica, or keep access entities in a replicated user directory on the server.

chant