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 1 commit
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,11 +12,11 @@ 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';
import { fieldHasCellActions } from '../../utils';
import { fieldHasCellActions, isLensSupportedType } from '../../utils';
import { TopNAction } from '../show_top_n_component';
import type { StartServices } from '../../../types';
import type { SecurityCellAction } from '../../types';
Expand All @@ -29,7 +29,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 +50,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
11 changes: 11 additions & 0 deletions x-pack/plugins/security_solution/public/actions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type { IEmbeddable } from '@kbn/embeddable-plugin/public';
import { LENS_EMBEDDABLE_TYPE, type Embeddable as LensEmbeddable } from '@kbn/lens-plugin/public';
import type { Serializable } from '@kbn/utility-types';
import { KBN_FIELD_TYPES } from '@kbn/field-types';
import { APP_UI_ID } from '../../common/constants';

// All cell actions are disabled for these fields in Security
Expand Down Expand Up @@ -35,3 +36,13 @@ export const isCountField = (
) => {
return fieldType === 'number' && sourceParamType === 'value_count';
};

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;
Copy link
Contributor

Choose a reason for hiding this comment

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

The isLensSupportedType is only used by showTopN cellAction, it should not be defined as a common util function. Can we move it to the showTopN action file that uses it?

Also, it is a bit confusing having isLensEmbeddable util function used by all lensActions, and the isLensSupportedType which is not used by lensActions, in the same place.

Copy link
Member Author

@machadoum machadoum Jul 3, 2023

Choose a reason for hiding this comment

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

Good catch! I moved it from public/actions/utils to public/common/utils/lens because I found out that detection had implemented the same function.