-
Notifications
You must be signed in to change notification settings - Fork 30
/
read_only_user.ts
163 lines (150 loc) · 4.16 KB
/
read_only_user.ts
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
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web'
import { randomUUID } from 'crypto'
/**
* An illustration of limitations and client-specific settings for users created in `READONLY = 1` mode.
*/
void (async () => {
const defaultClient = createClient()
// using the default (non-read-only) user to create a read-only one for the purposes of the example
const guid = randomUUID().replace(/-/g, '')
const readOnlyUsername = `clickhouse_js_examples_readonly_user_${guid}`
const readOnlyPassword = `${guid}_pwd`
const commands = [
`
CREATE USER ${readOnlyUsername}
IDENTIFIED WITH sha256_password BY '${readOnlyPassword}'
DEFAULT DATABASE default
SETTINGS readonly = 1
`,
`
GRANT SHOW TABLES, SELECT
ON default.*
TO ${readOnlyUsername}
`,
]
for (const query of commands) {
await defaultClient.command({
query,
clickhouse_settings: {
wait_end_of_query: 1,
},
})
}
console.log(
`Created user ${readOnlyUsername} with restricted access to the system database`,
)
printSeparator()
// and a test table with some data in there
const testTableName = 'clickhouse_js_examples_readonly_user_test_data'
await defaultClient.command({
query: `
CREATE OR REPLACE TABLE ${testTableName}
(id UInt64, name String)
ENGINE MergeTree()
ORDER BY (id)
`,
clickhouse_settings: {
wait_end_of_query: 1,
},
})
await defaultClient.insert({
table: testTableName,
values: [
{ id: 12, name: 'foo' },
{ id: 42, name: 'bar' },
],
format: 'JSONEachRow',
})
// Read-only user
let readOnlyUserClient = createClient({
username: readOnlyUsername,
password: readOnlyPassword,
})
// read-only user cannot insert the data into the table
await readOnlyUserClient
.insert({
table: testTableName,
values: [
{ id: 12, name: 'foo' },
{ id: 42, name: 'bar' },
],
format: 'JSONEachRow',
})
.catch((err) => {
console.error(
'[Expected error] Readonly user cannot insert the data into the table. Cause:\n',
err,
)
})
printSeparator()
// ... cannot query from system.users because no grant (system.numbers will still work, though)
await readOnlyUserClient
.query({
query: 'SELECT * FROM system.users LIMIT 5',
format: 'JSONEachRow',
})
.catch((err) => {
console.error(
'[Expected error] Cannot query system.users cause it was not granted. Cause:\n',
err,
)
})
printSeparator()
// ... can query the test table since it is granted
const rs = await readOnlyUserClient.query({
query: `SELECT * FROM ${testTableName}`,
format: 'JSONEachRow',
})
console.log('Select result:', await rs.json())
printSeparator()
// ... cannot use ClickHouse settings
await readOnlyUserClient.close()
readOnlyUserClient = createClient({
username: readOnlyUsername,
password: readOnlyPassword,
clickhouse_settings: {
send_progress_in_http_headers: 1,
},
})
await readOnlyUserClient
.query({
query: `SELECT * FROM ${testTableName}`,
format: 'JSONEachRow',
})
.catch((err) => {
console.error(
`[Expected error] Cannot modify 'send_progress_in_http_headers' setting in readonly mode. Cause:\n`,
err,
)
})
printSeparator()
// ... cannot use response compression. Request compression is still allowed.
await readOnlyUserClient.close()
readOnlyUserClient = createClient({
username: readOnlyUsername,
password: readOnlyPassword,
compression: {
response: true,
},
})
await readOnlyUserClient
.query({
query: `SELECT * FROM ${testTableName}`,
format: 'JSONEachRow',
})
.catch((err) => {
console.error(
`[Expected error] Cannot enable compression setting in readonly mode. Cause:\n`,
err,
)
})
printSeparator()
console.log('All done!')
await readOnlyUserClient.close()
await defaultClient.close()
})()
function printSeparator() {
console.log(
'------------------------------------------------------------------------',
)
}