forked from fbrcode/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBAC.sql
More file actions
234 lines (148 loc) · 6 KB
/
RBAC.sql
File metadata and controls
234 lines (148 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-- create user
CREATE USER 'test' IDENTIFIED WITH plaintext_password BY '123456';
SHOW USERS;
-- create role
CREATE ROLE ch_test;
SHOW ROLES;
-- create user with default role
CREATE USER 'test_1' IDENTIFIED WITH plaintext_password BY '123456' DEFAULT ROLE ch_test;
-- create user with expiration date
CREATE USER 'test_2' IDENTIFIED WITH plaintext_password BY '123456' VALID UNTIL '2025-01-01'
-- create user that is allowed to receive privileges from user "test"
CREATE USER 'test_3' IDENTIFIED WITH plaintext_password BY '123456' GRANTEES test
-- modify user to receive privileges from user "test_1"
ALTER USER test_3 GRANTEES test_1;
-- drop user(s)
DROP USER test, test_1, test_2, test_3;
------- Role
-- create database
CREATE DATABASE datasets;
SHOW DATABASES;
CREATE TABLE datasets.xyz (ID Int32, Name String) ENGINE = MergeTree ORDER BY ID;
SHOW TABLES FROM datasets;
DESCRIBE TABLE datasets.xyz;
INSERT INTO datasets.xyz VALUES (1, 'a'), (2, 'b'), (3, 'c');
SELECT * FROM datasets.xyz;
-- create role
CREATE ROLE 'test_role';
-- grant select privilege to role
GRANT SELECT ON datasets.* TO test_role;
-- revoke insert privilege from role
REVOKE INSERT ON datasets.* FROM test_role;
-- create user
CREATE USER 'test' IDENTIFIED WITH plaintext_password BY '123456';
-- grant role to user
GRANT test_role TO test;
-- login as user test and try to select and insert data
-- clickhouse client --user test --password 123456
-- show current user
SELECT currentUser();
SELECT * FROM datasets.xyz;
INSERT INTO datasets.xyz VALUES (4, 'd'), (5, 'e'), (6, 'f');
/*
Code: 497.
DB::Exception: Received from localhost:9000.
DB::Exception: test: Not enough privileges.
To execute this query, it's necessary to have the grant INSERT(ID, Name) ON datasets.xyz. (ACCESS_DENIED)
*/
-- rename role
ALTER ROLE test_role RENAME TO test_role_new;
DROP ROLE test_role_new;
SHOW ROLES;
----- Settings profile
INSERT INTO datasets.xyz SELECT * FROM generateRandom() LIMIT 10000;
-- display current settings profiles
SHOW SETTINGS PROFILES;
-- display details of a settings profile
SHOW CREATE SETTINGS PROFILE default;
-- create a settings profile
CREATE SETTINGS PROFILE settings_profile_1 SETTINGS max_threads = 8;
-- display details of a settings profile
SHOW CREATE SETTINGS PROFILE settings_profile_1;
-- create role based on settings profile
CREATE ROLE settings_profile_1_role SETTINGS PROFILE settings_profile_1;
-- grant select privilege to role
GRANT SELECT ON datasets.* TO settings_profile_1_role;
CREATE USER test_settings IDENTIFIED WITH PLAINTEXT_PASSWORD BY '123456' DEFAULT ROLE settings_profile_1_role;
-- login as user test_settings and try to select data
SELECT * FROM datasets.xyz LIMIT 100;
DROP PROFILE settings_profile_1;
-- display role grants
SHOW GRANTS FOR settings_profile_1_role;
-- show user details
SHOW CREATE USER test_settings;
--------- Quotas
-- assign role settings_profile_1_role to user test
GRANT settings_profile_1_role TO test;
-- display current quotas
SHOW QUOTAS;
-- create quota
CREATE QUOTA example_quota FOR INTERVAL 15 minute MAX queries = 2 TO test;
/* message when user test reaches the limit
Code: 201.
DB::Exception: Received from localhost:9000.
DB::Exception: Quota for user `test` for 900s has been exceeded: queries = 4/2.
Interval will end at 2025-01-17 14:45:00. Name of quota template: `example_quota`. (QUOTA_EXCEEDED)
*/
/* Quotas can be defined for the following parameters:
- queries
- query_selects
- query_inserts
- errors
- result_rows
- result_bytes
- read_rows
- read_bytes
- execution_time
*/
-- change quota limit
ALTER QUOTA IF EXISTS example_quota FOR INTERVAL 15 minute MAX queries = 20 TO test;
DROP QUOTA example_quota;
/* RBAC Example
- Admin Users
• Full access to the databases and Tables
• Perform all the RBAC actions
- Data Owner
• Full access to the Tables in the database called datasets
• Async inserts enabled
- Dashboard User
• Read access to the Tables in the database called datasets
• Allow 100 queries with 10 min interval
• Limit results to 1000 rows
*/
-- Init database
CREATE DATABASE datasets;
CREATE TABLE datasets.hits_v1 (WatchID Int32, UserAgentMajor Int32) ENGINE = MergeTree ORDER BY WatchID;
INSERT INTO datasets.hits_v1 SELECT * FROM generateRandom() LIMIT 1000000;
SELECT COUNT(*) FROM datasets.hits_v1;
-- Admin user
CREATE ROLE 'admin';
GRANT ALL ON *.* TO admin WITH GRANT OPTION;
CREATE USER admin_user identified WITH plaintext_password BY '123456';
GRANT admin TO admin_user;
-- Data owner
CREATE ROLE 'data_owner';
GRANT ALL ON datasets.* TO data_owner;
CREATE SETTINGS PROFILE async_insert_enable SETTINGS async_insert = 1 TO data_owner;
CREATE USER data_owner_user identified WITH plaintext_password BY '123456';
GRANT data_owner TO data_owner_user;
-- Read only
CREATE ROLE 'dashboard';
GRANT SELECT ON datasets.* TO dashboard;
CREATE QUOTA dashboard_quota FOR INTERVAL 10 minute MAX queries = 100, result_rows=1000 TO dashboard;
CREATE USER dashboard_user identified WITH plaintext_password BY '123456';
GRANT dashboard TO dashboard_user;
BACKUP TABLE employee TO Disk('backup_disk', 'employee_2024.zip');
RESTORE TABLE employee AS employee_restored_2024 FROM Disk('backup_disk', 'employee_2024.zip');
SELECT * FROM employee;
INSERT INTO employee VALUES ('e', 'city5');
-- Backup and Restore
BACKUP TABLE employee TO Disk('backup_disk', 'employee_2024_v2.zip')
SETTINGS base_backup = Disk('backup_disk', 'employee_2024.zip')
RESTORE TABLE employee AS employee_restored_2024_v2 FROM Disk('backup_disk', 'employee_2024_v2.zip');
BACKUP TABLE data TO S3('<S3 endpoint>/<directory>', '<Access key ID>', '<Secret access key>')
BACKUP TABLE employee TO AzureBlobStorage('<connection string>/<url>', '<container>', '<path>', '<account name>', '<account key>')
--
clickhouse-local -q "SELECT * FROM file('iris_csv.csv') LIMIT 5"
clickhouse-local -q "DESCRIBE file('iris_csv.csv')"
clickhouse-benchmark --query "SELECT COUNT(DISTINCT WatchID) FROM datasets.hits_v1 hv WHERE UserAgentMajor=24" --password 123456 --timelimit 10