[Actions] Add support for system actions in Rules APIs#161726
[Actions] Add support for system actions in Rules APIs#161726cnasikas wants to merge 149 commits intoelastic:system_actions_mvpfrom
Conversation
…ana into system_actions_registration
|
|
||
| const ruleActionSchema = schema.object({ | ||
| group: schema.string(), | ||
| group: schema.maybe(schema.string()), |
There was a problem hiding this comment.
System actions do not have a group. Changing this to optional to allow system actions to be created without a group.
| type: typeof RuleActionTypes.SYSTEM; | ||
| } | ||
|
|
||
| export type RuleAction = RuleDefaultAction | RuleSystemAction; |
There was a problem hiding this comment.
This type is used internally by the rules client.
| * | ||
| */ | ||
| export type AlertNavigationHandler = (rule: SanitizedRule) => string; | ||
| export type AlertNavigationHandler = (rule: SanitizedRuleResponse) => string; |
There was a problem hiding this comment.
The responses from the alerting APIs do not contain the type of action.
| options: BulkEditOptions<Params> | ||
| ): Promise<BulkEditResult<Params>> { | ||
| try { | ||
| bulkEditOperationsSchema.validate(options.operations); |
There was a problem hiding this comment.
Validation of the schema was missing from the method. The schema requires at least one operation to be set.
| rule.references = migratedActions.resultedReferences; | ||
| } | ||
|
|
||
| const ruleActions = injectReferencesIntoActions( |
There was a problem hiding this comment.
transformRuleAttributesToRuleDomain calls transformRawActionsToDomainActions which in turn injects the references into the actions. No need to do it twice.
| operation: schema.oneOf([schema.literal('add'), schema.literal('set')]), | ||
| field: schema.literal('actions'), | ||
| value: schema.arrayOf(bulkEditActionSchema), | ||
| value: schema.arrayOf(schema.oneOf([bulkEditDefaultActionSchema, bulkEditSystemActionSchema])), |
There was a problem hiding this comment.
Rule methods accept two types of actions.
| type: schema.literal(RuleActionTypes.DEFAULT), | ||
| }); | ||
|
|
||
| export const bulkEditSystemActionSchema = schema.object({ |
There was a problem hiding this comment.
System actions are not allowed to contain: group, alertsFilter, and frequency.
| references?: SavedObjectReference[]; | ||
| } | ||
|
|
||
| export const transformRawActionsToDomainActions = ({ |
There was a problem hiding this comment.
This function adds the id from the references and the type in all actions. The type is not persisted in ES.
| : null; | ||
|
|
||
| let actions = esRule.actions | ||
| ? injectReferencesIntoActions(id, esRule.actions as RawRule['actions'], references || []) |
There was a problem hiding this comment.
Logic moved inside transformRawActionsToDomainActions.
| } | ||
|
|
||
| const connectorAdapter = connectorAdapterRegistry.get(connectorTypeId); | ||
| const schema = connectorAdapter.ruleActionParamsSchema; |
There was a problem hiding this comment.
This is the schema of the system action params persisted in the rule.
| data: { ...alert, notifyWhen }, | ||
| data: { | ||
| ...alert, | ||
| actions: rewriteActionsReq(alert.actions, (connectorId: string) => |
There was a problem hiding this comment.
Adds the type to the actions.
|
|
||
| return res.ok({ | ||
| body: alertRes, | ||
| body: { ...alertRes, actions: rewriteActionsResLegacy(alertRes.actions) }, |
There was a problem hiding this comment.
Removes the type from the actions.
| return []; | ||
| } | ||
|
|
||
| return actions.map((action) => { |
There was a problem hiding this comment.
Adds the type to the actions.
| : {}), | ||
| })); | ||
|
|
||
| return actions.map((action) => { |
There was a problem hiding this comment.
Removes the type from the actions.
💔 Build FailedFailed CI Steps
Test Failures
Metrics [docs]Module Count
Page load bundle
History
To update your PR or re-run it, just comment with: cc @cnasikas |
| }, | ||
| } | ||
| : {}), | ||
| ...(alertsFilter && { alertsFilter }), |
There was a problem hiding this comment.
Thanks for fixing this.
ersin-erdal
left a comment
There was a problem hiding this comment.
Went through the code and left some comments.
I will run and test the PR locally then approve.
| renderActionParameterTemplates: (...args) => | ||
| renderActionParameterTemplates(actionTypeRegistry, ...args), | ||
| isSystemActionConnector: (connectorId: string): boolean => { | ||
| return !!this.inMemoryConnectors.find( |
There was a problem hiding this comment.
Nit: inMemoryConnectors.some could be better here.
| throw error; | ||
| } | ||
|
|
||
| const systemActions = context.inMemoryConnectors.filter((connector) => connector.isSystemAction); |
There was a problem hiding this comment.
I think these are connectors not actions (I know isSystemAction is misleading :) )
Even the return type is FindConnectorResult
| import { AlertingEventLogger } from '../lib/alerting_event_logger/alerting_event_logger'; | ||
|
|
||
| export interface RuleData<Params extends RuleTypeParams> extends LoadedIndirectParams<RawRule> { | ||
| indirectParams: RawRule; |
There was a problem hiding this comment.
I think indirectParams has been removed by mistake
|
Closing the PR in favor of smaller PRs to aid the review process |
Summary
In this PR we allow the usage of system actions through the Rules APIs. Major decisions:
typeproperty only inside the methods of the rule client.typeof the action. The framework will detect this automatically.typeto the actions before passing the request to the rule client.type.typefrom the actions before returning the response to the client.groupis now optional because system actions do not support it.typeof the action will not be persisted to ES.To keep the size of the PR small, and because it will be merged into a feature branch, I decided to work in the Create Rule, Update Rule, and Bulk Edit Rules APIs. I also follow TS errors and failed tests and update some of the other routes/client methods. Another PR will follow to handle the rest of the APIs and the CI errors produced by these APIs and another PR will follow to handle the APIs of Security Solution.
Issue: #160367
Depends on: #165671
Checklist
Delete any items that are not applicable to this PR.
For maintainers