Merge pull request #268 from spalger/fix/sniffingHttpsNodes
implement config.sniffedNodesProtocol
This commit is contained in:
@ -164,6 +164,10 @@ Defaults:::
|
|||||||
* jQuery Build: `"jquery"`
|
* jQuery Build: `"jquery"`
|
||||||
|
|
||||||
|
|
||||||
|
`sniffedNodesProtocol`[[config-sniffed-nodes-protocol]]:: `String` -- Defines the protocol that will be used to communicate with nodes discovered during sniffing.
|
||||||
|
|
||||||
|
Default::: If all of the hosts/host passed to the client via configuration use the same protocol then this defaults to that protocol, otherwise it defaults to `"http"`.
|
||||||
|
|
||||||
|
|
||||||
`ssl`[[config-ssl]]:: `Object` -- An object defining HTTPS/SSL configuration to use for all nodes. The properties of this mimic the options accepted by http://nodejs.org/docs/latest/api/tls.html#tls_tls_connect_port_host_options_callback[`tls.connect()`] with the exception of `rejectUnauthorized`, which defaults to `false` allowing self-signed certificates to work out-of-the-box.
|
`ssl`[[config-ssl]]:: `Object` -- An object defining HTTPS/SSL configuration to use for all nodes. The properties of this mimic the options accepted by http://nodejs.org/docs/latest/api/tls.html#tls_tls_connect_port_host_options_callback[`tls.connect()`] with the exception of `rejectUnauthorized`, which defaults to `false` allowing self-signed certificates to work out-of-the-box.
|
||||||
+
|
+
|
||||||
|
|||||||
@ -328,10 +328,16 @@ ConnectionPool.prototype.setHosts = function (hosts) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ConnectionPool.prototype.getAllHosts = function () {
|
||||||
|
return _.values(this.index).map(function (connection) {
|
||||||
|
return connection.host;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the conncetion pool, as well as all of it's connections
|
* Close the conncetion pool, as well as all of it's connections
|
||||||
*/
|
*/
|
||||||
ConnectionPool.prototype.close = function () {
|
ConnectionPool.prototype.close = function () {
|
||||||
this.setHosts([]);
|
this.setHosts([]);
|
||||||
};
|
};
|
||||||
ConnectionPool.prototype.empty = ConnectionPool.prototype.close;
|
ConnectionPool.prototype.empty = ConnectionPool.prototype.close;
|
||||||
|
|||||||
@ -9,6 +9,7 @@ var errors = require('./errors');
|
|||||||
var Host = require('./host');
|
var Host = require('./host');
|
||||||
var Promise = require('bluebird');
|
var Promise = require('bluebird');
|
||||||
var patchSniffOnConnectionFault = require('./transport/sniff_on_connection_fault');
|
var patchSniffOnConnectionFault = require('./transport/sniff_on_connection_fault');
|
||||||
|
var findCommonProtocol = require('./transport/find_common_protocol');
|
||||||
|
|
||||||
function Transport(config) {
|
function Transport(config) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -71,6 +72,12 @@ function Transport(config) {
|
|||||||
self.connectionPool.setHosts(hosts);
|
self.connectionPool.setHosts(hosts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.hasOwnProperty('sniffedNodesProtocol')) {
|
||||||
|
self.sniffedNodesProtocol = config.sniffedNodesProtocol || null;
|
||||||
|
} else {
|
||||||
|
self.sniffedNodesProtocol = findCommonProtocol(self.connectionPool.getAllHosts()) || null;
|
||||||
|
}
|
||||||
|
|
||||||
if (config.sniffOnStart) {
|
if (config.sniffOnStart) {
|
||||||
self.sniff();
|
self.sniff();
|
||||||
}
|
}
|
||||||
@ -344,6 +351,7 @@ Transport.prototype.sniff = function (cb) {
|
|||||||
var nodesToHostCallback = this.nodesToHostCallback;
|
var nodesToHostCallback = this.nodesToHostCallback;
|
||||||
var log = this.log;
|
var log = this.log;
|
||||||
var globalConfig = this._config;
|
var globalConfig = this._config;
|
||||||
|
var sniffedNodesProtocol = this.sniffedNodesProtocol;
|
||||||
|
|
||||||
// make cb a function if it isn't
|
// make cb a function if it isn't
|
||||||
cb = typeof cb === 'function' ? cb : _.noop;
|
cb = typeof cb === 'function' ? cb : _.noop;
|
||||||
@ -364,6 +372,10 @@ Transport.prototype.sniff = function (cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connectionPool.setHosts(_.map(hostsConfigs, function (hostConfig) {
|
connectionPool.setHosts(_.map(hostsConfigs, function (hostConfig) {
|
||||||
|
if (sniffedNodesProtocol) {
|
||||||
|
hostConfig.protocol = sniffedNodesProtocol;
|
||||||
|
}
|
||||||
|
|
||||||
return new Host(hostConfig, globalConfig);
|
return new Host(hostConfig, globalConfig);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/lib/transport/find_common_protocol.js
Normal file
14
src/lib/transport/find_common_protocol.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
var isEmpty = require('lodash').isEmpty;
|
||||||
|
|
||||||
|
module.exports = function (hosts) {
|
||||||
|
if (isEmpty(hosts)) return false;
|
||||||
|
|
||||||
|
var commonProtocol = hosts.shift().protocol;
|
||||||
|
for (var i = 0; i < hosts.length; i++) {
|
||||||
|
if (commonProtocol !== hosts[i].protocol) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return commonProtocol;
|
||||||
|
}
|
||||||
@ -24,6 +24,9 @@ function getConnection(transport, status) {
|
|||||||
return transport.connectionPool.getConnections(status || 'alive', 1).pop();
|
return transport.connectionPool.getConnections(status || 'alive', 1).pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function CustomConnectionPool() {}
|
||||||
|
CustomConnectionPool.prototype = Object.create(Transport.connectionPools.main.prototype);
|
||||||
|
|
||||||
describe('Transport Class', function () {
|
describe('Transport Class', function () {
|
||||||
|
|
||||||
describe('Constructor', function () {
|
describe('Constructor', function () {
|
||||||
@ -37,7 +40,6 @@ describe('Transport Class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Accepts a connection pool class and intanciates it at this.connectionPool', function () {
|
it('Accepts a connection pool class and intanciates it at this.connectionPool', function () {
|
||||||
function CustomConnectionPool() {}
|
|
||||||
var trans = new Transport({
|
var trans = new Transport({
|
||||||
connectionPool: CustomConnectionPool
|
connectionPool: CustomConnectionPool
|
||||||
});
|
});
|
||||||
@ -46,7 +48,7 @@ describe('Transport Class', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Accepts the name of a connectionPool class that is defined on Transport.connectionPools', function () {
|
it('Accepts the name of a connectionPool class that is defined on Transport.connectionPools', function () {
|
||||||
Transport.connectionPools.custom = function () {};
|
Transport.connectionPools.custom = CustomConnectionPool;
|
||||||
|
|
||||||
var trans = new Transport({
|
var trans = new Transport({
|
||||||
connectionPool: 'custom'
|
connectionPool: 'custom'
|
||||||
@ -91,6 +93,77 @@ describe('Transport Class', function () {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('config.sniffedNodesProtocol', function () {
|
||||||
|
it('Assigns to itself', function () {
|
||||||
|
var football = {};
|
||||||
|
var trans = new Transport({
|
||||||
|
sniffedNodesProtocol: football
|
||||||
|
});
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', football);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Defaults to null when no hosts given', function () {
|
||||||
|
var trans = new Transport({
|
||||||
|
hosts: []
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Defaults to "http" when a single http host given', function () {
|
||||||
|
var trans = new Transport({
|
||||||
|
hosts: [
|
||||||
|
new Host({
|
||||||
|
protocol: 'http'
|
||||||
|
})
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', 'http');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Defaults to "http" when multiple http host given', function () {
|
||||||
|
var trans = new Transport({
|
||||||
|
hosts: [
|
||||||
|
new Host(),
|
||||||
|
'http://google.com',
|
||||||
|
{
|
||||||
|
host: 'foo',
|
||||||
|
path: 'bar'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', 'http');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Defaults to "https" when a single https host given', function () {
|
||||||
|
var trans = new Transport({
|
||||||
|
host: {
|
||||||
|
protocol: 'https'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', 'https');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Defaults to "https" when every seed host uses https', function () {
|
||||||
|
var trans = new Transport({
|
||||||
|
hosts: [
|
||||||
|
'https://localhost:9200',
|
||||||
|
new Host({
|
||||||
|
protocol: 'https'
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
protocol: 'https'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(trans).to.have.property('sniffedNodesProtocol', 'https');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
describe('host config', function () {
|
describe('host config', function () {
|
||||||
it('rejects non-strings/objects', function () {
|
it('rejects non-strings/objects', function () {
|
||||||
expect(function () {
|
expect(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user