[Metrics UI] Add migration to fix incorrect action group spelling (#119626)

* Add a migration function for incorrectly spelt action group
This commit is contained in:
Kerry Gallagher 2021-12-06 11:22:16 +00:00 committed by GitHub
parent a296f6b434
commit d715c43517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 160 additions and 1 deletions

View file

@ -2055,6 +2055,62 @@ describe('successful migrations', () => {
undefined
);
});
describe('Metrics Inventory Threshold rule', () => {
test('Migrates incorrect action group spelling', () => {
const migration800 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['8.0.0'];
const actions = [
{
group: 'metrics.invenotry_threshold.fired',
params: {
level: 'info',
message:
'""{{alertName}} - {{context.group}} is in a state of {{context.alertState}} Reason: {{context.reason}}""',
},
actionRef: 'action_0',
actionTypeId: '.server-log',
},
];
const alert = getMockData({ alertTypeId: 'metrics.alert.inventory.threshold', actions });
expect(migration800(alert, migrationContext)).toMatchObject({
...alert,
attributes: {
...alert.attributes,
actions: [{ ...actions[0], group: 'metrics.inventory_threshold.fired' }],
},
});
});
test('Works with the correct action group spelling', () => {
const migration800 = getMigrations(encryptedSavedObjectsSetup, isPreconfigured)['8.0.0'];
const actions = [
{
group: 'metrics.inventory_threshold.fired',
params: {
level: 'info',
message:
'""{{alertName}} - {{context.group}} is in a state of {{context.alertState}} Reason: {{context.reason}}""',
},
actionRef: 'action_0',
actionTypeId: '.server-log',
},
];
const alert = getMockData({ alertTypeId: 'metrics.alert.inventory.threshold', actions });
expect(migration800(alert, migrationContext)).toMatchObject({
...alert,
attributes: {
...alert.attributes,
actions: [{ ...actions[0], group: 'metrics.inventory_threshold.fired' }],
},
});
});
});
});
});

View file

@ -129,7 +129,11 @@ export function getMigrations(
const migrationRules800 = createEsoMigration(
encryptedSavedObjects,
(doc: SavedObjectUnsanitizedDoc<RawAlert>): doc is SavedObjectUnsanitizedDoc<RawAlert> => true,
pipeMigrations(addThreatIndicatorPathToThreatMatchRules, addRACRuleTypes)
pipeMigrations(
addThreatIndicatorPathToThreatMatchRules,
addRACRuleTypes,
fixInventoryThresholdGroupId
)
);
return {
@ -751,6 +755,42 @@ function removePreconfiguredConnectorsFromReferences(
return doc;
}
// This fixes an issue whereby metrics.alert.inventory.threshold rules had the
// group for actions incorrectly spelt as metrics.invenotry_threshold.fired vs metrics.inventory_threshold.fired
function fixInventoryThresholdGroupId(
doc: SavedObjectUnsanitizedDoc<RawAlert>
): SavedObjectUnsanitizedDoc<RawAlert> {
if (doc.attributes.alertTypeId === 'metrics.alert.inventory.threshold') {
const {
attributes: { actions },
} = doc;
const updatedActions = actions
? actions.map((action) => {
// Wrong spelling
if (action.group === 'metrics.invenotry_threshold.fired') {
return {
...action,
group: 'metrics.inventory_threshold.fired',
};
} else {
return action;
}
})
: [];
return {
...doc,
attributes: {
...doc.attributes,
actions: updatedActions,
},
};
} else {
return doc;
}
}
function getCorrespondingAction(
actions: SavedObjectAttribute,
connectorRef: string

View file

@ -346,5 +346,19 @@ export default function createGetTests({ getService }: FtrProviderContext) {
FILEBEAT_7X_INDICATOR_PATH
);
});
it('8.0 migrates incorrect action group spellings on the Metrics Inventory Threshold rule type', async () => {
const response = await es.get<{ alert: RawAlert }>(
{
index: '.kibana',
id: 'alert:92237b30-4e03-11ec-9ab9-d980518a2d28',
},
{ meta: true }
);
expect(response.statusCode).to.eql(200);
expect(response.body._source?.alert?.actions?.[0].group).to.be(
'metrics.inventory_threshold.fired'
);
});
});
}

View file

@ -613,3 +613,52 @@
}
}
}
{
"type": "doc",
"value": {
"id": "alert:92237b30-4e03-11ec-9ab9-d980518a2d28",
"index": ".kibana_1",
"source": {
"alert" : {
"alertTypeId" : "metrics.alert.inventory.threshold",
"consumer" : "infrastructure",
"params" : {
},
"schedule" : {
"interval" : "1m"
},
"enabled" : true,
"actions" : [
{
"actionTypeId" : ".server-log",
"params" : {
"level" : "info",
"message" : "Alert message"
},
"actionRef" : "action_0",
"group" : "metrics.invenotry_threshold.fired"
}
],
"throttle" : null,
"apiKeyOwner" : null,
"apiKey" : null,
"createdBy" : "elastic",
"updatedBy" : "elastic",
"createdAt" : "2021-07-27T20:42:55.896Z",
"muteAll" : false,
"mutedInstanceIds" : [ ],
"scheduledTaskId" : null,
"tags": []
},
"type" : "alert",
"migrationVersion" : {
"alert" : "7.8.0"
},
"updated_at" : "2021-08-13T23:00:11.985Z",
"references": [
]
}
}
}