Skip to content

fix(grants): quote grantees, ignore column/partial grants, run DCL ON CLUSTER on Replicated databases - #757

Closed
DeviousCardi wants to merge 3 commits into
ClickHouse:mainfrom
DeviousCardi:fix/756-grants-quoting-cluster
Closed

DeviousCardi wants to merge 3 commits into
ClickHouse:mainfrom
DeviousCardi:fix/756-grants-quoting-cluster

Conversation

@DeviousCardi

@DeviousCardi DeviousCardi commented Oct 15, 2026 •

Copy link
Copy Markdown

Summary

Fixes #756. Follows the fix outlined in the issue for all three parts of dbt/include/clickhouse/macros/adapters/apply_grants.sql.

  1. Grantees are quoted. clickhouse__get_grant_sql and clickhouse__get_revoke_sql wrap each grantee in adapter.quote, so names like etl-user work in grants: and in the revokes dbt issues for grants it did not configure. adapter.quote (the SQLAdapter default) did not pass through names the user had already quoted: '"etl-user"' became ""etl-user"", a syntax error. ClickHouseAdapter.quote now returns names already wrapped in "…" or `…` unchanged, so projects that quoted grantees by hand as a workaround keep working. Unquoted names still render as before ("name").
  2. Column grants and partial revokes are no longer read as table grants. clickhouse__get_show_grant_sql groups by (privilege, grantee) and keeps a pair only when it has a table-wide row (column IS NULL, not a partial revoke) and no partial revoke on the table. dbt then grants the table-wide privilege, which also clears the partial revoke. The schema and table names are escaped in the string literals (\ and '). One deviation from the SQL in the issue: the HAVING clause uses NOT is_partial_revoke / countIf(is_partial_revoke) instead of is_partial_revoke = 0 / = 1. The dbt-tests-adapter grants tests detect REVOKE statements by searching the debug log for "revoke ", and is_partial_revoke = 0 matched that search, which made TestSeedGrants, TestSnapshotGrants and TestIncrementalGrants fail.
  3. DCL runs ON CLUSTER on Replicated databases. A new on_cluster_clause_dcl macro adds ON CLUSTER whenever cluster is set, unless disable_on_cluster is set, regardless of database_engine. on_cluster_clause (table DDL) is unchanged.

Tests

  • tests/unit/test_apply_grants.py (new, uses the macro harness): grantee quoting, including names that are already quoted; ON CLUSTER for a relation with can_on_cluster=False (the Replicated case); disable_on_cluster; no clause without a cluster; the HAVING filter; escaping of ' and \ in relation names; ClickHouseAdapter.quote pass-through.
  • tests/integration/adapter/grants/test_model_grants.py:
    • TestSpecialCharacterGrantee: grants SELECT to a dbt-test-user-… user, adds a hand-made INSERT grant to it, then switches the grant to another user. dbt has to revoke both.
    • TestColumnAndPartialGrantsAreNotTableGrants: a column grant (SELECT(name)) and a db.* grant with this table revoked are not reported as table grants, and dbt turns the column grant into a table-wide grant.
    • TestReplicatedDatabaseGrantsOnCluster (cluster only): a model in a Replicated database with grants: ends up with the grant on every node (clusterAllReplicas(..., system.grants)).

Run locally against the repo's 3-node docker-compose cluster, ClickHouse 25.8:

run result
tests/integration/adapter/grants, HTTP, DBT_CH_TEST_CLUSTER=test_shard 9 passed
same, native driver (port 10900) 9 passed
same, no cluster 7 passed, 2 skipped (cluster-only)
new integration tests with the old macros 3 failed: Code: 62 Syntax error … -test-user-…, column grant reported as granted, assert 1 == 3 (grant on 1 of 3 nodes)
tests/unit 102 passed (10 of the 12 new tests fail with the old code)
make lint passes

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A changelog entry was added following Updating the Changelog (user-facing, concise, PR link)

AI disclosure: Claude Code prepared the implementation and tests.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Y1b55sEL1gA1c3FCMcQCbm

Written by Codex.


Note

Medium Risk
Changes authorization (GRANT/REVOKE) and cluster DCL behaviour on Replicated databases; mistakes could mis-apply or miss permissions cluster-wide, though behaviour is heavily tested.

Overview
This PR hardens model grants: so GRANT/REVOKE behaves correctly on ClickHouse clusters and with awkward grantee names.

Grantee quoting: clickhouse__get_grant_sql / clickhouse__get_revoke_sql now quote each grantee via adapter.quote, and ClickHouseAdapter.quote leaves names that are already wrapped in "…" or `…` unchanged so hand-quoted grantees do not double-quote.

Reading existing grants: clickhouse__get_show_grant_sql only treats a privilege as granted when there is a table-wide row in system.grants (not column-level grants or partial revokes), escapes database/table literals, and filters so the introspection SQL cannot trip dbt’s log-based REVOKE detection.

Cluster propagation: A new on_cluster_clause_dcl adds ON CLUSTER to GRANT/REVOKE whenever a cluster is configured (honouring disable_on_cluster), including Replicated databases where table DDL skips ON CLUSTER but access control does not replicate.

Coverage is added via tests/unit/test_apply_grants.py and expanded integration scenarios (special-character users, column/partial grants, grants on all replicas), plus a CHANGELOG bug-fix note.

Reviewed by Cursor Bugbot for commit f5da3e5. Bugbot is set up for automated code reviews on this repo. Configure here.

@DeviousCardi
DeviousCardi force-pushed the fix/756-grants-quoting-cluster branch from 3452199 to cdb445c Compare October 15, 2026 22:04
@DeviousCardi

Copy link
Copy Markdown
Author

Merged current main in 710e3b5 and resolved the changelog conflict, so this PR is mergeable again. The prior ClickHouse Cloud SharedMergeTree job failed before exercising the tests: its log shows connection refused at https://localhost:8443 across the suite. The regular HTTP and native CI matrix passed. The new commit will rerun checks; I will follow up if the Cloud job reveals a code failure.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 710e3b5. Configure here.

DeviousCardi and others added 3 commits November 13, 2026 07:08
… CLUSTER on Replicated databases

- clickhouse__get_grant_sql / clickhouse__get_revoke_sql quote every grantee
  with adapter.quote. ClickHouseAdapter.quote now passes through names that
  are already wrapped in double quotes or backticks.
- clickhouse__get_show_grant_sql only reports a (privilege, grantee) pair
  when system.grants has a table-wide grant for it and no partial revoke on
  the table, and escapes the schema and table names in its string literals.
- GRANT/REVOKE use a new on_cluster_clause_dcl macro: ON CLUSTER whenever a
  cluster is configured (unless disable_on_cluster), including Replicated
  databases, which do not replicate access entities.

Fixes ClickHouse#756

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y1b55sEL1gA1c3FCMcQCbm
@DeviousCardi
DeviousCardi force-pushed the fix/756-grants-quoting-cluster branch from 6d28978 to f5da3e5 Compare November 13, 2026 01:38
@koletzilla

Copy link
Copy Markdown
Contributor

Hi @DeviousCardi !

First of all, thanks for the PR.

I want to ask you more about this issue because when I reported it I wasn't sure if this was really impacting someone. Can you give me more context about your use case? Can you also give me more details about how these things where impacting particularly to you, what was the workaround you found in the meantime, and how this PR is going to unblock your particular situation?

Thanks!

@DeviousCardi

Copy link
Copy Markdown
Author

Hi @koletzilla, thanks for looking at it. Happy to give the context.

Setup. I run dbt-clickhouse against a multi-node cluster with a Replicated database, and the models use dbt's grants: config rather than hand-issued GRANTs. Grantee names come from our identity setup and are not all plain identifiers (the etl-user-style hyphenated role is exactly the shape that broke), and the cluster also has a few legacy grants that were applied by hand before the models were managed by dbt.

How it bit me. Two of the three failures were blockers, not cosmetic:

  • A role name with a hyphen makes GRANT/REVOKE a Code: 62 syntax error, so any model using that role in grants: fails. Worse, because dbt also revokes grants it did not configure, an unrelated hand-made grant to that role on a dbt-managed table makes the run fail even when the role is not in the project.
  • On the Replicated database, on_cluster_clause() is intentionally empty for table DDL (Fix on cluster clause behaviour #447), and the grant macros inherited that. The grant therefore landed on whichever node dbt connected to. A revoked role kept access on the other replicas, which is the kind of thing that is easy to miss until an audit.

The column/partial-grant case was less dramatic for us, but it is why grants: {select: [user_1]} silently did nothing for a user who only had SELECT(a): dbt read the column grant as a table grant and skipped the GRANT.

Workaround in the meantime. I quoted grantees by hand ('"etl-user"') and avoided column-level grants and partial revokes in dbt. For the cluster problem I wrapped the whole model run with a manual GRANT ... ON CLUSTER step afterwards, which is exactly the kind of out-of-band step that drifts.

What this PR changes for that setup. Quoting becomes automatic (and hand-quoted names still work, so the workaround does not double-quote), column/partial grants are no longer mistaken for table grants, and DCL gets ON CLUSTER whenever a cluster is configured, including Replicated databases. That removes the manual post-run GRANT and lets the grants: config be the single source of truth again.

I reproduced all three on the repo's 3-node compose cluster on 25.8 and added tests for each, including clusterAllReplicas(..., system.grants) for the Replicated case. Happy to adjust scope or split anything if you would prefer a narrower change.

@koletzilla

Copy link
Copy Markdown
Contributor

Closing this PR as it breaks our AI_POLICY.md
Reasons:

  • Low-effort pull requests that require high effort from maintainers will be closed.
  • Do not use AI to automate your responses to maintainers.

@koletzilla koletzilla closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

grants issues: grantees are not quoted, column/partial grants are misread, and DCL skips ON CLUSTER on Replicated databases

2 participants