Skip to content

Commit

Permalink
[RAM] fix Slack API proxy (#169171)
Browse files Browse the repository at this point in the history
## Summary

FIX -> #168701


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
XavierM authored Oct 18, 2023
1 parent 121353c commit 7df3f96
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
21 changes: 20 additions & 1 deletion x-pack/plugins/actions/server/lib/axios_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import axios from 'axios';
import axios, { AxiosInstance } from 'axios';
import { Agent as HttpsAgent } from 'https';
import HttpProxyAgent from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
Expand Down Expand Up @@ -320,6 +320,25 @@ describe('request', () => {
expect(axiosMock.mock.calls[0][1].timeout).toBe(360000);
expect(axiosMock.mock.calls[1][1].timeout).toBe(360001);
});

test('throw an error if you use baseUrl in your axios instance', async () => {
await expect(async () => {
await request({
axios: {
...axios,
defaults: {
...axios.defaults,
baseURL: 'https://here-we-go.com',
},
} as unknown as AxiosInstance,
url: '/test',
logger,
configurationUtilities,
});
}).rejects.toThrowErrorMatchingInlineSnapshot(
`"Do not use \\"baseURL\\" in the creation of your axios instance because you will mostly break proxy"`
);
});
});

describe('patch', () => {
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/actions/server/lib/axios_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export const request = async <T = unknown>({
timeout?: number;
sslOverrides?: SSLSettings;
} & AxiosRequestConfig): Promise<AxiosResponse> => {
if (!isEmpty(axios?.defaults?.baseURL ?? '')) {
throw new Error(
`Do not use "baseURL" in the creation of your axios instance because you will mostly break proxy`
);
}
const { httpAgent, httpsAgent } = getCustomAgents(
configurationUtilities,
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const requestMock = utils.request as jest.Mock;

const services: Services = actionsMock.createServices();
const mockedLogger: jest.Mocked<Logger> = loggerMock.create();
const headers = {
Authorization: 'Bearer some token',
'Content-type': 'application/json; charset=UTF-8',
};

let connectorType: SlackApiConnectorType;
let configurationUtilities: jest.Mocked<ActionsConfigurationUtilities>;
Expand Down Expand Up @@ -266,9 +270,10 @@ describe('execute', () => {
expect(requestMock).toHaveBeenCalledWith({
axios,
configurationUtilities,
headers,
logger: mockedLogger,
method: 'post',
url: 'chat.postMessage',
url: 'https://slack.com/api/chat.postMessage',
data: { channel: 'general', text: 'some text' },
});

Expand Down Expand Up @@ -317,9 +322,10 @@ describe('execute', () => {
expect(requestMock).toHaveBeenCalledWith({
axios,
configurationUtilities,
headers,
logger: mockedLogger,
method: 'get',
url: 'conversations.info?channel=ZXCVBNM567',
url: 'https://slack.com/api/conversations.info?channel=ZXCVBNM567',
});

expect(response).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ describe('Slack API service', () => {
await service.validChannelId('channel_id_1');
expect(requestMock).toHaveBeenCalledWith({
axios,
headers: {
Authorization: 'Bearer token',
'Content-type': 'application/json; charset=UTF-8',
},
logger,
configurationUtilities,
method: 'get',
url: 'conversations.info?channel=channel_id_1',
url: 'https://slack.com/api/conversations.info?channel=channel_id_1',
});
});

Expand All @@ -146,10 +150,14 @@ describe('Slack API service', () => {
expect(requestMock).toHaveBeenCalledTimes(1);
expect(requestMock).toHaveBeenNthCalledWith(1, {
axios,
headers: {
Authorization: 'Bearer token',
'Content-type': 'application/json; charset=UTF-8',
},
logger,
configurationUtilities,
method: 'post',
url: 'chat.postMessage',
url: 'https://slack.com/api/chat.postMessage',
data: { channel: 'general', text: 'a message' },
});
});
Expand All @@ -166,10 +174,14 @@ describe('Slack API service', () => {
expect(requestMock).toHaveBeenCalledTimes(1);
expect(requestMock).toHaveBeenNthCalledWith(1, {
axios,
headers: {
Authorization: 'Bearer token',
'Content-type': 'application/json; charset=UTF-8',
},
logger,
configurationUtilities,
method: 'post',
url: 'chat.postMessage',
url: 'https://slack.com/api/chat.postMessage',
data: { channel: 'QWEERTYU987', text: 'a message' },
});
});
Expand All @@ -183,9 +195,13 @@ describe('Slack API service', () => {
expect(requestMock).toHaveBeenNthCalledWith(1, {
axios,
logger,
headers: {
Authorization: 'Bearer token',
'Content-type': 'application/json; charset=UTF-8',
},
configurationUtilities,
method: 'post',
url: 'chat.postMessage',
url: 'https://slack.com/api/chat.postMessage',
data: { channel: 'QWEERTYU987', text: 'a message' },
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,11 @@ export const createExternalService = (
throw Error(`[Action][${SLACK_CONNECTOR_NAME}]: Wrong configuration.`);
}

const axiosInstance = axios.create({
baseURL: SLACK_URL,
headers: {
Authorization: `Bearer ${token}`,
'Content-type': 'application/json; charset=UTF-8',
},
});
const axiosInstance = axios.create();
const headers = {
Authorization: `Bearer ${token}`,
'Content-type': 'application/json; charset=UTF-8',
};

const validChannelId = async (
channelId: string
Expand All @@ -138,7 +136,8 @@ export const createExternalService = (
configurationUtilities,
logger,
method: 'get',
url: `conversations.info?channel=${channelId}`,
headers,
url: `${SLACK_URL}conversations.info?channel=${channelId}`,
});
};
if (channelId.length === 0) {
Expand Down Expand Up @@ -198,9 +197,10 @@ export const createExternalService = (
const result: AxiosResponse<PostMessageResponse> = await request({
axios: axiosInstance,
method: 'post',
url: 'chat.postMessage',
url: `${SLACK_URL}chat.postMessage`,
logger,
data: { channel: channelToUse, text },
headers,
configurationUtilities,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,21 @@ export const validSlackApiChannelsRoute = (
): Promise<IKibanaResponse> {
const { authToken, channelIds } = req.body;

const axiosInstance = axios.create({
baseURL: SLACK_URL,
headers: {
Authorization: `Bearer ${authToken}`,
'Content-type': 'application/json; charset=UTF-8',
},
});
const axiosInstance = axios.create();

const validChannelId = (
channelId: string = ''
): Promise<AxiosResponse<ValidChannelResponse>> => {
return request<ValidChannelResponse>({
axios: axiosInstance,
configurationUtilities,
headers: {
Authorization: `Bearer ${authToken}`,
'Content-type': 'application/json; charset=UTF-8',
},
logger,
method: 'get',
url: `conversations.info?channel=${channelId}`,
url: `${SLACK_URL}conversations.info?channel=${channelId}`,
});
};

Expand Down

0 comments on commit 7df3f96

Please sign in to comment.