Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename getProxyAgents to getCustomAgents #89813

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Logger } from '../../../../../../src/core/server';
import { addTimeZoneToDate, request, patch, getErrorMessage } from './axios_utils';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { actionsConfigMock } from '../../actions_config.mock';
import { getProxyAgents } from './get_proxy_agents';
import { getCustomAgents } from './get_custom_agents';

const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;
const configurationUtilities = actionsConfigMock.create();
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('request', () => {
proxyRejectUnauthorizedCertificates: true,
proxyUrl: 'https://localhost:1212',
});
const { httpAgent, httpsAgent } = getProxyAgents(configurationUtilities, logger);
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger);

const res = await request({
axios,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { AxiosInstance, Method, AxiosResponse, AxiosBasicCredentials } from 'axios';
import { Logger } from '../../../../../../src/core/server';
import { getProxyAgents } from './get_proxy_agents';
import { getCustomAgents } from './get_custom_agents';
import { ActionsConfigurationUtilities } from '../../actions_config';

export const request = async <T = unknown>({
Expand All @@ -29,7 +29,7 @@ export const request = async <T = unknown>({
validateStatus?: (status: number) => boolean;
auth?: AxiosBasicCredentials;
}): Promise<AxiosResponse> => {
const { httpAgent, httpsAgent } = getProxyAgents(configurationUtilities, logger);
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger);

return await axios(url, {
...rest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import { Agent as HttpsAgent } from 'https';
import HttpProxyAgent from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { Logger } from '../../../../../../src/core/server';
import { getProxyAgents } from './get_proxy_agents';
import { getCustomAgents } from './get_custom_agents';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
import { actionsConfigMock } from '../../actions_config.mock';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

describe('getProxyAgents', () => {
describe('getCustomAgents', () => {
const configurationUtilities = actionsConfigMock.create();

test('get agents for valid proxy URL', () => {
configurationUtilities.getProxySettings.mockReturnValue({
proxyUrl: 'https://someproxyhost',
proxyRejectUnauthorizedCertificates: false,
});
const { httpAgent, httpsAgent } = getProxyAgents(configurationUtilities, logger);
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger);
expect(httpAgent instanceof HttpProxyAgent).toBeTruthy();
expect(httpsAgent instanceof HttpsProxyAgent).toBeTruthy();
});
Expand All @@ -31,13 +31,13 @@ describe('getProxyAgents', () => {
proxyUrl: ':nope: not a valid URL',
proxyRejectUnauthorizedCertificates: false,
});
const { httpAgent, httpsAgent } = getProxyAgents(configurationUtilities, logger);
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger);
expect(httpAgent).toBe(undefined);
expect(httpsAgent instanceof HttpsAgent).toBeTruthy();
});

test('return default agents for undefined proxy options', () => {
const { httpAgent, httpsAgent } = getProxyAgents(configurationUtilities, logger);
const { httpAgent, httpsAgent } = getCustomAgents(configurationUtilities, logger);
expect(httpAgent).toBe(undefined);
expect(httpsAgent instanceof HttpsAgent).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import { HttpsProxyAgent } from 'https-proxy-agent';
import { Logger } from '../../../../../../src/core/server';
import { ActionsConfigurationUtilities } from '../../actions_config';

interface GetProxyAgentsResponse {
interface GetCustomAgentsResponse {
httpAgent: HttpAgent | undefined;
httpsAgent: HttpsAgent | undefined;
}

export function getProxyAgents(
export function getCustomAgents(
configurationUtilities: ActionsConfigurationUtilities,
logger: Logger
): GetProxyAgentsResponse {
): GetCustomAgentsResponse {
const proxySettings = configurationUtilities.getProxySettings();
const defaultResponse = {
const defaultAgents = {
httpAgent: undefined,
httpsAgent: new HttpsAgent({
rejectUnauthorized: configurationUtilities.isRejectUnauthorizedCertificatesEnabled(),
}),
};

if (!proxySettings) {
return defaultResponse;
return defaultAgents;
}

logger.debug(`Creating proxy agents for proxy: ${proxySettings.proxyUrl}`);
Expand All @@ -38,7 +38,7 @@ export function getProxyAgents(
proxyUrl = new URL(proxySettings.proxyUrl);
} catch (err) {
logger.warn(`invalid proxy URL "${proxySettings.proxyUrl}" ignored`);
return defaultResponse;
return defaultAgents;
}

const httpAgent = new HttpProxyAgent(proxySettings.proxyUrl);
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/actions/server/builtin_action_types/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
ExecutorType,
} from '../types';
import { ActionsConfigurationUtilities } from '../actions_config';
import { getProxyAgents } from './lib/get_proxy_agents';
import { getCustomAgents } from './lib/get_custom_agents';

export type SlackActionType = ActionType<{}, ActionTypeSecretsType, ActionParamsType, unknown>;
export type SlackActionTypeExecutorOptions = ActionTypeExecutorOptions<
Expand Down Expand Up @@ -130,10 +130,10 @@ async function slackExecutor(
const { message } = params;
const proxySettings = configurationUtilities.getProxySettings();

const proxyAgents = getProxyAgents(configurationUtilities, logger);
const httpProxyAgent = webhookUrl.toLowerCase().startsWith('https')
? proxyAgents.httpsAgent
: proxyAgents.httpAgent;
const customAgents = getCustomAgents(configurationUtilities, logger);
const agent = webhookUrl.toLowerCase().startsWith('https')
? customAgents.httpsAgent
: customAgents.httpAgent;

if (proxySettings) {
logger.debug(`IncomingWebhook was called with proxyUrl ${proxySettings.proxyUrl}`);
Expand All @@ -143,7 +143,7 @@ async function slackExecutor(
// https://slack.dev/node-slack-sdk/webhook
// node-slack-sdk use Axios inside :)
const webhook = new IncomingWebhook(webhookUrl, {
agent: httpProxyAgent,
agent,
});
result = await webhook.send(message);
} catch (err) {
Expand Down