Skip to content

Commit

Permalink
fix bug: opts.rejectUnauthorized for SSL option (#171)
Browse files Browse the repository at this point in the history
* fix bug: opts.rejectUnauthorized for SSL option

* fix bug: opts.rejectUnauthorized for SSL option

* add initial test for cli

* refactored for unit testing
  • Loading branch information
nasyarobby authored Sep 5, 2023
1 parent 8d4b0d9 commit 93bb4c2
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 37 deletions.
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'
}

0 comments on commit 93bb4c2

Please sign in to comment.