Reduced the number of targets for a proxy server, only actions executions should be affected (#75839)

* Reduced the number of targets for a proxy server, only actions executions should be affected

* fixed typecheck
This commit is contained in:
Yuliia Naumenko 2020-08-25 06:33:04 -07:00 committed by GitHub
parent 90f0a294af
commit 59c4cd4a69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 127 additions and 42 deletions

View file

@ -4,32 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/
import http from 'http';
import httpProxy from 'http-proxy';
import { ToolingLog } from '@kbn/dev-utils';
export const getHttpProxyServer = async (
defaultKibanaTargetUrl: string,
targetUrl: string,
kbnTestServerConfig: any,
log: ToolingLog
): Promise<http.Server> => {
const proxy = httpProxy.createProxyServer({ secure: false, selfHandleResponse: false });
const proxyPort = getProxyPort(kbnTestServerConfig);
const proxyServer = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
const targetUrl = new URL(req.url ?? defaultKibanaTargetUrl);
if (targetUrl.hostname !== 'some.non.existent.com') {
proxy.web(req, res, {
target: `${targetUrl.protocol}//${targetUrl.hostname}:${targetUrl.port}`,
});
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.write('error on call some.non.existent.com');
res.end();
}
onProxyResHandler: (proxyRes?: unknown, req?: unknown, res?: unknown) => void
): Promise<httpProxy> => {
const proxyServer = httpProxy.createProxyServer({
target: targetUrl,
secure: false,
selfHandleResponse: false,
});
proxyServer.on('proxyRes', (proxyRes: unknown, req: unknown, res: unknown) => {
onProxyResHandler(proxyRes, req, res);
});
const proxyPort = getProxyPort(kbnTestServerConfig);
proxyServer.listen(proxyPort);
return proxyServer;

View file

@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import expect from '@kbn/expect';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
@ -35,6 +37,7 @@ const mapping = [
export default function jiraTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const configService = getService('config');
const mockJira = {
config: {
@ -80,7 +83,6 @@ export default function jiraTest({ getService }: FtrProviderContext) {
getExternalServiceSimulatorPath(ExternalServiceSimulator.JIRA)
);
});
describe('Jira - Action Creation', () => {
it('should return 200 when creating a jira action successfully', async () => {
const { body: createdAction } = await supertest
@ -292,6 +294,9 @@ export default function jiraTest({ getService }: FtrProviderContext) {
describe('Jira - Executor', () => {
let simulatedActionId: string;
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
before(async () => {
const { body } = await supertest
.post('/api/actions/action')
@ -307,6 +312,14 @@ export default function jiraTest({ getService }: FtrProviderContext) {
secrets: mockJira.secrets,
});
simulatedActionId = body.id;
proxyServer = await getHttpProxyServer(
kibanaServer.resolveUrl('/'),
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
describe('Validation', () => {
@ -529,6 +542,7 @@ export default function jiraTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);
expect(body).to.eql({
status: 'ok',
actionId: simulatedActionId,
@ -541,6 +555,12 @@ export default function jiraTest({ getService }: FtrProviderContext) {
});
});
});
after(() => {
if (proxyServer) {
proxyServer.close();
}
});
});
});
}

View file

@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import expect from '@kbn/expect';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
@ -17,16 +19,27 @@ import {
export default function pagerdutyTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const configService = getService('config');
describe('pagerduty action', () => {
let simulatedActionId = '';
let pagerdutySimulatorURL: string = '<could not determine kibana url>';
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
// need to wait for kibanaServer to settle ...
before(() => {
before(async () => {
pagerdutySimulatorURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.PAGERDUTY)
);
proxyServer = await getHttpProxyServer(
kibanaServer.resolveUrl('/'),
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
it('should return successfully when passed valid create parameters', async () => {
@ -145,6 +158,7 @@ export default function pagerdutyTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);
expect(result).to.eql({
status: 'ok',
actionId: simulatedActionId,
@ -203,5 +217,11 @@ 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(() => {
if (proxyServer) {
proxyServer.close();
}
});
});
}

View file

@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import expect from '@kbn/expect';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
@ -35,6 +37,7 @@ const mapping = [
export default function resilientTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const configService = getService('config');
const mockResilient = {
config: {
@ -292,6 +295,8 @@ export default function resilientTest({ getService }: FtrProviderContext) {
describe('IBM Resilient - Executor', () => {
let simulatedActionId: string;
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
before(async () => {
const { body } = await supertest
.post('/api/actions/action')
@ -307,6 +312,14 @@ export default function resilientTest({ getService }: FtrProviderContext) {
secrets: mockResilient.secrets,
});
simulatedActionId = body.id;
proxyServer = await getHttpProxyServer(
kibanaServer.resolveUrl('/'),
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
describe('Validation', () => {
@ -529,6 +542,7 @@ export default function resilientTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);
expect(body).to.eql({
status: 'ok',
actionId: simulatedActionId,
@ -541,6 +555,12 @@ export default function resilientTest({ getService }: FtrProviderContext) {
});
});
});
after(() => {
if (proxyServer) {
proxyServer.close();
}
});
});
});
}

View file

@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import expect from '@kbn/expect';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
@ -35,6 +37,7 @@ const mapping = [
export default function servicenowTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const configService = getService('config');
const mockServiceNow = {
config: {
@ -264,6 +267,8 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
describe('ServiceNow - Executor', () => {
let simulatedActionId: string;
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
before(async () => {
const { body } = await supertest
.post('/api/actions/action')
@ -279,6 +284,14 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
secrets: mockServiceNow.secrets,
});
simulatedActionId = body.id;
proxyServer = await getHttpProxyServer(
kibanaServer.resolveUrl('/'),
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
describe('Validation', () => {
@ -448,7 +461,7 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
},
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);
expect(result).to.eql({
status: 'ok',
actionId: simulatedActionId,
@ -461,6 +474,12 @@ export default function servicenowTest({ getService }: FtrProviderContext) {
});
});
});
after(() => {
if (proxyServer) {
proxyServer.close();
}
});
});
});
}

View file

@ -4,9 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import expect from '@kbn/expect';
import http from 'http';
import getPort from 'get-port';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import { getSlackServer } from '../../../../common/fixtures/plugins/actions_simulators/server/plugin';
@ -14,11 +16,14 @@ 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 configService = getService('config');
describe('slack action', () => {
let simulatedActionId = '';
let slackSimulatorURL: string = '';
let slackServer: http.Server;
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
// need to wait for kibanaServer to settle ...
before(async () => {
@ -28,6 +33,13 @@ export default function slackTest({ getService }: FtrProviderContext) {
slackServer.listen(availablePort);
}
slackSimulatorURL = `http://localhost:${availablePort}`;
proxyServer = await getHttpProxyServer(
slackSimulatorURL,
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
it('should return 200 when creating a slack action successfully', async () => {
@ -157,6 +169,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 () => {
@ -224,6 +237,9 @@ export default function slackTest({ getService }: FtrProviderContext) {
after(() => {
slackServer.close();
if (proxyServer) {
proxyServer.close();
}
});
});
}

View file

@ -4,10 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/
import httpProxy from 'http-proxy';
import http from 'http';
import expect from '@kbn/expect';
import { URL, format as formatUrl } from 'url';
import getPort from 'get-port';
import { getHttpProxyServer } from '../../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
import {
getExternalServiceSimulatorPath,
@ -31,6 +33,7 @@ function parsePort(url: Record<string, string>): Record<string, string | null |
export default function webhookTest({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const kibanaServer = getService('kibanaServer');
const configService = getService('config');
async function createWebhookAction(
webhookSimulatorURL: string,
@ -69,6 +72,8 @@ export default function webhookTest({ getService }: FtrProviderContext) {
let webhookSimulatorURL: string = '';
let webhookServer: http.Server;
let kibanaURL: string = '<could not determine kibana url>';
let proxyServer: httpProxy | undefined;
let proxyHaveBeenCalled = false;
// need to wait for kibanaServer to settle ...
before(async () => {
@ -80,6 +85,14 @@ export default function webhookTest({ getService }: FtrProviderContext) {
kibanaURL = kibanaServer.resolveUrl(
getExternalServiceSimulatorPath(ExternalServiceSimulator.WEBHOOK)
);
proxyServer = await getHttpProxyServer(
webhookSimulatorURL,
configService.get('kbnTestServer.serverArgs'),
() => {
proxyHaveBeenCalled = true;
}
);
});
it('should return 200 when creating a webhook action successfully', async () => {
@ -178,6 +191,7 @@ export default function webhookTest({ getService }: FtrProviderContext) {
})
.expect(200);
expect(proxyHaveBeenCalled).to.equal(true);
expect(result.status).to.eql('ok');
});
@ -241,6 +255,9 @@ export default function webhookTest({ getService }: FtrProviderContext) {
after(() => {
webhookServer.close();
if (proxyServer) {
proxyServer.close();
}
});
});
}

View file

@ -4,24 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
import http from 'http';
import { getHttpProxyServer } from '../../../common/lib/get_proxy_server';
import { FtrProviderContext } from '../../../common/ftr_provider_context';
// eslint-disable-next-line import/no-default-export
export default function actionsTests({ loadTestFile, getService }: FtrProviderContext) {
const configService = getService('config');
const kibanaServer = getService('kibanaServer');
const log = getService('log');
describe('Actions', () => {
let proxyServer: http.Server | undefined;
before(async () => {
proxyServer = await getHttpProxyServer(
kibanaServer.resolveUrl('/'),
configService.get('kbnTestServer.serverArgs'),
log
);
});
loadTestFile(require.resolve('./builtin_action_types/email'));
loadTestFile(require.resolve('./builtin_action_types/es_index'));
loadTestFile(require.resolve('./builtin_action_types/es_index_preconfigured'));
@ -39,11 +26,5 @@ export default function actionsTests({ loadTestFile, getService }: FtrProviderCo
loadTestFile(require.resolve('./get'));
loadTestFile(require.resolve('./list_action_types'));
loadTestFile(require.resolve('./update'));
after(() => {
if (proxyServer) {
proxyServer.close();
}
});
});
}