Restoring snoozeEndTime to AlertAttributesExcludedFromAAD (#135663)

* 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>
This commit is contained in:
Mike Côté 2022-07-05 09:06:19 -04:00 committed by GitHub
parent c0a629bb1b
commit 2d299348cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 25745 additions and 2 deletions

View file

@ -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',
@ -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',
];
@ -46,6 +50,7 @@ export type AlertAttributesExcludedFromAADType =
| 'updatedAt'
| 'executionStatus'
| 'monitoring'
| 'snoozeEndTime'
| 'snoozeSchedule'
| 'isSnoozedUntil';

View file

@ -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);

View file

@ -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'));
});
}

View file

@ -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');
});
});
});
});
}

View file

@ -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'));
});
}

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load diff