-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
183 additions
and
43 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,7 @@ | ||
import { makeJWT } from '../packages/client-node/__tests__/utils/jwt' | ||
|
||
/** Used to generate a JWT token for web testing (can't use `jsonwebtoken` library directly there) | ||
* See `package.json` -> `scripts` -> `test:web:integration:cloud_smt:jwt` */ | ||
;(() => { | ||
console.log(makeJWT()) | ||
})() |
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,40 @@ | ||
const webpackConfig = require('./webpack.dev.js') | ||
|
||
const TEST_TIMEOUT_MS = 400_000 | ||
|
||
module.exports = function (config) { | ||
config.set({ | ||
// base path that will be used to resolve all patterns (e.g. files, exclude) | ||
basePath: '', | ||
frameworks: ['webpack', 'jasmine'], | ||
// list of files / patterns to load in the browser | ||
files: ['packages/client-web/__tests__/jwt/*.test.ts'], | ||
exclude: [], | ||
webpack: webpackConfig, | ||
preprocessors: { | ||
'packages/client-common/**/*.ts': ['webpack', 'sourcemap'], | ||
'packages/client-web/**/*.ts': ['webpack', 'sourcemap'], | ||
'packages/client-common/__tests__/jwt/*.ts': ['webpack', 'sourcemap'], | ||
'packages/client-common/__tests__/utils/*.ts': ['webpack', 'sourcemap'], | ||
}, | ||
reporters: ['mocha'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
autoWatch: false, | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: ['ChromeHeadless', 'FirefoxHeadless'], | ||
browserNoActivityTimeout: TEST_TIMEOUT_MS, | ||
browserDisconnectTimeout: TEST_TIMEOUT_MS, | ||
// if true, Karma captures browsers, runs the tests and exits | ||
singleRun: true, | ||
client: { | ||
jasmine: { | ||
random: false, | ||
stopOnSpecFailure: false, | ||
stopSpecOnExpectationFailure: true, | ||
timeoutInterval: TEST_TIMEOUT_MS, | ||
}, | ||
}, | ||
}) | ||
} |
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
50 changes: 50 additions & 0 deletions
50
packages/client-node/__tests__/integration/node_jwt_auth.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,50 @@ | ||
import type { ClickHouseClient } from '@clickhouse/client-common' | ||
import { createTestClient, TestEnv, whenOnEnv } from '@test/utils' | ||
import { EnvKeys, getFromEnv } from '@test/utils/env' | ||
import { makeJWT } from '../utils/jwt' | ||
|
||
whenOnEnv(TestEnv.CloudSMT).describe('[Node.js] JWT auth', () => { | ||
let jwtClient: ClickHouseClient | ||
let url: string | ||
let jwt: string | ||
|
||
beforeAll(() => { | ||
url = `https://${getFromEnv(EnvKeys.host)}:8443` | ||
jwt = makeJWT() | ||
}) | ||
afterEach(async () => { | ||
await jwtClient.close() | ||
}) | ||
|
||
it('should work with client configuration', async () => { | ||
jwtClient = createTestClient({ | ||
url, | ||
auth: { | ||
access_token: jwt, | ||
}, | ||
}) | ||
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, | ||
auth: { | ||
username: 'gibberish', | ||
password: 'gibberish', | ||
}, | ||
}) | ||
const rs = await jwtClient.query({ | ||
query: 'SELECT 42 AS result', | ||
format: 'JSONEachRow', | ||
auth: { | ||
access_token: jwt, | ||
}, | ||
}) | ||
expect(await rs.json()).toEqual([{ result: 42 }]) | ||
}) | ||
}) |
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,18 @@ | ||
import jwt from 'jsonwebtoken' | ||
|
||
export function makeJWT(): string { | ||
const secret = process.env['CLICKHOUSE_CLOUD_JWT_SECRET'] | ||
if (secret === undefined) { | ||
throw new Error( | ||
'Environment variable CLICKHOUSE_CLOUD_JWT_SECRET is not set', | ||
) | ||
} | ||
const payload = { | ||
iss: 'ClickHouse', | ||
sub: 'CI_Test', | ||
aud: '1f7f78b8-da67-480b-8913-726fdd31d2fc', | ||
'clickhouse:roles': ['default'], | ||
'clickhouse:grants': [], | ||
} | ||
return jwt.sign(payload, secret, { expiresIn: '15m', algorithm: 'RS256' }) | ||
} |
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,52 @@ | ||
import type { ClickHouseClient } from '@clickhouse/client-common' | ||
import { createTestClient } from '@test/utils' | ||
import { EnvKeys, getFromEnv } from '@test/utils/env' | ||
|
||
/** Cannot use the jsonwebtoken library to generate the token: it is Node.js only. | ||
* The access token should be generated externally before running the test, | ||
* and set as the CLICKHOUSE_JWT_ACCESS_TOKEN environment variable */ | ||
describe('[Web] JWT auth', () => { | ||
let client: ClickHouseClient | ||
let url: string | ||
let jwt: string | ||
|
||
beforeAll(() => { | ||
url = `https://${getFromEnv(EnvKeys.host)}:8443` | ||
jwt = getFromEnv(EnvKeys.jwt_access_token) | ||
}) | ||
afterEach(async () => { | ||
await client.close() | ||
}) | ||
|
||
it('should work with client configuration', async () => { | ||
client = createTestClient({ | ||
url, | ||
auth: { | ||
access_token: jwt, | ||
}, | ||
}) | ||
const rs = await client.query({ | ||
query: 'SELECT 42 AS result', | ||
format: 'JSONEachRow', | ||
}) | ||
expect(await rs.json()).toEqual([{ result: 42 }]) | ||
}) | ||
|
||
it('should override the client instance auth', async () => { | ||
client = createTestClient({ | ||
url, | ||
auth: { | ||
username: 'gibberish', | ||
password: 'gibberish', | ||
}, | ||
}) | ||
const rs = await client.query({ | ||
query: 'SELECT 42 AS result', | ||
format: 'JSONEachRow', | ||
auth: { | ||
access_token: jwt, | ||
}, | ||
}) | ||
expect(await rs.json()).toEqual([{ result: 42 }]) | ||
}) | ||
}) |
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