Skip to content

Commit

Permalink
Merge pull request #2885 from simianhacker/fix/2872
Browse files Browse the repository at this point in the history
Refactor Proxy Authentication and Error Handling
  • Loading branch information
w33ble committed Feb 5, 2015
2 parents fa60c3f + aa3d030 commit 2d9346d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 31 deletions.
11 changes: 9 additions & 2 deletions src/server/bin/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,19 @@ if (program.host) {
var server = require('../');
var logger = require('../lib/logger');
server.start(function (err) {
if (!err && config.kibana.pid_file) {
fs.writeFile(config.kibana.pid_file, process.pid, function (err) {
// If we get here then things have gone sideways and we need to give up.
if (err) {
logger.fatal({ err: err });
process.exit(1);
}

if (config.kibana.pid_file) {
return fs.writeFile(config.kibana.pid_file, process.pid, function (err) {
if (err) {
logger.fatal('Failed to write PID file to %s', config.kibana.pid_file);
process.exit(1);
}
});
}

});
14 changes: 9 additions & 5 deletions src/server/config/kibana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ host: "0.0.0.0"
# The Elasticsearch instance to use for all your queries.
elasticsearch_url: "http://localhost:9200"

# If your Elasticsearch is protected with basic auth:
# elasticsearch_username: user
# elasticsearch_password: pass

# preserve_elasticsearch_host true will send the hostname specified in `elasticsearch`. If you set it to false,
# then the host you use to connect to *this* Kibana instance will be sent.
elasticsearch_preserve_host: true
Expand All @@ -19,12 +15,20 @@ elasticsearch_preserve_host: true
# and dashboards. It will create a new index if it doesn't already exist.
kibana_index: ".kibana"

# If your Elasticsearch is protected with basic auth, this is the user credentials
# used by the Kibana server to perform maintence on the kibana_index at statup. Your Kibana
# users will still need to authenticate with Elasticsearch (which is proxied thorugh
# the Kibana server)
# kibana_elasticsearch_username: user
# kibana_elasticsearch_password: pass


# The default application to load.
default_app_id: "discover"

# Time in milliseconds to wait for responses from the back end or elasticsearch.
# This must be > 0
request_timeout: 500000
request_timeout: 300000

# Time in milliseconds for Elasticsearch to wait for responses from shards.
# Set to 0 to disable.
Expand Down
1 change: 0 additions & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function onError(error) {
process.exit(1);
break;
default:
logger.error({ err: error });
throw error;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/server/lib/elasticsearch_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var config = require('../config');
var elasticsearch = require('elasticsearch');
var util = require('util');
var url = require('url');
var uri = url.parse(config.elasticsearch);
if (config.kibana.kibana_elasticsearch_username && config.kibana.kibana_elasticsearch_password) {
uri.auth = util.format('%s:%s', config.kibana.kibana_elasticsearch_username, config.kibana.kibana_elasticsearch_password);
}
module.exports = new elasticsearch.Client({
host: url.format(uri)
});

11 changes: 1 addition & 10 deletions src/server/lib/migrateConfig.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
var config = require('../config');
var elasticsearch = require('elasticsearch');
var upgrade = require('./upgradeConfig');
var util = require('util');
var url = require('url');
var uri = url.parse(config.elasticsearch);
if (config.kibana.elasticsearch_username && config.kibana.elasticsearch_password) {
uri.auth = util.format('%s:%s', config.kibana.elasticsearch_username, config.kibana.elasticsearch_password);
}
var client = new elasticsearch.Client({
host: url.format(uri)
});
var client = require('./elasticsearch_client');

module.exports = function () {
var options = {
Expand Down
6 changes: 1 addition & 5 deletions src/server/lib/upgradeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ var Promise = require('bluebird');
var isUpgradeable = require('./isUpgradeable');
var config = require('../config');
var _ = require('lodash');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: config.elasticsearch
});

var client = require('./elasticsearch_client');
module.exports = function (response) {
var newConfig = {};
// Check to see if there are any doc. If not then we can assume
Expand Down
11 changes: 3 additions & 8 deletions src/server/routes/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var fs = require('fs');
var url = require('url');
var target = url.parse(config.elasticsearch);
var join = require('path').join;
var logger = require('../lib/logger');


// If the target is backed by an SSL and a CA is provided via the config
Expand Down Expand Up @@ -67,13 +68,6 @@ router.use(function (req, res, next) {
options.body = req.rawBody;
}

// Support for handling basic auth
if (config.kibana.elasticsearch_username && config.kibana.elasticsearch_password) {
var code = new buffer.Buffer(config.kibana.elasticsearch_username + ':' + config.kibana.elasticsearch_password);
var auth = 'Basic ' + code.toString('base64');
options.headers.authorization = auth;
}

// To support the elasticsearch_preserve_host feature we need to change the
// host header to the target host header. I don't quite understand the value
// of this... but it's a feature we had before so I guess we are keeping it.
Expand All @@ -84,6 +78,7 @@ router.use(function (req, res, next) {
// Create the request and pipe the response
var esRequest = request(options);
esRequest.on('error', function (err) {
logger.error({ err: err });
var code = 502;
var body = { message: 'Bad Gateway' };

Expand All @@ -96,7 +91,7 @@ router.use(function (req, res, next) {
}

body.err = err.message;
res.status(code).json(body);
if (!res.headersSent) res.status(code).json(body);
});
esRequest.pipe(res);
});
Expand Down

0 comments on commit 2d9346d

Please sign in to comment.