-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtls.test.ts
190 lines (180 loc) · 5.6 KB
/
tls.test.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import type { ClickHouseClient } from '@clickhouse/client-common'
import { createTestClient } from '@test/utils'
import * as fs from 'fs'
import Http from 'http'
import https from 'node:https'
import type Stream from 'stream'
import { createClient } from '../../src'
import Https from 'https'
import http from 'http'
describe('[Node.js] TLS connection', () => {
let client: ClickHouseClient<Stream.Readable>
beforeEach(() => {
client = createTestClient()
})
afterEach(async () => {
await client.close()
})
const certsPath = '.docker/clickhouse/single_node_tls/certificates'
const ca_cert = fs.readFileSync(`${certsPath}/ca.crt`)
const cert = fs.readFileSync(`${certsPath}/client.crt`)
const key = fs.readFileSync(`${certsPath}/client.key`)
it('should work with basic TLS', async () => {
client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
tls: {
ca_cert,
},
})
const resultSet = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
})
expect(await resultSet.text()).toEqual('0\n1\n2\n')
})
it('should work with mutual TLS', async () => {
client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
username: 'cert_user',
tls: {
ca_cert,
cert,
key,
},
})
const resultSet = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
})
expect(await resultSet.text()).toEqual('0\n1\n2\n')
})
it('should fail when hostname does not match', async () => {
client = createClient({
url: 'https://localhost:8443',
username: 'cert_user',
tls: {
ca_cert,
cert,
key,
},
})
await expectAsync(
client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
}),
).toBeRejectedWith(
jasmine.objectContaining({
message: jasmine.stringContaining(
'Hostname/IP does not match certificate',
),
}),
)
})
it('should fail with invalid certificates', async () => {
client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
username: 'cert_user',
tls: {
ca_cert,
cert: fs.readFileSync(`${certsPath}/server.crt`),
key: fs.readFileSync(`${certsPath}/server.key`),
},
})
// FIXME: add proper error message matching (does not work on Node.js 18/20)
await expectAsync(
client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
}),
).toBeRejectedWithError()
})
// query only; the rest of the methods are tested in the auth.test.ts in the common package
describe('request auth override', () => {
it('should override the credentials with basic TLS', async () => {
client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
username: 'gibberish',
password: 'gibberish',
tls: {
ca_cert,
},
})
const resultSet = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
auth: {
username: 'default',
password: '',
},
})
expect(await resultSet.text()).toEqual('0\n1\n2\n')
})
it('should override the credentials with mutual TLS', async () => {
client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
username: 'gibberish',
password: 'gibberish',
tls: {
ca_cert,
cert,
key,
},
})
const resultSet = await client.query({
query: 'SELECT number FROM system.numbers LIMIT 3',
format: 'CSV',
auth: {
username: 'cert_user',
password: '',
},
})
expect(await resultSet.text()).toEqual('0\n1\n2\n')
})
describe('Custom HTTPS agent', () => {
it('should work with a custom HTTPS agent', async () => {
const httpsRequestStub = spyOn(Https, 'request').and.callThrough()
const agent = new https.Agent({
maxFreeSockets: 5,
ca: ca_cert,
})
const client = createClient({
url: 'https://server.clickhouseconnect.test:8443',
http_agent: agent,
http_headers: {
'X-ClickHouse-User': 'default',
'X-ClickHouse-Key': '',
},
set_basic_auth_header: false,
})
const rs = await client.query({
query: 'SELECT 144 AS result',
format: 'JSONEachRow',
})
expect(await rs.json()).toEqual([{ result: 144 }])
expect(httpsRequestStub).toHaveBeenCalledTimes(1)
const callArgs = httpsRequestStub.calls.mostRecent().args
expect(callArgs[1].agent).toBe(agent)
})
// does not really belong to the TLS test; keep it here for consistency
it('should work with a custom HTTP agent', async () => {
const httpRequestStub = spyOn(Http, 'request').and.callThrough()
const agent = new http.Agent({
maxFreeSockets: 5,
})
const client = createClient({
url: 'http://localhost:8123',
http_agent: agent,
})
const rs = await client.query({
query: 'SELECT 144 AS result',
format: 'JSONEachRow',
})
expect(await rs.json()).toEqual([{ result: 144 }])
expect(httpRequestStub).toHaveBeenCalledTimes(1)
const callArgs = httpRequestStub.calls.mostRecent().args
expect(callArgs[1].agent).toBe(agent)
})
})
})
})