Last active
January 20, 2020 13:52
-
-
Save fractalf/d08de3b59c32197ccd65 to your computer and use it in GitHub Desktop.
Custom HttpConnector for elasticsearch-js. Usage: connect to server with https self signed certificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var HttpConnector = require('elasticsearch/src/lib/connectors/http'); | |
var inherits = require('util').inherits; | |
var qs = require('querystring'); | |
var fs = require('fs'); | |
function CustomHttpConnector(host, config) { | |
HttpConnector.call(this, host, config); | |
} | |
inherits(CustomHttpConnector, HttpConnector); | |
// This function is copied and modified from elasticsearch-js/src/lib/connectors/http.js | |
CustomHttpConnector.prototype.makeReqParams = function (params) { | |
params = params || {}; | |
var host = this.host; | |
var reqParams = { | |
method: params.method || 'GET', | |
protocol: host.protocol + ':', | |
auth: host.auth, | |
hostname: host.host, | |
port: host.port, | |
path: (host.path || '') + (params.path || ''), | |
headers: host.getHeaders(params.headers), | |
agent: this.agent, | |
rejectUnauthorized: true, | |
ca: fs.readFileSync('publicCertificate.crt', 'utf8') | |
}; | |
if (!reqParams.path) { | |
reqParams.path = '/'; | |
} | |
var query = host.getQuery(params.query); | |
if (query) { | |
reqParams.path = reqParams.path + '?' + qs.stringify(query); | |
} | |
return reqParams; | |
}; | |
module.exports = CustomHttpConnector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var elasticsearch = require('elasticsearch'); | |
var CustomHttpConnector = require('./customHttpConnector'); | |
var Elasticsearch = function() { | |
this.client = new elasticsearch.Client({ | |
host: { | |
host: 'my.server.com', | |
port: '443', | |
protocol: 'https', | |
auth: 'user:passwd' | |
}, | |
keepAlive: true, | |
apiVerison: "1.3", | |
connectionClass: CustomHttpConnector | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Passing the public certificate of the server into the 'ca' option. I think this is a better solution than setting rejectUnauthorized: false