-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
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 4b05878
feat(webhook-action): Defined secret username and password validation
gmmorris c9e44bf
feat(webhook-action): Defined the basic configuration object with key…
gmmorris fee15cd
feat(webhook-action): Defined the advanced configurations for proxy a…
gmmorris 3520e6e
feat(webhook-action): Defined the the overriding url field on the con…
gmmorris 5f1a631
feat(webhook-action): Defined the basic parameters object for the action
gmmorris 1949a55
feat(webhook-action): Reduced HTTP methods down to just POST & PUT to…
gmmorris 5dbedcf
feat(webhook-action): Changed `username` field to `user` to align wit…
gmmorris 9ec86c6
refactor(webhook-action): Introduced enum in place of strings adding …
gmmorris d3fda32
refactor(alerting-integration-test): Extract Slack Simulator so its e…
gmmorris 4aee48e
Merge branch 'refactor/external-service-simulator' into actions/webhooks
gmmorris 06a1d54
refactor(webhook-action): Use the type system to differenciate betwee…
gmmorris 13bc887
feat(webhook-action): Exposed built in Webhook action
gmmorris 0e531e8
refactor(alerting-integration-test): Extracted Slack Service simulator
gmmorris 7d9b18f
fix(webhook-action): Merged rejigging of integrations tests
gmmorris a375358
feat(webhook-action): Webhook action makes athenticated calls to targ…
gmmorris 538ca67
feat(webhook-action): Webhook action support both POST and PUT as met…
gmmorris b1d0a28
feat(webhook-action): Webhook action now supports composite urls
gmmorris b9a84b6
feat(webhook-action): Webhook action can now handle unreachable and f…
gmmorris a5ca4bd
feat(webhook-action): Removed proxy and timeouts from the Webhook act…
gmmorris 016ddcb
fix(webhook-action): Fixed clash in SimualtedService whitelisting
gmmorris 05326d7
refactor(webhook-action): Actions now use shared schema and Axios as …
gmmorris b15392f
Merge branch 'master' into actions/webhooks
gmmorris d20f718
feat(webhook-action): Added i18n wrapping for an unrechableWebhook er…
gmmorris a94343a
Merge branch 'master' into actions/webhooks
gmmorris 402aacc
refactor(webhook-action): unify result and retry messaging beltween a…
gmmorris dcd9f75
refactor(webhook-action): export each function individually
gmmorris 290ae25
refactor(webhook-action): removed Hapi/basic and replaced with simple…
gmmorris b660982
refactor(webhook-action): cleaned up error handling from webhooks
gmmorris 6939311
fix(webhook-action): Ununified result messaging as i18n cant handle r…
gmmorris 495a9f7
disabled webhook action registration and integration tests inorder to…
gmmorris 3aa3400
fix(action-webhook): only return a retry in seconds if the service ha…
gmmorris 73414f4
refactor(action-webhook): removed composite url support
gmmorris 93ae024
refactor(action-webhook): removed unused code
gmmorris 6cd70f1
fix(action-webhook): removed unused type
gmmorris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
x-pack/legacy/plugins/actions/server/builtin_action_types/lib/result_type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
10 changes: 10 additions & 0 deletions
10
x-pack/legacy/plugins/actions/server/builtin_action_types/lib/schemas.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
209 changes: 209 additions & 0 deletions
209
x-pack/legacy/plugins/actions/server/builtin_action_types/webhook.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 ;))