Skip to content

Commit

Permalink
[RAM] Add toggle for AAD fields in alert templating (elastic#170162)
Browse files Browse the repository at this point in the history
## Summary

Reopen of elastic#161213

Closes elastic#160838

Feature can be activated via feature flag:
`xpack.trigger_actions_ui.enableExperimental:
['isMustacheAutocompleteOn', 'showMustacheAutocompleteSwitch']`
<img width="605" alt="Screenshot 2023-10-30 at 5 52 39 PM"
src="https://github.com/elastic/kibana/assets/1445834/da24b419-3b08-4014-be2f-99692773755f">
<img width="583" alt="Screenshot 2023-10-30 at 5 52 22 PM"
src="https://github.com/elastic/kibana/assets/1445834/fc5b8a1e-8202-4491-b4fb-694b70809f4d">



### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Julian Gernun <[email protected]>
Co-authored-by: Xavier Mouligneau <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
4 people authored Nov 14, 2023
1 parent a46923c commit 603a045
Show file tree
Hide file tree
Showing 42 changed files with 537 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const actionSchema = schema.object({
params: schema.recordOf(schema.string(), schema.maybe(schema.any()), { defaultValue: {} }),
frequency: schema.maybe(actionFrequencySchema),
alerts_filter: schema.maybe(actionAlertsFilterSchema),
use_alert_data_for_template: schema.maybe(schema.boolean()),
});

export const createBodySchema = schema.object({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface RuleAction {
params: RuleActionParams;
frequency?: RuleActionFrequency;
alertsFilter?: AlertsFilter;
useAlertDataForTemplate?: boolean;
}

export interface RuleLastRun {
Expand Down
19 changes: 17 additions & 2 deletions x-pack/plugins/alerting/server/alert/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { v4 as uuidV4 } from 'uuid';
import { AADAlert } from '@kbn/alerts-as-data-utils';
import { get, isEmpty } from 'lodash';
import { MutableAlertInstanceMeta } from '@kbn/alerting-state-types';
import { ALERT_UUID } from '@kbn/rule-data-utils';
Expand Down Expand Up @@ -36,7 +37,7 @@ export type PublicAlert<
Context extends AlertInstanceContext = AlertInstanceContext,
ActionGroupIds extends string = DefaultActionGroupId
> = Pick<
Alert<State, Context, ActionGroupIds>,
Alert<State, Context, ActionGroupIds, AADAlert>,
| 'getContext'
| 'getState'
| 'getUuid'
Expand All @@ -50,13 +51,15 @@ export type PublicAlert<
export class Alert<
State extends AlertInstanceState = AlertInstanceState,
Context extends AlertInstanceContext = AlertInstanceContext,
ActionGroupIds extends string = never
ActionGroupIds extends string = never,
AlertAsData extends AADAlert = AADAlert
> {
private scheduledExecutionOptions?: ScheduledExecutionOptions<State, Context, ActionGroupIds>;
private meta: MutableAlertInstanceMeta;
private state: State;
private context: Context;
private readonly id: string;
private alertAsData: AlertAsData | undefined;

constructor(id: string, { state, meta = {} }: RawAlertInstance = {}) {
this.id = id;
Expand All @@ -78,6 +81,18 @@ export class Alert<
return this.meta.uuid!;
}

isAlertAsData() {
return this.alertAsData !== undefined;
}

setAlertAsData(alertAsData: AlertAsData) {
this.alertAsData = alertAsData;
}

getAlertAsData() {
return this.alertAsData;
}

getStart(): string | null {
return this.state.start ? `${this.state.start}` : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const createRuleDataSchema = schema.object({
),
uuid: schema.maybe(schema.string()),
alertsFilter: schema.maybe(actionAlertsFilterSchema),
useAlertDataForTemplate: schema.maybe(schema.boolean()),
}),
{ defaultValue: [] }
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const actionDomainSchema = schema.object({
params: actionParamsSchema,
frequency: schema.maybe(actionFrequencySchema),
alertsFilter: schema.maybe(actionDomainAlertsFilterSchema),
useAlertDataAsTemplate: schema.maybe(schema.boolean()),
});

/**
Expand All @@ -89,4 +90,5 @@ export const actionSchema = schema.object({
params: actionParamsSchema,
frequency: schema.maybe(actionFrequencySchema),
alertsFilter: schema.maybe(actionAlertsFilterSchema),
useAlertDataForTemplate: schema.maybe(schema.boolean()),
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const actionsSchema = schema.arrayOf(
),
})
),
use_alert_data_for_template: schema.maybe(schema.boolean()),
}),
{ defaultValue: [] }
);
59 changes: 35 additions & 24 deletions x-pack/plugins/alerting/server/routes/lib/rewrite_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,28 @@ export const rewriteActionsReq = (
): NormalizedAlertAction[] => {
if (!actions) return [];

return actions.map(({ frequency, alerts_filter: alertsFilter, ...action }) => {
return {
...action,
...(frequency
? {
frequency: {
...omit(frequency, 'notify_when'),
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
});
return actions.map(
({
frequency,
alerts_filter: alertsFilter,
use_alert_data_for_template: useAlertDataForTemplate,
...action
}) => {
return {
...action,
useAlertDataForTemplate,
...(frequency
? {
frequency: {
...omit(frequency, 'notify_when'),
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
}
);
};

export const rewriteActionsRes = (actions?: RuleAction[]) => {
Expand All @@ -37,14 +45,17 @@ export const rewriteActionsRes = (actions?: RuleAction[]) => {
notify_when: notifyWhen,
});
if (!actions) return [];
return actions.map(({ actionTypeId, frequency, alertsFilter, ...action }) => ({
...action,
connector_type_id: actionTypeId,
...(frequency ? { frequency: rewriteFrequency(frequency) } : {}),
...(alertsFilter
? {
alerts_filter: alertsFilter,
}
: {}),
}));
return actions.map(
({ actionTypeId, frequency, alertsFilter, useAlertDataForTemplate, ...action }) => ({
...action,
connector_type_id: actionTypeId,
use_alert_data_for_template: useAlertDataForTemplate,
...(frequency ? { frequency: rewriteFrequency(frequency) } : {}),
...(alertsFilter
? {
alerts_filter: alertsFilter,
}
: {}),
})
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe('bulkEditRulesRoute', () => {
foo: true,
},
uuid: '123-456',
use_alert_data_for_template: false,
},
],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('createRuleRoute', () => {
},
connector_type_id: 'test',
uuid: '123-456',
use_alert_data_for_template: false,
},
],
};
Expand Down Expand Up @@ -198,6 +199,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -314,6 +316,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -431,6 +434,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down Expand Up @@ -548,6 +552,7 @@ describe('createRuleRoute', () => {
"params": Object {
"foo": true,
},
"useAlertDataForTemplate": undefined,
},
],
"alertTypeId": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@ import type { RuleParams } from '../../../../../../application/rule/types';
const transformCreateBodyActions = (actions: CreateRuleActionV1[]): CreateRuleData['actions'] => {
if (!actions) return [];

return actions.map(({ frequency, alerts_filter: alertsFilter, ...action }) => {
return {
group: action.group,
id: action.id,
params: action.params,
actionTypeId: action.actionTypeId,
...(action.uuid ? { uuid: action.uuid } : {}),
...(frequency
? {
frequency: {
summary: frequency.summary,
throttle: frequency.throttle,
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
});
return actions.map(
({
frequency,
alerts_filter: alertsFilter,
use_alert_data_for_template: useAlertDataForTemplate,
...action
}) => {
return {
group: action.group,
id: action.id,
params: action.params,
actionTypeId: action.actionTypeId,
useAlertDataForTemplate,
...(action.uuid ? { uuid: action.uuid } : {}),
...(frequency
? {
frequency: {
summary: frequency.summary,
throttle: frequency.throttle,
notifyWhen: frequency.notify_when,
},
}
: {}),
...(alertsFilter ? { alertsFilter } : {}),
};
}
);
};

export const transformCreateBody = <Params extends RuleParams = never>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('resolveRuleRoute', () => {
foo: true,
},
uuid: '123-456',
useAlertDataForTemplate: false,
},
],
consumer: 'bar',
Expand Down Expand Up @@ -101,6 +102,7 @@ describe('resolveRuleRoute', () => {
params: mockedRule.actions[0].params,
connector_type_id: mockedRule.actions[0].actionTypeId,
uuid: mockedRule.actions[0].uuid,
use_alert_data_for_template: mockedRule.actions[0].useAlertDataForTemplate,
},
],
outcome: 'aliasMatch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,21 @@ export const transformRuleToRuleResponse = <Params extends RuleParams = never>(
consumer: rule.consumer,
schedule: rule.schedule,
actions: rule.actions.map(
({ group, id, actionTypeId, params, frequency, uuid, alertsFilter }) => ({
({
group,
id,
actionTypeId,
params,
frequency,
uuid,
alertsFilter,
useAlertDataForTemplate,
}) => ({
group,
id,
params,
connector_type_id: actionTypeId,
use_alert_data_for_template: useAlertDataForTemplate ?? false,
...(frequency
? {
frequency: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/routes/update_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('updateRuleRoute', () => {
"params": Object {
"baz": true,
},
"useAlertDataForTemplate": undefined,
"uuid": "1234-5678",
},
],
Expand Down
Loading

0 comments on commit 603a045

Please sign in to comment.