diff --git a/x-pack/plugins/actions/server/lib/axios_utils.test.ts b/x-pack/plugins/actions/server/lib/axios_utils.test.ts index 0cbc3cdde0046..43f16b4863e9a 100644 --- a/x-pack/plugins/actions/server/lib/axios_utils.test.ts +++ b/x-pack/plugins/actions/server/lib/axios_utils.test.ts @@ -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'; @@ -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', () => { diff --git a/x-pack/plugins/actions/server/lib/axios_utils.ts b/x-pack/plugins/actions/server/lib/axios_utils.ts index bed2e512761a0..b623f427be681 100644 --- a/x-pack/plugins/actions/server/lib/axios_utils.ts +++ b/x-pack/plugins/actions/server/lib/axios_utils.ts @@ -41,6 +41,11 @@ export const request = async ({ timeout?: number; sslOverrides?: SSLSettings; } & AxiosRequestConfig): Promise => { + 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, diff --git a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/index.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/index.test.ts index 62e7cf771d3cb..37f0209e6f393 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/index.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/index.test.ts @@ -32,6 +32,10 @@ const requestMock = utils.request as jest.Mock; const services: Services = actionsMock.createServices(); const mockedLogger: jest.Mocked = loggerMock.create(); +const headers = { + Authorization: 'Bearer some token', + 'Content-type': 'application/json; charset=UTF-8', +}; let connectorType: SlackApiConnectorType; let configurationUtilities: jest.Mocked; @@ -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' }, }); @@ -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({ diff --git a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.test.ts b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.test.ts index 068bd8d5d923e..fa3e5d01b79d8 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.test.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.test.ts @@ -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', }); }); @@ -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' }, }); }); @@ -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' }, }); }); @@ -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' }, }); }); diff --git a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts index f4ecb95571257..63746cc85dc78 100644 --- a/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts +++ b/x-pack/plugins/stack_connectors/server/connector_types/slack_api/service.ts @@ -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 @@ -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) { @@ -198,9 +197,10 @@ export const createExternalService = ( const result: AxiosResponse = await request({ axios: axiosInstance, method: 'post', - url: 'chat.postMessage', + url: `${SLACK_URL}chat.postMessage`, logger, data: { channel: channelToUse, text }, + headers, configurationUtilities, }); diff --git a/x-pack/plugins/stack_connectors/server/routes/valid_slack_api_channels.ts b/x-pack/plugins/stack_connectors/server/routes/valid_slack_api_channels.ts index 434f989f56e92..cd2cb113a6750 100644 --- a/x-pack/plugins/stack_connectors/server/routes/valid_slack_api_channels.ts +++ b/x-pack/plugins/stack_connectors/server/routes/valid_slack_api_channels.ts @@ -48,13 +48,7 @@ export const validSlackApiChannelsRoute = ( ): Promise { 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 = '' @@ -62,9 +56,13 @@ export const validSlackApiChannelsRoute = ( return request({ 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}`, }); };