mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-02 08:58:32 +00:00
fix: gRPC oauth2 call is not taking ssl cert and proxy config (#6313)
* fix: grpc oauth2 * fix: review comments * fix: review comments
This commit is contained in:
@@ -67,7 +67,7 @@ const getCertsAndProxyConfig = async ({
|
||||
if (domain) {
|
||||
const hostRegex = '^(https:\\/\\/|grpc:\\/\\/|grpcs:\\/\\/)?' + domain.replaceAll('.', '\\.').replaceAll('*', '.*');
|
||||
const requestUrl = interpolateString(request.url, interpolationOptions);
|
||||
if (requestUrl.match(hostRegex)) {
|
||||
if (requestUrl && requestUrl.match(hostRegex)) {
|
||||
if (type === 'cert') {
|
||||
try {
|
||||
let certFilePath = interpolateString(clientCert?.certFilePath, interpolationOptions);
|
||||
|
||||
@@ -8,6 +8,7 @@ const { getCertsAndProxyConfig } = require('./cert-utils');
|
||||
const { interpolateString } = require('./interpolate-string');
|
||||
const path = require('node:path');
|
||||
const prepareGrpcRequest = require('./prepare-grpc-request');
|
||||
const { configureRequest } = require('./prepare-grpc-request');
|
||||
|
||||
// Creating grpcClient at module level so it can be accessed from window-all-closed event
|
||||
let grpcClient;
|
||||
@@ -53,6 +54,17 @@ const registerGrpcEventHandlers = (window) => {
|
||||
globalEnvironmentVariables: collection.globalEnvironmentVariables
|
||||
});
|
||||
|
||||
await configureRequest(
|
||||
preparedRequest,
|
||||
requestCopy,
|
||||
collection,
|
||||
preparedRequest.envVars,
|
||||
runtimeVariables,
|
||||
preparedRequest.processEnvVars,
|
||||
preparedRequest.promptVariables,
|
||||
certsAndProxyConfig
|
||||
);
|
||||
|
||||
// Extract certificate information from the config
|
||||
const { httpsAgentRequestFields } = certsAndProxyConfig;
|
||||
|
||||
@@ -190,6 +202,17 @@ const registerGrpcEventHandlers = (window) => {
|
||||
globalEnvironmentVariables: collection.globalEnvironmentVariables
|
||||
});
|
||||
|
||||
await configureRequest(
|
||||
preparedRequest,
|
||||
requestCopy,
|
||||
collection,
|
||||
preparedRequest.envVars,
|
||||
runtimeVariables,
|
||||
preparedRequest.processEnvVars,
|
||||
preparedRequest.promptVariables,
|
||||
certsAndProxyConfig
|
||||
);
|
||||
|
||||
// Extract certificate information from the config
|
||||
const { httpsAgentRequestFields } = certsAndProxyConfig;
|
||||
|
||||
|
||||
@@ -13,7 +13,54 @@ const processHeaders = (headers) => {
|
||||
});
|
||||
};
|
||||
|
||||
const prepareGrpcRequest = async (item, collection, environment, runtimeVariables, certsAndProxyConfig = {}) => {
|
||||
const placeOAuth2Token = (grpcRequest, credentials, tokenPlacement, tokenHeaderPrefix, tokenQueryKey) => {
|
||||
if (tokenPlacement === 'header') {
|
||||
grpcRequest.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`;
|
||||
} else {
|
||||
try {
|
||||
const url = new URL(grpcRequest.url);
|
||||
url?.searchParams?.set(tokenQueryKey, credentials?.access_token);
|
||||
grpcRequest.url = url?.toString();
|
||||
} catch (error) {
|
||||
console.error('Failed to parse URL for OAuth2 token placement:', error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const configureRequest = async (grpcRequest, request, collection, envVars, runtimeVariables, processEnvVars, promptVariables, certsAndProxyConfig) => {
|
||||
if (grpcRequest.oauth2) {
|
||||
let requestCopy = cloneDeep(grpcRequest);
|
||||
const { oauth2: { grantType, tokenPlacement, tokenHeaderPrefix, tokenQueryKey } = {} } = requestCopy || {};
|
||||
let credentials, credentialsId, oauth2Url, debugInfo;
|
||||
try {
|
||||
switch (grantType) {
|
||||
case 'authorization_code':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
placeOAuth2Token(grpcRequest, credentials, tokenPlacement, tokenHeaderPrefix, tokenQueryKey);
|
||||
break;
|
||||
case 'client_credentials':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
placeOAuth2Token(grpcRequest, credentials, tokenPlacement, tokenHeaderPrefix, tokenQueryKey);
|
||||
break;
|
||||
case 'password':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
placeOAuth2Token(grpcRequest, credentials, tokenPlacement, tokenHeaderPrefix, tokenQueryKey);
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to configure OAuth2 request:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const prepareGrpcRequest = async (item, collection, environment, runtimeVariables) => {
|
||||
const request = item.draft ? item.draft.request : item.request;
|
||||
const collectionRoot = collection?.draft?.root ? get(collection, 'draft.root', {}) : get(collection, 'root', {});
|
||||
const headers = {};
|
||||
@@ -65,56 +112,6 @@ const prepareGrpcRequest = async (item, collection, environment, runtimeVariable
|
||||
|
||||
grpcRequest = setAuthHeaders(grpcRequest, request, collectionRoot);
|
||||
|
||||
if (grpcRequest.oauth2) {
|
||||
let requestCopy = cloneDeep(grpcRequest);
|
||||
const { oauth2: { grantType, tokenPlacement, tokenHeaderPrefix, tokenQueryKey } = {} } = requestCopy || {};
|
||||
let credentials, credentialsId, oauth2Url, debugInfo;
|
||||
switch (grantType) {
|
||||
case 'authorization_code':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
if (tokenPlacement == 'header') {
|
||||
grpcRequest.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`;
|
||||
} else {
|
||||
try {
|
||||
const url = new URL(request.url);
|
||||
url?.searchParams?.set(tokenQueryKey, credentials?.access_token);
|
||||
request.url = url?.toString();
|
||||
} catch (error) { }
|
||||
}
|
||||
break;
|
||||
case 'client_credentials':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
if (tokenPlacement == 'header') {
|
||||
grpcRequest.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`;
|
||||
} else {
|
||||
try {
|
||||
const url = new URL(request.url);
|
||||
url?.searchParams?.set(tokenQueryKey, credentials?.access_token);
|
||||
request.url = url?.toString();
|
||||
} catch (error) { }
|
||||
}
|
||||
break;
|
||||
case 'password':
|
||||
interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid: collection.uid, certsAndProxyConfig }));
|
||||
grpcRequest.oauth2Credentials = { credentials, url: oauth2Url, collectionUid: collection.uid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid };
|
||||
if (tokenPlacement == 'header') {
|
||||
grpcRequest.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`;
|
||||
} else {
|
||||
try {
|
||||
const url = new URL(request.url);
|
||||
url?.searchParams?.set(tokenQueryKey, credentials?.access_token);
|
||||
request.url = url?.toString();
|
||||
} catch (error) { }
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
interpolateVars(grpcRequest, envVars, runtimeVariables, processEnvVars, promptVariables);
|
||||
processHeaders(grpcRequest.headers);
|
||||
|
||||
@@ -122,3 +119,4 @@ const prepareGrpcRequest = async (item, collection, environment, runtimeVariable
|
||||
};
|
||||
|
||||
module.exports = prepareGrpcRequest;
|
||||
module.exports.configureRequest = configureRequest;
|
||||
|
||||
Reference in New Issue
Block a user