From 9a325caeeeb06d6428691855791ab7a638b82d70 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Tue, 19 Nov 2024 10:22:55 +0530 Subject: [PATCH 1/2] 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) { From e9d459fa5e222bba5a153d53f3aa974d52431e12 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Thu, 21 Nov 2024 17:13:52 +0530 Subject: [PATCH 2/2] feat: create cert cert config cli command --- packages/bruno-cli/src/commands/run.js | 39 ++++++++++++++------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index e156474a9..57ae3afe6 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -259,9 +259,9 @@ const builder = async (yargs) => { type: 'boolean', description: 'Stop execution after a failure of a request, test, or assertion' }) - .option('ssl-cert-list', { + .option('client-cert-config', { type: 'string', - description: 'Path to the SSL client certificate list file used for securing the connection in the request' + description: 'Path to the Client certificate config file used for securing the connection in the request' }) .example('$0 run request.bru', 'Run a request') @@ -298,7 +298,7 @@ const builder = async (yargs) => { '$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'); + .example('$0 run --client-cert-config client-cert-config.json', 'Run a request with Client certificate configurations'); }; const handler = async function (argv) { @@ -319,7 +319,7 @@ const handler = async function (argv) { sandbox, testsOnly, bail, - sslCertList + clientCertConfig } = argv; const collectionPath = process.cwd(); @@ -337,30 +337,33 @@ const handler = async function (argv) { const brunoConfig = JSON.parse(brunoConfigFile); const collectionRoot = getCollectionRoot(collectionPath); - if (sslCertList) { + if (clientCertConfig) { try { - const sslCertListPathExists = await exists(sslCertList); - if (!sslCertListPathExists) { - console.error(chalk.red(`SSL Certificate List file "${sslCertList}" does not exist.`)); + const clientCertConfigExists = await exists(clientCertConfig); + if (!clientCertConfigExists) { + console.error(chalk.red(`Client Certificate Config file "${clientCertConfig}" does not exist.`)); process.exit(constants.EXIT_STATUS.ERROR_FILE_NOT_FOUND); } - const sslCertListFile = fs.readFileSync(sslCertList, 'utf8'); - let sslCertListJson; + const clientCertConfigFileContent = fs.readFileSync(clientCertConfig, 'utf8'); + let clientCertConfigJson; try { - sslCertListJson = JSON.parse(sslCertListFile); + clientCertConfigJson = JSON.parse(clientCertConfigFileContent); } catch (err) { - console.error(chalk.red(`Failed to parse SSL Certificate List JSON: ${err.message}`)); + console.error(chalk.red(`Failed to parse Client Certificate Config JSON: ${err.message}`)); process.exit(constants.EXIT_STATUS.ERROR_INVALID_JSON); } - if (brunoConfig.clientCertificates) { - brunoConfig.clientCertificates = { - ...brunoConfig.clientCertificates, - certs: [...brunoConfig.clientCertificates.certs, ...sslCertListJson] - }; + + if (clientCertConfigJson?.enabled && Array.isArray(clientCertConfigJson?.certs)) { + if (brunoConfig.clientCertificates) { + brunoConfig.clientCertificates.certs.push(...clientCertConfigJson.certs); + } else { + brunoConfig.clientCertificates = { certs: clientCertConfigJson.certs }; + } + console.log(chalk.green(`Client certificates has been added`)); } else { - brunoConfig.clientCertificates = { certs: sslCertListJson }; + console.warn(chalk.yellow(`Client certificate configuration is enabled, but it either contains no valid "certs" array or the added configuration has been set to false`)); } } catch (err) { console.error(chalk.red(`Unexpected error: ${err.message}`));