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 Solutions] Fix CellActions component should hide ShowTopN action for nested fields #159645

Merged
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 @@ -17,6 +17,7 @@ import { createStore } from '../../../common/store';
import { createShowTopNCellActionFactory } from './show_top_n';
import React from 'react';
import { createStartServicesMock } from '../../../common/lib/kibana/kibana_react.mock';
import { KBN_FIELD_TYPES } from '@kbn/field-types';

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

Expand Down Expand Up @@ -45,7 +46,7 @@ describe('createShowTopNCellActionFactory', () => {
value: 'the-value',
field: {
name: 'user.name',
type: 'keyword',
type: KBN_FIELD_TYPES.STRING,
aggregatable: true,
searchable: true,
},
Expand All @@ -71,35 +72,54 @@ describe('createShowTopNCellActionFactory', () => {
});

describe('isCompatible', () => {
it('should return true if everything is okay', async () => {
expect(await showTopNAction.isCompatible(context)).toEqual(true);
});

it('should return false if field esType does not support aggregations', async () => {
it('should return false if field is not aggregatable', async () => {
expect(
await showTopNAction.isCompatible({
...context,
data: [
{
field: { ...context.data[0].field, esTypes: ['text'] },
field: { ...context.data[0].field, aggregatable: false },
},
],
})
).toEqual(false);
});

it('should return false if field is not aggregatable', async () => {
it('should return false if field is nested', async () => {
expect(
await showTopNAction.isCompatible({
...context,
data: [
{
field: { ...context.data[0].field, aggregatable: false },
field: { ...context.data[0].field, subType: { nested: { path: 'test_path' } } },
},
],
})
).toEqual(false);
});

describe.each([
{ type: KBN_FIELD_TYPES.STRING, expectedValue: true },
{ type: KBN_FIELD_TYPES.BOOLEAN, expectedValue: true },
{ type: KBN_FIELD_TYPES.NUMBER, expectedValue: true },
{ type: KBN_FIELD_TYPES.IP, expectedValue: true },
{ type: KBN_FIELD_TYPES.DATE, expectedValue: false },
{ type: KBN_FIELD_TYPES.GEO_SHAPE, expectedValue: false },
{ type: KBN_FIELD_TYPES.IP_RANGE, expectedValue: false },
])('lens supported KBN types', ({ type, expectedValue }) => {
it(`should return ${expectedValue} when type is ${type}`, async () => {
expect(
await showTopNAction.isCompatible({
...context,
data: [
{
field: { ...context.data[0].field, type },
},
],
})
).toEqual(expectedValue);
});
});
});

describe('execute', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Router } from '@kbn/shared-ux-router';
import { i18n } from '@kbn/i18n';
import { createCellActionFactory, type CellActionTemplate } from '@kbn/cell-actions';
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';
import { ES_FIELD_TYPES } from '@kbn/field-types';
import { isDataViewFieldSubtypeNested } from '@kbn/es-query';
import { KibanaContextProvider } from '../../../common/lib/kibana';
import { APP_NAME, DEFAULT_DARK_MODE } from '../../../../common/constants';
import type { SecurityAppStore } from '../../../common/store';
Expand All @@ -21,6 +21,7 @@ import { TopNAction } from '../show_top_n_component';
import type { StartServices } from '../../../types';
import type { SecurityCellAction } from '../../types';
import { SecurityCellActionType } from '../../constants';
import { isLensSupportedType } from '../../../common/utils/lens';

const SHOW_TOP = (fieldName: string) =>
i18n.translate('xpack.securitySolution.actions.showTopTooltip', {
Expand All @@ -29,7 +30,6 @@ const SHOW_TOP = (fieldName: string) =>
});

const ICON = 'visBarVertical';
const UNSUPPORTED_ES_FIELD_TYPES = [ES_FIELD_TYPES.DATE, ES_FIELD_TYPES.TEXT];

export const createShowTopNCellActionFactory = createCellActionFactory(
({
Expand All @@ -51,9 +51,8 @@ export const createShowTopNCellActionFactory = createCellActionFactory(
return (
data.length === 1 &&
fieldHasCellActions(field.name) &&
(field.esTypes ?? []).every(
(esType) => !UNSUPPORTED_ES_FIELD_TYPES.includes(esType as ES_FIELD_TYPES)
) &&
isLensSupportedType(field.type) &&
!isDataViewFieldSubtypeNested(field) &&
!!field.aggregatable
);
},
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/security_solution/public/common/utils/lens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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 { KBN_FIELD_TYPES } from '@kbn/field-types';

const SUPPORTED_LENS_TYPES = new Set([
KBN_FIELD_TYPES.STRING,
KBN_FIELD_TYPES.BOOLEAN,
KBN_FIELD_TYPES.NUMBER,
KBN_FIELD_TYPES.IP,
]);

export const isLensSupportedType = (fieldType: string | undefined) =>
fieldType ? SUPPORTED_LENS_TYPES.has(fieldType as KBN_FIELD_TYPES) : false;
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { BrowserField } from '@kbn/timelines-plugin/common';
import type { GlobalTimeArgs } from '../../../../common/containers/use_global_time';
import { getScopeFromPath, useSourcererDataView } from '../../../../common/containers/sourcerer';
import { getAllFieldsByName } from '../../../../common/containers/source';
import { isLensSupportedType } from '../../../../common/utils/lens';

export interface UseInspectButtonParams extends Pick<GlobalTimeArgs, 'setQuery' | 'deleteQuery'> {
response: string;
Expand Down Expand Up @@ -65,11 +66,6 @@ export function isDataViewFieldSubtypeNested(field: Partial<BrowserField>) {
return !!subTypeNested?.nested?.path;
}

export function isLensSupportedType(fieldType: string | undefined) {
const supportedTypes = new Set(['string', 'boolean', 'number', 'ip']);
return fieldType ? supportedTypes.has(fieldType) : false;
}

export interface GetAggregatableFields {
[fieldName: string]: Partial<BrowserField>;
}
Expand Down