Skip to content

Commit

Permalink
fix: use TLS or SSL depending on ES version
Browse files Browse the repository at this point in the history
  • Loading branch information
neverovski committed Nov 28, 2023
1 parent 5e5b0d9 commit 9ca4723
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
const split = require('split2')
const { Client } = require('@elastic/elasticsearch')

function getEsVersion (opts) {
return Number(opts.esVersion || opts['es-version']) || 7
}

function initializeBulkHandler (opts, client, splitter) {
const esVersion = Number(opts.esVersion || opts['es-version']) || 7
const esVersion = getEsVersion(opts)
const index = opts.index || 'pino'
const buildIndexName = typeof index === 'function' ? index : null
const type = esVersion >= 7 ? undefined : (opts.type || 'log')
Expand Down Expand Up @@ -59,6 +63,9 @@ function initializeBulkHandler (opts, client, splitter) {
}

function pinoElasticSearch (opts = {}) {
const esVersion = getEsVersion(opts)
const tlsKey = esVersion >= 8 ? 'tls' : 'ssl'

if (opts['flush-bytes']) {
process.emitWarning('The "flush-bytes" option has been deprecated, use "flushBytes" instead')
}
Expand Down Expand Up @@ -123,7 +130,7 @@ function pinoElasticSearch (opts = {}) {
node: opts.node,
auth: opts.auth,
cloud: opts.cloud,
ssl: { rejectUnauthorized: opts.rejectUnauthorized, ...opts.tls }
[tlsKey]: { rejectUnauthorized: opts.rejectUnauthorized, ...opts.tls }
}

if (opts.caFingerprint) {
Expand Down
23 changes: 23 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,26 @@ test('make sure `tls.rejectUnauthorized` overrides deprecated `rejectUnauthorize
const log = pino(instance)
log.info(['info'], 'abc')
})

test('make sure esVersion >= 8 `tls.rejectUnauthorized` overrides deprecated `rejectUnauthorized`', (t) => {
t.plan(1)

const esVersion = 8
const rejectUnauthorized = true
const tls = { rejectUnauthorized: false }

const Client = function (config) {
t.equal(config.tls.rejectUnauthorized, tls.rejectUnauthorized)
}

Client.prototype.diagnostic = { on: () => {} }
Client.prototype.helpers = { async bulk () {} }

const elastic = proxyquire('../', {
'@elastic/elasticsearch': { Client }
})

const instance = elastic({ ...options, esVersion, rejectUnauthorized, tls })
const log = pino(instance)
log.info(['info'], 'abc')
})

0 comments on commit 9ca4723

Please sign in to comment.