forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_bin.js
115 lines (102 loc) · 3.32 KB
/
es_bin.js
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
#!/usr/bin/env node
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
const fs = require('fs');
const { format: formatUrl } = require('url');
const { exitCode, start, ssl } = JSON.parse(process.argv[2]);
const { createServer } = ssl ? require('https') : require('http');
const { ES_KEY_PATH, ES_CERT_PATH } = require('@kbn/dev-utils');
(function main() {
process.exitCode = exitCode;
if (!start) {
return;
}
let serverUrl;
const server = createServer(
{
// Note: the integration uses the ES_P12_PATH, but that keystore contains
// the same key/cert as ES_KEY_PATH and ES_CERT_PATH
key: ssl ? fs.readFileSync(ES_KEY_PATH) : undefined,
cert: ssl ? fs.readFileSync(ES_CERT_PATH) : undefined,
},
(req, res) => {
const url = new URL(req.url, serverUrl);
const send = (code, body, headers = {}) => {
res.writeHead(code, { 'content-type': 'application/json', ...headers });
res.end(JSON.stringify(body));
};
// ES client's Product check request: it checks some fields in the body and the header
if (url.pathname === '/') {
return send(
200,
{
name: 'es-bin',
cluster_name: 'elasticsearch',
cluster_uuid: 'k0sr2gr9S4OBtygmu9ndzA',
version: {
number: '8.0.0-SNAPSHOT',
build_flavor: 'default',
build_type: 'tar',
build_hash: 'b11c15b7e0af64f90c3eb9c52c2534b4f143a070',
build_date: '2021-08-03T19:32:39.781056185Z',
build_snapshot: true,
lucene_version: '8.9.0',
minimum_wire_compatibility_version: '7.15.0',
minimum_index_compatibility_version: '7.0.0',
},
tagline: 'You Know, for Search',
},
{ 'x-elastic-product': 'Elasticsearch' }
);
}
if (url.pathname === '/_xpack') {
return send(400, {
error: {
reason: 'foo bar',
},
});
}
if (url.pathname === '/_cluster/health') {
return send(
200,
{
status: 'green',
},
{ 'x-elastic-product': 'Elasticsearch' }
);
}
return send(404, {
error: {
reason: 'not found',
},
});
}
);
// setup server auto close after 5 second of silence
let serverCloseTimer;
const delayServerClose = () => {
clearTimeout(serverCloseTimer);
serverCloseTimer = setTimeout(() => server.close(), 5000);
};
server.on('request', delayServerClose);
server.on('listening', delayServerClose);
server.listen(0, '127.0.0.1', function () {
const { port, address: hostname } = server.address();
serverUrl = new URL(
formatUrl({
protocol: 'http:',
port,
hostname,
})
);
console.log(
`[o.e.h.AbstractHttpServerTransport] [computer] publish_address {127.0.0.1:${port}}, bound_addresses {[::1]:${port}}, {127.0.0.1:${port}}`
);
console.log('started');
});
})();