-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for role query parameters (#328)
Co-authored-by: Serge Klochkov <[email protected]>
Showing
10 changed files
with
533 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import type { ClickHouseError } from '@clickhouse/client' | ||
import { createClient } from '@clickhouse/client' // or '@clickhouse/client-web' | ||
|
||
/** | ||
* An example of specifying a role using query parameters | ||
* See https://clickhouse.com/docs/en/interfaces/http#setting-role-with-query-parameters | ||
*/ | ||
void (async () => { | ||
const format = 'JSON' | ||
const username = 'role_user' | ||
const password = 'role_user_password' | ||
const table1 = 'table_1' | ||
const table2 = 'table_2' | ||
|
||
// Create 2 tables, a role for each table allowing SELECT, and a user with access to those roles | ||
const defaultClient = createClient() | ||
await createOrReplaceUser(username, password) | ||
const table1Role = await createTableAndGrantAccess(table1, username) | ||
const table2Role = await createTableAndGrantAccess(table2, username) | ||
await defaultClient.close() | ||
|
||
// Create a client using a role that only has permission to query table1 | ||
const client = createClient({ | ||
username, | ||
password, | ||
role: table1Role, | ||
}) | ||
|
||
// Selecting from table1 is allowed using table1Role | ||
let rs = await client.query({ | ||
query: `select count(*) from ${table1}`, | ||
format, | ||
}) | ||
console.log( | ||
`Successfully queried from ${table1} using ${table1Role}. Result: `, | ||
(await rs.json()).data, | ||
) | ||
|
||
// Selecting from table2 is not allowed using table1Role | ||
await client | ||
.query({ query: `select count(*) from ${table2}`, format }) | ||
.catch((e: ClickHouseError) => { | ||
console.error( | ||
`Failed to qeury from ${table2} due to error with type: ${e.type}. Message: ${e.message}`, | ||
) | ||
}) | ||
|
||
// Override the client's role to table2Role, allowing a query to table2 | ||
rs = await client.query({ | ||
query: `select count(*) from ${table2}`, | ||
format, | ||
role: table2Role, | ||
}) | ||
console.log( | ||
`Successfully queried from ${table2} using ${table2Role}. Result: `, | ||
(await rs.json()).data, | ||
) | ||
|
||
// Selecting from table1 is no longer allowed, since table2Role is being used | ||
await client | ||
.query({ | ||
query: `select count(*) from ${table1}`, | ||
format, | ||
role: table2Role, | ||
}) | ||
.catch((e: ClickHouseError) => { | ||
console.error( | ||
`Failed to qeury from ${table1} due to error with type: ${e.type}. Message: ${e.message}`, | ||
) | ||
}) | ||
|
||
// Multiple roles can be specified to allowed querying from either table | ||
rs = await client.query({ | ||
query: `select count(*) from ${table1}`, | ||
format, | ||
role: [table1Role, table2Role], | ||
}) | ||
console.log( | ||
`Successfully queried from ${table1} using roles: [${table1Role}, ${table2Role}]. Result: `, | ||
(await rs.json()).data, | ||
) | ||
|
||
rs = await client.query({ | ||
query: `select count(*) from ${table2}`, | ||
format, | ||
role: [table1Role, table2Role], | ||
}) | ||
console.log( | ||
`Successfully queried from ${table2} using roles: [${table1Role}, ${table2Role}]. Result: `, | ||
(await rs.json()).data, | ||
) | ||
|
||
await client.close() | ||
|
||
async function createOrReplaceUser(username: string, password: string) { | ||
await defaultClient.command({ | ||
query: `CREATE USER OR REPLACE ${username} IDENTIFIED WITH plaintext_password BY '${password}'`, | ||
}) | ||
} | ||
|
||
async function createTableAndGrantAccess( | ||
tableName: string, | ||
username: string, | ||
) { | ||
const role = `${tableName}_role` | ||
|
||
await defaultClient.command({ | ||
query: ` | ||
CREATE OR REPLACE TABLE ${tableName} | ||
(id UInt32, name String, sku Array(UInt32)) | ||
ENGINE MergeTree() | ||
ORDER BY (id) | ||
`, | ||
}) | ||
|
||
await defaultClient.command({ query: `CREATE ROLE OR REPLACE ${role}` }) | ||
await defaultClient.command({ | ||
query: `GRANT SELECT ON ${tableName} TO ${role}`, | ||
}) | ||
await defaultClient.command({ query: `GRANT ${role} TO ${username}` }) | ||
|
||
return role | ||
} | ||
})() |
359 changes: 359 additions & 0 deletions
359
packages/client-common/__tests__/integration/role.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,359 @@ | ||
import type { ClickHouseClient } from '@clickhouse/client-common' | ||
import { createTestClient, TestEnv, whenOnEnv } from '@test/utils' | ||
import { getTestDatabaseName, guid } from '../utils' | ||
import { createSimpleTable } from '../fixtures/simple_table' | ||
import { assertJsonValues, jsonValues } from '../fixtures/test_data' | ||
|
||
describe('role settings', () => { | ||
let defaultClient: ClickHouseClient | ||
let client: ClickHouseClient | ||
|
||
let database: string | ||
let username: string | ||
let password: string | ||
let roleName1: string | ||
let roleName2: string | ||
|
||
beforeAll(async () => { | ||
defaultClient = createTestClient() | ||
username = `clickhousejs__user_with_roles_${guid()}` | ||
password = `CHJS_${guid()}` | ||
roleName1 = `TEST_ROLE_${guid()}` | ||
roleName2 = `TEST_ROLE_${guid()}` | ||
database = getTestDatabaseName() | ||
|
||
await defaultClient.command({ | ||
query: `CREATE USER ${username} IDENTIFIED WITH sha256_password BY '${password}' DEFAULT DATABASE ${database}`, | ||
}) | ||
await defaultClient.command({ | ||
query: `CREATE ROLE IF NOT EXISTS ${roleName1}`, | ||
}) | ||
await defaultClient.command({ | ||
query: `CREATE ROLE IF NOT EXISTS ${roleName2}`, | ||
}) | ||
await defaultClient.command({ | ||
query: `GRANT ${roleName1}, ${roleName2} TO ${username}`, | ||
}) | ||
await defaultClient.command({ | ||
query: `GRANT INSERT ON ${database}.* TO ${roleName1}`, | ||
}) | ||
await defaultClient.command({ | ||
query: `GRANT CREATE TABLE ON * TO ${roleName1}`, | ||
}) | ||
}) | ||
|
||
afterEach(async () => { | ||
await client.close() | ||
}) | ||
|
||
afterAll(async () => { | ||
await defaultClient.close() | ||
}) | ||
|
||
describe('for queries', () => { | ||
async function queryCurrentRoles(role?: string | Array<string>) { | ||
const rs = await client.query({ | ||
query: 'select currentRoles() as roles', | ||
format: 'JSONEachRow', | ||
role, | ||
}) | ||
|
||
const jsonResults = (await rs.json()) as { roles: string[] }[] | ||
return jsonResults[0].roles | ||
} | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should use a single role from the client configuration', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName1, | ||
}) | ||
|
||
const actualRoles = await queryCurrentRoles() | ||
expect(actualRoles).toEqual([roleName1]) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should use multiple roles from the client configuration', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: [roleName1, roleName2], | ||
}) | ||
|
||
const actualRoles = await queryCurrentRoles() | ||
expect(actualRoles.length).toBe(2) | ||
expect(actualRoles).toContain(roleName1) | ||
expect(actualRoles).toContain(roleName2) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should use single role from the query options', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: [roleName1, roleName2], | ||
}) | ||
|
||
const actualRoles = await queryCurrentRoles(roleName2) | ||
expect(actualRoles).toEqual([roleName2]) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should use multiple roles from the query options', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
}) | ||
|
||
const actualRoles = await queryCurrentRoles([roleName1, roleName2]) | ||
expect(actualRoles.length).toBe(2) | ||
expect(actualRoles).toContain(roleName1) | ||
expect(actualRoles).toContain(roleName2) | ||
}, | ||
) | ||
}) | ||
|
||
describe('for inserts', () => { | ||
let tableName: string | ||
|
||
beforeEach(async () => { | ||
tableName = `insert_test_${guid()}` | ||
await createSimpleTable(defaultClient, tableName) | ||
}) | ||
|
||
async function tryInsert(role?: string | Array<string>) { | ||
await client.insert({ | ||
table: tableName, | ||
values: jsonValues, | ||
format: 'JSONEachRow', | ||
role, | ||
}) | ||
} | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully insert when client specifies a role that is allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName1, | ||
}) | ||
|
||
await tryInsert() | ||
await assertJsonValues(defaultClient, tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully insert when client specifies multiple roles and at least one is allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: [roleName1, roleName2], | ||
}) | ||
|
||
await tryInsert() | ||
await assertJsonValues(defaultClient, tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should fail to insert when client specifies a role that is not allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await expectAsync(tryInsert()).toBeRejectedWith( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringContaining('Not enough privileges'), | ||
code: '497', | ||
type: 'ACCESS_DENIED', | ||
}), | ||
) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully insert when insert specifies a role that is allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await tryInsert(roleName1) | ||
await assertJsonValues(defaultClient, tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully insert when insert specifies multiple roles and at least one is allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await tryInsert([roleName1, roleName2]) | ||
await assertJsonValues(defaultClient, tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should fail to insert when insert specifies a role that is not allowed to insert', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName1, | ||
}) | ||
|
||
await expectAsync(tryInsert(roleName2)).toBeRejectedWith( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringContaining('Not enough privileges'), | ||
code: '497', | ||
type: 'ACCESS_DENIED', | ||
}), | ||
) | ||
}, | ||
) | ||
}) | ||
|
||
describe('for commands', () => { | ||
let tableName: string | ||
|
||
beforeEach(async () => { | ||
tableName = `command_role_test_${guid()}` | ||
}) | ||
|
||
async function tryCreateTable(role?: string | Array<string>) { | ||
const query = ` | ||
CREATE TABLE ${tableName} | ||
(id UInt64, name String, sku Array(UInt8), timestamp DateTime) | ||
ENGINE = MergeTree() | ||
ORDER BY (id) | ||
` | ||
await client.command({ query, role }) | ||
} | ||
|
||
async function checkCreatedTable(tableName: string) { | ||
const selectResult = await defaultClient.query({ | ||
query: `SELECT * from system.tables where name = '${tableName}'`, | ||
format: 'JSON', | ||
}) | ||
|
||
const { data, rows } = await selectResult.json<{ name: string }>() | ||
expect(rows).toBe(1) | ||
expect(data[0].name).toBe(tableName) | ||
} | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully create a table when client specifies a role that is allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName1, | ||
}) | ||
|
||
await tryCreateTable() | ||
await checkCreatedTable(tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully create table when client specifies multiple roles and at least one is allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: [roleName1, roleName2], | ||
}) | ||
|
||
await tryCreateTable() | ||
await checkCreatedTable(tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should fail to create a table when client specifies a role that is not allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await expectAsync(tryCreateTable()).toBeRejectedWith( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringContaining('Not enough privileges'), | ||
code: '497', | ||
type: 'ACCESS_DENIED', | ||
}), | ||
) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully create table when command specifies a role that is allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await tryCreateTable(roleName1) | ||
await checkCreatedTable(tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should successfully create table when command specifies multiple roles and at least one is allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName2, | ||
}) | ||
|
||
await tryCreateTable([roleName1, roleName2]) | ||
await checkCreatedTable(tableName) | ||
}, | ||
) | ||
|
||
whenOnEnv(TestEnv.LocalSingleNode).it( | ||
'should fail to create table when command specifies a role that is not allowed to create tables', | ||
async () => { | ||
client = createTestClient({ | ||
username, | ||
password, | ||
role: roleName1, | ||
}) | ||
|
||
await expectAsync(tryCreateTable(roleName2)).toBeRejectedWith( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringContaining('Not enough privileges'), | ||
code: '497', | ||
type: 'ACCESS_DENIED', | ||
}), | ||
) | ||
}, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters