Skip to content

Commit 9a66ec9

Browse files
[AI4DSOC] Change the Cases page to use the AI for SOC alerts table (elastic#218742)
## Summary While testing, we realized that the Cases alerts tab was showing the `DetectionEngineAlertsTable` and the normal alert details flyout, even in the AI4DSOC tier. This PR updates the logic to show the correct alerts table and the correct alert details flyout depending on the tier: - AI4DSOC will show the same table and flyout as the ones shown in the Alert summary page - the other tiers will continue showing the same table and flyout we show today under the Alerts page or any other pages (`DetectionEngineAlertsTable`) Switching the table allows us to tackle at once all the other related issues: - wrong flyout was being shown - too many row actions were being shown - wrong default columns, and wrong cell renderers ### Notes The approach is not ideal. We shouldn't have to check for the following ```typescript const AIForSOC = capabilities[SECURITY_FEATURE_ID].configurations; ``` in the code, but because of time constraints, this was the best approach. [A ticket](elastic#218741) has been opened to make sure we come back to this and implement the check the correct way later. Current (wrong) behavior https://github.com/user-attachments/assets/5d769f45-26d9-4631-af95-de38b0797ff9 New behavior https://github.com/user-attachments/assets/1f9a2e4d-50b7-40e6-8efa-1a0cfdbf5c9a ## How to test This needs to be ran in Serverless: - `yarn es serverless --projectType security` - `yarn serverless-security --no-base-path` You also need to enable the AI for SOC tier, by adding the following to your `serverless.security.dev.yaml` file: ``` xpack.securitySolutionServerless.productTypes: [ { product_line: 'ai_soc', product_tier: 'search_ai_lake' }, ] ``` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios Relates to elastic/security-team#11973 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
1 parent 154b14d commit 9a66ec9

5 files changed

Lines changed: 495 additions & 17 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import React from 'react';
9+
import { render } from '@testing-library/react';
10+
import type { DataView } from '@kbn/data-views-plugin/common';
11+
import { createStubDataView } from '@kbn/data-views-plugin/common/data_views/data_view.stub';
12+
import { TestProviders } from '../../../common/mock';
13+
import { Table } from './table';
14+
import type { PackageListItem } from '@kbn/fleet-plugin/common';
15+
import { installationStatuses } from '@kbn/fleet-plugin/common/constants';
16+
17+
const dataView: DataView = createStubDataView({ spec: {} });
18+
const packages: PackageListItem[] = [
19+
{
20+
id: 'splunk',
21+
icons: [{ src: 'icon.svg', path: 'mypath/icon.svg', type: 'image/svg+xml' }],
22+
name: 'splunk',
23+
status: installationStatuses.NotInstalled,
24+
title: 'Splunk',
25+
version: '0.1.0',
26+
},
27+
];
28+
const ruleResponse = {
29+
rules: [],
30+
isLoading: false,
31+
};
32+
const id = 'id';
33+
const query = { ids: { values: ['abcdef'] } };
34+
const onLoaded = jest.fn();
35+
36+
describe('<Table />', () => {
37+
it('should render all components', () => {
38+
const { getByTestId } = render(
39+
<TestProviders>
40+
<Table
41+
dataView={dataView}
42+
id={id}
43+
onLoaded={onLoaded}
44+
packages={packages}
45+
query={query}
46+
ruleResponse={ruleResponse}
47+
/>
48+
</TestProviders>
49+
);
50+
51+
expect(getByTestId('alertsTableErrorPrompt')).toBeInTheDocument();
52+
});
53+
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import React, { memo, useMemo } from 'react';
9+
import type { DataView } from '@kbn/data-views-plugin/common';
10+
import { AlertsTable } from '@kbn/response-ops-alerts-table';
11+
import type { PackageListItem } from '@kbn/fleet-plugin/common';
12+
import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
13+
import type { Alert } from '@kbn/alerting-types';
14+
import type { EuiDataGridColumn } from '@elastic/eui';
15+
import type { AdditionalTableContext } from '../../../detections/components/alert_summary/table/table';
16+
import {
17+
ACTION_COLUMN_WIDTH,
18+
ALERT_TABLE_CONSUMERS,
19+
columns,
20+
GRID_STYLE,
21+
ROW_HEIGHTS_OPTIONS,
22+
RULE_TYPE_IDS,
23+
TOOLBAR_VISIBILITY,
24+
} from '../../../detections/components/alert_summary/table/table';
25+
import { ActionsCell } from '../../../detections/components/alert_summary/table/actions_cell';
26+
import { getDataViewStateFromIndexFields } from '../../../common/containers/source/use_data_view';
27+
import { useKibana } from '../../../common/lib/kibana';
28+
import { CellValue } from '../../../detections/components/alert_summary/table/render_cell';
29+
import type { RuleResponse } from '../../../../common/api/detection_engine';
30+
31+
export interface TableProps {
32+
/**
33+
* DataView created for the alert summary page
34+
*/
35+
dataView: DataView;
36+
/**
37+
* Id to pass down to the ResponseOps alerts table
38+
*/
39+
id: string;
40+
/**
41+
* Callback fired when the alerts have been first loaded
42+
*/
43+
onLoaded?: (alerts: Alert[], columns: EuiDataGridColumn[]) => void;
44+
/**
45+
* List of installed AI for SOC integrations
46+
*/
47+
packages: PackageListItem[];
48+
/**
49+
* Query that contains the id of the alerts to display in the table
50+
*/
51+
query: Pick<QueryDslQueryContainer, 'bool' | 'ids'>;
52+
/**
53+
* Result from the useQuery to fetch all rules
54+
*/
55+
ruleResponse: {
56+
/**
57+
* Result from fetching all rules
58+
*/
59+
rules: RuleResponse[];
60+
/**
61+
* True while rules are being fetched
62+
*/
63+
isLoading: boolean;
64+
};
65+
}
66+
67+
/**
68+
* Component used in the Cases page under Alerts tab, only in the AI4DSOC tier.
69+
* It leverages a lot of configurations and constants from the Alert summary page alerts table, and renders the ResponseOps AlertsTable.
70+
*/
71+
export const Table = memo(
72+
({ dataView, id, onLoaded, packages, query, ruleResponse }: TableProps) => {
73+
const {
74+
services: { application, data, fieldFormats, http, licensing, notifications, settings },
75+
} = useKibana();
76+
const services = useMemo(
77+
() => ({
78+
data,
79+
http,
80+
notifications,
81+
fieldFormats,
82+
application,
83+
licensing,
84+
settings,
85+
}),
86+
[application, data, fieldFormats, http, licensing, notifications, settings]
87+
);
88+
89+
const dataViewSpec = useMemo(() => dataView.toSpec(), [dataView]);
90+
91+
const { browserFields } = useMemo(
92+
() => getDataViewStateFromIndexFields('', dataViewSpec.fields),
93+
[dataViewSpec.fields]
94+
);
95+
96+
const additionalContext: AdditionalTableContext = useMemo(
97+
() => ({
98+
packages,
99+
ruleResponse,
100+
}),
101+
[packages, ruleResponse]
102+
);
103+
104+
return (
105+
<AlertsTable
106+
actionsColumnWidth={ACTION_COLUMN_WIDTH}
107+
additionalContext={additionalContext}
108+
browserFields={browserFields}
109+
columns={columns}
110+
consumers={ALERT_TABLE_CONSUMERS}
111+
gridStyle={GRID_STYLE}
112+
id={id}
113+
onLoaded={onLoaded}
114+
query={query}
115+
renderActionsCell={ActionsCell}
116+
renderCellValue={CellValue}
117+
rowHeightsOptions={ROW_HEIGHTS_OPTIONS}
118+
ruleTypeIds={RULE_TYPE_IDS}
119+
services={services}
120+
toolbarVisibility={TOOLBAR_VISIBILITY}
121+
/>
122+
);
123+
}
124+
);
125+
126+
Table.displayName = 'Table';
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import React from 'react';
9+
import { render, screen, waitFor } from '@testing-library/react';
10+
import { AiForSOCAlertsTable, CONTENT_TEST_ID, ERROR_TEST_ID, SKELETON_TEST_ID } from './wrapper';
11+
import { useKibana } from '../../../common/lib/kibana';
12+
import { TestProviders } from '../../../common/mock';
13+
import { useFetchIntegrations } from '../../../detections/hooks/alert_summary/use_fetch_integrations';
14+
import { useFindRulesQuery } from '../../../detection_engine/rule_management/api/hooks/use_find_rules_query';
15+
16+
jest.mock('./table', () => ({
17+
Table: () => <div />,
18+
}));
19+
jest.mock('../../../common/lib/kibana');
20+
jest.mock('../../../detections/hooks/alert_summary/use_fetch_integrations');
21+
jest.mock('../../../detection_engine/rule_management/api/hooks/use_find_rules_query');
22+
23+
const id = 'id';
24+
const query = { ids: { values: ['abcdef'] } };
25+
const onLoaded = jest.fn();
26+
27+
describe('<AiForSOCAlertsTab />', () => {
28+
beforeEach(() => {
29+
jest.clearAllMocks();
30+
31+
(useFetchIntegrations as jest.Mock).mockReturnValue({
32+
installedPackages: [],
33+
isLoading: false,
34+
});
35+
(useFindRulesQuery as jest.Mock).mockReturnValue({
36+
data: [],
37+
isLoading: false,
38+
});
39+
});
40+
41+
it('should render a loading skeleton while creating the dataView', async () => {
42+
(useKibana as jest.Mock).mockReturnValue({
43+
services: {
44+
data: {
45+
dataViews: {
46+
create: jest.fn(),
47+
clearInstanceCache: jest.fn(),
48+
},
49+
},
50+
http: { basePath: { prepend: jest.fn() } },
51+
},
52+
});
53+
54+
render(<AiForSOCAlertsTable id={id} onLoaded={onLoaded} query={query} />);
55+
56+
await waitFor(() => {
57+
expect(screen.getByTestId(SKELETON_TEST_ID)).toBeInTheDocument();
58+
});
59+
});
60+
61+
it('should render a loading skeleton while fetching packages (integrations)', async () => {
62+
(useKibana as jest.Mock).mockReturnValue({
63+
services: {
64+
data: {
65+
dataViews: {
66+
create: jest.fn(),
67+
clearInstanceCache: jest.fn(),
68+
},
69+
},
70+
http: { basePath: { prepend: jest.fn() } },
71+
},
72+
});
73+
(useFetchIntegrations as jest.Mock).mockReturnValue({
74+
installedPackages: [],
75+
isLoading: true,
76+
});
77+
78+
render(<AiForSOCAlertsTable id={id} onLoaded={onLoaded} query={query} />);
79+
80+
expect(await screen.findByTestId(SKELETON_TEST_ID)).toBeInTheDocument();
81+
});
82+
83+
it('should render an error if the dataView fail to be created correctly', async () => {
84+
(useKibana as jest.Mock).mockReturnValue({
85+
services: {
86+
data: {
87+
dataViews: {
88+
create: jest.fn().mockReturnValue(undefined),
89+
clearInstanceCache: jest.fn(),
90+
},
91+
},
92+
},
93+
});
94+
95+
jest.mock('react', () => ({
96+
...jest.requireActual('react'),
97+
useEffect: jest.fn((f) => f()),
98+
}));
99+
100+
render(<AiForSOCAlertsTable id={id} onLoaded={onLoaded} query={query} />);
101+
102+
expect(await screen.findByTestId(ERROR_TEST_ID)).toHaveTextContent(
103+
'Unable to create data view'
104+
);
105+
});
106+
107+
it('should render the content', async () => {
108+
(useKibana as jest.Mock).mockReturnValue({
109+
services: {
110+
data: {
111+
dataViews: {
112+
create: jest
113+
.fn()
114+
.mockReturnValue({ getIndexPattern: jest.fn(), id: 'id', toSpec: jest.fn() }),
115+
clearInstanceCache: jest.fn(),
116+
},
117+
query: { filterManager: { getFilters: jest.fn() } },
118+
},
119+
},
120+
});
121+
122+
jest.mock('react', () => ({
123+
...jest.requireActual('react'),
124+
useEffect: jest.fn((f) => f()),
125+
}));
126+
127+
render(
128+
<TestProviders>
129+
<AiForSOCAlertsTable id={id} onLoaded={onLoaded} query={query} />
130+
</TestProviders>
131+
);
132+
133+
expect(await screen.findByTestId(CONTENT_TEST_ID)).toBeInTheDocument();
134+
});
135+
});

0 commit comments

Comments
 (0)