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

Adds a builtin action for triggering webhooks #43538

Merged
merged 35 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d8fa533
feat(webhook-action): Introduced new action type for webhooks
gmmorris Aug 19, 2019
4b05878
feat(webhook-action): Defined secret username and password validation
gmmorris Aug 19, 2019
c9e44bf
feat(webhook-action): Defined the basic configuration object with key…
gmmorris Aug 19, 2019
fee15cd
feat(webhook-action): Defined the advanced configurations for proxy a…
gmmorris Aug 19, 2019
3520e6e
feat(webhook-action): Defined the the overriding url field on the con…
gmmorris Aug 19, 2019
5f1a631
feat(webhook-action): Defined the basic parameters object for the action
gmmorris Aug 19, 2019
1949a55
feat(webhook-action): Reduced HTTP methods down to just POST & PUT to…
gmmorris Aug 20, 2019
5dbedcf
feat(webhook-action): Changed `username` field to `user` to align wit…
gmmorris Aug 20, 2019
9ec86c6
refactor(webhook-action): Introduced enum in place of strings adding …
gmmorris Aug 20, 2019
d3fda32
refactor(alerting-integration-test): Extract Slack Simulator so its e…
gmmorris Aug 20, 2019
4aee48e
Merge branch 'refactor/external-service-simulator' into actions/webhooks
gmmorris Aug 20, 2019
06a1d54
refactor(webhook-action): Use the type system to differenciate betwee…
gmmorris Aug 20, 2019
13bc887
feat(webhook-action): Exposed built in Webhook action
gmmorris Aug 20, 2019
0e531e8
refactor(alerting-integration-test): Extracted Slack Service simulator
gmmorris Aug 21, 2019
7d9b18f
fix(webhook-action): Merged rejigging of integrations tests
gmmorris Aug 21, 2019
a375358
feat(webhook-action): Webhook action makes athenticated calls to targ…
gmmorris Aug 21, 2019
538ca67
feat(webhook-action): Webhook action support both POST and PUT as met…
gmmorris Aug 21, 2019
b1d0a28
feat(webhook-action): Webhook action now supports composite urls
gmmorris Aug 21, 2019
b9a84b6
feat(webhook-action): Webhook action can now handle unreachable and f…
gmmorris Aug 21, 2019
a5ca4bd
feat(webhook-action): Removed proxy and timeouts from the Webhook act…
gmmorris Aug 21, 2019
016ddcb
fix(webhook-action): Fixed clash in SimualtedService whitelisting
gmmorris Aug 21, 2019
05326d7
refactor(webhook-action): Actions now use shared schema and Axios as …
gmmorris Aug 22, 2019
b15392f
Merge branch 'master' into actions/webhooks
gmmorris Aug 22, 2019
d20f718
feat(webhook-action): Added i18n wrapping for an unrechableWebhook er…
gmmorris Aug 22, 2019
a94343a
Merge branch 'master' into actions/webhooks
gmmorris Aug 22, 2019
402aacc
refactor(webhook-action): unify result and retry messaging beltween a…
gmmorris Aug 23, 2019
dcd9f75
refactor(webhook-action): export each function individually
gmmorris Aug 23, 2019
290ae25
refactor(webhook-action): removed Hapi/basic and replaced with simple…
gmmorris Aug 23, 2019
b660982
refactor(webhook-action): cleaned up error handling from webhooks
gmmorris Aug 23, 2019
6939311
fix(webhook-action): Ununified result messaging as i18n cant handle r…
gmmorris Aug 23, 2019
495a9f7
disabled webhook action registration and integration tests inorder to…
gmmorris Aug 23, 2019
3aa3400
fix(action-webhook): only return a retry in seconds if the service ha…
gmmorris Aug 23, 2019
73414f4
refactor(action-webhook): removed composite url support
gmmorris Aug 23, 2019
93ae024
refactor(action-webhook): removed unused code
gmmorris Aug 23, 2019
6cd70f1
fix(action-webhook): removed unused type
gmmorris Aug 23, 2019
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,19 +10,17 @@ import nodemailerServices from 'nodemailer/lib/well-known/services.json';

import { sendEmail, JSON_TRANSPORT_SERVICE } from './lib/send_email';
import { nullableType } from './lib/nullable';
import { portSchema } from './lib/schemas';
import { ActionType, ActionTypeExecutorOptions, ActionTypeExecutorResult } from '../types';

const PORT_MAX = 256 * 256 - 1;

// config definition

export type ActionTypeConfigType = TypeOf<typeof ConfigSchema>;

const ConfigSchema = schema.object(
{
service: nullableType(schema.string()),
host: nullableType(schema.string()),
port: nullableType(schema.number({ min: 1, max: PORT_MAX })),
port: nullableType(portSchema()),
secure: nullableType(schema.boolean()),
from: schema.string(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { ActionTypeRegistry } from '../action_type_registry';

import { actionType as serverLogActionType } from './server_log';
import { actionType as slackActionType } from './slack';
import { actionType as webhookActionType } from './webhook';
import { actionType as emailActionType } from './email';
import { actionType as indexActionType } from './es_index';

export function registerBuiltInActionTypes(actionTypeRegistry: ActionTypeRegistry) {
actionTypeRegistry.register(serverLogActionType);
actionTypeRegistry.register(slackActionType);
actionTypeRegistry.register(webhookActionType);
actionTypeRegistry.register(emailActionType);
actionTypeRegistry.register(indexActionType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
*/

// There appears to be an unexported implementation of Either in here: src/core/server/saved_objects/service/lib/repository.ts
// Which is basically the Haskel equivalent of Rust/ML/Scala's Result
// I'll reach out to other's in Kibana to see if we can merge these into one type

// eslint-disable-next-line @typescript-eslint/prefer-interface
export type Ok<T> = {
tag: 'ok';
value: T;
};
// eslint-disable-next-line @typescript-eslint/prefer-interface
export type Err<E> = {
tag: 'err';
error: E;
};
export type Result<T, E> = Ok<T> | Err<E>;

function asOk<T>(value: T): Ok<T> {
return {
tag: 'ok',
value,
};
}

function asErr<T>(error: T): Err<T> {
return {
tag: 'err',
error,
};
}

function isOk<T, E>(result: Result<T, E>): result is Ok<T> {
return result.tag === 'ok';
}

function isErr<T, E>(result: Result<T, E>): result is Err<E> {
return !isOk(result);
}

async function promiseResult<T, E>(future: Promise<T>): Promise<Result<T, E>> {
try {
return asOk(await future);
} catch (e) {
return asErr(e);
}
}

export { isOk, asOk, isErr, asErr, promiseResult };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason the export for the functions is done this way, vs prefixing the relevant functions with export?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha stupid coding standard from my last company that I never liked.
Happy to change this (more than happy ;))

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* 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 { schema } from '@kbn/config-schema';

const PORT_MAX = 256 * 256 - 1;
export const portSchema = () => schema.number({ min: 1, max: PORT_MAX });
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* 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 { actionType } from './webhook';
import { validateConfig, validateSecrets, validateParams } from '../lib';

describe('actionType', () => {
test('exposes the action as `webhook` on its Id and Name', () => {
expect(actionType.id).toEqual('.webhook');
expect(actionType.name).toEqual('webhook');
});
});

describe('secrets validation', () => {
test('succeeds when secrets is valid', () => {
const secrets: Record<string, any> = {
user: 'bob',
password: 'supersecret',
};
expect(validateSecrets(actionType, secrets)).toEqual(secrets);
});

test('fails when secret password is omitted', () => {
expect(() => {
validateSecrets(actionType, { user: 'bob' });
}).toThrowErrorMatchingInlineSnapshot(
`"error validating action type secrets: [password]: expected value of type [string] but got [undefined]"`
);
});

test('fails when secret user is omitted', () => {
expect(() => {
validateSecrets(actionType, {});
}).toThrowErrorMatchingInlineSnapshot(
`"error validating action type secrets: [user]: expected value of type [string] but got [undefined]"`
);
});
});

describe('config validation', () => {
const defaultValues: Record<string, any> = {
headers: null,
method: 'post',
};

const defaultCompositeUrlValues: Record<string, any> = {
scheme: 'http',
path: null,
};

test('config validation passes when only required fields are provided', () => {
const config: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
},
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
url: {
...defaultCompositeUrlValues,
...config.url,
},
});
});

test('config validation passes when valid schemes are provided', () => {
const httpConfig: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
scheme: 'http',
},
};
expect(validateConfig(actionType, httpConfig)).toEqual({
...defaultValues,
...httpConfig,
url: {
...defaultCompositeUrlValues,
...httpConfig.url,
},
});

const httpsConfig: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
scheme: 'https',
},
};
expect(validateConfig(actionType, httpsConfig)).toEqual({
...defaultValues,
url: {
...defaultCompositeUrlValues,
...httpsConfig.url,
},
});
});

test('should validate and throw error when scheme on config is invalid', () => {
const config: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
scheme: 'ftp',
},
};
expect(() => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [url]: types that failed validation:
- [url.0]: expected value of type [string] but got [Object]
- [url.1.scheme]: types that failed validation:"
`);
});

test('config validation passes when valid methods are provided', () => {
['post', 'put'].forEach(method => {
const config: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
},
method,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
...config,
url: {
...defaultCompositeUrlValues,
...config.url,
},
});
});
});

test('should validate and throw error when method on config is invalid', () => {
const config: Record<string, any> = {
url: {
host: 'mylisteningserver',
port: 9200,
},
method: 'https',
};
expect(() => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [method]: types that failed validation:
- [method.0]: expected value to equal [post] but got [https]
- [method.1]: expected value to equal [put] but got [https]"
`);
});

test('config validation passes when a url is specified', () => {
const config: Record<string, any> = {
url: 'http://mylisteningserver:9200/endpoint',
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
...config,
});
});

test('config validation passes when valid headers are provided', () => {
const config: Record<string, any> = {
url: 'http://mylisteningserver:9200/endpoint',
headers: {
'Content-Type': 'application/json',
},
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
...config,
});
});

test('should validate and throw error when headers on config is invalid', () => {
const config: Record<string, any> = {
url: 'http://mylisteningserver:9200/endpoint',
headers: 'application/json',
};
expect(() => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [headers]: types that failed validation:
- [headers.0]: expected value of type [object] but got [string]
- [headers.1]: expected value to equal [null] but got [application/json]"
`);
});
});

describe('params validation', () => {
test('param validation passes when no fields are provided as none are required', () => {
const params: Record<string, any> = {};
expect(validateParams(actionType, params)).toEqual({});
});

test('params validation passes when a valid body is provided', () => {
const params: Record<string, any> = {
body: 'count: {{ctx.payload.hits.total}}',
};
expect(validateParams(actionType, params)).toEqual({
...params,
});
});
});
Loading