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

Add cli options to pass cert information #437

Merged
merged 1 commit into from
Mar 24, 2022
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ Available options:
Enabling this option will slow down the load testing.
--renderStatusCodes
Print status codes and their respective statistics.
--cert
Path to cert chain in pem format
--key
Path to private key for specified cert in pem format
--ca
Path to trusted ca certificates for the test. This argument accepts both a single file as well as a list of files
--debug
Print connection errors to stderr.
Print connection errors to stderr.
-v/--version
Print the version number.
-h/--help
Expand Down
32 changes: 32 additions & 0 deletions autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ function parseArguments (argvs) {
}
}

argv.tlsOptions = {}

if (argv.cert) {
try {
argv.tlsOptions.cert = fs.readFileSync(argv.cert)
} catch (err) {
throw new Error(`Failed to load cert file: ${err.message}`)
}
}

if (argv.key) {
try {
argv.tlsOptions.key = fs.readFileSync(argv.key)
} catch (err) {
throw new Error(`Failed to load key file: ${err.message}`)
}
}

if (argv.ca) {
if (typeof argv.ca === 'string') {
argv.ca = [argv.ca]
} else if (Array.isArray(argv.ca._)) {
argv.ca = argv.ca._
}

try {
argv.tlsOptions.ca = argv.ca.map(caPath => fs.readFileSync(caPath))
} catch (err) {
throw new Error(`Failed to load ca file: ${err.message}`)
}
}

// This is to distinguish down the line whether it is
// run via command-line or programmatically
argv[Symbol.for('internal')] = true
Expand Down
43 changes: 43 additions & 0 deletions test/argumentParsing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,46 @@ test('parse argument with input file and multiple workers', (t) => {
t.equal(args.method, 'POST')
t.equal(args.body, fs.readFileSync(inputPath, 'utf8'))
})

test('parse argument with cert, key and multiple ca paths', (t) => {
t.plan(5)

const certPath = 'test/cert.pem'
const keyPath = 'test/key.pem'
const caPath1 = 'help.txt'
const caPath2 = 'package.json'
const args = Autocannon.parseArguments([
'-m', 'POST',
'--cert', certPath,
'--key', keyPath,
'--ca', '[', caPath1, caPath2, ']',
'http://localhost/foo/bar'
])

t.equal(args.url, 'http://localhost/foo/bar')
t.equal(args.method, 'POST')
t.same(args.tlsOptions.cert, fs.readFileSync(certPath))
t.same(args.tlsOptions.key, fs.readFileSync(keyPath))
t.same(args.tlsOptions.ca, [fs.readFileSync(caPath1), fs.readFileSync(caPath2)])
})

test('parse argument with cert, key and single ca path', (t) => {
t.plan(5)

const certPath = 'test/cert.pem'
const keyPath = 'test/key.pem'
const caPath = 'help.txt'
const args = Autocannon.parseArguments([
'-m', 'POST',
'--cert', certPath,
'--key', keyPath,
'--ca', caPath,
'http://localhost/foo/bar'
])

t.equal(args.url, 'http://localhost/foo/bar')
t.equal(args.method, 'POST')
t.same(args.tlsOptions.cert, fs.readFileSync(certPath))
t.same(args.tlsOptions.key, fs.readFileSync(keyPath))
t.same(args.tlsOptions.ca, [fs.readFileSync(caPath)])
})