Skip to content
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

[RAM] Add toggle for AAD fields in alert templating #170162

Merged
merged 39 commits into from Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0bd9cc7
first commit
jcger Jul 4, 2023
18f6b19
Merge branch 'main' of github.com:jcger/kibana into 158183-execution-…
jcger Jul 4, 2023
39e7854
Merge branch 'main' of github.com:elastic/kibana into 158183-executio…
XavierM Sep 14, 2023
8770756
Merge branch 'main' of github.com:elastic/kibana into 158183-executio…
XavierM Sep 14, 2023
e09c812
almost there + full plumbing for useAlertDataForTemplate
XavierM Sep 18, 2023
92a3551
clean up + move aad switch to a generic position
XavierM Sep 20, 2023
045576e
space between switch and form
jcger Sep 20, 2023
ff98eb1
fix jest tests
jcger Sep 20, 2023
c6a018c
update snapshot
jcger Sep 20, 2023
eac5015
add feature flags and autocomplete compont to triggers_actions_ui
jcger Sep 21, 2023
597b06d
fix test
jcger Sep 21, 2023
a3ac65c
fix tests
jcger Sep 21, 2023
4f1a43e
fix tests
jcger Sep 21, 2023
abdd1aa
fix tests
jcger Sep 21, 2023
c61caa6
Merge branch 'main' into 158183-execution-handler-refactor
jcger Sep 21, 2023
2d67457
rollback ff init and catch error
jcger Sep 21, 2023
78e00dd
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Sep 21, 2023
6905044
catch switch ff error
jcger Sep 25, 2023
3d33741
Merge branch '158183-execution-handler-refactor' of github.com:jcger/…
jcger Sep 25, 2023
25cd6e3
fix test
jcger Sep 25, 2023
a8a891e
Merge branch 'main' of github.com:jcger/kibana into 158183-execution-…
jcger Sep 25, 2023
c214fa5
Merge branch 'main' into 158183-execution-handler-refactor
jcger Sep 25, 2023
e6216f3
rollback state
jcger Sep 25, 2023
c277450
Merge branch '158183-execution-handler-refactor' of github.com:jcger/…
jcger Sep 25, 2023
24d3ccb
fix test
jcger Sep 26, 2023
ea05545
Merge branch 'main' of github.com:jcger/kibana into 158183-execution-…
jcger Sep 26, 2023
cc1ccb4
Merge remote-tracking branch 'upstream/main' into jcger/158183-execut…
Zacqary Oct 26, 2023
5feab9d
Clear values when switching to AAD template fields
Zacqary Oct 30, 2023
e92cd13
Fix typecheck
Zacqary Oct 30, 2023
72bb5eb
Fix lint
Zacqary Oct 30, 2023
7d693ec
Remove dependency on useQuery
Zacqary Oct 30, 2023
905b8d4
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Oct 30, 2023
49b3526
Make ruleTypeId optional
Zacqary Oct 30, 2023
0b20d79
Merge branch '158183-execution-handler-refactor' of https://github.co…
Zacqary Oct 30, 2023
2d8ac7a
Revert queryclient provider in jest
Zacqary Oct 30, 2023
2be9c9b
Add test for mustache switch
Zacqary Oct 31, 2023
8d05435
Merge remote-tracking branch 'upstream/main' into 158183-execution-ha…
Zacqary Oct 31, 2023
4382789
Merge remote-tracking branch 'upstream/main' into 158183-execution-ha…
Zacqary Nov 14, 2023
2e424f7
Fix resolve rule jest
Zacqary Nov 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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
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()),
});
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
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,
}
: {}),
})
);
};
Expand Up @@ -114,6 +114,7 @@ describe('bulkEditRulesRoute', () => {
foo: true,
},
uuid: '123-456',
use_alert_data_for_template: false,
},
],
}),
Expand Down
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
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
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
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
Expand Up @@ -132,6 +132,7 @@ describe('updateRuleRoute', () => {
"params": Object {
"baz": true,
},
"useAlertDataForTemplate": undefined,
"uuid": "1234-5678",
},
],
Expand Down