|
| 1 | +import type { ClickHouseError } from '@clickhouse/client' |
| 2 | +import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web' |
| 3 | + |
| 4 | +/** |
| 5 | + * An example of specifying a role using query parameters |
| 6 | + * See https://clickhouse.com/docs/en/interfaces/http#setting-role-with-query-parameters |
| 7 | + */ |
| 8 | +void (async () => { |
| 9 | + const format = 'JSONEachRow' |
| 10 | + const username = 'clickhouse_js_role_user' |
| 11 | + const password = 'role_user_password' |
| 12 | + const table1 = 'clickhouse_js_role_table_1' |
| 13 | + const table2 = 'clickhouse_js_role_table_2' |
| 14 | + |
| 15 | + // Create 2 tables, a role for each table allowing SELECT, and a user with access to those roles |
| 16 | + const defaultClient = createClient() |
| 17 | + await createOrReplaceUser(username, password) |
| 18 | + const table1Role = await createTableAndGrantAccess(table1, username) |
| 19 | + const table2Role = await createTableAndGrantAccess(table2, username) |
| 20 | + await defaultClient.close() |
| 21 | + |
| 22 | + // Create a client using a role that only has permission to query table1 |
| 23 | + const client = createClient({ |
| 24 | + username, |
| 25 | + password, |
| 26 | + // This role will be applied to all the queries by default, |
| 27 | + // unless it is overridden in a specific method call |
| 28 | + role: table1Role, |
| 29 | + }) |
| 30 | + |
| 31 | + // Selecting from table1 is allowed using table1Role |
| 32 | + const resultSet1 = await client.query({ |
| 33 | + query: `select count(*) from ${table1}`, |
| 34 | + format, |
| 35 | + }) |
| 36 | + console.log( |
| 37 | + `Successfully queried from ${table1} using ${table1Role}. Result: `, |
| 38 | + await resultSet1.json(), |
| 39 | + ) |
| 40 | + |
| 41 | + // Selecting from table2 is not allowed using table1Role, |
| 42 | + // which is set by default in the client instance |
| 43 | + await client |
| 44 | + .query({ query: `select count(*) from ${table2}`, format }) |
| 45 | + .catch((e: ClickHouseError) => { |
| 46 | + console.error( |
| 47 | + `Failed to query from ${table2}, as ${table1Role} does not have sufficient privileges. Expected error:`, |
| 48 | + e, |
| 49 | + ) |
| 50 | + }) |
| 51 | + |
| 52 | + // Override the client's role to table2Role, allowing a query to table2 |
| 53 | + const resultSet2 = await client.query({ |
| 54 | + query: `select count(*) from ${table2}`, |
| 55 | + role: table2Role, |
| 56 | + format, |
| 57 | + }) |
| 58 | + console.log( |
| 59 | + `Successfully queried from ${table2} using ${table2Role}. Result:`, |
| 60 | + await resultSet2.json(), |
| 61 | + ) |
| 62 | + |
| 63 | + // Selecting from table1 is no longer allowed, since table2Role is being used |
| 64 | + await client |
| 65 | + .query({ |
| 66 | + query: `select count(*) from ${table1}`, |
| 67 | + role: table2Role, |
| 68 | + format, |
| 69 | + }) |
| 70 | + .catch((e: ClickHouseError) => { |
| 71 | + console.error( |
| 72 | + `Failed to query from ${table1}, as ${table2Role} does not have sufficient privileges. Expected error:`, |
| 73 | + e, |
| 74 | + ) |
| 75 | + }) |
| 76 | + |
| 77 | + // Multiple roles can be specified to allowed querying from either table |
| 78 | + const resultSet3 = await client.query({ |
| 79 | + query: `select count(*) from ${table1}`, |
| 80 | + role: [table1Role, table2Role], |
| 81 | + format, |
| 82 | + }) |
| 83 | + console.log( |
| 84 | + `Successfully queried from ${table1} using roles: [${table1Role}, ${table2Role}]. Result:`, |
| 85 | + await resultSet3.json(), |
| 86 | + ) |
| 87 | + |
| 88 | + const resultSet4 = await client.query({ |
| 89 | + query: `select count(*) from ${table2}`, |
| 90 | + role: [table1Role, table2Role], |
| 91 | + format, |
| 92 | + }) |
| 93 | + console.log( |
| 94 | + `Successfully queried from ${table2} using roles: [${table1Role}, ${table2Role}]. Result: `, |
| 95 | + await resultSet4.json(), |
| 96 | + ) |
| 97 | + |
| 98 | + await client.close() |
| 99 | + |
| 100 | + async function createOrReplaceUser(username: string, password: string) { |
| 101 | + await defaultClient.command({ |
| 102 | + query: `CREATE USER OR REPLACE ${username} IDENTIFIED WITH plaintext_password BY '${password}'`, |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + async function createTableAndGrantAccess( |
| 107 | + tableName: string, |
| 108 | + username: string, |
| 109 | + ) { |
| 110 | + const role = `${tableName}_role` |
| 111 | + |
| 112 | + await defaultClient.command({ |
| 113 | + query: ` |
| 114 | + CREATE OR REPLACE TABLE ${tableName} |
| 115 | + (id UInt32, name String, sku Array(UInt32)) |
| 116 | + ENGINE MergeTree() |
| 117 | + ORDER BY (id) |
| 118 | + `, |
| 119 | + }) |
| 120 | + |
| 121 | + await defaultClient.command({ query: `CREATE ROLE OR REPLACE ${role}` }) |
| 122 | + await defaultClient.command({ |
| 123 | + query: `GRANT SELECT ON ${tableName} TO ${role}`, |
| 124 | + }) |
| 125 | + await defaultClient.command({ query: `GRANT ${role} TO ${username}` }) |
| 126 | + |
| 127 | + return role |
| 128 | + } |
| 129 | +})() |
0 commit comments