Skip to content

Commit

Permalink
Restoring snoozeEndTime to AlertAttributesExcludedFromAAD (elastic#13…
Browse files Browse the repository at this point in the history
…5663)

* Restoring snoozeEndTime to AlertAttributesExcludedFromAAD

* Apply suggestions from code review

Co-authored-by: Gidi Meir Morris <github@gidi.io>

* Temp fix migration

* New way to do migrations

* Add two scenarios

* Skip functional test

* Revert some archive changes

Co-authored-by: Ying Mao <ying.mao@elastic.co>
Co-authored-by: Gidi Meir Morris <github@gidi.io>
  • Loading branch information
3 people authored Jul 5, 2022
1 parent c0a629b commit 2d29934
Show file tree
Hide file tree
Showing 7 changed files with 25,745 additions and 2 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { isRuleExportable } from './is_rule_exportable';
import { RuleTypeRegistry } from '../rule_type_registry';
export { partiallyUpdateAlert } from './partially_update_alert';

// Use caution when removing items from this array! Any field which has
// ever existed in the rule SO must be included in this array to prevent
// decryption failures during migration.
export const AlertAttributesExcludedFromAAD = [
'scheduledTaskId',
'muteAll',
Expand All @@ -30,6 +33,7 @@ export const AlertAttributesExcludedFromAAD = [
'updatedAt',
'executionStatus',
'monitoring',
'snoozeEndTime', // field removed in 8.2, but must be retained in case an rule created/updated in 8.2 is being migrated
'snoozeSchedule',
'isSnoozedUntil',
];
Expand All @@ -46,6 +50,7 @@ export type AlertAttributesExcludedFromAADType =
| 'updatedAt'
| 'executionStatus'
| 'monitoring'
| 'snoozeEndTime'
| 'snoozeSchedule'
| 'isSnoozedUntil';

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export class TaskRunner<

const ruleIsSnoozed = isRuleSnoozed(rule);
if (ruleIsSnoozed) {
this.markRuleAsSnoozed(rule.id, rulesClient);
await this.markRuleAsSnoozed(rule.id, rulesClient);
}
if (!ruleIsSnoozed && this.shouldLogAndScheduleActionsForAlerts()) {
const mutedAlertIdsSet = new Set(mutedInstanceIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function alertingTests({ loadTestFile, getService }: FtrProviderC
// Do not place test files here, due to https://github.com/elastic/kibana/issues/123059

// note that this test will destroy existing spaces
loadTestFile(require.resolve('./migrations'));
loadTestFile(require.resolve('./migrations.ts'));
loadTestFile(require.resolve('./migrations/index.ts'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 expect from '@kbn/expect';
import type { RawRule } from '@kbn/alerting-plugin/server/types';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function createGetTests({ getService }: FtrProviderContext) {
const es = getService('es');
const retry = getService('retry');
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');

// Race condition between task manager running tasks and Kibana running the migrations after loading the ES Archive
describe.skip('migrates 8.2.0 rules to the latest version approriately', () => {
let testStart: null | number = null;
before(async () => {
testStart = Date.now();
await esArchiver.load('x-pack/test/functional/es_archives/alerting/8_2_0');
});

after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/alerting/8_2_0');
});

describe('rule with null snoozeEndTime value', async () => {
it('has snoozeEndTime removed', async () => {
const response = await es.get<{ alert: RawRule }>(
{
index: '.kibana',
id: 'alert:bdfce750-fba0-11ec-9157-2f379249da99',
},
{ meta: true }
);

expect(response.statusCode).to.equal(200);
expect(response.body._source?.alert?.snoozeEndTime).to.be(undefined);
expect(response.body._source?.alert?.snoozeSchedule).not.to.be(undefined);

const snoozeSchedule = response.body._source?.alert.snoozeSchedule!;
expect(snoozeSchedule.length).to.eql(0);
});

it('runs successfully after migration', async () => {
await retry.try(async () => {
const { body: rule } = await supertest.get(
`/api/alerting/rule/bdfce750-fba0-11ec-9157-2f379249da99`
);
expect(Date.parse(rule.execution_status.last_execution_date)).to.be.greaterThan(
testStart!
);
expect(rule.execution_status.status).to.be('ok');
});
});
});

describe('rules with snoozeEndTime value', async () => {
it('has snoozeEndTime migrated to snoozeSchedule', async () => {
const response = await es.get<{ alert: RawRule }>(
{
index: '.kibana',
id: 'alert:402084f0-fbb8-11ec-856c-39466bd4c433',
},
{ meta: true }
);

expect(response.statusCode).to.equal(200);
expect(response.body._source?.alert?.snoozeEndTime).to.be(undefined);
expect(response.body._source?.alert?.snoozeSchedule).not.to.be(undefined);

const snoozeSchedule = response.body._source?.alert.snoozeSchedule!;
expect(snoozeSchedule.length).to.eql(1);
});

it('runs successfully after migration', async () => {
await retry.try(async () => {
const { body: rule } = await supertest.get(
`/api/alerting/rule/402084f0-fbb8-11ec-856c-39466bd4c433`
);
expect(Date.parse(rule.execution_status.last_execution_date)).to.be.greaterThan(
testStart!
);
expect(rule.execution_status.status).to.be('ok');
});
});
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 { FtrProviderContext } from '../../../../common/ftr_provider_context';

// eslint-disable-next-line import/no-default-export
export default function migrationTests({ loadTestFile, getService }: FtrProviderContext) {
describe('Migrations', () => {
loadTestFile(require.resolve('./8_2_0'));
});
}
22,034 changes: 22,034 additions & 0 deletions x-pack/test/functional/es_archives/alerting/8_2_0/data.json

Large diffs are not rendered by default.

Loading

0 comments on commit 2d29934

Please sign in to comment.