Make host card overview space aware (#113983)

* Make host card overview space aware

* Add cypress test

* Move getHostRiskIndex to helpers

* Fix cypress test

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Khristinin Nikita 2021-10-10 20:45:30 +02:00 committed by GitHub
parent 5285858252
commit 30aeb8106c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 88 additions and 18 deletions

View file

@ -334,7 +334,7 @@ export const ELASTIC_NAME = 'estc';
export const METADATA_TRANSFORM_STATS_URL = `/api/transform/transforms/${METADATA_TRANSFORMS_PATTERN}/_stats`;
export const HOST_RISK_SCORES_INDEX = 'ml_host_risk_score_latest';
export const RISKY_HOSTS_INDEX_PREFIX = 'ml_host_risk_score_latest_';
export const TRANSFORM_STATES = {
ABORTING: 'aborting',

View file

@ -17,8 +17,12 @@ import {
import { loginAndWaitForPage } from '../../tasks/login';
import { OVERVIEW_URL } from '../../urls/navigation';
import { cleanKibana } from '../../tasks/common';
import { changeSpace } from '../../tasks/kibana_navigation';
import { createSpace, removeSpace } from '../../tasks/api_calls/spaces';
import { esArchiverLoad, esArchiverUnload } from '../../tasks/es_archiver';
const testSpaceName = 'test';
describe('Risky Hosts Link Panel', () => {
before(() => {
cleanKibana();
@ -40,10 +44,12 @@ describe('Risky Hosts Link Panel', () => {
describe('enabled module', () => {
before(() => {
esArchiverLoad('risky_hosts');
createSpace(testSpaceName);
});
after(() => {
esArchiverUnload('risky_hosts');
removeSpace(testSpaceName);
});
it('renders disabled dashboard module as expected when there are no hosts in the selected time period', () => {
@ -57,13 +63,19 @@ describe('Risky Hosts Link Panel', () => {
cy.get(`${OVERVIEW_RISKY_HOSTS_TOTAL_EVENT_COUNT}`).should('have.text', 'Showing: 0 hosts');
});
it('renders dashboard module as expected when there are hosts in the selected time period', () => {
it('renders space aware dashboard module as expected when there are hosts in the selected time period', () => {
loginAndWaitForPage(OVERVIEW_URL);
cy.get(
`${OVERVIEW_RISKY_HOSTS_LINKS} ${OVERVIEW_RISKY_HOSTS_LINKS_WARNING_INNER_PANEL}`
).should('not.exist');
cy.get(`${OVERVIEW_RISKY_HOSTS_VIEW_DASHBOARD_BUTTON}`).should('be.disabled');
cy.get(`${OVERVIEW_RISKY_HOSTS_TOTAL_EVENT_COUNT}`).should('have.text', 'Showing: 1 host');
changeSpace(testSpaceName);
cy.visit(`/s/${testSpaceName}${OVERVIEW_URL}`);
cy.get(`${OVERVIEW_RISKY_HOSTS_VIEW_DASHBOARD_BUTTON}`).should('be.disabled');
cy.get(`${OVERVIEW_RISKY_HOSTS_TOTAL_EVENT_COUNT}`).should('have.text', 'Showing: 0 hosts');
cy.get(`${OVERVIEW_RISKY_HOSTS_ENABLE_MODULE_BUTTON}`).should('exist');
});
});
});

View file

@ -25,3 +25,7 @@ export const OVERVIEW_PAGE =
export const TIMELINES_PAGE =
'[data-test-subj="collapsibleNavGroup-securitySolution"] [title="Timelines"]';
export const SPACES_BUTTON = '[data-test-subj="spacesNavSelector"]';
export const getGoToSpaceMenuItem = (space: string) => `[data-test-subj="${space}-gotoSpace"]`;

View file

@ -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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
export const createSpace = (id: string) => {
cy.request({
method: 'POST',
url: 'api/spaces/space',
body: {
id,
name: id,
},
headers: { 'kbn-xsrf': 'cypress-creds' },
});
};
export const removeSpace = (id: string) => {
cy.request(`/api/spaces/space/${id}`);
};

View file

@ -5,7 +5,11 @@
* 2.0.
*/
import { KIBANA_NAVIGATION_TOGGLE } from '../screens/kibana_navigation';
import {
KIBANA_NAVIGATION_TOGGLE,
SPACES_BUTTON,
getGoToSpaceMenuItem,
} from '../screens/kibana_navigation';
export const navigateFromKibanaCollapsibleTo = (page: string) => {
cy.get(page).click();
@ -14,3 +18,9 @@ export const navigateFromKibanaCollapsibleTo = (page: string) => {
export const openKibanaNavigation = () => {
cy.get(KIBANA_NAVIGATION_TOGGLE).click();
};
export const changeSpace = (space: string) => {
cy.get(`${SPACES_BUTTON}`).click();
cy.get(getGoToSpaceMenuItem(space)).click();
cy.get(`[data-test-subj="space-avatar-${space}"]`).should('exist');
};

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { parseRoute } from './helpers';
import { parseRoute, getHostRiskIndex } from './helpers';
describe('public helpers parseRoute', () => {
it('should properly parse hash route', () => {
@ -54,3 +54,9 @@ describe('public helpers parseRoute', () => {
});
});
});
describe('public helpers export getHostRiskIndex', () => {
it('should properly return index if space is specified', () => {
expect(getHostRiskIndex('testName')).toEqual('ml_host_risk_score_latest_testName');
});
});

View file

@ -9,7 +9,14 @@ import { isEmpty } from 'lodash/fp';
import { matchPath } from 'react-router-dom';
import { CoreStart } from '../../../../src/core/public';
import { ALERTS_PATH, APP_ID, EXCEPTIONS_PATH, RULES_PATH, UEBA_PATH } from '../common/constants';
import {
ALERTS_PATH,
APP_ID,
EXCEPTIONS_PATH,
RULES_PATH,
UEBA_PATH,
RISKY_HOSTS_INDEX_PREFIX,
} from '../common/constants';
import {
FactoryQueryTypes,
StrategyResponseType,
@ -147,3 +154,7 @@ export const isDetectionsPath = (pathname: string): boolean => {
strict: false,
});
};
export const getHostRiskIndex = (spaceId: string): string => {
return `${RISKY_HOSTS_INDEX_PREFIX}${spaceId}`;
};

View file

@ -12,12 +12,11 @@ import { useDispatch } from 'react-redux';
import { useAppToasts } from '../../../common/hooks/use_app_toasts';
import { useKibana } from '../../../common/lib/kibana';
import { inputsActions } from '../../../common/store/actions';
import { HOST_RISK_SCORES_INDEX } from '../../../../common/constants';
import { isIndexNotFoundError } from '../../../common/utils/exceptions';
import { HostsRiskScore } from '../../../../common';
import { useHostsRiskScoreComplete } from './use_hosts_risk_score_complete';
import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features';
import { getHostRiskIndex } from '../../../helpers';
export const QUERY_ID = 'host_risk_score';
const noop = () => {};
@ -50,7 +49,7 @@ export const useHostsRiskScore = ({
const [loading, setLoading] = useState<boolean>(riskyHostsFeatureEnabled);
const { addError } = useAppToasts();
const { data } = useKibana().services;
const { data, spaces } = useKibana().services;
const dispatch = useDispatch();
@ -99,14 +98,18 @@ export const useHostsRiskScore = ({
useEffect(() => {
if (riskyHostsFeatureEnabled && (hostName || timerange)) {
start({
data,
timerange: timerange ? { to: timerange.to, from: timerange.from, interval: '' } : undefined,
hostName,
defaultIndex: [HOST_RISK_SCORES_INDEX],
spaces.getActiveSpace().then((space) => {
start({
data,
timerange: timerange
? { to: timerange.to, from: timerange.from, interval: '' }
: undefined,
hostName,
defaultIndex: [getHostRiskIndex(space.id)],
});
});
}
}, [start, data, timerange, hostName, riskyHostsFeatureEnabled]);
}, [start, data, timerange, hostName, riskyHostsFeatureEnabled, spaces]);
if ((!hostName && !timerange) || !riskyHostsFeatureEnabled) {
return null;

View file

@ -9,6 +9,7 @@ import { CoreStart } from '../../../../src/core/public';
import { HomePublicPluginSetup } from '../../../../src/plugins/home/public';
import { DataPublicPluginStart } from '../../../../src/plugins/data/public';
import { EmbeddableStart } from '../../../../src/plugins/embeddable/public';
import { SpacesPluginStart } from '../../../plugins/spaces/public';
import { LensPublicStart } from '../../../plugins/lens/public';
import { NewsfeedPublicPluginStart } from '../../../../src/plugins/newsfeed/public';
import { Start as InspectorStart } from '../../../../src/plugins/inspector/public';
@ -67,6 +68,7 @@ export interface StartPlugins {
timelines: TimelinesUIStart;
uiActions: UiActionsStart;
ml?: MlPluginStart;
spaces: SpacesPluginStart;
}
export type StartServices = CoreStart &

View file

@ -2,7 +2,7 @@
"type":"doc",
"value":{
"id":"a4cf452c1e0375c3d4412cb550bd1783358468a3b3b777da4829d72c7d6fb74f",
"index":"ml_host_risk_score_latest",
"index":"ml_host_risk_score_latest_default",
"source":{
"@timestamp":"2021-03-10T14:51:05.766Z",
"risk_score":21,

View file

@ -1,7 +1,7 @@
{
"type": "index",
"value": {
"index": "ml_host_risk_score_latest",
"index": "ml_host_risk_score_latest_default",
"mappings": {
"properties": {
"@timestamp": {
@ -40,8 +40,8 @@
"settings": {
"index": {
"lifecycle": {
"name": "ml_host_risk_score_latest",
"rollover_alias": "ml_host_risk_score_latest"
"name": "ml_host_risk_score_latest_default",
"rollover_alias": "ml_host_risk_score_latest_default"
},
"mapping": {
"total_fields": {