forked from gdemarcsek/aws-es-kibana
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
172 lines (154 loc) · 4.84 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
#!/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('basic-auth-connect');
var compress = require('compression');
var url = require('url')
var sslRedirect = require('heroku-ssl-redirect');
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.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('s', {
alias: 'silent',
default: false,
demand: false,
describe: 'remove figlet banner'
})
.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 TARGETHOST = url.parse(TARGET).hostname;
var BIND_ADDRESS = argv.b;
var PORT = argv.p;
var REQ_LIMIT = argv.l;
var credentials;
var chain = new AWS.CredentialProviderChain();
chain.resolve(function (err, resolved) {
if (err) throw err;
else credentials = resolved;
});
function getCredentials(req, res, next) {
return credentials.get(function (err) {
if (err) return next(err);
else return next();
});
}
var proxy = httpProxy.createProxyServer({
target: TARGET,
changeOrigin: true,
secure: true
});
var app = express();
app.use(sslRedirect());
app.use(compress());
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(getCredentials);
app.use(function (req, res) {
var bufferStream;
if (Buffer.isBuffer(req.body)) {
var bufferStream = new stream.PassThrough();
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'] = TARGETHOST;
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/');