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

[Security Solution][Fix] Empty Alert Table when upgrading from 8.8.x -> 8.9 #162063

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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,
Copy link
Contributor Author

@logeekal logeekal Jul 17, 2023

Choose a reason for hiding this comment

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

},
};
});
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) {
Copy link
Contributor

@michaelolo24 michaelolo24 Jul 17, 2023

Choose a reason for hiding this comment

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

Can we have one more early exit here?

const nonCompliant = alertTableData.visibleColumns.some(val => typeof val !== 'string')

if (!nonCompliant) return

... or this if you prefer

if (nonCompliant) {
  alertTableData.visibleColumns.map(...)
}

Right now the map will most likely run every time someone loads the table for every version after this

Copy link
Contributor Author

@logeekal logeekal Jul 17, 2023

Choose a reason for hiding this comment

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

You are right.. made these changes with a little tweak.

const visibleColumns =
alertTableData.visibleColumns as DataTableState['dataTable']['tableById'][string]['columns'];
const v89CompliantFormat = visibleColumns.every((val: unknown) => typeof val === 'string');
if (v89CompliantFormat) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sweet, this works! 👍🏾

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