Skip to content

Commit

Permalink
[SECURITY_SOLUTION][ENDPOINT] Fix Endpoint Host page sometimes incorr…
Browse files Browse the repository at this point in the history
…ectly showing onboarding UI (#73222) (#73841)

* Fix incorrectly showing the onboarding message on hosts
* Allow loading of the Details even if list load failed
* refactor UT for the policy list view
* Refactor UT for host list
  • Loading branch information
paul-tavares authored Jul 30, 2020
1 parent ab103ff commit fcf0b63
Show file tree
Hide file tree
Showing 18 changed files with 526 additions and 266 deletions.
105 changes: 103 additions & 2 deletions x-pack/plugins/security_solution/common/endpoint/generate_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ import seedrandom from 'seedrandom';
import {
AlertEvent,
EndpointEvent,
EndpointStatus,
Host,
HostMetadata,
OSFields,
HostPolicyResponse,
HostPolicyResponseActionStatus,
OSFields,
PolicyData,
EndpointStatus,
} from './types';
import { factory as policyFactory } from './models/policy_config';
import { parentEntityId } from './models/event';
import {
GetAgentConfigsResponseItem,
GetPackagesResponse,
} from '../../../ingest_manager/common/types/rest_spec';
import {
AgentConfigStatus,
EsAssetReference,
InstallationStatus,
KibanaAssetReference,
} from '../../../ingest_manager/common/types/models';

export type Event = AlertEvent | EndpointEvent;
/**
Expand Down Expand Up @@ -1062,6 +1072,97 @@ export class EndpointDocGenerator {
};
}

/**
* Generate an Agent Configuration (ingest)
*/
public generateAgentConfig(): GetAgentConfigsResponseItem {
return {
id: this.seededUUIDv4(),
name: 'Agent Config',
status: AgentConfigStatus.Active,
description: 'Some description',
namespace: 'default',
monitoring_enabled: ['logs', 'metrics'],
revision: 2,
updated_at: '2020-07-22T16:36:49.196Z',
updated_by: 'elastic',
package_configs: ['852491f0-cc39-11ea-bac2-cdbf95b4b41a'],
agents: 0,
};
}

/**
* Generate an EPM Package for Endpoint
*/
public generateEpmPackage(): GetPackagesResponse['response'][0] {
return {
name: 'endpoint',
title: 'Elastic Endpoint',
version: '0.5.0',
description: 'This is the Elastic Endpoint package.',
type: 'solution',
download: '/epr/endpoint/endpoint-0.5.0.tar.gz',
path: '/package/endpoint/0.5.0',
icons: [
{
src: '/package/endpoint/0.5.0/img/logo-endpoint-64-color.svg',
size: '16x16',
type: 'image/svg+xml',
},
],
status: 'installed' as InstallationStatus,
savedObject: {
type: 'epm-packages',
id: 'endpoint',
attributes: {
installed_kibana: [
{ id: '826759f0-7074-11ea-9bc8-6b38f4d29a16', type: 'dashboard' },
{ id: '1cfceda0-728b-11ea-9bc8-6b38f4d29a16', type: 'visualization' },
{ id: '1e525190-7074-11ea-9bc8-6b38f4d29a16', type: 'visualization' },
{ id: '55387750-729c-11ea-9bc8-6b38f4d29a16', type: 'visualization' },
{ id: '92b1edc0-706a-11ea-9bc8-6b38f4d29a16', type: 'visualization' },
{ id: 'a3a3bd10-706b-11ea-9bc8-6b38f4d29a16', type: 'map' },
] as KibanaAssetReference[],
installed_es: [
{ id: 'logs-endpoint.alerts', type: 'index_template' },
{ id: 'events-endpoint', type: 'index_template' },
{ id: 'logs-endpoint.events.file', type: 'index_template' },
{ id: 'logs-endpoint.events.library', type: 'index_template' },
{ id: 'metrics-endpoint.metadata', type: 'index_template' },
{ id: 'metrics-endpoint.metadata_mirror', type: 'index_template' },
{ id: 'logs-endpoint.events.network', type: 'index_template' },
{ id: 'metrics-endpoint.policy', type: 'index_template' },
{ id: 'logs-endpoint.events.process', type: 'index_template' },
{ id: 'logs-endpoint.events.registry', type: 'index_template' },
{ id: 'logs-endpoint.events.security', type: 'index_template' },
{ id: 'metrics-endpoint.telemetry', type: 'index_template' },
] as EsAssetReference[],
es_index_patterns: {
alerts: 'logs-endpoint.alerts-*',
events: 'events-endpoint-*',
file: 'logs-endpoint.events.file-*',
library: 'logs-endpoint.events.library-*',
metadata: 'metrics-endpoint.metadata-*',
metadata_mirror: 'metrics-endpoint.metadata_mirror-*',
network: 'logs-endpoint.events.network-*',
policy: 'metrics-endpoint.policy-*',
process: 'logs-endpoint.events.process-*',
registry: 'logs-endpoint.events.registry-*',
security: 'logs-endpoint.events.security-*',
telemetry: 'metrics-endpoint.telemetry-*',
},
name: 'endpoint',
version: '0.5.0',
internal: false,
removable: false,
},
references: [],
updated_at: '2020-06-24T14:41:23.098Z',
version: 'Wzc0LDFd',
},
};
}

/**
* Generates a Host Policy response message
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ interface ServerReturnedHostNonExistingPolicies {
payload: HostState['nonExistingPolicies'];
}

interface ServerReturnedHostExistValue {
type: 'serverReturnedHostExistValue';
payload: boolean;
}

export type HostAction =
| ServerReturnedHostList
| ServerFailedToReturnHostList
Expand All @@ -92,6 +97,7 @@ export type HostAction =
| ServerFailedToReturnPoliciesForOnboarding
| UserSelectedEndpointPolicy
| ServerCancelledHostListLoading
| ServerReturnedHostExistValue
| ServerCancelledPolicyItemsLoading
| ServerReturnedEndpointPackageInfo
| ServerReturnedHostNonExistingPolicies;
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('HostList store concerns', () => {
policyItemsLoading: false,
endpointPackageInfo: undefined,
nonExistingPolicies: {},
hostsExist: true,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpSetup } from 'kibana/public';
import { HttpStart } from 'kibana/public';
import { HostInfo, HostResultList } from '../../../../../common/endpoint/types';
import { GetPolicyListResponse } from '../../policy/types';
import { ImmutableMiddlewareFactory } from '../../../../common/store';
Expand All @@ -28,6 +28,8 @@ export const hostMiddlewareFactory: ImmutableMiddlewareFactory<HostState> = (cor
return ({ getState, dispatch }) => (next) => async (action) => {
next(action);
const state = getState();

// Host list
if (
action.type === 'userChangedUrl' &&
isOnHostPage(state) &&
Expand Down Expand Up @@ -89,6 +91,19 @@ export const hostMiddlewareFactory: ImmutableMiddlewareFactory<HostState> = (cor
// No hosts, so we should check to see if there are policies for onboarding
if (hostResponse && hostResponse.hosts.length === 0) {
const http = coreStart.http;

// The original query to the list could have had an invalid param (ex. invalid page_size),
// so we check first if hosts actually do exist before pulling in data for the onboarding
// messages.
if (await doHostsExist(http)) {
return;
}

dispatch({
type: 'serverReturnedHostExistValue',
payload: false,
});

try {
const policyDataResponse: GetPolicyListResponse = await sendGetEndpointSpecificPackageConfigs(
http,
Expand Down Expand Up @@ -119,6 +134,8 @@ export const hostMiddlewareFactory: ImmutableMiddlewareFactory<HostState> = (cor
});
}
}

// Host Details
if (action.type === 'userChangedUrl' && hasSelectedHost(state) === true) {
dispatch({
type: 'serverCancelledPolicyItemsLoading',
Expand Down Expand Up @@ -160,7 +177,6 @@ export const hostMiddlewareFactory: ImmutableMiddlewareFactory<HostState> = (cor
type: 'serverFailedToReturnHostList',
payload: error,
});
return;
}
} else {
dispatch({
Expand Down Expand Up @@ -217,7 +233,7 @@ export const hostMiddlewareFactory: ImmutableMiddlewareFactory<HostState> = (cor
};

const getNonExistingPoliciesForHostsList = async (
http: HttpSetup,
http: HttpStart,
hosts: HostResultList['hosts'],
currentNonExistingPolicies: HostState['nonExistingPolicies']
): Promise<HostState['nonExistingPolicies'] | undefined> => {
Expand Down Expand Up @@ -274,3 +290,23 @@ const getNonExistingPoliciesForHostsList = async (

return nonExisting;
};

const doHostsExist = async (http: HttpStart): Promise<boolean> => {
try {
return (
(
await http.post<HostResultList>('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: 0 }, { page_size: 1 }],
}),
})
).hosts.length !== 0
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(`error while trying to check if hosts exist`);
// eslint-disable-next-line no-console
console.error(error);
}
return false;
};
Loading

0 comments on commit fcf0b63

Please sign in to comment.