From d7a7254c08657623addfeef5a2946279d9b2f3d2 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Wed, 12 Aug 2020 22:34:20 -0700 Subject: [PATCH] Added functional tests --- .../alerting_api_integration/basic/config.ts | 1 + .../alerting_api_integration/common/config.ts | 8 +++++ .../common/lib/get_proxy_server.ts | 30 +++++++++++++++++++ .../security_and_spaces/config.ts | 1 + .../actions/builtin_action_types/jira.ts | 15 ++++++++++ .../actions/builtin_action_types/pagerduty.ts | 16 ++++++++++ .../actions/builtin_action_types/resilient.ts | 15 ++++++++++ .../builtin_action_types/servicenow.ts | 15 ++++++++++ .../actions/builtin_action_types/slack.ts | 12 ++++++++ .../actions/builtin_action_types/webhook.ts | 12 ++++++++ .../spaces_only/config.ts | 6 +++- 11 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 x-pack/test/alerting_api_integration/common/lib/get_proxy_server.ts diff --git a/x-pack/test/alerting_api_integration/basic/config.ts b/x-pack/test/alerting_api_integration/basic/config.ts index f9c248ec3d56f..f58b7753b74f7 100644 --- a/x-pack/test/alerting_api_integration/basic/config.ts +++ b/x-pack/test/alerting_api_integration/basic/config.ts @@ -11,4 +11,5 @@ export default createTestConfig('basic', { disabledPlugins: [], license: 'basic', ssl: true, + enableActionsProxy: false, }); diff --git a/x-pack/test/alerting_api_integration/common/config.ts b/x-pack/test/alerting_api_integration/common/config.ts index 4947cdbf55484..3e5d744984b3a 100644 --- a/x-pack/test/alerting_api_integration/common/config.ts +++ b/x-pack/test/alerting_api_integration/common/config.ts @@ -5,6 +5,7 @@ */ import path from 'path'; +import getPort from 'get-port'; import fs from 'fs'; import { CA_CERT_PATH } from '@kbn/dev-utils'; import { FtrConfigProviderContext } from '@kbn/test/types/ftr'; @@ -15,6 +16,7 @@ interface CreateTestConfigOptions { license: string; disabledPlugins?: string[]; ssl?: boolean; + enableActionsProxy: boolean; } // test.not-enabled is specifically not enabled @@ -56,6 +58,10 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions) fs.statSync(path.resolve(__dirname, 'fixtures', 'plugins', file)).isDirectory() ); + const actionsProxyUrl = options.enableActionsProxy + ? `http://localhost:${await getPort()}` + : undefined; + return { testFiles: [require.resolve(`../${name}/tests/`)], servers, @@ -85,6 +91,8 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions) ])}`, '--xpack.encryptedSavedObjects.encryptionKey="wuGNaIhoMpk5sO4UBxgr3NyW1sFcLgIf"', `--xpack.actions.enabledActionTypes=${JSON.stringify(enabledActionTypes)}`, + `--xpack.actions.proxyUrl=${actionsProxyUrl}`, + '--xpack.actions.rejectUnauthorizedCertificates=false', '--xpack.eventLog.logEntries=true', `--xpack.actions.preconfigured=${JSON.stringify({ 'my-slack1': { diff --git a/x-pack/test/alerting_api_integration/common/lib/get_proxy_server.ts b/x-pack/test/alerting_api_integration/common/lib/get_proxy_server.ts new file mode 100644 index 0000000000000..4eb955a31329a --- /dev/null +++ b/x-pack/test/alerting_api_integration/common/lib/get_proxy_server.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import httpProxy from 'http-proxy'; + +export const getHttpProxyServer = ( + targetUrl: string, + onProxyResHandler: (proxyRes?: unknown, req?: unknown, res?: unknown) => void +): httpProxy => { + const proxyServer = httpProxy.createProxyServer({ + target: targetUrl, + secure: false, + selfHandleResponse: false, + }); + proxyServer.on('proxyRes', (proxyRes: unknown, req: unknown, res: unknown) => { + onProxyResHandler(proxyRes, req, res); + }); + return proxyServer; +}; + +export const getProxyUrl = (kbnTestServerConfig: any) => { + const proxySlackSimulatorURL = kbnTestServerConfig + .find((val: string) => val.startsWith('--xpack.actions.proxyUrl=')) + .replace('--xpack.actions.proxyUrl=', ''); + + return new URL(proxySlackSimulatorURL); +}; diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/config.ts b/x-pack/test/alerting_api_integration/security_and_spaces/config.ts index 081b901c47fc3..97f53ae2c3664 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/config.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/config.ts @@ -11,4 +11,5 @@ export default createTestConfig('security_and_spaces', { disabledPlugins: [], license: 'trial', ssl: true, + enableActionsProxy: true, }); diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts index 24931f11d4999..ae57b3319cd74 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/jira.ts @@ -6,6 +6,7 @@ import expect from '@kbn/expect'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -35,6 +36,7 @@ const mapping = [ export default function jiraTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const kibanaServer = getService('kibanaServer'); + const config = getService('config'); const mockJira = { config: { @@ -73,12 +75,19 @@ export default function jiraTest({ getService }: FtrProviderContext) { }; let jiraSimulatorURL: string = ''; + let proxyServer: any; + let proxyHaveBeenCalled = false; describe('Jira', () => { before(() => { jiraSimulatorURL = kibanaServer.resolveUrl( getExternalServiceSimulatorPath(ExternalServiceSimulator.JIRA) ); + proxyServer = getHttpProxyServer(jiraSimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); }); describe('Jira - Action Creation', () => { @@ -529,6 +538,8 @@ export default function jiraTest({ getService }: FtrProviderContext) { }) .expect(200); + expect(proxyHaveBeenCalled).to.equal(true); + expect(body).to.eql({ status: 'ok', actionId: simulatedActionId, @@ -542,5 +553,9 @@ export default function jiraTest({ getService }: FtrProviderContext) { }); }); }); + + after(() => { + proxyServer.close(); + }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/pagerduty.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/pagerduty.ts index f4fcbb65ab5a3..a76d20293870e 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/pagerduty.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/pagerduty.ts @@ -6,6 +6,7 @@ import expect from '@kbn/expect'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -17,16 +18,25 @@ import { export default function pagerdutyTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const kibanaServer = getService('kibanaServer'); + const config = getService('config'); describe('pagerduty action', () => { let simulatedActionId = ''; let pagerdutySimulatorURL: string = ''; + let proxyServer: any; + let proxyHaveBeenCalled = false; // need to wait for kibanaServer to settle ... before(() => { pagerdutySimulatorURL = kibanaServer.resolveUrl( getExternalServiceSimulatorPath(ExternalServiceSimulator.PAGERDUTY) ); + + proxyServer = getHttpProxyServer(pagerdutySimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); }); it('should return successfully when passed valid create parameters', async () => { @@ -144,6 +154,8 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) { }, }) .expect(200); + expect(proxyHaveBeenCalled).to.equal(true); + expect(result).to.eql({ status: 'ok', actionId: simulatedActionId, @@ -202,5 +214,9 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) { expect(result.message).to.match(/error posting pagerduty event: http status 502/); expect(result.retry).to.equal(true); }); + + after(() => { + proxyServer.close(); + }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/resilient.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/resilient.ts index 94feabb556a51..440ee625a1af3 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/resilient.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/resilient.ts @@ -6,6 +6,7 @@ import expect from '@kbn/expect'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -35,6 +36,7 @@ const mapping = [ export default function resilientTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const kibanaServer = getService('kibanaServer'); + const config = getService('config'); const mockResilient = { config: { @@ -73,12 +75,19 @@ export default function resilientTest({ getService }: FtrProviderContext) { }; let resilientSimulatorURL: string = ''; + let proxyServer: any; + let proxyHaveBeenCalled = false; describe('IBM Resilient', () => { before(() => { resilientSimulatorURL = kibanaServer.resolveUrl( getExternalServiceSimulatorPath(ExternalServiceSimulator.RESILIENT) ); + proxyServer = getHttpProxyServer(resilientSimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); }); describe('IBM Resilient - Action Creation', () => { @@ -529,6 +538,8 @@ export default function resilientTest({ getService }: FtrProviderContext) { }) .expect(200); + expect(proxyHaveBeenCalled).to.equal(true); + expect(body).to.eql({ status: 'ok', actionId: simulatedActionId, @@ -542,5 +553,9 @@ export default function resilientTest({ getService }: FtrProviderContext) { }); }); }); + + after(() => { + proxyServer.close(); + }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts index d3b72d01216d0..10b36caa29946 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/servicenow.ts @@ -6,6 +6,7 @@ import expect from '@kbn/expect'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { @@ -35,6 +36,7 @@ const mapping = [ export default function servicenowTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const kibanaServer = getService('kibanaServer'); + const config = getService('config'); const mockServiceNow = { config: { @@ -72,12 +74,20 @@ export default function servicenowTest({ getService }: FtrProviderContext) { }; let servicenowSimulatorURL: string = ''; + let proxyServer: any; + let proxyHaveBeenCalled = false; describe('ServiceNow', () => { before(() => { servicenowSimulatorURL = kibanaServer.resolveUrl( getExternalServiceSimulatorPath(ExternalServiceSimulator.SERVICENOW) ); + + proxyServer = getHttpProxyServer(servicenowSimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); }); describe('ServiceNow - Action Creation', () => { @@ -448,6 +458,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) { }, }) .expect(200); + expect(proxyHaveBeenCalled).to.equal(true); expect(result).to.eql({ status: 'ok', @@ -462,5 +473,9 @@ export default function servicenowTest({ getService }: FtrProviderContext) { }); }); }); + + after(() => { + proxyServer.close(); + }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/slack.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/slack.ts index c68bcaa0ad4e8..45f9ba369dc23 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/slack.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/slack.ts @@ -7,6 +7,7 @@ import expect from '@kbn/expect'; import http from 'http'; import getPort from 'get-port'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin'; @@ -14,18 +15,27 @@ import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simu // eslint-disable-next-line import/no-default-export export default function slackTest({ getService }: FtrProviderContext) { const supertest = getService('supertest'); + const config = getService('config'); describe('slack action', () => { let simulatedActionId = ''; let slackSimulatorURL: string = ''; let slackServer: http.Server; + let proxyServer: any; + let proxyHaveBeenCalled = false; // need to wait for kibanaServer to settle ... before(async () => { slackServer = await getSlackServer(); const availablePort = await getPort({ port: 9000 }); slackServer.listen(availablePort); slackSimulatorURL = `http://localhost:${availablePort}`; + + proxyServer = getHttpProxyServer(slackSimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(config.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); }); it('should return 200 when creating a slack action successfully', async () => { @@ -155,6 +165,7 @@ export default function slackTest({ getService }: FtrProviderContext) { }) .expect(200); expect(result.status).to.eql('ok'); + expect(proxyHaveBeenCalled).to.equal(true); }); it('should handle an empty message error', async () => { @@ -222,6 +233,7 @@ export default function slackTest({ getService }: FtrProviderContext) { after(() => { slackServer.close(); + proxyServer.close(); }); }); } diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/webhook.ts b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/webhook.ts index 8f17ab54184b5..ce5119e96b65a 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/webhook.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/tests/actions/builtin_action_types/webhook.ts @@ -8,6 +8,7 @@ import http from 'http'; import getPort from 'get-port'; import expect from '@kbn/expect'; import { URL, format as formatUrl } from 'url'; +import { getHttpProxyServer, getProxyUrl } from '../../../../common/lib/get_proxy_server'; import { FtrProviderContext } from '../../../../common/ftr_provider_context'; import { getExternalServiceSimulatorPath, @@ -31,6 +32,7 @@ function parsePort(url: Record): Record { webhookServer = await getWebhookServer(); @@ -76,6 +80,12 @@ export default function webhookTest({ getService }: FtrProviderContext) { webhookServer.listen(availablePort); webhookSimulatorURL = `http://localhost:${availablePort}`; + proxyServer = getHttpProxyServer(webhookSimulatorURL, () => { + proxyHaveBeenCalled = true; + }); + const proxyUrl = getProxyUrl(configService.get('kbnTestServer.serverArgs')); + proxyServer.listen(Number(proxyUrl.port)); + kibanaURL = kibanaServer.resolveUrl( getExternalServiceSimulatorPath(ExternalServiceSimulator.WEBHOOK) ); @@ -140,6 +150,7 @@ export default function webhookTest({ getService }: FtrProviderContext) { .expect(200); expect(result.status).to.eql('ok'); + expect(proxyHaveBeenCalled).to.equal(true); }); it('should support the POST method against webhook target', async () => { @@ -240,6 +251,7 @@ export default function webhookTest({ getService }: FtrProviderContext) { after(() => { webhookServer.close(); + proxyServer.close(); }); }); } diff --git a/x-pack/test/alerting_api_integration/spaces_only/config.ts b/x-pack/test/alerting_api_integration/spaces_only/config.ts index c79c26ef68752..f9860b642f13a 100644 --- a/x-pack/test/alerting_api_integration/spaces_only/config.ts +++ b/x-pack/test/alerting_api_integration/spaces_only/config.ts @@ -7,4 +7,8 @@ import { createTestConfig } from '../common/config'; // eslint-disable-next-line import/no-default-export -export default createTestConfig('spaces_only', { disabledPlugins: ['security'], license: 'trial' }); +export default createTestConfig('spaces_only', { + disabledPlugins: ['security'], + license: 'trial', + enableActionsProxy: false, +});