Skip to content

Commit

Permalink
[8.9] [Security Solution][Fix] Empty Alert Table when upgrading from …
Browse files Browse the repository at this point in the history
…8.8.x -> 8.9 (#162063) (#162120)

# Backport

This will backport the following commits from `main` to `8.9`:
- [[Security Solution][Fix] Empty Alert Table when upgrading from 8.8.x
-> 8.9 (#162063)](#162063)

<!--- Backport version: 8.9.7 -->

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

<!--BACKPORT [{"author":{"name":"Jatin
Kathuria","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-07-18T08:57:12Z","message":"[Security
Solution][Fix] Empty Alert Table when upgrading from 8.8.x -> 8.9
(#162063)\n\n## Summary\r\n\r\nWhen users upgrade from `8.8.x` -> `8.9`
version, users observe empty\r\ntable as shown
below.\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/7485038/20549edb-07b9-4124-a0ac-7515cf0e2796)\r\n\r\n\r\nBelow
are steps to reproduce this issue and test it:\r\n\r\n1. Boot
[email protected]\r\n2. Clear Local storage.\r\n3. Go to Security ->
Alerts\r\n4. Add Columns `_id` or any other column \r\n5. Upgrade to
`8.9`\r\n6. The table will empty as shown in above
screenshot.\r\n\r\n\r\n## Fix\r\n\r\nThis fix saperates out the migraton
from 8.7 -> 8.8 and add a new\r\nmigration for upgrading from 8.8 ->
8.9\r\n\r\n`migrateAlertTableStateToTriggerActionsState` migrates table
from `v8.7\r\n->
v8.8`,\r\n\r\n`migrateTriggerActionsVisibleColumnsAlertTable88xTo89`
migrates from\r\n`v8.8.x` -> `v8.9`\r\n\r\nCombining both of them may
lead to issues when users are migrating from\r\n`v8.7` -> `v8.9` or
`v8.8` ->
`v8.9`","sha":"0516caed1de3b72efa3f5c1282975c34962f7999","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","Team:Threat
Hunting:Investigations","v8.9.0","v8.10.0"],"number":162063,"url":"https://github.com/elastic/kibana/pull/162063","mergeCommit":{"message":"[Security
Solution][Fix] Empty Alert Table when upgrading from 8.8.x -> 8.9
(#162063)\n\n## Summary\r\n\r\nWhen users upgrade from `8.8.x` -> `8.9`
version, users observe empty\r\ntable as shown
below.\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/7485038/20549edb-07b9-4124-a0ac-7515cf0e2796)\r\n\r\n\r\nBelow
are steps to reproduce this issue and test it:\r\n\r\n1. Boot
[email protected]\r\n2. Clear Local storage.\r\n3. Go to Security ->
Alerts\r\n4. Add Columns `_id` or any other column \r\n5. Upgrade to
`8.9`\r\n6. The table will empty as shown in above
screenshot.\r\n\r\n\r\n## Fix\r\n\r\nThis fix saperates out the migraton
from 8.7 -> 8.8 and add a new\r\nmigration for upgrading from 8.8 ->
8.9\r\n\r\n`migrateAlertTableStateToTriggerActionsState` migrates table
from `v8.7\r\n->
v8.8`,\r\n\r\n`migrateTriggerActionsVisibleColumnsAlertTable88xTo89`
migrates from\r\n`v8.8.x` -> `v8.9`\r\n\r\nCombining both of them may
lead to issues when users are migrating from\r\n`v8.7` -> `v8.9` or
`v8.8` ->
`v8.9`","sha":"0516caed1de3b72efa3f5c1282975c34962f7999"}},"sourceBranch":"main","suggestedTargetBranches":["8.9"],"targetPullRequestStates":[{"branch":"8.9","label":"v8.9.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/162063","number":162063,"mergeCommit":{"message":"[Security
Solution][Fix] Empty Alert Table when upgrading from 8.8.x -> 8.9
(#162063)\n\n## Summary\r\n\r\nWhen users upgrade from `8.8.x` -> `8.9`
version, users observe empty\r\ntable as shown
below.\r\n\r\n\r\n![image](https://github.com/elastic/kibana/assets/7485038/20549edb-07b9-4124-a0ac-7515cf0e2796)\r\n\r\n\r\nBelow
are steps to reproduce this issue and test it:\r\n\r\n1. Boot
[email protected]\r\n2. Clear Local storage.\r\n3. Go to Security ->
Alerts\r\n4. Add Columns `_id` or any other column \r\n5. Upgrade to
`8.9`\r\n6. The table will empty as shown in above
screenshot.\r\n\r\n\r\n## Fix\r\n\r\nThis fix saperates out the migraton
from 8.7 -> 8.8 and add a new\r\nmigration for upgrading from 8.8 ->
8.9\r\n\r\n`migrateAlertTableStateToTriggerActionsState` migrates table
from `v8.7\r\n->
v8.8`,\r\n\r\n`migrateTriggerActionsVisibleColumnsAlertTable88xTo89`
migrates from\r\n`v8.8.x` -> `v8.9`\r\n\r\nCombining both of them may
lead to issues when users are migrating from\r\n`v8.7` -> `v8.9` or
`v8.8` -> `v8.9`","sha":"0516caed1de3b72efa3f5c1282975c34962f7999"}}]}]
BACKPORT-->

Co-authored-by: Jatin Kathuria <[email protected]>
  • Loading branch information
kibanamachine and logeekal authored Jul 18, 2023
1 parent 0e4b23b commit 63549cb
Show file tree
Hide file tree
Showing 3 changed files with 798 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import {
getAllDataTablesInStorage,
addTableInStorage,
migrateAlertTableStateToTriggerActionsState,
migrateTriggerActionsVisibleColumnsAlertTable88xTo89,
} from '.';

import { mockDataTableModel, createSecuritySolutionStorageMock } from '../../../common/mock';
import { useKibana } from '../../../common/lib/kibana';
import { VIEW_SELECTION } from '../../../../common/constants';
import type { DataTableModel, DataTableState } from '@kbn/securitysolution-data-table';
import { TableId } from '@kbn/securitysolution-data-table';
import { v88xAlertOrignalData, v89xAlertsOriginalData } from './test.data';

jest.mock('../../../common/lib/kibana');

Expand Down Expand Up @@ -788,7 +790,7 @@ describe('SiemLocalStorage', () => {
});
});

describe('Trigger Actions Alert Table Migration', () => {
describe('Trigger Actions Alert Table Migration -> Migration from 8.7', () => {
const legacyDataTableState: DataTableState['dataTable']['tableById'] = {
'alerts-page': {
queryFields: [],
Expand Down Expand Up @@ -1261,17 +1263,66 @@ describe('SiemLocalStorage', () => {
],
sort: [{ '@timestamp': { order: 'desc' } }],
visibleColumns: [
'@timestamp',
'kibana.alert.rule.name',
'kibana.alert.severity',
'kibana.alert.risk_score',
'kibana.alert.reason',
'host.name',
'user.name',
'process.name',
'file.name',
'source.ip',
'destination.ip',
{
columnHeaderType: 'not-filtered',
id: '@timestamp',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Rule',
id: 'kibana.alert.rule.name',
initialWidth: 180,
linkField: 'kibana.alert.rule.uuid',
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Severity',
id: 'kibana.alert.severity',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Risk Score',
id: 'kibana.alert.risk_score',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Reason',
id: 'kibana.alert.reason',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'host.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'user.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'process.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'file.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'source.ip',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'destination.ip',
initialWidth: 180,
},
],
},
},
Expand Down Expand Up @@ -1344,17 +1395,66 @@ describe('SiemLocalStorage', () => {
{ 'kibana.alert.rule.name': { order: 'desc' } },
],
visibleColumns: [
'@timestamp',
'kibana.alert.rule.name',
'kibana.alert.severity',
'kibana.alert.risk_score',
'kibana.alert.reason',
'host.name',
'user.name',
'process.name',
'file.name',
'source.ip',
'destination.ip',
{
columnHeaderType: 'not-filtered',
id: '@timestamp',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Rule',
id: 'kibana.alert.rule.name',
initialWidth: 180,
linkField: 'kibana.alert.rule.uuid',
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Severity',
id: 'kibana.alert.severity',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Risk Score',
id: 'kibana.alert.risk_score',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
displayAsText: 'Reason',
id: 'kibana.alert.reason',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'host.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'user.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'process.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'file.name',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'source.ip',
initialWidth: 180,
},
{
columnHeaderType: 'not-filtered',
id: 'destination.ip',
initialWidth: 180,
},
],
},
},
Expand Down Expand Up @@ -1387,4 +1487,44 @@ describe('SiemLocalStorage', () => {
}
});
});

describe('should migrate Alert Table visible columns from v8.8.x', () => {
// PR: https://github.com/elastic/kibana/pull/161054
beforeEach(() => storage.clear());
it('should migrate correctly when upgrading from 8.8.x -> 8.9', () => {
Object.keys(v88xAlertOrignalData).forEach((k) => {
storage.set(k, v88xAlertOrignalData[k as keyof typeof v88xAlertOrignalData]);
});

migrateTriggerActionsVisibleColumnsAlertTable88xTo89(storage);

Object.keys(v89xAlertsOriginalData).forEach((k) => {
const expectedResult = v89xAlertsOriginalData[k as keyof typeof v89xAlertsOriginalData];
expect(storage.get(k)).toMatchObject(expectedResult);
});
});
it('should be a no-op when reinstalling from 8.9 when data is already present.', () => {
Object.keys(v89xAlertsOriginalData).forEach((k) => {
storage.set(k, v89xAlertsOriginalData[k as keyof typeof v89xAlertsOriginalData]);
});

migrateTriggerActionsVisibleColumnsAlertTable88xTo89(storage);

Object.keys(v89xAlertsOriginalData).forEach((k) => {
const expectedResult = v89xAlertsOriginalData[k as keyof typeof v89xAlertsOriginalData];
expect(storage.get(k)).toMatchObject(expectedResult);
});
});

it('should be a no-op when installing 8.9 for the first time', () => {
migrateTriggerActionsVisibleColumnsAlertTable88xTo89(storage);

expect(
storage.get('detection-engine-alert-table-securitySolution-alerts-page-gridView')
).toBeNull();
expect(
storage.get('detection-engine-alert-table-securitySolution-rule-details-gridView')
).toBeNull();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ export const migrateLegacyTimelinesToSecurityDataTable = (legacyTimelineTables:
}, {} as { [K in TableIdLiteral]: DataTableModel });
};

/*
*
* This migraiton only works for upgrading from
* 8.7 -> 8.8. Please do not edit this migration for any
* future release.
*
* If there is a migration that is required to be done for
* any future release. It should written as a saperate piece of code
* and should be called after this migration
*
* **/

export const migrateAlertTableStateToTriggerActionsState = (
storage: Storage,
legacyDataTableState: DataTableState['dataTable']['tableById']
Expand All @@ -100,7 +112,7 @@ export const migrateAlertTableStateToTriggerActionsState = (
sort: legacyDataTableState[tableKey].sort.map((sortCandidate) => ({
[sortCandidate.columnId]: { order: sortCandidate.sortDirection },
})),
visibleColumns: legacyDataTableState[tableKey].columns.map((c) => c.id),
visibleColumns: legacyDataTableState[tableKey].columns,
},
};
});
Expand All @@ -110,7 +122,47 @@ export const migrateAlertTableStateToTriggerActionsState = (
storage.set(key, stateObj[key]);
})
);
return Object.assign(legacyDataTableState, triggersActionsState);
};

/*
*
* Used for migrating Alert Table from 8.8 => 8.9
* */
export const migrateTriggerActionsVisibleColumnsAlertTable88xTo89 = (storage: Storage) => {
const localStorageKeys = [
`detection-engine-alert-table-${ALERTS_TABLE_REGISTRY_CONFIG_IDS.ALERTS_PAGE}-gridView`,
`detection-engine-alert-table-${ALERTS_TABLE_REGISTRY_CONFIG_IDS.RULE_DETAILS}-gridView`,
];

localStorageKeys.forEach((key) => {
const alertTableData = storage.get(key);
if (!alertTableData) {
return;
}
if ('visibleColumns' in alertTableData) {
const visibleColumns =
alertTableData.visibleColumns as DataTableState['dataTable']['tableById'][string]['columns'];
const v89CompliantFormat = visibleColumns.every((val: unknown) => typeof val === 'string');
if (v89CompliantFormat) {
return;
}
const newVisibleColumns = visibleColumns
.map((visibleCol) => {
if (typeof visibleCol === 'string') {
// if column format is 8.9 compliant already
return visibleCol;
}
// if column format is 8.8
return visibleCol.id;
})
.filter(Boolean);

storage.set(key, {
...alertTableData,
visibleColumns: newVisibleColumns,
});
}
});
};

/**
Expand Down Expand Up @@ -155,7 +207,8 @@ export const getDataTablesInStorageByIds = (storage: Storage, tableIds: TableIdL
}
}

allDataTables = migrateAlertTableStateToTriggerActionsState(storage, allDataTables);
migrateAlertTableStateToTriggerActionsState(storage, allDataTables);
migrateTriggerActionsVisibleColumnsAlertTable88xTo89(storage);

return tableIds.reduce((acc, tableId) => {
const tableModel = allDataTables[tableId];
Expand Down
Loading

0 comments on commit 63549cb

Please sign in to comment.