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] Adds auto-incrementing revision field to rules #147398

Merged
merged 80 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
542bf21
Initial plumbing of revision through alerting and security
spong Dec 12, 2022
57c17dc
Adding revision to FTRs
spong Dec 13, 2022
04033c8
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 13, 2022
6d3aa3d
Fixing typechecks and more FTRs
spong Dec 13, 2022
9b28134
More typecheck fixes throughout triggers and actions ui
spong Dec 13, 2022
ef64bb1
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 13, 2022
96f386b
More typecheck fixes throughout security
spong Dec 13, 2022
31b143a
Additional typecheck and FTR fixes
spong Dec 13, 2022
bdd5285
Jest fixes and detections FTR fixes
spong Dec 14, 2022
f70b505
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 14, 2022
9371731
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 14, 2022
396ccc1
Fixes additional detections FTRs
spong Dec 14, 2022
82feace
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 15, 2022
3087e63
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Dec 16, 2022
d71e752
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 4, 2023
3120bae
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 23, 2023
b33d3e4
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 25, 2023
6d63ae4
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 25, 2023
7f5d57f
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 26, 2023
1f5ce9f
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 27, 2023
98b57a3
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Jan 30, 2023
daf5099
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 7, 2023
98098b5
Reuse incrementVersion in update()
spong Feb 7, 2023
e6fef49
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 7, 2023
40fa9b0
Adds migration and bulk edit support
spong Feb 7, 2023
e122adb
Adds incrementRevision logic and fixing tests
spong Feb 8, 2023
673d1c5
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 8, 2023
e4279f4
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 8, 2023
4a80747
Resolving remaining test failures
spong Feb 8, 2023
bd87b0d
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 8, 2023
f0f5c32
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 9, 2023
60abd10
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 9, 2023
827ecb3
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 10, 2023
e8f40c6
Update alert SO hash
spong Feb 10, 2023
937b2ed
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 10, 2023
e1ff58a
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 13, 2023
61dc2c0
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 13, 2023
8270e78
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 13, 2023
f7dbf41
Merge branch 'main' into adds-revision-field-to-rules
spong Feb 14, 2023
d162cd8
Remove specific version check from increment logic
spong Feb 14, 2023
ac244db
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 14, 2023
a71a309
Adds migration tests, updates bulk edit snooze tests, update_api_key …
spong Feb 15, 2023
4f971d6
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 15, 2023
2a567c1
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 15, 2023
a1a21e6
Fixes updateAPIKey tests and reset SO registered type hash
spong Feb 15, 2023
88a18f8
Removes revision update on snooze/mute/apiKeyUpdate and updates bulkE…
spong Feb 15, 2023
cc1926b
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 15, 2023
9b7e948
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 16, 2023
440d424
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 16, 2023
cbd1bed
Comments from review and expanding tests
spong Feb 22, 2023
4a39c4d
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 22, 2023
65ce48b
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 22, 2023
2b3ea72
Updates SO hash since last update from merge from main
spong Feb 22, 2023
1784b14
Updating new uuid tests to include revision
spong Feb 22, 2023
c8697d2
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 22, 2023
1a7da2f
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 22, 2023
5619b02
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 23, 2023
ef66c8d
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 23, 2023
62ca943
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 25, 2023
f949386
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 25, 2023
f3b5c4e
Adds shouldIncrementRevision bypass, fixes imports, and clarifies map…
spong Feb 27, 2023
152d53c
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 27, 2023
53a5941
Updating alert SO hash and fixing basic import test
spong Feb 27, 2023
9bd4ffd
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 27, 2023
e6f5a7b
Removing remaining todo's
spong Feb 28, 2023
73ccb27
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Feb 28, 2023
320c706
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 1, 2023
24eae01
Addressing PR comments
spong Mar 1, 2023
d894736
Fixes multi increment when adding bulk actions
spong Mar 2, 2023
640526e
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 2, 2023
78834e3
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 7, 2023
8ce5da6
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 8, 2023
41d32b3
Updating SO hash
spong Mar 8, 2023
adf18cc
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 8, 2023
eef57f6
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 9, 2023
2028524
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 9, 2023
7896ea9
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 10, 2023
646c2c2
Merge branch 'main' of github.com:elastic/kibana into adds-revision-f…
spong Mar 10, 2023
a575eb8
Skips synthetics MonitorSummaryTab flakey test
spong Mar 10, 2023
d2366cf
Updates new export tests to exclude revision
spong Mar 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
Object {
"action": "6cfc277ed3211639e37546ac625f4a68f2494215",
"action_task_params": "5f419caba96dd8c77d0f94013e71d43890e3d5d6",
"alert": "785240e3137f5eb1a0f8986e5b8eff99780fc04f",
"alert": "1e4cd6941f1eb39c729c646e91fbfb9700de84b9",
"api_key_pending_invalidation": "16e7bcf8e78764102d7f525542d5b616809a21ee",
"apm-indices": "d19dd7fb51f2d2cbc1f8769481721e0953f9a6d2",
"apm-server-schema": "1d42f17eff9ec6c16d3a9324d9539e2d123d0a9a",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const mockRule: RuleTableItem = {
ruleType: 'Test Rule Type',
isEditable: true,
enabledInLicense: true,
revision: 0,
};

export const RulesListNotifyBadgeSandbox = ({ triggersActionsUi }: SandboxProps) => {
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 @@ -161,6 +161,7 @@ export interface Rule<Params extends RuleTypeParams = never> {
isSnoozedUntil?: Date | null;
lastRun?: RuleLastRun | null;
nextRun?: Date | null;
revision: number;
running?: boolean | null;
viewInAppRelativeUrl?: string;
}
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerting/public/alert_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ describe('loadRule', () => {
"params": Object {
"x": 42,
},
"revision": 0,
"schedule": Object {
"interval": "1s",
},
Expand Down Expand Up @@ -268,6 +269,7 @@ function getApiRule() {
updated_by: '2889684073',
mute_all: false,
muted_alert_ids: [],
revision: 0,
schedule: {
interval: '1s',
},
Expand Down Expand Up @@ -333,5 +335,6 @@ function getRule(): Rule<{ x: number }> {
lastExecutionDate: RuleExecuteDate,
lastDuration: 1194,
},
revision: 0,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('common_transformations', () => {
notify_when: 'onActiveAlert',
mute_all: false,
muted_alert_ids: ['bob', 'jim'],
revision: 0,
execution_status: {
last_execution_date: dateExecuted.toISOString(),
last_duration: 42,
Expand Down Expand Up @@ -185,6 +186,7 @@ describe('common_transformations', () => {
],
},
},
"revision": 0,
"schedule": Object {
"interval": "1s",
},
Expand Down Expand Up @@ -229,6 +231,7 @@ describe('common_transformations', () => {
notify_when: 'onActiveAlert',
mute_all: false,
muted_alert_ids: ['bob', 'jim'],
revision: 0,
execution_status: {
last_execution_date: dateExecuted.toISOString(),
status: 'error',
Expand Down Expand Up @@ -344,6 +347,7 @@ describe('common_transformations', () => {
"nextRun": 2021-12-15T12:34:55.789Z,
"notifyWhen": "onActiveAlert",
"params": Object {},
"revision": 0,
"schedule": Object {
"interval": "1s",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,5 @@ const BaseRule: SanitizedRule<{ bar: boolean }> = {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('bulkEditInternalRulesRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

const mockedAlerts: Array<SanitizedRule<{}>> = [mockedAlert];
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/clone_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('cloneRuleRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

const ruleToClone: AsApiContract<CreateOptions<{ bar: boolean }>['data']> = {
Expand All @@ -90,6 +91,7 @@ describe('cloneRuleRoute', () => {
created_at: mockedRule.createdAt,
updated_at: mockedRule.updatedAt,
id: mockedRule.id,
revision: 0,
execution_status: {
status: mockedRule.executionStatus.status,
last_execution_date: mockedRule.executionStatus.lastExecutionDate,
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/create_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('createRuleRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

const ruleToCreate: AsApiContract<CreateOptions<{ bar: boolean }>['data']> = {
Expand All @@ -93,6 +94,7 @@ describe('createRuleRoute', () => {
created_at: mockedAlert.createdAt,
updated_at: mockedAlert.updatedAt,
id: mockedAlert.id,
revision: mockedAlert.revision,
execution_status: {
status: mockedAlert.executionStatus.status,
last_execution_date: mockedAlert.executionStatus.lastExecutionDate,
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/get_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('getRuleRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

const getResult: AsApiContract<SanitizedRule<{ bar: boolean }>> = {
Expand All @@ -76,6 +77,7 @@ describe('getRuleRoute', () => {
created_at: mockedAlert.createdAt,
updated_at: mockedAlert.updatedAt,
id: mockedAlert.id,
revision: mockedAlert.revision,
execution_status: {
status: mockedAlert.executionStatus.status,
last_execution_date: mockedAlert.executionStatus.lastExecutionDate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('createAlertRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

it('creates an alert with proper parameters', async () => {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/routes/legacy/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('getAlertRoute', () => {
status: 'unknown',
lastExecutionDate: new Date('2020-08-20T19:23:38Z'),
},
revision: 0,
};

it('gets an alert with proper parameters', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const sampleRule: SanitizedRule<RuleTypeParams> & { activeSnoozes?: string[] } =
},
},
nextRun: DATE_2020,
revision: 0,
};

describe('rewriteRule', () => {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/resolve_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('resolveRuleRoute', () => {
},
outcome: 'aliasMatch',
alias_target_id: '2',
revision: 0,
};

const resolveResult: AsApiContract<ResolvedSanitizedRule<{ bar: boolean }>> = {
Expand All @@ -88,6 +89,7 @@ describe('resolveRuleRoute', () => {
created_at: mockedRule.createdAt,
updated_at: mockedRule.updatedAt,
id: mockedRule.id,
revision: mockedRule.revision,
execution_status: {
status: mockedRule.executionStatus.status,
last_execution_date: mockedRule.executionStatus.lastExecutionDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { UpdateOptions } from '..';
import { mockedDateString } from '../tests/lib';
import { incrementRevision } from './increment_revision';
import { SavedObject } from '@kbn/core/server';
import { RawRule, RuleTypeParams } from '../../types';

describe('incrementRevision', () => {
const currentRule: SavedObject<RawRule> = {
id: '1',
type: 'alert',
attributes: {
enabled: true,
name: 'rule-name',
tags: ['tag-1', 'tag-2'],
alertTypeId: '123',
consumer: 'rule-consumer',
legacyId: null,
schedule: { interval: '1s' },
actions: [],
params: {},
createdBy: null,
updatedBy: null,
createdAt: mockedDateString,
updatedAt: mockedDateString,
apiKey: null,
apiKeyOwner: null,
throttle: null,
notifyWhen: null,
muteAll: false,
mutedInstanceIds: [],
executionStatus: {
status: 'unknown',
lastExecutionDate: '2020-08-20T19:23:38Z',
error: null,
warning: null,
},
revision: 0,
},
references: [],
};

const updateOptions: UpdateOptions<RuleTypeParams> = {
id: '1',
data: {
schedule: {
interval: '1m',
},
name: 'abc',
tags: ['foo'],
params: {
bar: true,
risk_score: 40,
severity: 'low',
},
throttle: null,
notifyWhen: 'onActiveAlert',
actions: [],
},
};
const updatedParams: RuleTypeParams = { bar: true, risk_score: 40, severity: 'low' };

it('should return the current revision if no attrs or params are updated', () => {
// @ts-expect-error
expect(incrementRevision(currentRule, { data: {} }, {})).toBe(0);
});

it('should increment the revision if a root level attr is updated', () => {
expect(incrementRevision(currentRule, updateOptions, {})).toBe(1);
});

it('should increment the revision if a rule param is updated', () => {
// @ts-expect-error
expect(incrementRevision(currentRule, { data: {} }, updatedParams)).toBe(1);
});

it('should not increment the revision if an excluded attr is updated', () => {
// @ts-expect-error
expect(incrementRevision(currentRule, { data: { activeSnoozes: 'excludedValue' } }, {})).toBe(
0
);
});

it('should not increment the revision if an excluded param is updated', () => {
expect(
incrementRevision(
currentRule,
// @ts-expect-error
{ data: {} },
{ isSnoozedUntil: '1970-01-02T00:00:00.000Z' }
)
).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { SavedObject } from '@kbn/core/server';
import { get, isEqual } from 'lodash';
import { RawRule, RuleTypeParams } from '../../types';
import { fieldsToExcludeFromRevisionUpdates, UpdateOptions } from '..';

export function incrementRevision<Params extends RuleTypeParams>(
spong marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybeIncrementRevision or getNextRevision would be more precise since it doesn't always increment it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it a generic function? Can we use just the RuleTypeParams type?

currentRule: SavedObject<RawRule>,
{ data }: UpdateOptions<Params>,
updatedParams: RuleTypeParams
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the first glance, these parameters are obscure and it would be helpful to add JSDoc comments clarifying what they exactly mean.

For instance, updatedParams: RuleTypeParams -- is this the same as data.params?

): number {
// Diff root level attrs
for (const [field, value] of Object.entries(data).filter(([key]) => key !== 'params')) {
if (
!fieldsToExcludeFromRevisionUpdates.has(field) &&
!isEqual(value, get(currentRule.attributes, field))
) {
return currentRule.attributes.revision + 1;
}
}

// Diff rule params
for (const [field, value] of Object.entries(updatedParams)) {
if (
!fieldsToExcludeFromRevisionUpdates.has(field) &&
!isEqual(value, get(currentRule.attributes.params, field))
) {
return currentRule.attributes.revision + 1;
}
}
return currentRule.attributes.revision;
}
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/server/rules_client/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { scheduleTask } from './schedule_task';
export { createNewAPIKeySet } from './create_new_api_key_set';
export { recoverRuleAlerts } from './recover_rule_alerts';
export { addUuid } from './add_uuid';
export { incrementRevision } from './increment_revision';
Loading