Repository navigation
fix(grants): quote grantees, ignore column/partial grants, run DCL ON CLUSTER on Replicated databases - #757
DeviousCardi wants to merge 3 commits into
Conversation
3452199 to
cdb445c
Compare
|
Merged current |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 710e3b5. Configure here.
… 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
6d28978 to
f5da3e5
Compare
|
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! |
|
Hi @koletzilla, thanks for looking at it. Happy to give the context. Setup. I run dbt-clickhouse against a multi-node cluster with a How it bit me. Two of the three failures were blockers, not cosmetic:
The column/partial-grant case was less dramatic for us, but it is why Workaround in the meantime. I quoted grantees by hand ( 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 I reproduced all three on the repo's 3-node compose cluster on 25.8 and added tests for each, including |
|
Closing this PR as it breaks our AI_POLICY.md
|

Summary
Fixes #756. Follows the fix outlined in the issue for all three parts of
dbt/include/clickhouse/macros/adapters/apply_grants.sql.clickhouse__get_grant_sqlandclickhouse__get_revoke_sqlwrap each grantee inadapter.quote, so names likeetl-userwork ingrants:and in the revokes dbt issues for grants it did not configure.adapter.quote(theSQLAdapterdefault) did not pass through names the user had already quoted:'"etl-user"'became""etl-user"", a syntax error.ClickHouseAdapter.quotenow 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").clickhouse__get_show_grant_sqlgroups 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: theHAVINGclause usesNOT is_partial_revoke/countIf(is_partial_revoke)instead ofis_partial_revoke = 0/= 1. The dbt-tests-adapter grants tests detect REVOKE statements by searching the debug log for"revoke ", andis_partial_revoke = 0matched that search, which madeTestSeedGrants,TestSnapshotGrantsandTestIncrementalGrantsfail.ON CLUSTERon Replicated databases. A newon_cluster_clause_dclmacro addsON CLUSTERwheneverclusteris set, unlessdisable_on_clusteris set, regardless ofdatabase_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 CLUSTERfor a relation withcan_on_cluster=False(the Replicated case);disable_on_cluster; no clause without a cluster; theHAVINGfilter; escaping of'and\in relation names;ClickHouseAdapter.quotepass-through.tests/integration/adapter/grants/test_model_grants.py:TestSpecialCharacterGrantee: grants SELECT to adbt-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 adb.*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 aReplicateddatabase withgrants: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:
tests/integration/adapter/grants, HTTP,DBT_CH_TEST_CLUSTER=test_shardCode: 62 Syntax error … -test-user-…, column grant reported as granted,assert 1 == 3(grant on 1 of 3 nodes)tests/unitmake lintChecklist
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_sqlnow quote each grantee viaadapter.quote, andClickHouseAdapter.quoteleaves names that are already wrapped in"…"or`…`unchanged so hand-quoted grantees do not double-quote.Reading existing grants:
clickhouse__get_show_grant_sqlonly treats a privilege as granted when there is a table-wide row insystem.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_dcladdsON CLUSTERto GRANT/REVOKE whenever a cluster is configured (honouringdisable_on_cluster), includingReplicateddatabases where table DDL skipsON CLUSTERbut access control does not replicate.Coverage is added via
tests/unit/test_apply_grants.pyand 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.