-
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
[Alerting] fixes to allow pre-configured actions to be executed #63432
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,14 +6,20 @@ | |
|
||
import { SavedObjectsClientContract } from '../../../../src/core/server'; | ||
import { TaskManagerStartContract } from '../../task_manager/server'; | ||
import { GetBasePathFunction, RawAction, ActionTypeRegistryContract } from './types'; | ||
import { | ||
GetBasePathFunction, | ||
RawAction, | ||
ActionTypeRegistryContract, | ||
PreConfiguredAction, | ||
} from './types'; | ||
|
||
interface CreateExecuteFunctionOptions { | ||
taskManager: TaskManagerStartContract; | ||
getScopedSavedObjectsClient: (request: any) => SavedObjectsClientContract; | ||
getBasePath: GetBasePathFunction; | ||
isESOUsingEphemeralEncryptionKey: boolean; | ||
actionTypeRegistry: ActionTypeRegistryContract; | ||
preconfiguredActions: PreConfiguredAction[]; | ||
} | ||
|
||
export interface ExecuteOptions { | ||
|
@@ -29,6 +35,7 @@ export function createExecuteFunction({ | |
actionTypeRegistry, | ||
getScopedSavedObjectsClient, | ||
isESOUsingEphemeralEncryptionKey, | ||
preconfiguredActions, | ||
}: CreateExecuteFunctionOptions) { | ||
return async function execute({ id, params, spaceId, apiKey }: ExecuteOptions) { | ||
if (isESOUsingEphemeralEncryptionKey === true) { | ||
|
@@ -61,9 +68,9 @@ export function createExecuteFunction({ | |
}; | ||
|
||
const savedObjectsClient = getScopedSavedObjectsClient(fakeRequest); | ||
const actionSavedObject = await savedObjectsClient.get<RawAction>('action', id); | ||
const actionTypeId = await getActionTypeId(id); | ||
|
||
actionTypeRegistry.ensureActionTypeEnabled(actionSavedObject.attributes.actionTypeId); | ||
actionTypeRegistry.ensureActionTypeEnabled(actionTypeId); | ||
|
||
const actionTaskParamsRecord = await savedObjectsClient.create('action_task_params', { | ||
actionId: id, | ||
|
@@ -72,13 +79,23 @@ export function createExecuteFunction({ | |
}); | ||
|
||
await taskManager.schedule({ | ||
taskType: `actions:${actionSavedObject.attributes.actionTypeId}`, | ||
taskType: `actions:${actionTypeId}`, | ||
params: { | ||
spaceId, | ||
actionTaskParamsId: actionTaskParamsRecord.id, | ||
}, | ||
state: {}, | ||
scope: ['actions'], | ||
}); | ||
|
||
async function getActionTypeId(actionId: string): Promise<string> { | ||
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. As a side note, we might want to look at denormalizing the action type along side action id, where appropriate, to avoid this SO lookup just to get the action type. OTOH, it's only when actions are executed, so ... it's not on every turn of an alert ... 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. (Moment not writing a comment haunts me): I think the lookup is also there to ensure the user has access to read that action, otherwise it would fail the attempt to create a task executing that action. Sigh note, this would also mean pre-configured actions don't work with feature controls at this time. I will open a separate issue for that, to test and validate that theory but the implementation below looks good. 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. Created issue here: #63496. |
||
const pcAction = preconfiguredActions.find(action => action.id === actionId); | ||
if (pcAction) { | ||
return pcAction.actionTypeId; | ||
} | ||
|
||
const actionSO = await savedObjectsClient.get<RawAction>('action', actionId); | ||
return actionSO.attributes.actionTypeId; | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,27 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions) | |
xyzSecret2: 'credential2', | ||
}, | ||
}, | ||
{ | ||
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. More pre-configured actions, used in the tests below. Show up in the various |
||
id: 'preconfigured-es-index-action', | ||
actionTypeId: '.index', | ||
name: 'preconfigured_es_index_action', | ||
config: { | ||
index: 'functional-test-actions-index-preconfigured', | ||
refresh: true, | ||
executionTimeField: 'timestamp', | ||
}, | ||
}, | ||
{ | ||
id: 'preconfigured.test.index-record', | ||
actionTypeId: 'test.index-record', | ||
name: 'Test:_Preconfigured_Index_Record', | ||
config: { | ||
unencrypted: 'ignored-but-required', | ||
}, | ||
secrets: { | ||
encrypted: 'this-is-also-ignored-and-also-required', | ||
}, | ||
}, | ||
])}`, | ||
...disabledPlugins.map(key => `--xpack.${key}.enabled=false`), | ||
`--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'alerts')}`, | ||
|
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.
This is a copy of the test above it, only changing to use a preconfigured action.