-
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 whitelisting to slack and pagerduty action types #52989
Merged
gmmorris
merged 7 commits into
elastic:master
from
gmmorris:actions/whitelist-pagerduty-slack
Dec 16, 2019
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
176e09f
enforce whitelisting on pagerduty target urls
gmmorris e9412ad
enforce whitelisting on slack webhook target urls
gmmorris b006941
updated docs in regards to the whitelisting of built-in action type
gmmorris 7e14a47
clean up actions index
gmmorris 0e84a46
fixed case in doc
gmmorris 69db420
Merge branch 'master' into actions/whitelist-pagerduty-slack
elasticmachine 0ad0f03
Merge branch 'master' into actions/whitelist-pagerduty-slack
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
14 changes: 14 additions & 0 deletions
14
x-pack/legacy/plugins/actions/server/actions_config.mock.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,14 @@ | ||
/* | ||
* 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 { ActionsConfigurationUtilities } from './actions_config'; | ||
|
||
export const configUtilsMock: ActionsConfigurationUtilities = { | ||
isWhitelistedHostname: _ => true, | ||
isWhitelistedUri: _ => true, | ||
ensureWhitelistedHostname: _ => {}, | ||
ensureWhitelistedUri: _ => {}, | ||
}; |
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
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
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 |
---|---|---|
|
@@ -5,11 +5,10 @@ | |
*/ | ||
|
||
import { ActionType, Services, ActionTypeExecutorOptions } from '../types'; | ||
import { ActionTypeRegistry } from '../action_type_registry'; | ||
import { savedObjectsClientMock } from '../../../../../../src/core/server/mocks'; | ||
import { ActionExecutor, validateParams, validateSecrets, TaskRunnerFactory } from '../lib'; | ||
import { validateParams, validateSecrets } from '../lib'; | ||
import { getActionType } from './slack'; | ||
import { taskManagerMock } from '../../../task_manager/task_manager.mock'; | ||
import { configUtilsMock } from '../actions_config.mock'; | ||
|
||
const ACTION_TYPE_ID = '.slack'; | ||
|
||
|
@@ -18,47 +17,19 @@ const services: Services = { | |
savedObjectsClient: savedObjectsClientMock.create(), | ||
}; | ||
|
||
let actionTypeRegistry: ActionTypeRegistry; | ||
let actionType: ActionType; | ||
|
||
async function mockSlackExecutor(options: ActionTypeExecutorOptions): Promise<any> { | ||
const { params } = options; | ||
const { message } = params; | ||
if (message == null) throw new Error('message property required in parameter'); | ||
|
||
const failureMatch = message.match(/^failure: (.*)$/); | ||
if (failureMatch != null) { | ||
const failMessage = failureMatch[1]; | ||
throw new Error(`slack mockExecutor failure: ${failMessage}`); | ||
} | ||
|
||
return { | ||
text: `slack mockExecutor success: ${message}`, | ||
}; | ||
} | ||
|
||
beforeAll(() => { | ||
actionTypeRegistry = new ActionTypeRegistry({ | ||
taskManager: taskManagerMock.create(), | ||
taskRunnerFactory: new TaskRunnerFactory(new ActionExecutor()), | ||
}); | ||
actionTypeRegistry.register(getActionType({ executor: mockSlackExecutor })); | ||
actionType = actionTypeRegistry.get(ACTION_TYPE_ID); | ||
|
||
test('ensure action type is valid', () => { | ||
expect(actionType).toBeTruthy(); | ||
actionType = getActionType({ | ||
async executor(options: ActionTypeExecutorOptions): Promise<any> {}, | ||
configurationUtilities: configUtilsMock, | ||
}); | ||
}); | ||
|
||
describe('action is registered', () => { | ||
test('gets registered with builtin actions', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is essentially an integration test and is covered by the function test suite |
||
expect(actionTypeRegistry.has(ACTION_TYPE_ID)).toEqual(true); | ||
}); | ||
|
||
describe('action registeration', () => { | ||
test('returns action type', () => { | ||
const returnedActionType = actionTypeRegistry.get(ACTION_TYPE_ID); | ||
expect(returnedActionType.id).toEqual(ACTION_TYPE_ID); | ||
expect(returnedActionType.name).toEqual('slack'); | ||
expect(actionType.id).toEqual(ACTION_TYPE_ID); | ||
expect(actionType.name).toEqual('slack'); | ||
}); | ||
}); | ||
|
||
|
@@ -104,9 +75,64 @@ describe('validateActionTypeSecrets()', () => { | |
`"error validating action type secrets: [webhookUrl]: expected value of type [string] but got [number]"` | ||
); | ||
}); | ||
|
||
test('should validate and pass when the slack webhookUrl is whitelisted', () => { | ||
actionType = getActionType({ | ||
configurationUtilities: { | ||
...configUtilsMock, | ||
ensureWhitelistedUri: url => { | ||
expect(url).toEqual('https://api.slack.com/'); | ||
}, | ||
}, | ||
}); | ||
|
||
expect(validateSecrets(actionType, { webhookUrl: 'https://api.slack.com/' })).toEqual({ | ||
webhookUrl: 'https://api.slack.com/', | ||
}); | ||
}); | ||
|
||
test('config validation returns an error if the specified URL isnt whitelisted', () => { | ||
actionType = getActionType({ | ||
configurationUtilities: { | ||
...configUtilsMock, | ||
ensureWhitelistedUri: url => { | ||
throw new Error(`target url is not whitelisted`); | ||
}, | ||
}, | ||
}); | ||
|
||
expect(() => { | ||
validateSecrets(actionType, { webhookUrl: 'https://api.slack.com/' }); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"error validating action type secrets: error configuring slack action: target url is not whitelisted"` | ||
); | ||
}); | ||
}); | ||
|
||
describe('execute()', () => { | ||
beforeAll(() => { | ||
async function mockSlackExecutor(options: ActionTypeExecutorOptions): Promise<any> { | ||
const { params } = options; | ||
const { message } = params; | ||
if (message == null) throw new Error('message property required in parameter'); | ||
|
||
const failureMatch = message.match(/^failure: (.*)$/); | ||
if (failureMatch != null) { | ||
const failMessage = failureMatch[1]; | ||
throw new Error(`slack mockExecutor failure: ${failMessage}`); | ||
} | ||
|
||
return { | ||
text: `slack mockExecutor success: ${message}`, | ||
}; | ||
} | ||
|
||
actionType = getActionType({ | ||
executor: mockSlackExecutor, | ||
configurationUtilities: configUtilsMock, | ||
}); | ||
}); | ||
|
||
test('calls the mock executor with success', async () => { | ||
const response = await actionType.executor({ | ||
actionId: 'some-id', | ||
|
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.
hahaha I just created one of these for another PR!!!!
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.
It was the obvious refactor :)