Changing to callWithRequest

- Remove auth handler
- Add callWithRequest
- Create noAuthClient
This commit is contained in:
Chris Cowan 2015-09-18 14:26:09 -07:00
parent 4886bf9e4e
commit bdd0dc23b5
4 changed files with 28 additions and 29 deletions

View file

@ -1,7 +1,6 @@
module.exports = function (kibana) {
var healthCheck = require('./lib/health_check');
var exposeClient = require('./lib/expose_client');
var exposeAuthHandler = require('./lib/expose_auth_handler');
var createProxy = require('./lib/create_proxy');
return new kibana.Plugin({
@ -34,7 +33,6 @@ module.exports = function (kibana) {
// Expose the client to the server
exposeClient(server);
exposeAuthHandler(server);
createProxy(server, 'GET', '/{paths*}');
createProxy(server, 'POST', '/_mget');
createProxy(server, 'POST', '/{index}/_search');

View file

@ -0,0 +1,19 @@
const _ = require('lodash');
const Promise = require('bluebird');
const Boom = require('boom');
module.exports = (client) => {
return (req, endpoint, params = {}) => {
if (req.headers.authorization) {
_.set(params, 'headers.authorization', req.headers.authorization);
}
const api = _.get(client, endpoint).bind(client);
console.log(params);
return api(params).catch((err) => {
if (err.status === 401) {
const options = { realm: 'Authorization Required' };
return Promise.reject(Boom.unauthorized(err.body, 'Basic', options));
}
return Promise.reject(err);
});
};
};

View file

@ -1,20 +0,0 @@
module.exports = (server) => {
function authHandler(fn) {
return function (req, reply) {
return fn(req, reply)
.then(reply)
.catch(function (err) {
if (err.status === 401) {
return reply(err.body)
.header('WWW-Authenticate', 'Basic realm="Authorization Required"')
.code(401);
}
reply(err);
});
};
}
server.expose('authHandler', authHandler);
};

View file

@ -3,6 +3,7 @@ var _ = require('lodash');
var fs = require('fs');
var util = require('util');
var url = require('url');
var callWithRequest = require('./call_with_request');
module.exports = function (server) {
var config = server.config();
@ -17,18 +18,14 @@ module.exports = function (server) {
clientKey: config.get('elasticsearch.ssl.key'),
ca: config.get('elasticsearch.ssl.ca'),
apiVersion: config.get('elasticsearch.apiVersion'),
keepAlive: true
keepAlive: true,
auth: true
});
var uri = url.parse(options.url);
var authorization;
if (options.req) {
if (_.get(options.req, 'headers.authorization')) {
authorization = _.get(options.req, 'headers.authorization').replace('Basic ', '');
uri.auth = new Buffer(authorization, 'base64').toString('utf8');
}
} else if (options.username && options.password) {
if (options.auth && options.username && options.password) {
uri.auth = util.format('%s:%s', options.username, options.password);
}
@ -63,8 +60,13 @@ module.exports = function (server) {
var client = createClient();
server.on('close', _.bindKey(client, 'close'));
var noAuthClient = createClient({ auth: false });
server.on('close', _.bindKey(noAuthClient, 'close'));
server.expose('client', client);
server.expose('createClient', createClient);
server.expose('callWithRequest', callWithRequest(noAuthClient));
return client;