-
Notifications
You must be signed in to change notification settings - Fork 22
/
elasticsearch_client.ts
42 lines (34 loc) · 1.04 KB
/
elasticsearch_client.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
import elasticsearch, { Client } from 'elasticsearch';
import config from './config';
import fs from 'fs';
export function getClient(): Client {
let scheme = 'http';
let ssl_body = {
rejectUnauthorized: false,
ca: '',
cert: '',
key: ''
};
if (config.get().es_ssl) {
scheme = 'https';
ssl_body.rejectUnauthorized = config.get().ea_verify_certs;
if (config.get().es_ca_certs) {
ssl_body.ca = fs.readFileSync(config.get().es_ca_certs, 'utf8');
}
if (config.get().es_client_cert) {
ssl_body.cert = fs.readFileSync(config.get().es_client_cert, 'utf8');
}
if (config.get().es_client_key) {
ssl_body.key = fs.readFileSync(config.get().es_client_key, 'utf8');
}
}
let auth = '';
if (config.get().es_username && config.get().es_password) {
auth = `${config.get().es_username}:${config.get().es_password}@`;
}
var client = new elasticsearch.Client({
hosts: [ `${scheme}://${auth}${config.get().es_host}:${config.get().es_port}`],
ssl: ssl_body
});
return client;
}