Skip to content

Commit

Permalink
Actions add proxy support (#74289)
Browse files Browse the repository at this point in the history
* Added proxy support for action types

* Fixed tests

* added rejectUnauthorizedCertificates config setting

* removed slack not used code

* Fixed Slack proxy

* fixed typecheck errors

* Cleanup code

* Fixed slack

* Added unit tests

* added proxy server for test

* Fixed build

* Added functional tests

* fixed due to comments

* Fixed tests and some changes due to comments

* Fixed functional tests

* fixed circular deps

* Added proxy unit test to action type
  • Loading branch information
YulNaumenko authored Aug 14, 2020
1 parent 64b8b88 commit 52bd6d9
Show file tree
Hide file tree
Showing 45 changed files with 827 additions and 195 deletions.
12 changes: 7 additions & 5 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"@storybook/addon-storyshots": "^5.3.19",
"@storybook/react": "^5.3.19",
"@storybook/theming": "^5.3.19",
"@testing-library/jest-dom": "^5.8.0",
"@testing-library/react": "^9.3.2",
"@testing-library/react-hooks": "^3.2.1",
"@testing-library/jest-dom": "^5.8.0",
"@types/angular": "^1.6.56",
"@types/archiver": "^3.1.0",
"@types/base64-js": "^1.2.5",
Expand All @@ -72,8 +72,9 @@
"@types/gulp": "^4.0.6",
"@types/hapi__wreck": "^15.0.1",
"@types/he": "^1.1.1",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/history": "^4.7.3",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/http-proxy": "^1.17.4",
"@types/jest": "^25.2.3",
"@types/jest-specific-snapshot": "^0.5.4",
"@types/joi": "^13.4.2",
Expand All @@ -94,6 +95,7 @@
"@types/object-hash": "^1.3.0",
"@types/papaparse": "^5.0.3",
"@types/pngjs": "^3.3.2",
"@types/pretty-ms": "^5.0.0",
"@types/prop-types": "^15.5.3",
"@types/proper-lockfile": "^3.0.1",
"@types/puppeteer": "^1.20.1",
Expand All @@ -109,18 +111,17 @@
"@types/redux-actions": "^2.6.1",
"@types/set-value": "^2.0.0",
"@types/sinon": "^7.0.13",
"@types/stats-lite": "^2.2.0",
"@types/styled-components": "^5.1.0",
"@types/supertest": "^2.0.5",
"@types/tar-fs": "^1.16.1",
"@types/testing-library__jest-dom": "^5.7.0",
"@types/tinycolor2": "^1.4.1",
"@types/use-resize-observer": "^6.0.0",
"@types/uuid": "^3.4.4",
"@types/webpack-env": "^1.15.2",
"@types/xml-crypto": "^1.4.0",
"@types/xml2js": "^0.4.5",
"@types/stats-lite": "^2.2.0",
"@types/pretty-ms": "^5.0.0",
"@types/webpack-env": "^1.15.2",
"@welldone-software/why-did-you-render": "^4.0.0",
"abab": "^1.0.4",
"autoprefixer": "^9.7.4",
Expand Down Expand Up @@ -227,6 +228,7 @@
"@turf/circle": "6.0.1",
"@turf/distance": "6.0.1",
"@turf/helpers": "6.0.1",
"@types/http-proxy-agent": "^2.0.2",
"angular": "^1.8.0",
"angular-resource": "1.8.0",
"angular-sanitize": "1.8.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { TypeOf } from '@kbn/config-schema';
import { Logger } from '../../../../../../src/core/server';

import {
ExternalIncidentServiceConfigurationSchema,
Expand Down Expand Up @@ -122,7 +123,12 @@ export interface ExternalServiceApi {

export interface CreateExternalServiceBasicArgs {
api: ExternalServiceApi;
createExternalService: (credentials: ExternalServiceCredentials) => ExternalService;
createExternalService: (
credentials: ExternalServiceCredentials,
logger: Logger,
proxySettings?: any
) => ExternalService;
logger: Logger;
}

export interface CreateExternalServiceArgs extends CreateExternalServiceBasicArgs {
Expand Down
16 changes: 11 additions & 5 deletions x-pack/plugins/actions/server/builtin_action_types/case/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const mapParams = (
export const createConnectorExecutor = ({
api,
createExternalService,
logger,
}: CreateExternalServiceBasicArgs) => async (
execOptions: ActionTypeExecutorOptions<
ExternalIncidentServiceConfiguration,
Expand All @@ -83,10 +84,14 @@ export const createConnectorExecutor = ({
actionId,
};

const externalService = createExternalService({
config,
secrets,
});
const externalService = createExternalService(
{
config,
secrets,
},
logger,
execOptions.proxySettings
);

if (!api[subAction]) {
throw new Error('[Action][ExternalService] Unsupported subAction type.');
Expand Down Expand Up @@ -122,10 +127,11 @@ export const createConnector = ({
validate,
createExternalService,
validationSchema,
logger,
}: CreateExternalServiceArgs) => {
return ({
configurationUtilities,
executor = createConnectorExecutor({ api, createExternalService }),
executor = createConnectorExecutor({ api, createExternalService, logger }),
}: CreateActionTypeArgs): ActionType => ({
...config,
validate: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ describe('execute()', () => {
"message": "a message to you",
"subject": "the subject",
},
"proxySettings": undefined,
"routing": Object {
"bcc": Array [
"[email protected]",
Expand Down Expand Up @@ -326,6 +327,7 @@ describe('execute()', () => {
"message": "a message to you",
"subject": "the subject",
},
"proxySettings": undefined,
"routing": Object {
"bcc": Array [
"[email protected]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async function executor(
subject: params.subject,
message: params.message,
},
proxySettings: execOptions.proxySettings,
};

let result;
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/actions/server/builtin_action_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export function registerBuiltInActionTypes({
actionTypeRegistry.register(getIndexActionType({ logger }));
actionTypeRegistry.register(getPagerDutyActionType({ logger, configurationUtilities }));
actionTypeRegistry.register(getServerLogActionType({ logger }));
actionTypeRegistry.register(getSlackActionType({ configurationUtilities }));
actionTypeRegistry.register(getSlackActionType({ logger, configurationUtilities }));
actionTypeRegistry.register(getWebhookActionType({ logger, configurationUtilities }));
actionTypeRegistry.register(getServiceNowActionType({ logger, configurationUtilities }));
actionTypeRegistry.register(getJiraActionType({ configurationUtilities }));
actionTypeRegistry.register(getResilientActionType({ configurationUtilities }));
actionTypeRegistry.register(getJiraActionType({ logger, configurationUtilities }));
actionTypeRegistry.register(getResilientActionType({ logger, configurationUtilities }));
}
32 changes: 22 additions & 10 deletions x-pack/plugins/actions/server/builtin_action_types/jira/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Logger } from '../../../../../../src/core/server';
import { createConnector } from '../case/utils';
import { ActionType } from '../../types';

import { api } from './api';
import { config } from './config';
import { validate } from './validators';
import { createExternalService } from './service';
import { JiraSecretConfiguration, JiraPublicConfiguration } from './schema';
import { ActionsConfigurationUtilities } from '../../actions_config';

export const getActionType = createConnector({
api,
config,
validate,
createExternalService,
validationSchema: {
config: JiraPublicConfiguration,
secrets: JiraSecretConfiguration,
},
});
export function getActionType({
logger,
configurationUtilities,
}: {
logger: Logger;
configurationUtilities: ActionsConfigurationUtilities;
}): ActionType {
return createConnector({
api,
config,
validate,
createExternalService,
validationSchema: {
config: JiraPublicConfiguration,
secrets: JiraSecretConfiguration,
},
logger,
})({ configurationUtilities });
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import axios from 'axios';
import { createExternalService } from './service';
import * as utils from '../lib/axios_utils';
import { ExternalService } from '../case/types';
import { Logger } from '../../../../../../src/core/server';
import { loggingSystemMock } from '../../../../../../src/core/server/mocks';
const logger = loggingSystemMock.create().get() as jest.Mocked<Logger>;

jest.mock('axios');
jest.mock('../lib/axios_utils', () => {
Expand All @@ -26,10 +29,13 @@ describe('Jira service', () => {
let service: ExternalService;

beforeAll(() => {
service = createExternalService({
config: { apiUrl: 'https://siem-kibana.atlassian.net', projectKey: 'CK' },
secrets: { apiToken: 'token', email: '[email protected]' },
});
service = createExternalService(
{
config: { apiUrl: 'https://siem-kibana.atlassian.net', projectKey: 'CK' },
secrets: { apiToken: 'token', email: '[email protected]' },
},
logger
);
});

beforeEach(() => {
Expand All @@ -39,37 +45,49 @@ describe('Jira service', () => {
describe('createExternalService', () => {
test('throws without url', () => {
expect(() =>
createExternalService({
config: { apiUrl: null, projectKey: 'CK' },
secrets: { apiToken: 'token', email: '[email protected]' },
})
createExternalService(
{
config: { apiUrl: null, projectKey: 'CK' },
secrets: { apiToken: 'token', email: '[email protected]' },
},
logger
)
).toThrow();
});

test('throws without projectKey', () => {
expect(() =>
createExternalService({
config: { apiUrl: 'test.com', projectKey: null },
secrets: { apiToken: 'token', email: '[email protected]' },
})
createExternalService(
{
config: { apiUrl: 'test.com', projectKey: null },
secrets: { apiToken: 'token', email: '[email protected]' },
},
logger
)
).toThrow();
});

test('throws without username', () => {
expect(() =>
createExternalService({
config: { apiUrl: 'test.com' },
secrets: { apiToken: '', email: '[email protected]' },
})
createExternalService(
{
config: { apiUrl: 'test.com' },
secrets: { apiToken: '', email: '[email protected]' },
},
logger
)
).toThrow();
});

test('throws without password', () => {
expect(() =>
createExternalService({
config: { apiUrl: 'test.com' },
secrets: { apiToken: '', email: undefined },
})
createExternalService(
{
config: { apiUrl: 'test.com' },
secrets: { apiToken: '', email: undefined },
},
logger
)
).toThrow();
});
});
Expand All @@ -92,6 +110,7 @@ describe('Jira service', () => {
expect(requestMock).toHaveBeenCalledWith({
axios,
url: 'https://siem-kibana.atlassian.net/rest/api/2/issue/1',
logger,
});
});

Expand Down Expand Up @@ -146,6 +165,7 @@ describe('Jira service', () => {
expect(requestMock).toHaveBeenCalledWith({
axios,
url: 'https://siem-kibana.atlassian.net/rest/api/2/issue',
logger,
method: 'post',
data: {
fields: {
Expand Down Expand Up @@ -210,6 +230,7 @@ describe('Jira service', () => {

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
method: 'put',
url: 'https://siem-kibana.atlassian.net/rest/api/2/issue/1',
data: { fields: { summary: 'title', description: 'desc' } },
Expand Down Expand Up @@ -272,6 +293,7 @@ describe('Jira service', () => {

expect(requestMock).toHaveBeenCalledWith({
axios,
logger,
method: 'post',
url: 'https://siem-kibana.atlassian.net/rest/api/2/issue/1/comment',
data: { body: 'comment' },
Expand Down
Loading

0 comments on commit 52bd6d9

Please sign in to comment.