↑ 点击关注 ↑ 更多技术干货
业务场景
Hello World
── conf├── config.xml└── users.xml
clickhouse server --config=/etc/clickhouse/conf/config.xml
The server tracks changes in config files... and reloads the settings for users and clusters on the fly (https://clickhouse.com/docs/en/operations/configuration-files/#implementation-details)
<?xml version="1.0"?><yandex><logger><level>trace</level><log>/tmp/log/clickhouse-server.log</log><errorlog>/tmp/log/clickhouse-server.err.log</errorlog><size>1000M</size><count>10</count></logger><query_log><database>system</database><table>query_log</table><partition_by>toYYYYMM(event_date)</partition_by><flush_interval_milliseconds>1000</flush_interval_milliseconds></query_log><tcp_port>9000</tcp_port><listen_host>127.0.0.1</listen_host><access_control_path>/tmp/ledzeppelin/</access_control_path><max_concurrent_queries>500</max_concurrent_queries><mark_cache_size>5368709120</mark_cache_size><path>./clickhouse/</path><users_config>users.xml</users_config></yandex>
<?xml version="1.0"?><yandex><profiles><default><readonly>1</readonly></default><pA><max_memory_usage>10G</max_memory_usage><max_memory_usage_for_user>10G</max_memory_usage_for_user><max_memory_usage_for_all_queries>10G</max_memory_usage_for_all_queries><max_query_size>1073741824</max_query_size><readonly>1</readonly></pA></profiles><users><zeppelin><!-- <password_sha256_hex></password_sha256_hex> --><password>password</password><networks><ip>::/0</ip></networks><profile>pA</profile><quota>qA</quota></zeppelin></users><quotas><qA><interval><!-- 以秒为单位 --><duration>10</duration><!-- 10 秒内只能查询一次 --><queries>2</queries><errors>0</errors><result_rows>0</result_rows><read_rows>0</read_rows><execution_time>0</execution_time></interval></qA></quotas></yandex>


Quotas 和 Profile
Quota Profile
Quotas allow you to limit resource usage over a period of time or track the use of resources. A settings profile is a collection of settings grouped under the same name.

A maximum number of rows that can be read from a table when running a query. (https://clickhouse.com/docs/en/operations/settings/query-complexity/#max-rows-to-read)




SQL 驱动的访问控制
We recommend using SQL-driven workflow. Both of the configuration methods work simultaneously, so if you use the server configuration files for managing accounts and access rights, you can smoothly switch to SQL-driven workflow. (https://clickhouse.com/docs/en/operations/access-rights/)
ClickHouse 推荐使用 SQL 驱动的访问控制方式。第 2 节展示了”Server configuration files users.xml and config.xml“ 的控制方式,对于不复杂的场景而言,已经够用。配置文件的方式涉及到安全问题,并且在增加用户的时候存在不灵活的问题,包括管理静态用户,动态(临时)用户。现在我们将通过一系列的实际操作验证SQL驱动方式的可行性。
通过 default用户创建超级管理员 ledzeppelin 以及用户 zeppelin。(如果在 users.xml 定义了 zeppelin, 之后就无法更改, 报错: Cannot update user zeppelin in users.xml because this storage is readonly)。 解除 default 的权限。 以超级管理员的身份, 通过 SQL 动态配置 zeppelin 资源

<user><default><!-- 其他配置 --><access_management>1</access_management></default></user>
CREATE USER ledzeppelin;GRANT ALL ON *.* TO ledzeppelin WITH GRANT OPTION;CREATE USER zeppelin;
CREATE USER MJ IDENTIFIED WITH sha256_password BY 'qwerty';
clickhouse-client -u MJ --password qwerty
clickhouse-client -u MJ --password ${CLICKHOUSE_MJ_PASSWORD}
REVOKE ALL ON *.* FROM defaultQuery id: 7ab4a62a-62d1-4d0b-bbfe-c27d0e595f650 rows in set. Elapsed: 0.002 sec.Received exception from server (version 21.10.2):Code: 495. DB::Exception: Received from localhost:9000. DB::Exception: Cannot update user `default` in users.xml because this storage is readonly: Couldn't update user `default`. Successfully updated: none. (ACCESS_STORAGE_READONLY)
<profiles><!-- Profile that allows only read queries. --><readonly_profile><readonly>2</readonly><!-- 其他配置 --></readonly_profile></profiles>
<users><profile> readonly_profile</profile><!-- 其他配置 --></users>
<access_management>1</access_management>
CREATE SETTINGS PROFILE IF NOT EXISTS z_profileSETTINGSreadonly = 2READONLY
CREATE QUOTA IF NOT EXISTS z_quotaFOR INTERVAL 10 secondMAX queries 1
CREATE ROLE IF NOT EXISTS z_role
GRANT SELECT ON db.* TO z_role;
CREATE ROLE IF NOT EXISTS gc_role
ALTER SETTINGS PROFILE z_profile TO z_roleALTER QUOTA z_quota TO z_role;
SELECT * FROM system.role_grants WHERE user_name LIKE 'zeppelin'
GRANT z_role TO zeppelin
换一个 role 改当前 role 的 profile/quota
`revoke z_role from zeppelin` 修改 z_role 下的 profile 或 quota `grant z_role from zeppelin`
修改 z_role 下的 profile 或 quota `revoke z_role from zeppelin` `grant z_role from zeppelin`
CREATE QUOTA IF NOT EXISTS z_quota_5FOR INTERVAL 5 secondMAX queries 1
ALTER QUOTA z_quota TO gc_role;
ALTER QUOTA z_quota_5 TO z_role;
revoke z_role from zeppelin;grant z_role to zeppelin;
SELECTname,apply_to_listFROM system.quotasWHERE name LIKE 'z_quota_5'Query id: dc5b2ece-c792-4585-95d8-8b4275631664┌─name──────┬─apply_to_list─┐│ z_quota_5 │ ['z_role'] │└───────────┴───────────────┘1 rows in set. Elapsed: 0.004 sec.
CREATE SETTINGS PROFILE IF NOT EXISTS z_profile_permission_type_0SETTINGSreadonly = 0READONLY
ALTER SETTINGS PROFILE z_profile TO gc_role
ALTER SETTINGS PROFILE z_profile_permission_type_0 TO z_role
revoke z_role from zeppelin;grant z_role to zeppelin;
SELECTname,apply_to_listFROM system.settings_profilesQuery id: 51d1c2de-ced7-4558-a164-020a76a53d97┌─name────────────────────────┬─apply_to_list─┐│ readonly │ [] ││ default │ [] ││ z_profile │ ['gc_role'] ││ z_profile_permission_type_0 │ ['z_role'] │└─────────────────────────────┴───────────────┘
5.SQL驱动设置持久化
Path to a folder where a ClickHouse server stores user and role configurations created by SQL commands.
设置位置: config.xml
<yandex>...<access_control_path>/var/lib/clickhouse/access/ledzeppelin/</access_control_path>...</yandex
╭─/var/lib/clickhouse/access/ledzeppelin╰─➤ ls094e5b76-1b4d-97a5-43cd-bbb3ccceb688.sql 9c492851-bf60-7753-7c14-c05f4be46b8e.sql2949a246-848f-fc07-f1b9-a3f7246b31a0.sql f340165d-0ec4-f90e-fb55-74c8d0cfd7ae.sql394e47fd-b701-d017-6857-0d8744d830b1.sql quotas.list3d44c8a1-01b3-6f68-eff7-1e20a9080296.sql roles.list3f447b54-b515-aa93-bacc-1a649aeac19c.sql row_policies.list674912da-5593-e497-c5d8-d058e916c7a5.sql settings_profiles.list6c44ad5c-4354-c154-048e-8e293ff427ab.sql users.list

结论
参考
https://altinity.com/blog/goodbye-xml-hello-sql-clickhouse-user-management-goes-pro
https://clickhouse.com/docs/en/operations/access-rights/
Quotas:https://clickhouse.com/docs/en/operations/quotas/
Settings Profiles:
https://clickhouse.com/docs/en/operations/settings/settings-profiles/
ClickHouse 的推荐方式:
https://clickhouse.com/docs/en/operations/access-rights/#enabling-access-control
关于 GrowingIO
作为国内领先的一站式数据增长引擎整体方案服务商,GrowingIO以数据智能分析为核心,通过构建客户数据平台,打造增长营销闭环,帮助企业提升数据驱动能力,赋能商业决策、实现业务增长。
GrowingIO专注于零售、电商、保险、酒旅航司、教育、内容社区等行业,成立以来,累计服务超过1500家企业级客户,获得LVMH集团、百事、达能、老佛爷百货、戴尔、lululemon、美素佳儿、宜家、乐高、美的、海尔、安踏、汉光百货、上汽集团、广汽蔚来、理想汽车、招商仁和人寿、飞鹤、红星美凯龙、东方航空、滴滴、新东方、喜茶、每日优鲜、奈雪的茶、永辉超市等客户的青睐。
招聘信息
GrowingIO技术团队是一个活力四射、对技术充满激情的团队,多个岗位持续招聘中!诚招前端工程师/大数据工程师/Java工程师等,欢迎有兴趣的同学投递简历至:jianli@growingio.com(邮件标题请注明具体岗位名称),更多职位及信息可进入招聘官网查看。

点击「阅读原文」获取 GrowingIO 15 天免费试用!
↓↓↓




