Skip to content

Commit

Permalink
FTS tests for servicenow
Browse files Browse the repository at this point in the history
  • Loading branch information
stephmilovic committed Jan 15, 2020
1 parent d2c39a7 commit cd05bdb
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface PostServiceNowOptions {
secrets: SecretsType;
}

// post an event to pagerduty
// post an event to serviceNow
export async function postServiceNow(options: PostServiceNowOptions): Promise<AxiosResponse> {
const { apiUrl, data, headers, secrets } = options;
const axiosOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function validateSecrets(
export type ParamsType = TypeOf<typeof ParamsSchema>;

const ParamsSchema = schema.object({
comments: schema.string(),
comments: schema.maybe(schema.string()),
short_description: schema.string(),
});

Expand Down Expand Up @@ -112,7 +112,6 @@ async function serviceNowExecutor(
const config = execOptions.config as ConfigType;
const secrets = execOptions.secrets as SecretsType;
const params = execOptions.params as ParamsType;

const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
Expand All @@ -131,7 +130,7 @@ async function serviceNowExecutor(
serviceMessage: err.message,
};
}
if (response.status === 201) {
if (response.status === 200 || response.status === 201 || response.status === 204) {
return {
status: 'ok',
actionId,
Expand Down
11 changes: 6 additions & 5 deletions x-pack/test/alerting_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ interface CreateTestConfigOptions {

// test.not-enabled is specifically not enabled
const enabledActionTypes = [
'.server-log',
'.slack',
'.email',
'.index',
'.pagerduty',
'.server-log',
'.servicenow',
'.slack',
'.webhook',
'test.noop',
'test.index-record',
'test.authorization',
'test.failing',
'test.index-record',
'test.noop',
'test.rate-limit',
'test.authorization',
];

// eslint-disable-next-line import/no-default-export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
import Hapi from 'hapi';
import { ActionType } from '../../../../../../legacy/plugins/actions';

import { initPlugin as initPagerduty } from './pagerduty_simulation';
import { initPlugin as initServiceNow } from './servicenow_simulation';
import { initPlugin as initSlack } from './slack_simulation';
import { initPlugin as initWebhook } from './webhook_simulation';
import { initPlugin as initPagerduty } from './pagerduty_simulation';

const NAME = 'actions-FTS-external-service-simulators';

export enum ExternalServiceSimulator {
PAGERDUTY = 'pagerduty',
SERVICENOW = 'servicenow',
SLACK = 'slack',
WEBHOOK = 'webhook',
PAGERDUTY = 'pagerduty',
}

export function getExternalServiceSimulatorPath(service: ExternalServiceSimulator): string {
return `/api/_${NAME}/${service}`;
}

export function getAllExternalServiceSimulatorPaths(): string[] {
return Object.values(ExternalServiceSimulator).map(service =>
const allPaths = Object.values(ExternalServiceSimulator).map(service =>
getExternalServiceSimulatorPath(service)
);
allPaths.push(`/api/_${NAME}/${ExternalServiceSimulator.SERVICENOW}/api/now/v1/table/incident`);
return allPaths;
}

// eslint-disable-next-line import/no-default-export
Expand Down Expand Up @@ -67,9 +71,10 @@ export default function(kibana: any) {
},
});

initPagerduty(server, getExternalServiceSimulatorPath(ExternalServiceSimulator.PAGERDUTY));
initServiceNow(server, getExternalServiceSimulatorPath(ExternalServiceSimulator.SERVICENOW));
initSlack(server, getExternalServiceSimulatorPath(ExternalServiceSimulator.SLACK));
initWebhook(server, getExternalServiceSimulatorPath(ExternalServiceSimulator.WEBHOOK));
initPagerduty(server, getExternalServiceSimulatorPath(ExternalServiceSimulator.PAGERDUTY));
},
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* 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 Joi from 'joi';
import Hapi from 'hapi';

interface ServiceNowRequest extends Hapi.Request {
payload: {
comments: string;
short_description: string;
};
}
export function initPlugin(server: Hapi.Server, path: string) {
server.route({
method: 'POST',
path,
options: {
auth: false,
validate: {
options: { abortEarly: false },
payload: Joi.object().keys({
comments: Joi.string(),
short_description: Joi.string(),
}),
},
},
handler: servicenowHandler,
});

server.route({
method: 'POST',
path: `${path}/api/now/v1/table/incident`,
options: {
auth: false,
validate: {
options: { abortEarly: false },
payload: Joi.object().keys({
comments: Joi.string(),
short_description: Joi.string(),
}),
},
},
handler: servicenowHandler,
});
}
// ServiceNow simulator: create a servicenow action pointing here, and you can get
// different responses based on the message posted. See the README.md for
// more info.

function servicenowHandler(request: ServiceNowRequest, h: any) {
const body = request.payload;
const text = body && body.short_description;
if (text == null) {
return jsonResponse(h, 400, 'bad request to servicenow simulator');
}

switch (text) {
case 'success':
return jsonResponse(h, 200, 'Success');

case 'created':
return jsonResponse(h, 201, 'Created');

case 'no_text':
return jsonResponse(h, 204, 'Success');

case 'invalid_payload':
return jsonResponse(h, 400, 'Bad Request');

case 'unauthorized':
return jsonResponse(h, 401, 'Unauthorized');

case 'forbidden':
return jsonResponse(h, 403, 'Forbidden');

case 'not_found':
return jsonResponse(h, 404, 'Not found');

case 'not_allowed':
return jsonResponse(h, 405, 'Method not allowed');

case 'not_acceptable':
return jsonResponse(h, 406, 'Not acceptable');

case 'unsupported':
return jsonResponse(h, 415, 'Unsupported media type');

case 'status_500':
return jsonResponse(h, 500, 'simulated servicenow 500 response');

case 'rate_limit':
const response = {
retry_after: 1,
ok: false,
error: 'rate_limited',
};

return h
.response(response)
.type('application/json')
.header('retry-after', '1')
.code(429);
}

return jsonResponse(h, 400, 'unknown request to servicenow simulator');
}

function jsonResponse(h: any, code: number, object?: any) {
if (object == null) {
return h.response('').code(code);
}

return h
.response(JSON.stringify(object))
.type('application/json')
.code(code);
}
Loading

0 comments on commit cd05bdb

Please sign in to comment.