From 18afb732384b2ff3fdb2839ecaa541a2b92a0f38 Mon Sep 17 00:00:00 2001 From: Brent Shikoski Date: Fri, 8 Sep 2023 20:53:18 -0500 Subject: [PATCH 1/4] support custom cacerts in electron app --- packages/bruno-electron/src/ipc/network/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 1aa31f4a7..29860758b 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -154,6 +154,21 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { rejectUnauthorized: false }); } + else { + const cacertArray = [preferences['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; + cacertFile = cacertArray.find(el => el); + if (cacertFile && cacertFile.length > 1) { + try { + const fs = require('fs'); + caCrt = fs.readFileSync(cacertFile) + request.httpsAgent = new https.Agent({ + ca: caCrt + }); + } catch(err) { + console.log('Error reading CA cert file:' + cacertFile, err); + } + } + } const response = await axios(request); From e328a4615ef683e8d83054260050f6b47cc11304 Mon Sep 17 00:00:00 2001 From: Brent Shikoski Date: Fri, 8 Sep 2023 20:56:12 -0500 Subject: [PATCH 2/4] cacert support through process environment vars --- packages/bruno-cli/src/runner/run-single-request.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 506408da2..60d26d280 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -53,7 +53,8 @@ const runSingleRequest = async function (filename, bruJson, collectionPath, coll }); } else { - const cacert = options['cacert']; + const cacertArray = [options['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; + const cacertFile = cacertArray.find(el => el); if (cacert && cacert.length > 1) { try { caCrt = fs.readFileSync(cacert) From 593210456a975cad09702f86f315cd90d651738f Mon Sep 17 00:00:00 2001 From: Brent Shikoski Date: Fri, 8 Sep 2023 21:23:27 -0500 Subject: [PATCH 3/4] refactor code around https.Agent for flexibility --- .../src/runner/run-single-request.js | 19 +++++++++++-------- .../bruno-electron/src/ipc/network/index.js | 18 ++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 60d26d280..bb461edb3 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -47,26 +47,29 @@ const runSingleRequest = async function (filename, bruJson, collectionPath, coll const options = getOptions(); const insecure = get(options, 'insecure', false); + const httpsAgentRequestFields = {}; if(insecure) { - request.httpsAgent = new https.Agent({ - rejectUnauthorized: false - }); + httpsAgentRequestFields['rejectUnauthorized'] = false; } else { const cacertArray = [options['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; - const cacertFile = cacertArray.find(el => el); + const cacert = cacertArray.find(el => el); if (cacert && cacert.length > 1) { try { - caCrt = fs.readFileSync(cacert) - request.httpsAgent = new https.Agent({ - ca: caCrt - }); + caCrt = fs.readFileSync(cacert); + httpsAgentRequestFields['ca'] = caCrt; } catch(err) { console.log('Error reading CA cert file:' + cacert, err); } } } + if (Object.keys(httpsAgentRequestFields).length > 0) { + request.httpsAgent = new https.Agent({ + ...httpsAgentRequestFields + }); + } + // stringify the request url encoded params if(request.headers['content-type'] === 'application/x-www-form-urlencoded') { request.data = qs.stringify(request.data); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 29860758b..a8cba7400 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -148,11 +148,9 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { const preferences = getPreferences(); const sslVerification = get(preferences, 'request.sslVerification', true); - + const httpsAgentRequestFields = {}; if(!sslVerification) { - request.httpsAgent = new https.Agent({ - rejectUnauthorized: false - }); + httpsAgentRequestFields['rejectUnauthorized'] = false; } else { const cacertArray = [preferences['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; @@ -160,16 +158,20 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { if (cacertFile && cacertFile.length > 1) { try { const fs = require('fs'); - caCrt = fs.readFileSync(cacertFile) - request.httpsAgent = new https.Agent({ - ca: caCrt - }); + caCrt = fs.readFileSync(cacertFile); + httpsAgentRequestFields['ca'] = caCrt; } catch(err) { console.log('Error reading CA cert file:' + cacertFile, err); } } } + if (Object.keys(httpsAgentRequestFields).length > 0) { + request.httpsAgent = new https.Agent({ + ...httpsAgentRequestFields + }); + } + const response = await axios(request); // run post-response vars From b07bb67943b85f7cb4afbfae14c8a9976e0e32c1 Mon Sep 17 00:00:00 2001 From: Brent Shikoski Date: Fri, 8 Sep 2023 21:33:09 -0500 Subject: [PATCH 4/4] consistent formatting --- packages/bruno-cli/src/runner/run-single-request.js | 4 ++-- packages/bruno-electron/src/ipc/network/index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index bb461edb3..a831bc65c 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -54,7 +54,7 @@ const runSingleRequest = async function (filename, bruJson, collectionPath, coll else { const cacertArray = [options['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; const cacert = cacertArray.find(el => el); - if (cacert && cacert.length > 1) { + if(cacert && cacert.length > 1) { try { caCrt = fs.readFileSync(cacert); httpsAgentRequestFields['ca'] = caCrt; @@ -64,7 +64,7 @@ const runSingleRequest = async function (filename, bruJson, collectionPath, coll } } - if (Object.keys(httpsAgentRequestFields).length > 0) { + if(Object.keys(httpsAgentRequestFields).length > 0) { request.httpsAgent = new https.Agent({ ...httpsAgentRequestFields }); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index a8cba7400..3052cbdc5 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -155,7 +155,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { else { const cacertArray = [preferences['cacert'], process.env.SSL_CERT_FILE, process.env.NODE_EXTRA_CA_CERTS]; cacertFile = cacertArray.find(el => el); - if (cacertFile && cacertFile.length > 1) { + if(cacertFile && cacertFile.length > 1) { try { const fs = require('fs'); caCrt = fs.readFileSync(cacertFile); @@ -166,7 +166,7 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => { } } - if (Object.keys(httpsAgentRequestFields).length > 0) { + if(Object.keys(httpsAgentRequestFields).length > 0) { request.httpsAgent = new https.Agent({ ...httpsAgentRequestFields });