From 9a325caeeeb06d6428691855791ab7a638b82d70 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Tue, 19 Nov 2024 10:22:55 +0530 Subject: [PATCH] feat: add ssl-cert-list option for secure connections in CLI run command --- packages/bruno-cli/src/commands/run.js | 43 ++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index 58b3cdf80..e156474a9 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -259,6 +259,11 @@ const builder = async (yargs) => { type: 'boolean', description: 'Stop execution after a failure of a request, test, or assertion' }) + .option('ssl-cert-list', { + type: 'string', + description: 'Path to the SSL client certificate list file used for securing the connection in the request' + }) + .example('$0 run request.bru', 'Run a request') .example('$0 run request.bru --env local', 'Run a request with the environment set to local') .example('$0 run folder', 'Run all requests in a folder') @@ -292,7 +297,8 @@ const builder = async (yargs) => { .example( '$0 run folder --cacert myCustomCA.pem --ignore-truststore', 'Use a custom CA certificate exclusively when validating the peers of the requests in the specified folder.' - ); + ) + .example('$0 run --ssl-cert-list ssl-cert-list.json', 'Run a request with SSL client certificate list'); }; const handler = async function (argv) { @@ -312,7 +318,8 @@ const handler = async function (argv) { reporterHtml, sandbox, testsOnly, - bail + bail, + sslCertList } = argv; const collectionPath = process.cwd(); @@ -330,6 +337,38 @@ const handler = async function (argv) { const brunoConfig = JSON.parse(brunoConfigFile); const collectionRoot = getCollectionRoot(collectionPath); + if (sslCertList) { + try { + const sslCertListPathExists = await exists(sslCertList); + if (!sslCertListPathExists) { + console.error(chalk.red(`SSL Certificate List file "${sslCertList}" does not exist.`)); + process.exit(constants.EXIT_STATUS.ERROR_FILE_NOT_FOUND); + } + + const sslCertListFile = fs.readFileSync(sslCertList, 'utf8'); + let sslCertListJson; + + try { + sslCertListJson = JSON.parse(sslCertListFile); + } catch (err) { + console.error(chalk.red(`Failed to parse SSL Certificate List JSON: ${err.message}`)); + process.exit(constants.EXIT_STATUS.ERROR_INVALID_JSON); + } + if (brunoConfig.clientCertificates) { + brunoConfig.clientCertificates = { + ...brunoConfig.clientCertificates, + certs: [...brunoConfig.clientCertificates.certs, ...sslCertListJson] + }; + } else { + brunoConfig.clientCertificates = { certs: sslCertListJson }; + } + } catch (err) { + console.error(chalk.red(`Unexpected error: ${err.message}`)); + process.exit(constants.EXIT_STATUS.ERROR_UNKNOWN); + } + } + + if (filename && filename.length) { const pathExists = await exists(filename); if (!pathExists) {