From dc13c3c502c71abd4d842ac845367c5d3ad39eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar=20Biset?= <43619595+jbiset@users.noreply.github.com> Date: Wed, 26 Jun 2024 11:43:30 -0300 Subject: [PATCH] Fix error when changing api in agent preview of endpoints summary (#6802) * Added HOC withGuard to endpoint summary agent preview syscollector, stats and configuration tabs * Changed HOCs order in agent-stats * Added CHANGELOG * Fixed pinning agent in NavigationService, fixed breadcrumb when there is no agent data and it is elevated to parent component withGuard of unselected agent * Removed unused import and changed navigate path to endpoints summary --- CHANGELOG.md | 1 + .../globalBreadcrumb/platformBreadcrumb.tsx | 63 +++++++++-------- .../components/common/modules/main-agent.tsx | 16 +---- .../common/welcome/agents-welcome.js | 28 +------- .../endpoints-summary/agent/index.tsx | 68 ++++++++++++++----- .../wz-agent-selector-service.ts | 7 +- .../configuration/configuration-main.js | 3 +- 7 files changed, 97 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18c3182d70..9e59f9bd08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to the Wazuh app project will be documented in this file. - Added HAProxy helper settings to cluster configuration [#6653](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6653) - Added ability to open the report file or Reporting application from the toast message [#6558](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6558) - Added support for agents to Office 365 [#6558](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6558) +- Added pinned agent data validation when rendering the Inventory data, Stats and Configuration tabs in Agent preview of Endpoints Summary [#6800](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6800) ### Changed diff --git a/plugins/main/public/components/common/globalBreadcrumb/platformBreadcrumb.tsx b/plugins/main/public/components/common/globalBreadcrumb/platformBreadcrumb.tsx index 070c35b1b8..cf690035ce 100644 --- a/plugins/main/public/components/common/globalBreadcrumb/platformBreadcrumb.tsx +++ b/plugins/main/public/components/common/globalBreadcrumb/platformBreadcrumb.tsx @@ -6,34 +6,43 @@ export const setBreadcrumbs = (breadcrumbs, router) => { if (breadcrumbs === '' || breadcrumbs === undefined) { return; } - const breadcrumbsCustom = breadcrumbs?.map(breadcrumb => - breadcrumb.agent - ? { - className: - 'euiLink euiLink--subdued osdBreadcrumbs wz-vertical-align-middle', - onClick: ev => { - ev.stopPropagation(); - if (getWzCurrentAppID() === endpointSummary.id) { - NavigationService.getInstance().navigate( - `/agents?tab=welcome&agent=${breadcrumb.agent.id}`, - ); - } else { - NavigationService.getInstance().navigateToApp( - endpointSummary.id, - { - path: `#/agents?tab=welcome&agent=${breadcrumb.agent.id}`, - }, - ); - } + const breadcrumbsCustom = breadcrumbs + ?.map(breadcrumb => + breadcrumb?.agent?.id + ? { + className: + 'euiLink euiLink--subdued osdBreadcrumbs wz-vertical-align-middle', + onClick: ev => { + ev.stopPropagation(); + if (getWzCurrentAppID() === endpointSummary.id) { + NavigationService.getInstance().navigate( + `/agents?tab=welcome&agent=${breadcrumb.agent.id}`, + ); + } else { + NavigationService.getInstance().navigateToApp( + endpointSummary.id, + { + path: `#/agents?tab=welcome&agent=${breadcrumb.agent.id}`, + }, + ); + } + }, + truncate: true, + text: breadcrumb.agent.name, + } + : /* + Some use cases cause get Breadcrumbs to have the agent property + undefined. In this case a null is added and then with the filter it + is filtered + */ + typeof breadcrumb.agent !== 'undefined' + ? null + : { + ...breadcrumb, + className: 'osdBreadcrumbs', }, - truncate: true, - text: breadcrumb.agent.name, - } - : { - ...breadcrumb, - className: 'osdBreadcrumbs', - }, - ); + ) + .filter(value => value); getCore().chrome.setBreadcrumbs(breadcrumbsCustom); diff --git a/plugins/main/public/components/common/modules/main-agent.tsx b/plugins/main/public/components/common/modules/main-agent.tsx index ef91794fa1..6255d974bd 100644 --- a/plugins/main/public/components/common/modules/main-agent.tsx +++ b/plugins/main/public/components/common/modules/main-agent.tsx @@ -14,7 +14,6 @@ import React, { Component, Fragment } from 'react'; import { EuiFlexGroup, EuiFlexItem, - EuiCallOut, EuiTitle, EuiButtonEmpty, } from '@elastic/eui'; @@ -106,7 +105,6 @@ export class MainModuleAgent extends Component { render() { const { agent, section, selectView } = this.props; - const title = this.renderTitle(); const ModuleTabView = (this.props.tabs || []).find( tab => tab.id === selectView, ); @@ -118,11 +116,11 @@ export class MainModuleAgent extends Component { : 'wz-module' } > -
-
{title}
-
{agent && agent.os && ( +
+
{this.renderTitle()}
+
)} - {(!agent || !agent.os) && ( - - )}
); } diff --git a/plugins/main/public/components/common/welcome/agents-welcome.js b/plugins/main/public/components/common/welcome/agents-welcome.js index c050965e29..7f5c9c5ac7 100644 --- a/plugins/main/public/components/common/welcome/agents-welcome.js +++ b/plugins/main/public/components/common/welcome/agents-welcome.js @@ -24,7 +24,6 @@ import { EuiToolTip, EuiButtonIcon, EuiPageBody, - EuiLink, } from '@elastic/eui'; import { FimEventsTable, @@ -43,10 +42,7 @@ import { withErrorBoundary, withGlobalBreadcrumb, withGuard } from '../hocs'; import { compose } from 'redux'; import { API_NAME_AGENT_STATUS } from '../../../../common/constants'; import { WAZUH_MODULES } from '../../../../common/wazuh-modules'; -import { - PromptAgentNeverConnected, - PromptNoSelectedAgent, -} from '../../agents/prompts'; +import { PromptAgentNeverConnected } from '../../agents/prompts'; import { WzButton } from '../buttons'; import { Applications, @@ -83,28 +79,6 @@ export const AgentsWelcome = compose( : []), ]; }), - withGuard( - props => !(props.agent && props.agent.id), - () => ( - <> - - You need to select an agent or return to - - - Endpoint summary - - - - } - /> - - ), - ), withGuard( props => props.agent.status === API_NAME_AGENT_STATUS.NEVER_CONNECTED, PromptAgentNeverConnected, diff --git a/plugins/main/public/components/endpoints-summary/agent/index.tsx b/plugins/main/public/components/endpoints-summary/agent/index.tsx index 987bd7c6b9..d30b618052 100644 --- a/plugins/main/public/components/endpoints-summary/agent/index.tsx +++ b/plugins/main/public/components/endpoints-summary/agent/index.tsx @@ -1,11 +1,15 @@ import React, { useState, useEffect } from 'react'; -import { EuiPage, EuiPageBody, EuiProgress } from '@elastic/eui'; +import { EuiPage, EuiPageBody, EuiProgress, EuiLink } from '@elastic/eui'; import { AgentsWelcome } from '../../common/welcome/agents-welcome'; import { Agent } from '../types'; import { MainSyscollector } from '../../agents/syscollector/main'; import { MainAgentStats } from '../../agents/stats'; import WzManagementConfiguration from '../../../controllers/management/components/management/configuration/configuration-main.js'; -import { withErrorBoundary, withRouteResolvers } from '../../common/hocs'; +import { + withErrorBoundary, + withGuard, + withRouteResolvers, +} from '../../common/hocs'; import { compose } from 'redux'; import { PinnedAgentManager } from '../../wz-agent-selector/wz-agent-selector-service'; import { MainModuleAgent } from '../../common/modules/main-agent'; @@ -18,11 +22,46 @@ import { import { useRouterSearch } from '../../common/hooks/use-router-search'; import { Redirect, Route, Switch } from '../../router-search'; import NavigationService from '../../../react-services/navigation-service'; +import { connect } from 'react-redux'; +import { PromptNoSelectedAgent } from '../../agents/prompts'; +import { RedirectAppLinks } from '../../../../../../src/plugins/opensearch_dashboards_react/public'; +import { getCore } from '../../../kibana-services'; +import { endpointSummary } from '../../../utils/applications'; + +const mapStateToProps = state => ({ + agent: state.appStateReducers?.currentAgentData, +}); export const AgentView = compose( withErrorBoundary, withRouteResolvers({ enableMenu, ip, nestedResolve, savedSearch }), -)(() => { + connect(mapStateToProps), + withGuard( + props => !(props.agent && props.agent.id), + () => ( + <> + + You need to select an agent or return to + + + NavigationService.getInstance().navigate(`/agents-preview`) + } + > + Endpoint summary + + + + } + /> + + ), + ), +)(({ agent: agentData }) => { const { tab = 'welcome' } = useRouterSearch(); const navigationService = NavigationService.getInstance(); @@ -35,23 +74,20 @@ export const AgentView = compose( const pinnedAgentManager = new PinnedAgentManager(); - const [agent, setAgent] = useState(); const [isLoadingAgent, setIsLoadingAgent] = useState(true); - const getAgent = async () => { + const syncAgent = async () => { setIsLoadingAgent(true); await pinnedAgentManager.syncPinnedAgentSources(); - const isPinnedAgent = pinnedAgentManager.isPinnedAgent(); - setAgent(isPinnedAgent ? pinnedAgentManager.getPinnedAgent() : null); setIsLoadingAgent(false); }; useEffect(() => { - getAgent(); + syncAgent(); }, [tab]); const switchTab = (tab: string) => { - navigationService.navigate(`/agents?tab=${tab}&agent=${agent?.id}`); + navigationService.navigate(`/agents?tab=${tab}&agent=${agentData?.id}`); }; if (isLoadingAgent) { @@ -67,21 +103,21 @@ export const AgentView = compose( return ( - - + + - - + + - - + + diff --git a/plugins/main/public/components/wz-agent-selector/wz-agent-selector-service.ts b/plugins/main/public/components/wz-agent-selector/wz-agent-selector-service.ts index 90fbf9c511..3b4902ca84 100644 --- a/plugins/main/public/components/wz-agent-selector/wz-agent-selector-service.ts +++ b/plugins/main/public/components/wz-agent-selector/wz-agent-selector-service.ts @@ -42,16 +42,15 @@ export class PinnedAgentManager { const includesAgentViewURL = this.navigationService .getPathname() .includes(this.AGENT_VIEW_URL); + const params = this.navigationService.getParams(); - const urlSearchParams = this.navigationService.getParams(); - - urlSearchParams.set( + params.set( includesAgentViewURL ? PinnedAgentManager.AGENT_ID_VIEW_KEY : PinnedAgentManager.AGENT_ID_URL_VIEW_KEY, String(agentData?.id), ); - this.navigationService.renewURL(urlSearchParams); + this.navigationService.renewURL(params); } unPinAgent(): void { diff --git a/plugins/main/public/controllers/management/components/management/configuration/configuration-main.js b/plugins/main/public/controllers/management/components/management/configuration/configuration-main.js index a75adf780d..6d71818717 100644 --- a/plugins/main/public/controllers/management/components/management/configuration/configuration-main.js +++ b/plugins/main/public/controllers/management/components/management/configuration/configuration-main.js @@ -9,7 +9,6 @@ * * Find more information about this on the LICENSE file. */ - import WzConfigurationSwitch from './configuration-switch'; import { withErrorBoundary, @@ -23,7 +22,7 @@ export default compose( withErrorBoundary, withGlobalBreadcrumb(props => { let breadcrumb = false; - if (props.agent.id === '000') { + if (props.agent?.id === '000') { breadcrumb = [{ text: settings.breadcrumbLabel }]; } else { breadcrumb = [