forked from santthosh/aws-es-kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (186 loc) · 5.88 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env node
var AWS = require('aws-sdk');
var http = require('http');
var httpProxy = require('http-proxy');
var express = require('express');
var bodyParser = require('body-parser');
var stream = require('stream');
var figlet = require('figlet');
var basicAuth = require('express-basic-auth');
var compress = require('compression');
const fs = require('fs');
const homedir = require('os').homedir();
var 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.AUTH_USER ||process.env.USER,
demand: false,
describe: 'the username to access the proxy'
})
.option('a', {
alias: 'password',
default: process.env.AUTH_PASSWORD || process.env.PASSWORD,
demand: false,
describe: 'the password to access the proxy'
})
.option('s', {
alias: 'silent',
default: false,
demand: false,
describe: 'remove figlet banner'
})
.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 || '10000kb',
demand: false,
describe: 'request limit'
})
.help()
.version()
.strict();
var argv = yargs.argv;
var ENDPOINT = 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.
var REGION = argv.r;
if (!REGION) {
var 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);
}
}
var TARGET = process.env.ENDPOINT || argv._[0];
if (!TARGET.match(/^https?:\/\//)) {
TARGET = 'https://' + TARGET;
}
var BIND_ADDRESS = argv.b;
var PORT = argv.p;
var REQ_LIMIT = argv.l;
var credentials;
var PROFILE = process.env.AWS_PROFILE;
if (!PROFILE) {
var chain = new AWS.CredentialProviderChain();
chain.resolve(function (err, resolved) {
if (err) throw err;
else credentials = resolved;
});
} else {
credentials = new AWS.SharedIniFileCredentials({profile: PROFILE});
AWS.config.credentials = credentials;
}
function getCredentials(req, res, next) {
return credentials.get(function (err) {
if (err) return next(err);
else return next();
});
}
var options = {
target: TARGET,
changeOrigin: true,
secure: true
};
var proxy = httpProxy.createProxyServer(options);
var app = express();
app.use(compress());
app.use(bodyParser.raw({limit: REQ_LIMIT, type: function() { return true; }}));
app.use(getCredentials);
if (argv.H) {
app.get(argv.H, function (req, res) {
res.setHeader('Content-Type', 'text/plain');
res.send('ok');
});
}
if (argv.u && argv.a) {
var users = {};
var user = argv.u || process.env.USER || process.env.AUTH_USER;
var pass = argv.a || process.env.PASSWORD || process.env.AUTH_PASSWORD;
users[user] = pass;
app.use(basicAuth({
users: users,
challenge: true
}));
}
app.use(async function (req, res) {
var bufferStream;
if (Buffer.isBuffer(req.body)) {
var bufferStream = new stream.PassThrough();
await bufferStream.end(req.body);
}
proxy.web(req, res, {buffer: bufferStream});
});
proxy.on('proxyReq', function (proxyReq, req) {
var endpoint = new AWS.Endpoint(ENDPOINT);
var 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;
var 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);
if(!argv.s) {
console.log(figlet.textSync('AWS ES Proxy!', {
font: 'Speed',
horizontalLayout: 'default',
verticalLayout: 'default'
}));
}
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);
}
fs.watch(`${homedir}/.aws/credentials`, (eventType, filename) => {
credentials = new AWS.SharedIniFileCredentials({profile: PROFILE});
AWS.config.credentials = credentials;
});