diff --git a/packages/bruno-electron/src/ipc/network/awsv4auth-helper.js b/packages/bruno-electron/src/ipc/network/awsv4auth-helper.js index d5e4a896f..e3c990624 100644 --- a/packages/bruno-electron/src/ipc/network/awsv4auth-helper.js +++ b/packages/bruno-electron/src/ipc/network/awsv4auth-helper.js @@ -5,6 +5,24 @@ function isStrPresent(str) { return str && str !== '' && str !== 'undefined'; } +async function resolveCredentials(request) { + const awsv4 = request.awsv4config; + if (isStrPresent(awsv4.profileName)) { + try { + credentialsProvider = fromIni({ + profile: awsv4.profileName + }); + credentials = await credentialsProvider(); + awsv4.accessKeyId = credentials.accessKeyId; + awsv4.secretAccessKey = credentials.secretAccessKey; + awsv4.sessionToken = credentials.sessionToken; + } catch { + console.error('Failed to fetch credentials from AWS profile.'); + } + } + return awsv4; +} + function addAwsV4Interceptor(axiosInstance, request) { if (!request.awsv4config) { console.warn('No Auth Config found!'); @@ -12,38 +30,27 @@ function addAwsV4Interceptor(axiosInstance, request) { } const awsv4 = request.awsv4config; - if (!isStrPresent(awsv4.profileName) && (!isStrPresent(awsv4.accessKeyId) || !isStrPresent(awsv4.secretAccessKey))) { + if (!isStrPresent(awsv4.accessKeyId) || !isStrPresent(awsv4.secretAccessKey)) { console.warn('Required Auth Fields are not present'); return; } - let credentials = { - accessKeyId: awsv4.accessKeyId, - secretAccessKey: awsv4.secretAccessKey, - sessionToken: awsv4.sessionToken - }; - - if (isStrPresent(awsv4.profileName)) { - try { - credentials = fromIni({ - profile: awsv4.profileName - }); - } catch { - console.error('Failed to fetch credentials from AWS profile.'); - } - } - const interceptor = aws4Interceptor({ options: { region: awsv4.region, service: awsv4.service }, - credentials + credentials: { + accessKeyId: awsv4.accessKeyId, + secretAccessKey: awsv4.secretAccessKey, + sessionToken: awsv4.sessionToken + } }); + axiosInstance.interceptors.request.use(interceptor); - console.log('Added AWS V4 interceptor to axios.'); } module.exports = { - addAwsV4Interceptor + addAwsV4Interceptor, + resolveCredentials }; diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 36e7e6c2c..233f1550c 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -17,7 +17,7 @@ const { getPreferences } = require('../../store/preferences'); const { getProcessEnvVars } = require('../../store/process-env'); const { getBrunoConfig } = require('../../store/bruno-config'); const { makeAxiosInstance } = require('./axios-instance'); -const { addAwsV4Interceptor } = require('./awsv4auth-helper'); +const { addAwsV4Interceptor, resolveCredentials } = require('./awsv4auth-helper'); // override the default escape function to prevent escaping Mustache.escape = function (value) { @@ -248,6 +248,7 @@ const registerNetworkIpc = (mainWindow) => { const axiosInstance = makeAxiosInstance(); if (request.awsv4config) { + request.awsv4config = await resolveCredentials(request); addAwsV4Interceptor(axiosInstance, request); delete request.awsv4config; }