Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT auth support #360

Merged
merged 6 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 54 additions & 12 deletions packages/client-common/__tests__/integration/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { type ClickHouseClient } from '@clickhouse/client-common'
import { createSimpleTable } from '@test/fixtures/simple_table'
import { getAuthFromEnv } from '@test/utils/env'
import { EnvKeys, getAuthFromEnv, getFromEnv } from '@test/utils/env'
import { createTestClient, guid } from '../utils'

describe('authentication', () => {
let client: ClickHouseClient
let invalidAuthClient: ClickHouseClient
beforeEach(() => {
client = createTestClient({
username: 'gibberish',
password: 'gibberish',
invalidAuthClient = createTestClient({
auth: {
username: 'gibberish',
password: 'gibberish',
},
})
})
afterEach(async () => {
await client.close()
await invalidAuthClient.close()
})

it('provides authentication error details', async () => {
await expectAsync(
client.query({
invalidAuthClient.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
}),
).toBeRejectedWith(
Expand Down Expand Up @@ -51,13 +53,13 @@ describe('authentication', () => {
it('should with with insert and select', async () => {
tableName = `simple_table_${guid()}`
await createSimpleTable(defaultClient, tableName)
await client.insert({
await invalidAuthClient.insert({
table: tableName,
format: 'JSONEachRow',
values,
auth,
})
const rs = await client.query({
const rs = await invalidAuthClient.query({
query: `SELECT * FROM ${tableName} ORDER BY id ASC`,
format: 'JSONEachRow',
auth,
Expand All @@ -68,11 +70,11 @@ describe('authentication', () => {
it('should work with command and select', async () => {
tableName = `simple_table_${guid()}`
await createSimpleTable(defaultClient, tableName)
await client.command({
await invalidAuthClient.command({
query: `INSERT INTO ${tableName} VALUES (1, 'foo', [3, 4])`,
auth,
})
const rs = await client.query({
const rs = await invalidAuthClient.query({
query: `SELECT * FROM ${tableName} ORDER BY id ASC`,
format: 'JSONEachRow',
auth,
Expand All @@ -81,7 +83,7 @@ describe('authentication', () => {
})

it('should work with exec', async () => {
const { stream } = await client.exec({
const { stream } = await invalidAuthClient.exec({
query: 'SELECT 42, 144 FORMAT CSV',
auth,
})
Expand All @@ -94,4 +96,44 @@ describe('authentication', () => {
expect(result).toEqual('42,144\n')
})
})

// FIXME
xdescribe('JWT auth', () => {
let jwtClient: ClickHouseClient
afterEach(async () => {
await jwtClient.close()
})

it('should work with client configuration', async () => {
jwtClient = createTestClient({
url: `https://${getFromEnv(EnvKeys.host)}:8443`,
auth: {
access_token: getFromEnv(EnvKeys.jwt_access_token),
},
})
const rs = await jwtClient.query({
query: 'SELECT 42 AS result',
format: 'JSONEachRow',
})
expect(await rs.json()).toEqual([{ result: 42 }])
})

it('should override the client instance auth', async () => {
jwtClient = createTestClient({
url: `https://${getFromEnv(EnvKeys.host)}:8443`,
auth: {
username: 'gibberish',
password: 'gibberish',
},
})
const rs = await jwtClient.query({
query: 'SELECT 42 AS result',
format: 'JSONEachRow',
auth: {
access_token: getFromEnv(EnvKeys.jwt_access_token),
},
})
expect(await rs.json()).toEqual([{ result: 42 }])
})
})
})
Loading
Loading