Skip to content

Instantly share code, notes, and snippets.

@spalger
Last active August 29, 2015 13:57
Show Gist options
  • Save spalger/8cf4977d6ac871a5f1e2 to your computer and use it in GitHub Desktop.
Save spalger/8cf4977d6ac871a5f1e2 to your computer and use it in GitHub Desktop.
Augment elasticsearch-js's HttpConnector class to implement special Agent config
var elasticsearch = require('elasticsearch');
var CustomHttpConnector = require('./custom_http_connector');
module.exports = new elasticsearch.Client({
connectionClass: CustomHttpConnector
});
module.exports = CustomHttpConnector;
var EsHttpConnector = require('elasticsearch/src/lib/connectors/http');
var inherits = require('util').inherits;
function CustomHttpConnector(host, config) {
EsHttpConnector.call(this, host, config);
}
inherits(CustomHttpConnector, EsHttpConnector);
// override the createAgent method to use whatever you would like.
CustomHttpConnector.prototype.createAgent = function (config) {
var Agent = this.hand.Agent; // the class
if (config.forever) {
config.keepAlive = config.forever;
}
if (config.keepAlive) {
// forever-agent doesn't seem to be compatible with 0.11, so don't use it
// Agent = this.host.protocol === 'https' ? ForeverAgent.SSL : ForeverAgent;
this.on('status set', this.bound.onStatusSet);
}
return new Agent(this.makeAgentConfig(config));
};
@brettwsnare
Copy link

My ES instance is sitting behind a proxy which requires me to present my server cert. Can I use this same technique to set the following:

key: Private key to use for SSL.
passphrase: A string of passphrase for the private key or pfx.
cert: Public x509 certificate to use.
ca: An authority certificate or array of authority certificates to check the remote host against.

UPDATE: Yes, I was able to send all of my Node.js HTTPS config options using this technique(https://gist.github.com/spenceralger/8cf4977d6ac871a5f1e2/e200e07a8e55c3068b98a138c0ad8deaff12c529). Thank you for posting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment