From 26a5580ca16b357c224dd0ac9d7e139a677520d0 Mon Sep 17 00:00:00 2001 From: Garrett Spong Date: Thu, 29 Aug 2019 22:48:15 -0600 Subject: [PATCH] [SIEM] Adds Connections (Pewpew) Map to Network Page (#43965) (#44450) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary This PR uses the Embeddables API to embed a [Connections Map](https://github.com/elastic/kibana/pull/41504) on the Network Page of the SIEM App. A [Map Configuration](https://github.com/elastic/kibana/pull/43878) is generated on page load for configured Kibana Index Patterns that match the `siem:defaultIndex` setting in Kibana Advanced Settings, with a `Source`, `Destination`, and `Line` layer being created for each configured Kibana Index Pattern. Features includes: * Global KQL Bar Filtering * Global Timerange Filtering * Global Refresh * Click on a feature on the map to view details in tooltip * Line Selection: shows `Total Source/Destination Bytes` and `Total Documents` in tooltip * Source/Destination Selection: shows `host.name` and `host.ip` * Send filter to Global KQL Bar from Source/Destination feature tooltip (`host.name` and `host.ip`) * Informative error when index patterns aren't configured that links to specific beats setup documentation (e.g. [auditbeat setup docs](https://www.elastic.co/guide/en/beats/auditbeat/current/load-kibana-dashboards.html)) #### Dark Theme: ![image](https://user-images.githubusercontent.com/2946766/63667505-4dbe1900-c791-11e9-9b1e-9b690eafc405.png) #### Light Theme: ![image](https://user-images.githubusercontent.com/2946766/63667685-f2d8f180-c791-11e9-840e-6d3fbe0adaf2.png) ### Visual Feature Catalogue®
Global KQL Bar Filtering ![pewpew_global_kql_filter](https://user-images.githubusercontent.com/2946766/63803146-72210f00-c8d1-11e9-951a-18571bbf9069.gif)
Global Timerange Filtering + Refreshing ![pewpew_global_time_filter](https://user-images.githubusercontent.com/2946766/63803214-9b419f80-c8d1-11e9-8349-e4f40fa39200.gif)
Filter on `host.name` or `host.ip` From Map Tooltip ![pewpew_filter_on_property](https://user-images.githubusercontent.com/2946766/63803302-d6dc6980-c8d1-11e9-990d-c364b53e247b.gif)
Using Field Formatters in Tooltip to link to Host/Network Details ![pewpew_field_formatter_links](https://user-images.githubusercontent.com/2946766/63803333-e65bb280-c8d1-11e9-9743-81165e0f78d7.gif)
Error displayed when Index Patterns aren't available ![image](https://user-images.githubusercontent.com/2946766/63804367-418ea480-c8d4-11e9-9357-73418a197809.png)
### Checklist Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR. - [x] This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) * Tested and all functionality works in Chrome/FF/Safari * IE11 fails to load map with `mapbox-gl.js` exception. Details: https://github.com/elastic/kibana/issues/44155 - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md) - [ ] [Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials * Not yet, but will work with @benskelker on this. - [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios - [ ] ~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~ ### For maintainers - [ ] ~This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process)~ - [x] This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process) --- .../components/embeddables/__mocks__/mock.ts | 168 +++++++++++++ .../__snapshots__/embedded_map.test.tsx.snap | 14 ++ ...ndex_patterns_missing_prompt.test.tsx.snap | 71 ++++++ .../actions/apply_siem_filter_action.test.tsx | 192 +++++++++++++++ .../actions/apply_siem_filter_action.tsx | 78 ++++++ .../embeddables/embedded_map.test.tsx | 61 +++++ .../components/embeddables/embedded_map.tsx | 223 ++++++++++++++++++ .../index_patterns_missing_prompt.test.tsx | 22 ++ .../index_patterns_missing_prompt.tsx | 72 ++++++ .../components/embeddables/map_config.test.ts | 59 +++++ .../components/embeddables/map_config.ts | 178 ++++++++++++++ .../components/embeddables/translations.ts | 36 +++ .../public/components/embeddables/types.ts | 27 +++ .../components/ml_popover/__mocks__/api.tsx | 23 +- .../siem/public/components/ml_popover/api.tsx | 15 +- .../components/ml_popover/helpers.test.tsx | 32 +++ .../public/components/ml_popover/helpers.tsx | 26 +- .../ml_popover/hooks/use_index_patterns.tsx | 7 +- .../components/ml_popover/ml_popover.tsx | 15 +- .../siem/public/pages/network/network.tsx | 18 +- .../siem/public/store/network/selectors.ts | 7 + 21 files changed, 1325 insertions(+), 19 deletions(-) create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/__mocks__/mock.ts create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/embedded_map.test.tsx.snap create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/index_patterns_missing_prompt.test.tsx.snap create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.test.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.test.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.test.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.tsx create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/map_config.test.ts create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/map_config.ts create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/translations.ts create mode 100644 x-pack/legacy/plugins/siem/public/components/embeddables/types.ts diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/__mocks__/mock.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/__mocks__/mock.ts new file mode 100644 index 0000000000000..e6751eeb2de70 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/__mocks__/mock.ts @@ -0,0 +1,168 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { IndexPatternMapping } from '../types'; + +export const mockIndexPatternIds: IndexPatternMapping[] = [ + { title: 'filebeat-*', id: '8c7323ac-97ad-4b53-ac0a-40f8f691a918' }, +]; + +export const mockSourceLayer = { + sourceDescriptor: { + id: 'uuid.v4()', + type: 'ES_SEARCH', + geoField: 'source.geo.location', + filterByMapBounds: false, + tooltipProperties: ['host.name', 'host.ip'], + useTopHits: false, + topHitsTimeField: '@timestamp', + topHitsSize: 1, + indexPatternId: '8c7323ac-97ad-4b53-ac0a-40f8f691a918', + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#3cb44b' } }, + lineColor: { type: 'STATIC', options: { color: '#FFFFFF' } }, + lineWidth: { type: 'STATIC', options: { size: 1 } }, + iconSize: { type: 'STATIC', options: { size: 6 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'arrow-es' } }, + }, + }, + id: 'uuid.v4()', + label: `filebeat-* | Source Point`, + minZoom: 0, + maxZoom: 24, + alpha: 0.75, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: 'source.geo.location:* and destination.geo.location:*', language: 'kuery' }, + joins: [], +}; + +export const mockDestinationLayer = { + sourceDescriptor: { + id: 'uuid.v4()', + type: 'ES_SEARCH', + geoField: 'destination.geo.location', + filterByMapBounds: true, + tooltipProperties: ['host.name', 'host.ip'], + useTopHits: false, + topHitsTimeField: '@timestamp', + topHitsSize: 1, + indexPatternId: '8c7323ac-97ad-4b53-ac0a-40f8f691a918', + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#e6194b' } }, + lineColor: { type: 'STATIC', options: { color: '#FFFFFF' } }, + lineWidth: { type: 'STATIC', options: { size: 1 } }, + iconSize: { type: 'STATIC', options: { size: 6 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'airfield' } }, + }, + }, + id: 'uuid.v4()', + label: `filebeat-* | Destination Point`, + minZoom: 0, + maxZoom: 24, + alpha: 0.75, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: 'source.geo.location:* and destination.geo.location:*', language: 'kuery' }, +}; + +export const mockLineLayer = { + sourceDescriptor: { + type: 'ES_PEW_PEW', + id: 'uuid.v4()', + indexPatternId: '8c7323ac-97ad-4b53-ac0a-40f8f691a918', + sourceGeoField: 'source.geo.location', + destGeoField: 'destination.geo.location', + metrics: [ + { type: 'sum', field: 'source.bytes', label: 'Total Src Bytes' }, + { type: 'sum', field: 'destination.bytes', label: 'Total Dest Bytes' }, + { type: 'count', label: 'Total Documents' }, + ], + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#e6194b' } }, + lineColor: { + type: 'DYNAMIC', + options: { + color: 'Green to Red', + field: { label: 'count', name: 'doc_count', origin: 'source' }, + useCustomColorRamp: false, + }, + }, + lineWidth: { + type: 'DYNAMIC', + options: { + minSize: 1, + maxSize: 4, + field: { label: 'count', name: 'doc_count', origin: 'source' }, + }, + }, + iconSize: { type: 'STATIC', options: { size: 10 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'airfield' } }, + }, + }, + id: 'uuid.v4()', + label: `filebeat-* | Line`, + minZoom: 0, + maxZoom: 24, + alpha: 1, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: '', language: 'kuery' }, +}; + +export const mockLayerList = [ + { + sourceDescriptor: { type: 'EMS_TMS', isAutoSelect: true }, + id: 'uuid.v4()', + label: null, + minZoom: 0, + maxZoom: 24, + alpha: 1, + visible: true, + applyGlobalQuery: true, + style: { type: 'TILE', properties: {} }, + type: 'TILE', + }, + mockLineLayer, + mockDestinationLayer, + mockSourceLayer, +]; + +export const mockLayerListDouble = [ + { + sourceDescriptor: { type: 'EMS_TMS', isAutoSelect: true }, + id: 'uuid.v4()', + label: null, + minZoom: 0, + maxZoom: 24, + alpha: 1, + visible: true, + applyGlobalQuery: true, + style: { type: 'TILE', properties: {} }, + type: 'TILE', + }, + mockLineLayer, + mockDestinationLayer, + mockSourceLayer, + mockLineLayer, + mockDestinationLayer, + mockSourceLayer, +]; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/embedded_map.test.tsx.snap b/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/embedded_map.test.tsx.snap new file mode 100644 index 0000000000000..e25746c4f41af --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/embedded_map.test.tsx.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`EmbeddedMap renders correctly against snapshot 1`] = ` + + + + + + +`; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/index_patterns_missing_prompt.test.tsx.snap b/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/index_patterns_missing_prompt.test.tsx.snap new file mode 100644 index 0000000000000..173f7a4e346d5 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/__snapshots__/index_patterns_missing_prompt.test.tsx.snap @@ -0,0 +1,71 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`IndexPatternsMissingPrompt renders correctly against snapshot 1`] = ` + + Configure index patterns + + } + body={ + +

+ An ECS compliant Kibana index pattern must be configured to view event data on the map. When using beats, you can run the following setup commands to create the required Kibana index patterns, otherwise you can configure them manually within Kibana settings. +

+

+ + auditbeat-* + + , + + filebeat-* + + , + + packetbeat-* + + , + + winlogbeat-* + +

+
+ } + iconColor="subdued" + iconType="gisApp" + title={ +

+ Required Index Patterns Not Configured +

+ } + titleSize="xs" +/> +`; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.test.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.test.tsx new file mode 100644 index 0000000000000..47efbcce16867 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.test.tsx @@ -0,0 +1,192 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { get } from 'lodash/fp'; + +import { ApplySiemFilterAction, getExpressionFromArray } from './apply_siem_filter_action'; +// @ts-ignore Missing type defs as maps moves to Typescript +import { MAP_SAVED_OBJECT_TYPE } from '../../../../../maps/common/constants'; +import { Action } from '../../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/lib/actions'; +import { expectError } from '../../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/tests/helpers'; +import { + EmbeddableInput, + EmbeddableOutput, + IEmbeddable, +} from '../../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/lib/embeddables'; +import { Filter } from '@kbn/es-query'; + +// Using type narrowing to remove all the any's -- https://github.com/elastic/kibana/pull/43965/files#r318796100 +const isEmbeddable = ( + embeddable: unknown +): embeddable is IEmbeddable => { + return get('type', embeddable) != null; +}; + +const isTriggerContext = (triggerContext: unknown): triggerContext is { filters: Filter[] } => { + return typeof triggerContext === 'object'; +}; + +describe('ApplySiemFilterAction', () => { + let applyFilterQueryFromKueryExpression: (expression: string) => void; + + beforeEach(() => { + applyFilterQueryFromKueryExpression = jest.fn(expression => {}); + }); + + test('it is an instance of Action', () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + expect(action).toBeInstanceOf(Action); + }); + + test('it has APPLY_SIEM_FILTER_ACTION_ID type and id', () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + expect(action.id).toBe('APPLY_SIEM_FILTER_ACTION_ID'); + expect(action.type).toBe('APPLY_SIEM_FILTER_ACTION_ID'); + }); + + test('it has expected display name', () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + expect(action.getDisplayName()).toMatchInlineSnapshot(`"Apply filter"`); + }); + + describe('#isCompatible', () => { + test('when embeddable type is MAP_SAVED_OBJECT_TYPE and triggerContext filters exist, returns true', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: MAP_SAVED_OBJECT_TYPE, + }; + if (isEmbeddable(embeddable)) { + const result = await action.isCompatible({ + embeddable, + triggerContext: { + filters: [], + }, + }); + expect(result).toBe(true); + } else { + throw new Error('Invalid embeddable in unit test'); + } + }); + + test('when embeddable type is MAP_SAVED_OBJECT_TYPE and triggerContext does not exist, returns false', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: MAP_SAVED_OBJECT_TYPE, + }; + if (isEmbeddable(embeddable)) { + const result = await action.isCompatible({ + embeddable, + }); + expect(result).toBe(false); + } else { + throw new Error('Invalid embeddable in unit test'); + } + }); + + test('when embeddable type is MAP_SAVED_OBJECT_TYPE and triggerContext filters do not exist, returns false', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: MAP_SAVED_OBJECT_TYPE, + }; + const triggerContext = {}; + if (isEmbeddable(embeddable) && isTriggerContext(triggerContext)) { + const result = await action.isCompatible({ + embeddable, + triggerContext, + }); + expect(result).toBe(false); + } else { + throw new Error('Invalid embeddable/triggerContext in unit test'); + } + }); + + test('when embeddable type is not MAP_SAVED_OBJECT_TYPE, returns false', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: 'defaultEmbeddable', + }; + if (isEmbeddable(embeddable)) { + const result = await action.isCompatible({ + embeddable, + triggerContext: { + filters: [], + }, + }); + expect(result).toBe(false); + } else { + throw new Error('Invalid embeddable in unit test'); + } + }); + }); + + describe('#execute', () => { + test('it throws an error when triggerContext not set', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: MAP_SAVED_OBJECT_TYPE, + }; + if (isEmbeddable(embeddable)) { + const error = expectError(() => + action.execute({ + embeddable, + }) + ); + expect(error).toBeInstanceOf(Error); + } else { + throw new Error('Invalid embeddable in unit test'); + } + }); + + test('it calls applyFilterQueryFromKueryExpression() with valid expression', async () => { + const action = new ApplySiemFilterAction({ applyFilterQueryFromKueryExpression }); + const embeddable = { + type: MAP_SAVED_OBJECT_TYPE, + getInput: () => ({ + query: { query: '' }, + }), + }; + const triggerContext = { + filters: [ + { + query: { + match: { + 'host.name': { + query: 'zeek-newyork-sha-aa8df15', + type: 'phrase', + }, + }, + }, + }, + ], + }; + if (isEmbeddable(embeddable) && isTriggerContext(triggerContext)) { + await action.execute({ + embeddable, + triggerContext, + }); + + expect( + (applyFilterQueryFromKueryExpression as jest.Mock<(expression: string) => void>).mock + .calls[0][0] + ).toBe('host.name: "zeek-newyork-sha-aa8df15"'); + } else { + throw new Error('Invalid embeddable/triggerContext in unit test'); + } + }); + }); +}); + +describe('#getExpressionFromArray', () => { + test('it returns an empty expression if no filterValues are provided', () => { + const layerList = getExpressionFromArray('host.id', []); + expect(layerList).toEqual(''); + }); + + test('it returns a valid expression when provided multiple filterValues', () => { + const layerList = getExpressionFromArray('host.id', ['xavier', 'angela', 'frank']); + expect(layerList).toEqual('(host.id: "xavier" OR host.id: "angela" OR host.id: "frank")'); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.tsx new file mode 100644 index 0000000000000..f25117ea3a0dc --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/actions/apply_siem_filter_action.tsx @@ -0,0 +1,78 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { Filter } from '@kbn/es-query'; +import { getOr } from 'lodash/fp'; +import { i18n } from '@kbn/i18n'; +// @ts-ignore Missing type defs as maps moves to Typescript +import { MAP_SAVED_OBJECT_TYPE } from '../../../../../maps/common/constants'; +import { + Action, + ActionContext, +} from '../../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/lib/actions'; +import { IEmbeddable } from '../../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/lib/embeddables'; + +export const APPLY_SIEM_FILTER_ACTION_ID = 'APPLY_SIEM_FILTER_ACTION_ID'; + +export class ApplySiemFilterAction extends Action { + public readonly type = APPLY_SIEM_FILTER_ACTION_ID; + private readonly applyFilterQueryFromKueryExpression: (expression: string) => void; + + constructor({ + applyFilterQueryFromKueryExpression, + }: { + applyFilterQueryFromKueryExpression: (filterQuery: string) => void; + }) { + super(APPLY_SIEM_FILTER_ACTION_ID); + this.applyFilterQueryFromKueryExpression = applyFilterQueryFromKueryExpression; + } + + public getDisplayName() { + return i18n.translate('xpack.siem.components.embeddables.actions.applySiemFilterActionTitle', { + defaultMessage: 'Apply filter', + }); + } + + public async isCompatible( + context: ActionContext + ): Promise { + return ( + context.embeddable.type === MAP_SAVED_OBJECT_TYPE && + context.triggerContext != null && + context.triggerContext.filters !== undefined + ); + } + + public execute({ + embeddable, + triggerContext, + }: ActionContext) { + if (!triggerContext) { + throw new Error('Applying a filter requires a filter as context'); + } + + // Parse queryExpression from queryDSL and apply to SIEM global KQL Bar via redux + const filterObject = getOr(null, 'filters[0].query.match', triggerContext); + + if (filterObject != null) { + const filterQuery = getOr('', 'query.query', embeddable.getInput()); + const filterKey = Object.keys(filterObject)[0]; + + const filterExpression = Array.isArray(filterObject[filterKey].query) + ? getExpressionFromArray(filterKey, filterObject[filterKey].query) + : `${filterKey}: "${filterObject[filterKey].query}"`; + + this.applyFilterQueryFromKueryExpression( + filterQuery.length > 0 ? `${filterQuery} and ${filterExpression}` : filterExpression + ); + } + } +} + +export const getExpressionFromArray = (filterKey: string, filterValues: string[]) => + filterValues.length > 0 + ? `(${filterValues.map(filterValue => `${filterKey}: "${filterValue}"`).join(' OR ')})` + : ''; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.test.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.test.tsx new file mode 100644 index 0000000000000..ba5e196e18683 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.test.tsx @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { shallow } from 'enzyme'; +import toJson from 'enzyme-to-json'; +import * as React from 'react'; +import { EmbeddedMap } from './embedded_map'; +import { inputsModel } from '../../store/inputs'; + +jest.mock('ui/new_platform', () => ({ + npStart: { + core: { + injectedMetadata: { + getKibanaVersion: () => '8.0.0', + }, + }, + }, + npSetup: { + core: { + uiSettings: { + get$: () => 'world', + }, + }, + }, +})); + +describe('EmbeddedMap', () => { + let applyFilterQueryFromKueryExpression: (expression: string) => void; + let setQuery: ({ + id, + inspect, + loading, + refetch, + }: { + id: string; + inspect: inputsModel.InspectQuery | null; + loading: boolean; + refetch: inputsModel.Refetch; + }) => void; + + beforeEach(() => { + applyFilterQueryFromKueryExpression = jest.fn(expression => {}); + setQuery = jest.fn(); + }); + + test('renders correctly against snapshot', () => { + const wrapper = shallow( + + ); + expect(toJson(wrapper)).toMatchSnapshot(); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx new file mode 100644 index 0000000000000..baa100d245267 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/embedded_map.tsx @@ -0,0 +1,223 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { EuiFlexGroup, EuiSpacer } from '@elastic/eui'; +import * as React from 'react'; +import { useEffect, useState } from 'react'; +import { npStart } from 'ui/new_platform'; +import { SavedObjectFinder } from 'ui/saved_objects/components/saved_object_finder'; +import uuid from 'uuid'; + +import styled from 'styled-components'; +import { start } from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/legacy'; +import { + APPLY_FILTER_ACTION, + APPLY_FILTER_TRIGGER, + CONTEXT_MENU_TRIGGER, + EmbeddablePanel, + PANEL_BADGE_TRIGGER, + ViewMode, +} from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; +// @ts-ignore Missing type defs as maps moves to Typescript +import { MAP_SAVED_OBJECT_TYPE } from '../../../../maps/common/constants'; +import { Loader } from '../loader'; +import { + APPLY_SIEM_FILTER_ACTION_ID, + ApplySiemFilterAction, +} from './actions/apply_siem_filter_action'; +import { useIndexPatterns } from '../ml_popover/hooks/use_index_patterns'; +import { getLayerList } from './map_config'; +import { useKibanaUiSetting } from '../../lib/settings/use_kibana_ui_setting'; +import { DEFAULT_INDEX_KEY } from '../../../common/constants'; +import { getIndexPatternTitleIdMapping } from '../ml_popover/helpers'; +import { IndexPatternsMissingPrompt } from './index_patterns_missing_prompt'; +import { + EmbeddableOutput, + IEmbeddable, +} from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/lib/embeddables'; +import { IndexPatternMapping, MapEmbeddableInput } from './types'; +import * as i18n from './translations'; +import { inputsModel } from '../../store/inputs'; + +// Used for setQuery to get a hook for when the user requests a refresh. Scope to page type if using map elsewhere +const ID = 'embeddedMap'; + +const EmbeddableWrapper = styled(EuiFlexGroup)` + position: relative; + height: 400px; + margin: 0; + + .mapToolbarOverlay__button { + display: none; + } +`; + +export interface EmbeddedMapProps { + applyFilterQueryFromKueryExpression: (expression: string) => void; + queryExpression: string; + startDate: number; + endDate: number; + setQuery: (params: { + id: string; + inspect: inputsModel.InspectQuery | null; + loading: boolean; + refetch: inputsModel.Refetch; + }) => void; +} + +export const EmbeddedMap = React.memo( + ({ applyFilterQueryFromKueryExpression, queryExpression, startDate, endDate, setQuery }) => { + const [embeddable, setEmbeddable] = React.useState | null>(null); + const [isLoading, setIsLoading] = useState(true); + const [isError, setIsError] = useState(false); + + const [loadingKibanaIndexPatterns, kibanaIndexPatterns] = useIndexPatterns(); + const [siemDefaultIndices] = useKibanaUiSetting(DEFAULT_INDEX_KEY); + + const loadEmbeddable = async (id: string, indexPatterns: IndexPatternMapping[]) => { + try { + const factory = start.getEmbeddableFactory(MAP_SAVED_OBJECT_TYPE); + + const state = { + layerList: getLayerList(indexPatterns), + title: i18n.MAP_TITLE, + }; + const input = { + id, + filters: [], + hidePanelTitles: true, + query: { query: queryExpression, language: 'kuery' }, + refreshConfig: { value: 0, pause: true }, + timeRange: { + from: new Date(startDate).toISOString(), + to: new Date(endDate).toISOString(), + }, + viewMode: ViewMode.VIEW, + isLayerTOCOpen: false, + openTOCDetails: [], + hideFilterActions: false, + mapCenter: { lon: -1.05469, lat: 15.96133, zoom: 1 }, + }; + + // @ts-ignore method added in https://github.com/elastic/kibana/pull/43878 + const embeddableObject = await factory.createFromState(state, input); + + // Wire up to app refresh action + setQuery({ + id: ID, + inspect: null, + loading: false, + refetch: embeddableObject.reload, + }); + + setEmbeddable(embeddableObject); + } catch (e) { + // TODO: Throw toast https://github.com/elastic/siem-team/issues/449 + } + }; + + /** + * Temporary Embeddables API configuration override until ability to edit actions is addressed: + * https://github.com/elastic/kibana/issues/43643 + */ + const setupEmbeddablesAPI = (): boolean => { + try { + const actions = start.getTriggerActions(APPLY_FILTER_TRIGGER); + const actionLoaded = actions.some(a => a.id === APPLY_SIEM_FILTER_ACTION_ID); + if (!actionLoaded) { + const siemFilterAction = new ApplySiemFilterAction({ + applyFilterQueryFromKueryExpression, + }); + start.registerAction(siemFilterAction); + start.attachAction(APPLY_FILTER_TRIGGER, siemFilterAction.id); + + start.detachAction(CONTEXT_MENU_TRIGGER, 'CUSTOM_TIME_RANGE'); + start.detachAction(PANEL_BADGE_TRIGGER, 'CUSTOM_TIME_RANGE_BADGE'); + start.detachAction(APPLY_FILTER_TRIGGER, APPLY_FILTER_ACTION); + } + return true; + } catch (e) { + // TODO: Throw toast https://github.com/elastic/siem-team/issues/449 + return false; + } + }; + + // Initial Load useEffect + useEffect(() => { + setIsLoading(true); + + const importIfNotExists = async () => { + const matchingIndexPatterns = kibanaIndexPatterns.filter(ip => + siemDefaultIndices.includes(ip.attributes.title) + ); + + const setupSuccessfully = setupEmbeddablesAPI(); + + // Ensure at least one `siem:defaultIndex` index pattern exists before trying to import + if (matchingIndexPatterns.length === 0 || !setupSuccessfully) { + setIsLoading(false); + setIsError(true); + return; + } + + await loadEmbeddable(uuid.v4(), getIndexPatternTitleIdMapping(matchingIndexPatterns)); + setIsLoading(false); + }; + + if (!loadingKibanaIndexPatterns && kibanaIndexPatterns.length > 0) { + importIfNotExists(); + } + }, [loadingKibanaIndexPatterns, kibanaIndexPatterns]); + + // queryExpression updated useEffect + useEffect(() => { + if (embeddable != null && queryExpression != null) { + const query = { query: queryExpression, language: 'kuery' }; + embeddable.updateInput({ query }); + } + }, [queryExpression]); + + // DateRange updated useEffect + useEffect(() => { + if (embeddable != null && startDate != null && endDate != null) { + const timeRange = { + from: new Date(startDate).toISOString(), + to: new Date(endDate).toISOString(), + }; + embeddable.updateInput({ timeRange }); + } + }, [startDate, endDate]); + + return ( + <> + + {embeddable != null ? ( + + ) : !isLoading && isError ? ( + + ) : ( + + )} + + + + ); + } +); + +EmbeddedMap.displayName = 'EmbeddedMap'; diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.test.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.test.tsx new file mode 100644 index 0000000000000..48a49835b284f --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.test.tsx @@ -0,0 +1,22 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { shallow } from 'enzyme'; +import toJson from 'enzyme-to-json'; +import * as React from 'react'; +import { IndexPatternsMissingPrompt } from './index_patterns_missing_prompt'; + +jest.mock('ui/documentation_links', () => ({ + ELASTIC_WEBSITE_URL: 'https://www.elastic.co', + DOC_LINK_VERSION: 'current', +})); + +describe('IndexPatternsMissingPrompt', () => { + test('renders correctly against snapshot', () => { + const wrapper = shallow(); + expect(toJson(wrapper)).toMatchSnapshot(); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.tsx b/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.tsx new file mode 100644 index 0000000000000..709725b853dd2 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/index_patterns_missing_prompt.tsx @@ -0,0 +1,72 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { ELASTIC_WEBSITE_URL, DOC_LINK_VERSION } from 'ui/documentation_links'; +import { EuiButton, EuiEmptyPrompt, EuiLink } from '@elastic/eui'; + +import * as React from 'react'; +import chrome from 'ui/chrome'; + +import * as i18n from './translations'; + +interface DocMapping { + beat: string; + docLink: string; +} + +export const IndexPatternsMissingPrompt = React.memo(() => { + const beatsSetupDocMapping: DocMapping[] = [ + { + beat: 'auditbeat', + docLink: `${ELASTIC_WEBSITE_URL}/guide/en/beats/auditbeat/${DOC_LINK_VERSION}/load-kibana-dashboards.html`, + }, + { + beat: 'filebeat', + docLink: `${ELASTIC_WEBSITE_URL}/guide/en/beats/filebeat/${DOC_LINK_VERSION}/load-kibana-dashboards.html`, + }, + { + beat: 'packetbeat', + docLink: `${ELASTIC_WEBSITE_URL}/guide/en/beats/packetbeat/${DOC_LINK_VERSION}/load-kibana-dashboards.html`, + }, + { + beat: 'winlogbeat', + docLink: `${ELASTIC_WEBSITE_URL}/guide/en/beats/winlogbeat/${DOC_LINK_VERSION}/load-kibana-dashboards.html`, + }, + ]; + + return ( + {i18n.ERROR_TITLE}} + titleSize="xs" + body={ + <> +

{i18n.ERROR_DESCRIPTION}

+ +

+ {beatsSetupDocMapping + .map(v => ( + + {`${v.beat}-*`} + + )) + .reduce((acc, v) => [acc, ', ', v])} +

+ + } + actions={ + + {i18n.ERROR_BUTTON} + + } + /> + ); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.test.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.test.ts new file mode 100644 index 0000000000000..bc4e6130f56f4 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.test.ts @@ -0,0 +1,59 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { getDestinationLayer, getLayerList, getLineLayer, getSourceLayer } from './map_config'; +import { + mockDestinationLayer, + mockIndexPatternIds, + mockLayerList, + mockLayerListDouble, + mockLineLayer, + mockSourceLayer, +} from './__mocks__/mock'; + +jest.mock('uuid', () => { + return { + v4: jest.fn(() => 'uuid.v4()'), + }; +}); + +describe('map_config', () => { + describe('#getLayerList', () => { + test('it returns the complete layerList with a source, destination, and line layer', () => { + const layerList = getLayerList(mockIndexPatternIds); + expect(layerList).toStrictEqual(mockLayerList); + }); + + test('it returns the complete layerList for multiple indices', () => { + const layerList = getLayerList([...mockIndexPatternIds, ...mockIndexPatternIds]); + expect(layerList).toStrictEqual(mockLayerListDouble); + }); + }); + + describe('#getSourceLayer', () => { + test('it returns a source layer', () => { + const layerList = getSourceLayer(mockIndexPatternIds[0].title, mockIndexPatternIds[0].id); + expect(layerList).toStrictEqual(mockSourceLayer); + }); + }); + + describe('#getDestinationLayer', () => { + test('it returns a destination layer', () => { + const layerList = getDestinationLayer( + mockIndexPatternIds[0].title, + mockIndexPatternIds[0].id + ); + expect(layerList).toStrictEqual(mockDestinationLayer); + }); + }); + + describe('#getLineLayer', () => { + test('it returns a line layer', () => { + const layerList = getLineLayer(mockIndexPatternIds[0].title, mockIndexPatternIds[0].id); + expect(layerList).toStrictEqual(mockLineLayer); + }); + }); +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.ts new file mode 100644 index 0000000000000..7499903ddec88 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/map_config.ts @@ -0,0 +1,178 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import uuid from 'uuid'; +import { IndexPatternMapping } from './types'; + +/** + * Returns `Source/Destination Point-to-point` Map LayerList configuration, with a source, + * destination, and line layer for each of the provided indexPatterns + * + * @param indexPatternIds array of indexPatterns' title and id + */ +export const getLayerList = (indexPatternIds: IndexPatternMapping[]) => { + return [ + { + sourceDescriptor: { type: 'EMS_TMS', isAutoSelect: true }, + id: uuid.v4(), + label: null, + minZoom: 0, + maxZoom: 24, + alpha: 1, + visible: true, + applyGlobalQuery: true, + style: { type: 'TILE', properties: {} }, + type: 'TILE', + }, + ...indexPatternIds.reduce((acc: object[], { title, id }) => { + return [ + ...acc, + getLineLayer(title, id), + getDestinationLayer(title, id), + getSourceLayer(title, id), + ]; + }, []), + ]; +}; + +/** + * Returns Document Data Source layer configuration ('source.geo.location') for the given + * indexPattern title/id + * + * @param indexPatternTitle used as layer name in LayerToC UI: "${indexPatternTitle} | Source point" + * @param indexPatternId used as layer's indexPattern to query for data + */ +export const getSourceLayer = (indexPatternTitle: string, indexPatternId: string) => ({ + sourceDescriptor: { + id: uuid.v4(), + type: 'ES_SEARCH', + geoField: 'source.geo.location', + filterByMapBounds: false, + tooltipProperties: ['host.name', 'host.ip'], + useTopHits: false, + topHitsTimeField: '@timestamp', + topHitsSize: 1, + indexPatternId, + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#3cb44b' } }, + lineColor: { type: 'STATIC', options: { color: '#FFFFFF' } }, + lineWidth: { type: 'STATIC', options: { size: 1 } }, + iconSize: { type: 'STATIC', options: { size: 6 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'arrow-es' } }, + }, + }, + id: uuid.v4(), + label: `${indexPatternTitle} | Source Point`, + minZoom: 0, + maxZoom: 24, + alpha: 0.75, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: 'source.geo.location:* and destination.geo.location:*', language: 'kuery' }, + joins: [], +}); + +/** + * Returns Document Data Source layer configuration ('destination.geo.location') for the given + * indexPattern title/id + * + * @param indexPatternTitle used as layer name in LayerToC UI: "${indexPatternTitle} | Destination point" + * @param indexPatternId used as layer's indexPattern to query for data + */ +export const getDestinationLayer = (indexPatternTitle: string, indexPatternId: string) => ({ + sourceDescriptor: { + id: uuid.v4(), + type: 'ES_SEARCH', + geoField: 'destination.geo.location', + filterByMapBounds: true, + tooltipProperties: ['host.name', 'host.ip'], + useTopHits: false, + topHitsTimeField: '@timestamp', + topHitsSize: 1, + indexPatternId, + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#e6194b' } }, + lineColor: { type: 'STATIC', options: { color: '#FFFFFF' } }, + lineWidth: { type: 'STATIC', options: { size: 1 } }, + iconSize: { type: 'STATIC', options: { size: 6 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'airfield' } }, + }, + }, + id: uuid.v4(), + label: `${indexPatternTitle} | Destination Point`, + minZoom: 0, + maxZoom: 24, + alpha: 0.75, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: 'source.geo.location:* and destination.geo.location:*', language: 'kuery' }, +}); + +/** + * Returns Point-to-point Data Source layer configuration ('source.geo.location' & + * 'source.geo.location') for the given indexPattern title/id + * + * @param indexPatternTitle used as layer name in LayerToC UI: "${indexPatternTitle} | Line" + * @param indexPatternId used as layer's indexPattern to query for data + */ +export const getLineLayer = (indexPatternTitle: string, indexPatternId: string) => ({ + sourceDescriptor: { + type: 'ES_PEW_PEW', + id: uuid.v4(), + indexPatternId, + sourceGeoField: 'source.geo.location', + destGeoField: 'destination.geo.location', + metrics: [ + { type: 'sum', field: 'source.bytes', label: 'Total Src Bytes' }, + { type: 'sum', field: 'destination.bytes', label: 'Total Dest Bytes' }, + { type: 'count', label: 'Total Documents' }, + ], + }, + style: { + type: 'VECTOR', + properties: { + fillColor: { type: 'STATIC', options: { color: '#e6194b' } }, + lineColor: { + type: 'DYNAMIC', + options: { + color: 'Green to Red', + field: { label: 'count', name: 'doc_count', origin: 'source' }, + useCustomColorRamp: false, + }, + }, + lineWidth: { + type: 'DYNAMIC', + options: { + minSize: 1, + maxSize: 4, + field: { label: 'count', name: 'doc_count', origin: 'source' }, + }, + }, + iconSize: { type: 'STATIC', options: { size: 10 } }, + iconOrientation: { type: 'STATIC', options: { orientation: 0 } }, + symbol: { options: { symbolizeAs: 'circle', symbolId: 'airfield' } }, + }, + }, + id: uuid.v4(), + label: `${indexPatternTitle} | Line`, + minZoom: 0, + maxZoom: 24, + alpha: 1, + visible: true, + applyGlobalQuery: true, + type: 'VECTOR', + query: { query: '', language: 'kuery' }, +}); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/translations.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/translations.ts new file mode 100644 index 0000000000000..df84b68e203cb --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/translations.ts @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { i18n } from '@kbn/i18n'; + +export const MAP_TITLE = i18n.translate( + 'xpack.siem.components.embeddables.maps.embeddablePanelTitle', + { + defaultMessage: 'Source -> Destination Point-to-Point Map', + } +); + +export const ERROR_TITLE = i18n.translate( + 'xpack.siem.components.embeddables.indexPatternsMissingPrompt.errorTitle', + { + defaultMessage: 'Required Index Patterns Not Configured', + } +); + +export const ERROR_DESCRIPTION = i18n.translate( + 'xpack.siem.components.embeddables.indexPatternsMissingPrompt.errorDescription', + { + defaultMessage: + 'An ECS compliant Kibana index pattern must be configured to view event data on the map. When using beats, you can run the following setup commands to create the required Kibana index patterns, otherwise you can configure them manually within Kibana settings.', + } +); + +export const ERROR_BUTTON = i18n.translate( + 'xpack.siem.components.embeddables.indexPatternsMissingPrompt.errorButtonLabel', + { + defaultMessage: 'Configure index patterns', + } +); diff --git a/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts new file mode 100644 index 0000000000000..62f99688f9f8b --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/components/embeddables/types.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { Filter as ESFilterType } from '@kbn/es-query'; +import { TimeRange } from 'ui/timefilter'; +import { EmbeddableInput } from '../../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; + +export interface MapEmbeddableInput extends EmbeddableInput { + filters: ESFilterType[]; + query: { + query: string; + language: string; + }; + refreshConfig: { + isPaused: boolean; + interval: number; + }; + timeRange?: TimeRange; +} + +export interface IndexPatternMapping { + title: string; + id: string; +} diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/__mocks__/api.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/__mocks__/api.tsx index f6c80a85df9f2..09e8502f33069 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/__mocks__/api.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/__mocks__/api.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Group, Job } from '../types'; +import { Group, IndexPatternSavedObject, Job } from '../types'; export const mockGroupsResponse: Group[] = [ { @@ -119,3 +119,24 @@ export const mockEmbeddedJobIds = [ 'siem-api-suspicious_login_activity_ecs', 'siem-api-rare_process_windows_ecs', ]; + +export const mockIndexPatternSavedObjects: IndexPatternSavedObject[] = [ + { + type: 'index-pattern', + id: '2d1fe420-eeee-11e9-ad95-4b5e687c2aee', + attributes: { + title: 'filebeat-*', + }, + updated_at: '2019-08-26T04:30:09.111Z', + version: 'WzE4LLwxXQ==', + }, + { + type: 'index-pattern', + id: '5463ec70-c7ba-ffff-ad95-4b5e687c2aee', + attributes: { + title: 'auditbeat-*', + }, + updated_at: '2019-08-26T04:31:12.934Z', + version: 'WzELLywxXQ==', + }, +]; diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx index 06cf323192d49..6f77bf45736ab 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/api.tsx @@ -9,6 +9,7 @@ import { CloseJobsResponse, Group, IndexPatternResponse, + IndexPatternSavedObject, Job, MlSetupArgs, SetupMlResponse, @@ -23,7 +24,7 @@ import { import { useKibanaUiSetting } from '../../lib/settings/use_kibana_ui_setting'; import { DEFAULT_KBN_VERSION } from '../../../common/constants'; -const emptyIndexPattern: string[] = []; +const emptyIndexPattern: IndexPatternSavedObject[] = []; /** * Fetches ML Groups Data @@ -198,12 +199,12 @@ export const jobsSummary = async ( /** * Fetches Configured Index Patterns from the Kibana saved objects API (as ML does during create job flow) - * + * TODO: Used by more than just ML now -- refactor to shared component https://github.com/elastic/siem-team/issues/448 * @param headers */ export const getIndexPatterns = async ( headers: Record -): Promise => { +): Promise => { const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION); const response = await fetch( `${chrome.getBasePath()}/api/saved_objects/_find?type=index-pattern&fields=title&fields=type&per_page=10000`, @@ -222,13 +223,7 @@ export const getIndexPatterns = async ( const results: IndexPatternResponse = await response.json(); if (results.saved_objects && Array.isArray(results.saved_objects)) { - return results.saved_objects.reduce( - (acc: string[], v) => [ - ...acc, - ...(v.attributes && v.attributes.title ? [v.attributes.title] : []), - ], - [] - ); + return results.saved_objects; } else { return emptyIndexPattern; } diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.test.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.test.tsx index 84bb6dea972f9..05e8a8779e4fd 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.test.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.test.tsx @@ -7,11 +7,14 @@ import { mockConfigTemplates, mockEmbeddedJobIds, + mockIndexPatternSavedObjects, mockInstalledJobIds, mockJobsSummaryResponse, } from './__mocks__/api'; import { getConfigTemplatesToInstall, + getIndexPatternTitleIdMapping, + getIndexPatternTitles, getJobsToDisplay, getJobsToInstall, searchFilter, @@ -120,4 +123,33 @@ describe('helpers', () => { expect(jobsToDisplay.length).toEqual(1); }); }); + + describe('getIndexPatternTitles', () => { + test('returns empty array when no index patterns are provided', () => { + const indexPatternTitles = getIndexPatternTitles([]); + expect(indexPatternTitles.length).toEqual(0); + }); + + test('returns titles when index patterns are provided', () => { + const indexPatternTitles = getIndexPatternTitles(mockIndexPatternSavedObjects); + expect(indexPatternTitles.length).toEqual(2); + }); + }); + + describe('getIndexPatternTitleIdMapping', () => { + test('returns empty array when no index patterns are provided', () => { + const indexPatternTitleIdMapping = getIndexPatternTitleIdMapping([]); + expect(indexPatternTitleIdMapping.length).toEqual(0); + }); + + test('returns correct mapping when index patterns are provided', () => { + const indexPatternTitleIdMapping = getIndexPatternTitleIdMapping( + mockIndexPatternSavedObjects + ); + expect(indexPatternTitleIdMapping).toEqual([ + { id: '2d1fe420-eeee-11e9-ad95-4b5e687c2aee', title: 'filebeat-*' }, + { id: '5463ec70-c7ba-ffff-ad95-4b5e687c2aee', title: 'auditbeat-*' }, + ]); + }); + }); }); diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.tsx index bdc311d235fd5..c17763bc60917 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/helpers.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { ConfigTemplate, Job } from './types'; +import { ConfigTemplate, IndexPatternSavedObject, Job } from './types'; /** * Returns all `jobIds` for each configTemplate provided @@ -67,3 +67,27 @@ export const searchFilter = (jobs: Job[], filterQuery?: string): Job[] => ? true : job.id.includes(filterQuery) || job.description.includes(filterQuery) ); + +/** + * Returns a string array of Index Pattern Titles + * + * @param indexPatterns IndexPatternSavedObject[] as provided from the useIndexPatterns() hook + */ +export const getIndexPatternTitles = (indexPatterns: IndexPatternSavedObject[]): string[] => + indexPatterns.reduce((acc: string[], v) => [...acc, v.attributes.title], []); + +/** + * Returns a mapping of indexPatternTitle to indexPatternId + * + * @param indexPatterns IndexPatternSavedObject[] as provided from the useIndexPatterns() hook + */ +export const getIndexPatternTitleIdMapping = ( + indexPatterns: IndexPatternSavedObject[] +): Array<{ title: string; id: string }> => + indexPatterns.reduce((acc: Array<{ title: string; id: string }>, v) => { + if (v.attributes && v.attributes.title) { + return [...acc, { title: v.attributes.title, id: v.id }]; + } else { + return acc; + } + }, []); diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/hooks/use_index_patterns.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/hooks/use_index_patterns.tsx index ebb35c525c474..941b64b4dcc08 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/hooks/use_index_patterns.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/hooks/use_index_patterns.tsx @@ -13,11 +13,14 @@ import { useKibanaUiSetting } from '../../../lib/settings/use_kibana_ui_setting' import { DEFAULT_KBN_VERSION } from '../../../../common/constants'; import * as i18n from './translations'; +import { IndexPatternSavedObject } from '../types'; -type Return = [boolean, string[]]; +type Return = [boolean, IndexPatternSavedObject[]]; + +// TODO: Used by more than just ML now -- refactor to shared component https://github.com/elastic/siem-team/issues/448 export const useIndexPatterns = (refreshToggle = false): Return => { - const [indexPatterns, setIndexPatterns] = useState([]); + const [indexPatterns, setIndexPatterns] = useState([]); const [isLoading, setIsLoading] = useState(true); const [, dispatchToaster] = useStateToaster(); const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION); diff --git a/x-pack/legacy/plugins/siem/public/components/ml_popover/ml_popover.tsx b/x-pack/legacy/plugins/siem/public/components/ml_popover/ml_popover.tsx index f07ee26c1cf5d..80d35d981c7e7 100644 --- a/x-pack/legacy/plugins/siem/public/components/ml_popover/ml_popover.tsx +++ b/x-pack/legacy/plugins/siem/public/components/ml_popover/ml_popover.tsx @@ -21,7 +21,12 @@ import { UpgradeContents } from './upgrade_contents'; import { FilterGroup } from './jobs_table/filter_group'; import { ShowingCount } from './jobs_table/showing_count'; import { PopoverDescription } from './popover_description'; -import { getConfigTemplatesToInstall, getJobsToDisplay, getJobsToInstall } from './helpers'; +import { + getConfigTemplatesToInstall, + getIndexPatternTitles, + getJobsToDisplay, + getJobsToInstall, +} from './helpers'; import { configTemplates, siemJobPrefix } from './config_templates'; import { useStateToaster } from '../toasters'; import { errorToToaster } from '../ml/api/error_to_toaster'; @@ -100,6 +105,8 @@ export const MlPopover = React.memo(() => { const [kbnVersion] = useKibanaUiSetting(DEFAULT_KBN_VERSION); const headers = { 'kbn-version': kbnVersion }; + const configuredIndexPatternTitles = getIndexPatternTitles(configuredIndexPatterns); + // Enable/Disable Job & Datafeed -- passed to JobsTable for use as callback on JobSwitch const enableDatafeed = async (jobName: string, latestTimestampMs: number, enable: boolean) => { // Max start time for job is no more than two weeks ago to ensure job performance @@ -136,7 +143,7 @@ export const MlPopover = React.memo(() => { const configTemplatesToInstall = getConfigTemplatesToInstall( configTemplates, installedJobIds, - configuredIndexPatterns || [] + configuredIndexPatternTitles || [] ); // Filter installed job to show all 'siem' group jobs or just embedded @@ -152,7 +159,7 @@ export const MlPopover = React.memo(() => { useEffect(() => { if ( jobSummaryData != null && - configuredIndexPatterns.length > 0 && + configuredIndexPatternTitles.length > 0 && configTemplatesToInstall.length > 0 ) { const setupJobs = async () => { @@ -178,7 +185,7 @@ export const MlPopover = React.memo(() => { }; setupJobs(); } - }, [jobSummaryData, configuredIndexPatterns]); + }, [jobSummaryData, configuredIndexPatternTitles]); if (!capabilities.isPlatinumOrTrialLicense) { // If the user does not have platinum show upgrade UI diff --git a/x-pack/legacy/plugins/siem/public/pages/network/network.tsx b/x-pack/legacy/plugins/siem/public/pages/network/network.tsx index af4b53525e1e0..28f395e5cf303 100644 --- a/x-pack/legacy/plugins/siem/public/pages/network/network.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/network/network.tsx @@ -33,12 +33,15 @@ import { AnomaliesNetworkTable } from '../../components/ml/tables/anomalies_netw import { scoreIntervalToDateTime } from '../../components/ml/score/score_interval_to_datetime'; import { setAbsoluteRangeDatePicker as dispatchSetAbsoluteRangeDatePicker } from '../../store/inputs/actions'; import { InputsModelId } from '../../store/inputs/constants'; +import { EmbeddedMap } from '../../components/embeddables/embedded_map'; +import { NetworkFilter } from '../../containers/network'; const NetworkTopNFlowTableManage = manageQuery(NetworkTopNFlowTable); const NetworkDnsTableManage = manageQuery(NetworkDnsTable); const KpiNetworkComponentManage = manageQuery(KpiNetworkComponent); interface NetworkComponentReduxProps { filterQuery: string; + queryExpression: string; setAbsoluteRangeDatePicker: ActionCreator<{ id: InputsModelId; from: number; @@ -70,7 +73,7 @@ export const getFlexDirection = () => { }; const NetworkComponent = React.memo( - ({ filterQuery, setAbsoluteRangeDatePicker }) => { + ({ filterQuery, queryExpression, setAbsoluteRangeDatePicker }) => { return ( {({ indicesExist, indexPattern }) => @@ -88,6 +91,17 @@ const NetworkComponent = React.memo( {({ to, from, setQuery, isInitializing }) => ( <> + + {({ applyFilterQueryFromKueryExpression }) => ( + + )} + { const getNetworkFilterQueryAsJson = networkSelectors.networkFilterQueryAsJson(); + const getNetworkFilterExpression = networkSelectors.networkFilterExpression(); const mapStateToProps = (state: State) => ({ filterQuery: getNetworkFilterQueryAsJson(state, networkModel.NetworkType.page) || '', + queryExpression: getNetworkFilterExpression(state, networkModel.NetworkType.page) || '', }); return mapStateToProps; }; diff --git a/x-pack/legacy/plugins/siem/public/store/network/selectors.ts b/x-pack/legacy/plugins/siem/public/store/network/selectors.ts index f600e03f45447..3a11949b3990b 100644 --- a/x-pack/legacy/plugins/siem/public/store/network/selectors.ts +++ b/x-pack/legacy/plugins/siem/public/store/network/selectors.ts @@ -47,6 +47,13 @@ export const networkFilterQueryAsJson = () => network => (network.filterQuery ? network.filterQuery.serializedQuery : null) ); +export const networkFilterExpression = () => + createSelector( + selectNetworkByType, + network => + network.filterQuery && network.filterQuery.kuery ? network.filterQuery.kuery.expression : null + ); + export const networkFilterQueryAsKuery = () => createSelector( selectNetworkByType,