Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: opts.rejectUnauthorized for SSL option #171

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 60 additions & 37 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ function start (opts) {
opts.cloud = { id: opts.cloud }
}

if (opts.rejectUnauthorized) {
opts.rejectUnauthorized = opts.rejectUnauthorized !== 'false'
}

const stream = pinoElasticSearch(opts)

stream.on('unknown', (line, error) => {
Expand All @@ -45,47 +49,66 @@ function start (opts) {
console.error('Elasticsearch server error:', error)
})

if (opts.rejectUnauthorized) {
opts.rejectUnauthorized = opts.rejectUnauthorized !== 'false'
}
pump(process.stdin, stream)
}

const flags = minimist(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h',
node: 'n',
index: 'i',
'flush-bytes': 'f',
'flush-interval': 't',
'trace-level': 'l',
username: 'u',
password: 'p',
'api-key': 'k',
cloud: 'c',
'read-config': 'r'
},
default: {
node: 'http://localhost:9200'
}
})

const allowedProps = ['node', 'index', 'flush-bytes', 'flush-interval', 'trace-level', 'username', 'password', 'api-key', 'cloud', 'es-version', 'rejectUnauthorized']
function startCli (flags) {
const allowedProps = [
'node',
'index',
'flush-bytes',
'flush-interval',
'trace-level',
'username',
'password',
'api-key',
'cloud',
'es-version',
'rejectUnauthorized'
]

if (flags['read-config']) {
if (flags['read-config'].match(/.*\.json$/) !== null) {
const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), flags['read-config']), 'utf-8'))
allowedProps.forEach(key => {
if (config[key]) { flags[key] = config[key] }
})
if (flags['read-config']) {
if (flags['read-config'].match(/.*\.json$/) !== null) {
const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), flags['read-config']), 'utf-8'))
allowedProps.forEach(key => {
if (config[key] !== undefined) {
flags[key] = config[key]
}
})
}
if (flags['read-config'].match(/.*\.js$/) !== null) {
const config = require(path.join(process.cwd(), flags['read-config']))
allowedProps.forEach(key => {
if (config[key] !== undefined) {
flags[key] = config[key]
}
})
}
}

if (flags['read-config'].match(/.*\.js$/) !== null) {
const config = require(path.join(process.cwd(), flags['read-config']))
allowedProps.forEach(key => {
if (config[key]) { flags[key] = config[key] }
})
}
start(flags)
}
start(flags)

if (require.main === module) {
startCli(minimist(process.argv.slice(2), {
alias: {
version: 'v',
help: 'h',
node: 'n',
index: 'i',
'flush-bytes': 'f',
'flush-interval': 't',
'trace-level': 'l',
username: 'u',
password: 'p',
'api-key': 'k',
cloud: 'c',
'read-config': 'r'
},
default: {
node: 'http://localhost:9200'
}
}))
}

module.exports = startCli
107 changes: 107 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'
const test = require('tap').test
const proxyquire = require('proxyquire')

test('CLI: arg node should passed to client constructor', async (t) => {
const cli = proxyquire('../cli.js', {
pump: () => { },
'./lib.js': (opts) => {
t.same(opts, { node: 'https://custom-node-url:9999' })
return {
on: () => { }
}
}
})

cli({ node: 'https://custom-node-url:9999' })
})

test('CLI: arg rejectUnauthorized, if set to \'true\', should passed as true (bool) to client constructor', async (t) => {
const cli = proxyquire('../cli.js', {
pump: () => { },
'./lib.js': (opts) => {
t.same(opts, {
node: 'https://custom-node-url:9999',
rejectUnauthorized: true
})
return {
on: () => { }
}
}
})

cli({
node: 'https://custom-node-url:9999',
rejectUnauthorized: 'true'
})
})

test('CLI: arg rejectUnauthorized, if set to \'false\', should passed as false (bool) to client constructor', async (t) => {
const cli = proxyquire('../cli.js', {
pump: () => { },
'./lib.js': (opts) => {
t.same(opts, {
node: 'https://custom-node-url:9999',
rejectUnauthorized: false
})
return {
on: () => { }
}
}
})

cli({
node: 'https://custom-node-url:9999',
rejectUnauthorized: 'false'
})
})

test('CLI: arg rejectUnauthorized, if set to anything instead of true or false, should passed as true (bool) to client constructor', async (t) => {
const cli = proxyquire('../cli.js', {
pump: () => { },
'./lib.js': (opts) => {
t.same(opts, {
node: 'https://custom-node-url:9999',
rejectUnauthorized: true
})
return {
on: () => { }
}
}
})

cli({
node: 'https://custom-node-url:9999',
rejectUnauthorized: 'anything'
})
})

test('CLI: if arg.read-config is set, should read the config file and passed the value (only allowed values)', async (t) => {
const cli = proxyquire('../cli.js', {
pump: () => { },
'./lib.js': (opts) => {
t.same(opts, {
index: 'custom-index',
node: 'https://localhost:9200',
rejectUnauthorized: false,
auth: {
username: 'elastic',
password: 'pass'
},
// some keys are redundant, it is intended as it is.
// (see function start() in cli.js)
'read-config': 'test/exampleConfig.js',
username: 'elastic',
password: 'pass'
})
return {
on: () => { }
}
}
})

cli({
node: 'https://custom-node-url:9999',
'read-config': 'test/exampleConfig.js'
})
})
7 changes: 7 additions & 0 deletions test/exampleConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
index: 'custom-index',
node: 'https://localhost:9200',
rejectUnauthorized: false,
username: 'elastic',
password: 'pass'
}