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

[SIEM] Fixes Host Details Events Table to only show events for specified Host #57388

Merged
merged 2 commits into from
Feb 12, 2020
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 @@ -11,10 +11,11 @@ import { MemoryRouter } from 'react-router-dom';
import { mockIndexPattern } from '../../../mock/index_pattern';
import { TestProviders } from '../../../mock/test_providers';
import { HostDetailsTabs } from './details_tabs';
import { SetAbsoluteRangeDatePicker } from './types';
import { HostDetailsTabsProps, SetAbsoluteRangeDatePicker } from './types';
import { hostDetailsPagePath } from '../types';
import { type } from './utils';
import { useMountAppended } from '../../../utils/use_mount_appended';
import { getHostDetailsPageFilters } from './helpers';

jest.mock('../../../containers/source', () => ({
indicesExistOrDataTemporarilyUnavailable: () => true,
Expand All @@ -41,6 +42,23 @@ describe('body', () => {
uncommonProcesses: 'UncommonProcessQueryTabBody',
anomalies: 'AnomaliesQueryTabBody',
events: 'EventsQueryTabBody',
alerts: 'HostAlertsQueryTabBody',
};

const mockHostDetailsPageFilters = getHostDetailsPageFilters('host-1');

const filterQuery = JSON.stringify({
bool: {
must: [],
filter: [{ match_all: {} }, { match_phrase: { 'host.name': { query: 'host-1' } } }],
should: [],
must_not: [],
},
});

const componentProps: Record<string, Partial<HostDetailsTabsProps>> = {
events: { pageFilters: mockHostDetailsPageFilters },
alerts: { pageFilters: mockHostDetailsPageFilters },
};
const mount = useMountAppended();

Expand All @@ -59,7 +77,8 @@ describe('body', () => {
hostDetailsPagePath={hostDetailsPagePath}
indexPattern={mockIndexPattern}
type={type}
filterQuery='{"bool":{"must":[],"filter":[{"match_all":{}},{"match_phrase":{"host.name":{"query":"host-1"}}}],"should":[],"must_not":[]}}'
pageFilters={mockHostDetailsPageFilters}
filterQuery={filterQuery}
/>
</MemoryRouter>
</TestProviders>
Expand All @@ -68,8 +87,7 @@ describe('body', () => {
// match against everything but the functions to ensure they are there as expected
expect(wrapper.find(componentName).props()).toMatchObject({
endDate: 0,
filterQuery:
'{"bool":{"must":[],"filter":[{"match_all":{}},{"match_phrase":{"host.name":{"query":"host-1"}}}],"should":[],"must_not":[]}}',
filterQuery,
skip: false,
startDate: 0,
type: 'details',
Expand All @@ -93,6 +111,7 @@ describe('body', () => {
title: 'filebeat-*,auditbeat-*,packetbeat-*',
},
hostName: 'host-1',
...(componentProps[path] != null ? componentProps[path] : []),
});
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const HostDetailsTabs = React.memo<HostDetailsTabsProps>(
/>
<Route
path={`${hostDetailsPagePath}/:tabName(${HostsTableType.events})`}
render={() => <EventsQueryTabBody {...tabProps} />}
render={() => <EventsQueryTabBody {...tabProps} pageFilters={pageFilters} />}
/>
<Route
path={`${hostDetailsPagePath}/:tabName(${HostsTableType.alerts})`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getHostDetailsEventsKqlQueryExpression } from './helpers';
import { getHostDetailsEventsKqlQueryExpression, getHostDetailsPageFilters } from './helpers';
import { esFilters } from '../../../../../../../../src/plugins/data/common/es_query';

describe('hosts page helpers', () => {
describe('getHostDetailsEventsKqlQueryExpression', () => {
Expand Down Expand Up @@ -35,4 +36,33 @@ describe('hosts page helpers', () => {
).toEqual('');
});
});

describe('getHostDetailsPageFilters', () => {
it('correctly constructs pageFilters for the given hostName', () => {
const expected: esFilters.Filter[] = [
{
meta: {
alias: null,
negate: false,
disabled: false,
type: 'phrase',
key: 'host.name',
value: 'host-1',
params: {
query: 'host-1',
},
},
query: {
match: {
'host.name': {
query: 'host-1',
type: 'phrase',
},
},
},
},
];
expect(getHostDetailsPageFilters('host-1')).toEqual(expected);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Thanks for the test!

Copy link
Contributor

Choose a reason for hiding this comment

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

Optionally you can do this trick:

    it('correctly constructs pageFilters for the given hostName', () => {
      const expected: esFilters.Filter[] = [
        {
          meta: {
            alias: null,
            negate: false,
            disabled: false,
            type: 'phrase',
            key: 'host.name',
            value: 'host-1',
            params: {
              query: 'host-1',
            },
          },
          query: {
            match: {
              'host.name': {
                query: 'host-1',
                type: 'phrase',
              },
            },
          },
        },
      ];
      expect(getHostDetailsPageFilters('host-1')).toEqual(expected);

And if that type ever changes your type script will blow up before the test does and ensure that your expected type here always matches your filter type.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooh nice little improvement there -- thanks @FrankHassanabad! I've made the change and will take this little tidbit with me going forward 🙂

});
25 changes: 25 additions & 0 deletions x-pack/legacy/plugins/siem/public/pages/hosts/details/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { escapeQueryValue } from '../../../lib/keury';
import { esFilters } from '../../../../../../../../src/plugins/data/common/es_query';

/** Returns the kqlQueryExpression for the `Events` widget on the `Host Details` page */
export const getHostDetailsEventsKqlQueryExpression = ({
Expand All @@ -22,3 +23,27 @@ export const getHostDetailsEventsKqlQueryExpression = ({
return hostName.length ? `host.name: ${escapeQueryValue(hostName)}` : '';
}
};

export const getHostDetailsPageFilters = (hostName: string): esFilters.Filter[] => [
{
meta: {
alias: null,
negate: false,
disabled: false,
type: 'phrase',
key: 'host.name',
value: hostName,
params: {
query: hostName,
},
},
query: {
match: {
'host.name': {
query: hostName,
type: 'phrase',
},
},
},
},
];
30 changes: 6 additions & 24 deletions x-pack/legacy/plugins/siem/public/pages/hosts/details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { EuiHorizontalRule, EuiSpacer } from '@elastic/eui';
import React, { useContext, useEffect, useCallback } from 'react';
import React, { useContext, useEffect, useCallback, useMemo } from 'react';
import { connect } from 'react-redux';
import { StickyContainer } from 'react-sticky';
import { compose } from 'redux';
Expand Down Expand Up @@ -41,6 +41,7 @@ import { HostDetailsTabs } from './details_tabs';
import { navTabsHostDetails } from './nav_tabs';
import { HostDetailsComponentProps, HostDetailsProps } from './types';
import { type } from './utils';
import { getHostDetailsPageFilters } from './helpers';

const HostOverviewManage = manageQuery(HostOverview);
const KpiHostDetailsManage = manageQuery(KpiHostsComponent);
Expand All @@ -64,29 +65,10 @@ const HostDetailsComponent = React.memo<HostDetailsComponentProps>(
}, [setHostDetailsTablesActivePageToZero, detailName]);
const capabilities = useContext(MlCapabilitiesContext);
const kibana = useKibana();
const hostDetailsPageFilters: esFilters.Filter[] = [
{
meta: {
alias: null,
negate: false,
disabled: false,
type: 'phrase',
key: 'host.name',
value: detailName,
params: {
query: detailName,
},
},
query: {
match: {
'host.name': {
query: detailName,
type: 'phrase',
},
},
},
},
];
const hostDetailsPageFilters: esFilters.Filter[] = useMemo(
() => getHostDetailsPageFilters(detailName),
[detailName]
);
const getFilters = () => [...hostDetailsPageFilters, ...filters];
const narrowDateRange = useCallback(
(min: number, max: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const EventsQueryTabBody = ({
deleteQuery,
endDate,
filterQuery,
pageFilters,
setQuery,
skip,
startDate,
Expand Down Expand Up @@ -73,6 +74,7 @@ export const EventsQueryTabBody = ({
end={endDate}
id={HOSTS_PAGE_TIMELINE_ID}
start={startDate}
pageFilters={pageFilters}
/>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface QueryTabBodyProps {
export type HostsComponentsQueryProps = QueryTabBodyProps & {
deleteQuery?: ({ id }: { id: string }) => void;
indexPattern: IIndexPattern;
pageFilters?: esFilters.Filter[];
skip: boolean;
setQuery: SetQuery;
updateDateRange?: UpdateDateRange;
Expand Down