-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssl-connection.ts
More file actions
75 lines (64 loc) · 1.86 KB
/
Copy pathssl-connection.ts
File metadata and controls
75 lines (64 loc) · 1.86 KB
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
/**
* SSL Connection Example
*
* This example demonstrates how to establish a secure SSL/TLS connection
* to IoTDB.
*/
import { Session } from '../src';
import * as fs from 'fs';
import * as path from 'path';
async function main() {
// Create a session with SSL enabled
const session = new Session({
host: 'localhost',
port: 6667,
username: 'root',
password: 'root',
enableSSL: true,
sslOptions: {
// Path to CA certificate
ca: fs.readFileSync(path.join(__dirname, 'certs', 'ca.crt')),
// Path to client certificate (if required)
cert: fs.readFileSync(path.join(__dirname, 'certs', 'client.crt')),
// Path to client private key (if required)
key: fs.readFileSync(path.join(__dirname, 'certs', 'client.key')),
// Whether to reject unauthorized certificates
rejectUnauthorized: true,
},
});
try {
console.log('Opening secure SSL connection...');
await session.open();
console.log('Secure connection established');
// Execute operations over SSL
const dataSet = await session.executeQueryStatement('SHOW DATABASES');
const databases = [];
while (await dataSet.hasNext()) {
databases.push(dataSet.next().getFields());
}
await dataSet.close();
console.log('Databases:', databases);
} catch (error) {
console.error('Error:', error);
} finally {
await session.close();
console.log('Connection closed');
}
}
// For self-signed certificates or testing, you can use:
export async function selfSignedExample() {
const session = new Session({
host: 'localhost',
port: 6667,
username: 'root',
password: 'root',
enableSSL: true,
sslOptions: {
rejectUnauthorized: false, // Only for testing!
},
});
await session.open();
// ... operations ...
await session.close();
}
main().catch(console.error);