Skip to content

Commit

Permalink
[8.11] [RAM][Bug] Fix bug preventing filters from being added to cond…
Browse files Browse the repository at this point in the history
…itional actions (#171048) (#171362)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[RAM][Bug] Fix bug preventing filters from being added to conditional
actions (#171048)](#171048)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Jiawei
Wu","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-11-15T19:38:50Z","message":"[RAM][Bug]
Fix bug preventing filters from being added to conditional actions
(#171048)\n\n## Summary\r\nResolves:
https://github.com/elastic/kibana/issues/171036\r\n\r\nFixes a bug where
the schema name for the conditional action filter\r\n`$state` was
incorrectly name. This prevented the user from adding\r\nfilters to
conditional actions as the schema validation would reject
the\r\nrequest.\r\n\r\n### To verify the fix:\r\n\r\n1. Create a
rule\r\n2. Add a conditional action\r\n3. Assert that filters can be
added to the rule\r\n4. Rule is saved\r\n5. Repeat 1 - 4 but this time,
update the rule. \r\n\r\n### Checklist\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"fbb232815b85961ac09b6abbe369eb7b3760752d","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:ResponseOps","Feature:Alerting/RulesManagement","v8.12.0","v8.11.1","v8.10.5"],"number":171048,"url":"https://github.com/elastic/kibana/pull/171048","mergeCommit":{"message":"[RAM][Bug]
Fix bug preventing filters from being added to conditional actions
(#171048)\n\n## Summary\r\nResolves:
https://github.com/elastic/kibana/issues/171036\r\n\r\nFixes a bug where
the schema name for the conditional action filter\r\n`$state` was
incorrectly name. This prevented the user from adding\r\nfilters to
conditional actions as the schema validation would reject
the\r\nrequest.\r\n\r\n### To verify the fix:\r\n\r\n1. Create a
rule\r\n2. Add a conditional action\r\n3. Assert that filters can be
added to the rule\r\n4. Rule is saved\r\n5. Repeat 1 - 4 but this time,
update the rule. \r\n\r\n### Checklist\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"fbb232815b85961ac09b6abbe369eb7b3760752d"}},"sourceBranch":"main","suggestedTargetBranches":["8.11","8.10"],"targetPullRequestStates":[{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/171048","number":171048,"mergeCommit":{"message":"[RAM][Bug]
Fix bug preventing filters from being added to conditional actions
(#171048)\n\n## Summary\r\nResolves:
https://github.com/elastic/kibana/issues/171036\r\n\r\nFixes a bug where
the schema name for the conditional action filter\r\n`$state` was
incorrectly name. This prevented the user from adding\r\nfilters to
conditional actions as the schema validation would reject
the\r\nrequest.\r\n\r\n### To verify the fix:\r\n\r\n1. Create a
rule\r\n2. Add a conditional action\r\n3. Assert that filters can be
added to the rule\r\n4. Rule is saved\r\n5. Repeat 1 - 4 but this time,
update the rule. \r\n\r\n### Checklist\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"fbb232815b85961ac09b6abbe369eb7b3760752d"}},{"branch":"8.11","label":"v8.11.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.10","label":"v8.10.5","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
  • Loading branch information
JiaweiWu authored Nov 16, 2023
1 parent 94d47b3 commit 46e0cda
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { schema } from '@kbn/config-schema';
import { ruleNotifyWhenV1 } from '../../../response';
import { filterStateStore as filterStateStoreV1 } from '../../../response/constants/v1';
import {
validateNotifyWhenV1,
validateDurationV1,
Expand Down Expand Up @@ -37,7 +38,14 @@ export const actionAlertsFilterSchema = schema.object({
schema.object({
query: schema.maybe(schema.recordOf(schema.string(), schema.any())),
meta: schema.recordOf(schema.string(), schema.any()),
state$: schema.maybe(schema.object({ store: schema.string() })),
$state: schema.maybe(
schema.object({
store: schema.oneOf([
schema.literal(filterStateStoreV1.APP_STATE),
schema.literal(filterStateStoreV1.GLOBAL_STATE),
]),
})
),
})
),
dsl: schema.maybe(schema.string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const ruleExecutionStatusWarningReason = {
MAX_QUEUED_ACTIONS: 'maxQueuedActions',
} as const;

export const filterStateStore = {
APP_STATE: 'appState',
GLOBAL_STATE: 'globalState',
} as const;

export type RuleNotifyWhen = typeof ruleNotifyWhen[keyof typeof ruleNotifyWhen];
export type RuleLastRunOutcomeValues =
typeof ruleLastRunOutcomeValues[keyof typeof ruleLastRunOutcomeValues];
Expand All @@ -52,3 +57,4 @@ export type RuleExecutionStatusErrorReason =
typeof ruleExecutionStatusErrorReason[keyof typeof ruleExecutionStatusErrorReason];
export type RuleExecutionStatusWarningReason =
typeof ruleExecutionStatusWarningReason[keyof typeof ruleExecutionStatusWarningReason];
export type FilterStateStore = typeof filterStateStore[keyof typeof filterStateStore];
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ruleExecutionStatusErrorReason as ruleExecutionStatusErrorReasonV1,
ruleExecutionStatusWarningReason as ruleExecutionStatusWarningReasonV1,
ruleLastRunOutcomeValues as ruleLastRunOutcomeValuesV1,
filterStateStore as filterStateStoreV1,
} from '../constants/v1';

export const ruleParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()));
Expand Down Expand Up @@ -43,7 +44,14 @@ const actionAlertsFilterSchema = schema.object({
schema.object({
query: schema.maybe(schema.recordOf(schema.string(), schema.any())),
meta: schema.recordOf(schema.string(), schema.any()),
state$: schema.maybe(schema.object({ store: schema.string() })),
$state: schema.maybe(
schema.object({
store: schema.oneOf([
schema.literal(filterStateStoreV1.APP_STATE),
schema.literal(filterStateStoreV1.GLOBAL_STATE),
]),
})
),
})
),
})
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/application/rule/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ export const ruleExecutionStatusWarningReason = {
MAX_ALERTS: 'maxAlerts',
MAX_QUEUED_ACTIONS: 'maxQueuedActions',
} as const;

export const filterStateStore = {
APP_STATE: 'appState',
GLOBAL_STATE: 'globalState',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from '../../../../rules_client/lib';
import { generateAPIKeyName, apiKeyAsRuleDomainProperties } from '../../../../rules_client/common';
import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events';
import { RulesClientContext } from '../../../../rules_client/types';
import { RulesClientContext, NormalizedAlertAction } from '../../../../rules_client/types';
import { RuleDomain, RuleParams } from '../../types';
import { SanitizedRule } from '../../../../types';
import {
Expand Down Expand Up @@ -55,7 +55,11 @@ export async function createRule<Params extends RuleParams = never>(
): Promise<SanitizedRule<Params>> {
const { data: initialData, options, allowMissingConnectorSecrets } = createParams;

const data = { ...initialData, actions: addGeneratedActionValues(initialData.actions) };
// TODO (http-versioning): Remove this cast when we fix addGeneratedActionValues
const data = {
...initialData,
actions: addGeneratedActionValues(initialData.actions as NormalizedAlertAction[]),
};

const id = options?.id || SavedObjectsUtils.generateId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@

import { schema } from '@kbn/config-schema';
import { notifyWhenSchema } from './notify_when_schema';
import { filterStateStore } from '../constants';

export const actionParamsSchema = schema.recordOf(schema.string(), schema.maybe(schema.any()));

const actionAlertsFilterQueryFiltersSchema = schema.arrayOf(
schema.object({
query: schema.maybe(schema.recordOf(schema.string(), schema.any())),
meta: schema.recordOf(schema.string(), schema.any()),
state$: schema.maybe(schema.object({ store: schema.string() })),
$state: schema.maybe(
schema.object({
store: schema.oneOf([
schema.literal(filterStateStore.APP_STATE),
schema.literal(filterStateStore.GLOBAL_STATE),
]),
})
),
})
);

Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/application/rule/types/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ruleExecutionStatusValues,
ruleExecutionStatusErrorReason,
ruleExecutionStatusWarningReason,
filterStateStore,
} from '../constants';
import {
ruleParamsSchema,
Expand All @@ -33,6 +34,7 @@ export type RuleExecutionStatusErrorReason =
typeof ruleExecutionStatusErrorReason[keyof typeof ruleExecutionStatusErrorReason];
export type RuleExecutionStatusWarningReason =
typeof ruleExecutionStatusWarningReason[keyof typeof ruleExecutionStatusWarningReason];
export type FilterStateStore = typeof filterStateStore[keyof typeof filterStateStore];

export type RuleParams = TypeOf<typeof ruleParamsSchema>;
export type RuleSnoozeSchedule = TypeOf<typeof snoozeScheduleSchema>;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/raw_rule_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const rawRuleAlertsFilterSchema = schema.object({
params: schema.maybe(schema.recordOf(schema.string(), schema.any())), // better type?
value: schema.maybe(schema.string()),
}),
state$: schema.maybe(
$state: schema.maybe(
schema.object({
store: schema.oneOf([schema.literal('appState'), schema.literal('globalState')]),
})
Expand Down
10 changes: 9 additions & 1 deletion x-pack/plugins/alerting/server/routes/lib/actions_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { FilterStateStore } from '@kbn/es-query';
import { validateTimezone } from './validate_timezone';
import { validateDurationSchema } from '../../lib';
import { validateHours } from './validate_hours';
Expand Down Expand Up @@ -36,7 +37,14 @@ export const actionsSchema = schema.arrayOf(
schema.object({
query: schema.maybe(schema.recordOf(schema.string(), schema.any())),
meta: schema.recordOf(schema.string(), schema.any()),
state$: schema.maybe(schema.object({ store: schema.string() })),
$state: schema.maybe(
schema.object({
store: schema.oneOf([
schema.literal(FilterStateStore.APP_STATE),
schema.literal(FilterStateStore.GLOBAL_STATE),
]),
})
),
})
),
dsl: schema.maybe(schema.string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => {
await testSubjects.click('notifyWhenSelect');
await testSubjects.click('onThrottleInterval');
await testSubjects.setValue('throttleInput', '10');

// Alerts search bar (conditional actions)
await testSubjects.click('alertsFilterQueryToggle');

await pageObjects.header.waitUntilLoadingHasFinished();
await testSubjects.click('addFilter');
await testSubjects.click('filterFieldSuggestionList');
await comboBox.set('filterFieldSuggestionList', '_id');
await comboBox.set('filterOperatorList', 'is not');
await testSubjects.setValue('filterParams', 'fake-rule-id');
await testSubjects.click('saveFilter');
await testSubjects.setValue('queryInput', '_id: *');

const messageTextArea = await find.byCssSelector('[data-test-subj="messageTextArea"]');
expect(await messageTextArea.getAttribute('value')).to.eql(
`Rule '{{rule.name}}' is active for group '{{context.group}}':
Expand Down

0 comments on commit 46e0cda

Please sign in to comment.