forked from santthosh/aws-es-kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (168 loc) · 4.65 KB
/
index.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
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
#!/usr/bin/env node
const AWS = require('aws-sdk');
const http = require('http');
const httpProxy = require('http-proxy');
const express = require('express');
const bodyParser = require('body-parser');
const stream = require('stream');
const basicAuth = require('basic-auth-connect');
const compression = require('compression');
const yargs = require('yargs')
.usage('usage: $0 [options] <aws-es-cluster-endpoint>')
.option('b', {
alias: 'bind-address',
default: process.env.BIND_ADDRESS || '127.0.0.1',
demand: false,
describe: 'the ip address to bind to',
type: 'string'
})
.option('p', {
alias: 'port',
default: process.env.PORT || 9200,
demand: false,
describe: 'the port to bind to',
type: 'number'
})
.option('r', {
alias: 'region',
default: process.env.REGION,
demand: false,
describe: 'the region of the Elasticsearch cluster',
type: 'string'
})
.option('u', {
alias: 'user',
default: process.env.USER,
demand: false,
describe: 'the username to access the proxy'
})
.option('a', {
alias: 'password',
default: process.env.PASSWORD,
demand: false,
describe: 'the password to access the proxy'
})
.option('H', {
alias: 'health-path',
default: process.env.HEALTH_PATH,
demand: false,
describe: 'URI path for health check',
type: 'string'
})
.option('l', {
alias: 'limit',
default: process.env.LIMIT || '10mb',
demand: false,
describe: 'request limit'
})
.help()
.version()
.strict();
const argv = yargs.argv;
const BIND_ADDRESS = argv.b;
const PORT = argv.p;
const REQ_LIMIT = argv.l;
const ENDPOINT = process.env.ENDPOINT || argv._[0];
let REGION = argv.r;
let TARGET = process.env.ENDPOINT || argv._[0];
if (!ENDPOINT) {
yargs.showHelp();
process.exit(1);
}
// Try to infer the region if it is not provided as an argument.
if (!REGION) {
const m = ENDPOINT.match(/\.([^.]+)\.es\.amazonaws\.com\.?$/);
if (m) {
REGION = m[1];
} else {
console.error(`
Region cannot be parsed from endpoint address.
Either the endpoint must end in .<region>.es.amazonaws.com or --region should be provided as an argument.
`);
yargs.showHelp();
process.exit(1);
}
}
if (!TARGET.match(/^https?:\/\//)) {
TARGET = 'https://' + TARGET;
}
let credentials;
const chain = new AWS.CredentialProviderChain();
chain.resolve(function (err, resolved) {
if (err) {
throw err;
} else {
credentials = resolved;
}
});
const proxy = httpProxy.createProxyServer({
target: TARGET,
changeOrigin: true,
secure: true
});
const app = express();
app.use(compression());
if (argv.u && argv.a) {
app.use(basicAuth(argv.u, argv.a));
}
app.use(bodyParser.raw({
limit: REQ_LIMIT,
type: function () { return true; }
}));
app.use(function (req, res, next) {
return credentials.get(function (err) {
if (err) {
return next(err);
}
return next();
});
});
if (argv.H) {
app.get(argv.H, function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.send('ok');
});
}
app.use(function (req, res) {
let bufferStream;
if (Buffer.isBuffer(req.body)) {
bufferStream = new stream.PassThrough();
bufferStream.end(req.body);
}
proxy.web(req, res, { buffer: bufferStream });
});
proxy.on('proxyReq', function (proxyReq, req) {
const endpoint = new AWS.Endpoint(ENDPOINT);
const request = new AWS.HttpRequest(endpoint);
request.method = proxyReq.method;
request.path = proxyReq.path;
request.region = REGION;
if (Buffer.isBuffer(req.body)) {
request.body = req.body;
}
if (!request.headers) {
request.headers = {};
}
request.headers['presigned-expires'] = false;
request.headers['Host'] = endpoint.hostname;
const signer = new AWS.Signers.V4(request, 'es');
signer.addAuthorization(credentials, new Date());
proxyReq.setHeader('Host', request.headers['Host']);
proxyReq.setHeader('X-Amz-Date', request.headers['X-Amz-Date']);
proxyReq.setHeader('Authorization', request.headers['Authorization']);
if (request.headers['x-amz-security-token']) {
proxyReq.setHeader('x-amz-security-token', request.headers['x-amz-security-token']);
}
});
proxy.on('proxyRes', function (proxyReq, req, res) {
if (req.url.match(/\.(css|js|img|font)/)) {
res.setHeader('Cache-Control', 'public, max-age=86400');
}
});
http.createServer(app).listen(PORT, BIND_ADDRESS);
console.log('* * * aws-es-proxy * * *');
console.log(`AWS ES cluster available at http://${BIND_ADDRESS}:${PORT}`);
console.log(`Kibana available at http://${BIND_ADDRESS}:${PORT}/_plugin/kibana/`);
if (argv.H) {
console.log(`Health endpoint enabled at http://${BIND_ADDRESS}:${PORT}${argv.H}`);
}