From 1d03c23eda9cb690a39352e9eb36e15cc4edc064 Mon Sep 17 00:00:00 2001 From: Antonio <34042064+Desvelao@users.noreply.github.com> Date: Wed, 6 Apr 2022 11:46:05 +0200 Subject: [PATCH] [BUG][Reports] Generate reports not manage the Wazuh API token expiration (#3881) * fix: Fixed the generating PDF reports doesn't manage the refreshing of token when it expires - Replaced the method to do the request from `GenericRequest.request` to `WzRequest.genericReq` that handles the refreshing of token - Adapted request payload and endpoint expected parameters for ApiID and the index pattern title - Removed not used params of some reports endpoints - Remove message of generating pdf report when the process fails * changelog: Add PR to changelog * changelog: Moved entry --- CHANGELOG.md | 1 + public/react-services/reporting.js | 15 ++++++++++----- server/controllers/wazuh-reporting.ts | 18 +++++++----------- server/routes/wazuh-reporting.ts | 10 ++++++++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7664400153..9dffb4f207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -143,6 +143,7 @@ All notable changes to the Wazuh app project will be documented in this file. - Fixed agents details card style [#3845](https://github.com/wazuh/wazuh-kibana-app/pull/3845) [#3860](https://github.com/wazuh/wazuh-kibana-app/pull/3860) - Fixed routing redirection in events documents discover links [#3866](https://github.com/wazuh/wazuh-kibana-app/pull/3866) - Fixed health-check [#3868](https://github.com/wazuh/wazuh-kibana-app/pull/3868) +- Fixed an error when generating PDF reports due to Wazuh API token expiration [#3881](https://github.com/wazuh/wazuh-kibana-app/pull/3881) - Fixed the table of Vulnerabilities/Inventory doesn't reload when changing the selected agent [#3901](https://github.com/wazuh/wazuh-kibana-app/pull/3901) - Fixed backslash breaking exported JSON result [#3909](https://github.com/wazuh/wazuh-kibana-app/pull/3909) - Fixed the Events view multiple "The index pattern was refreshed successfully" toast [#3937](https://github.com/wazuh/wazuh-kibana-app/pull/3937) diff --git a/public/react-services/reporting.js b/public/react-services/reporting.js index 44cce0a17a..8a053b9b24 100644 --- a/public/react-services/reporting.js +++ b/public/react-services/reporting.js @@ -13,12 +13,12 @@ import $ from 'jquery'; import moment from 'moment'; import { WazuhConfig } from '../react-services/wazuh-config'; -import { GenericRequest } from '../react-services/generic-request'; +import { AppState } from './app-state'; +import { WzRequest } from './wz-request'; import { Vis2PNG } from '../factories/vis2png'; import { RawVisualizations } from '../factories/raw-visualizations'; import { VisHandlers } from '../factories/vis-handlers'; -import { getToasts } from '../kibana-services'; -import { getAngularModule } from '../kibana-services'; +import { getAngularModule, getDataPlugin, getToasts } from '../kibana-services'; import { UI_LOGGER_LEVELS } from '../../common/constants'; import { UI_ERROR_SEVERITIES } from './error-orchestrator/types'; import { getErrorOrchestrator } from './common-services'; @@ -109,11 +109,13 @@ export class ReportingService { section: agents ? 'agents' : 'overview', agents, browserTimezone, + indexPatternTitle: (await getDataPlugin().indexPatterns.get(AppState.getCurrentPattern())).title, + apiId: JSON.parse(AppState.getCurrentAPI()).id }; const apiEndpoint = tab === 'syscollector' ? `/reports/agents/${agents}/inventory` : `/reports/modules/${tab}`; - await GenericRequest.request('POST', apiEndpoint, data); + await WzRequest.genericReq('POST', apiEndpoint, data); this.$rootScope.reportBusy = false; this.$rootScope.reportStatus = false; @@ -128,6 +130,7 @@ export class ReportingService { } catch (error) { this.$rootScope.reportBusy = false; this.$rootScope.reportStatus = false; + this.$rootScope.$applyAsync(); const options = { context: `${ReportingService.name}.startVis2Png`, level: UI_LOGGER_LEVELS.ERROR, @@ -160,10 +163,11 @@ export class ReportingService { tab: type, browserTimezone, components, + apiId: JSON.parse(AppState.getCurrentAPI()).id }; const apiEndpoint = type === 'agentConfig' ? `/reports/agents/${obj.id}` : `/reports/groups/${obj.name}`; - await GenericRequest.request('POST', apiEndpoint, data); + await WzRequest.genericReq('POST', apiEndpoint, data); this.$rootScope.reportBusy = false; this.$rootScope.reportStatus = false; @@ -178,6 +182,7 @@ export class ReportingService { } catch (error) { this.$rootScope.reportBusy = false; this.$rootScope.reportStatus = false; + this.$rootScope.$applyAsync(); const options = { context: `${ReportingService.name}.startConfigReport`, level: UI_LOGGER_LEVELS.ERROR, diff --git a/server/controllers/wazuh-reporting.ts b/server/controllers/wazuh-reporting.ts index b18a0e67e9..48e7952f0b 100644 --- a/server/controllers/wazuh-reporting.ts +++ b/server/controllers/wazuh-reporting.ts @@ -1144,9 +1144,10 @@ export class WazuhReportingCtrl { tables, name, section, + indexPatternTitle, + apiId } = request.body; const { moduleID } = request.params; - const { id: apiId, pattern: indexPattern } = request.headers; const { from, to } = time || {}; // Init const printer = new ReportPrinter(); @@ -1176,7 +1177,7 @@ export class WazuhReportingCtrl { new Date(from).getTime(), new Date(to).getTime(), sanitizedFilters, - indexPattern, + indexPatternTitle, agents ); } @@ -1219,10 +1220,8 @@ export class WazuhReportingCtrl { ) { try { log('reporting:createReportsGroups', `Report started`, 'info'); - const { browserTimezone, searchBar, filters, time, name, components } = request.body; + const { name, components, apiId } = request.body; const { groupID } = request.params; - const { id: apiId, pattern: indexPattern } = request.headers; - const { from, to } = time || {}; // Init const printer = new ReportPrinter(); @@ -1493,10 +1492,8 @@ export class WazuhReportingCtrl { ) { try { log('reporting:createReportsAgents', `Report started`, 'info'); - const { browserTimezone, searchBar, filters, time, name, components } = request.body; + const { name, components, apiId } = request.body; const { agentID } = request.params; - const { id: apiId } = request.headers; - const { from, to } = time || {}; const printer = new ReportPrinter(); @@ -1744,9 +1741,8 @@ export class WazuhReportingCtrl { ) { try { log('reporting:createReportsAgentsInventory', `Report started`, 'info'); - const { browserTimezone, searchBar, filters, time, name } = request.body; + const { searchBar, filters, time, name, indexPatternTitle, apiId } = request.body; const { agentID } = request.params; - const { id: apiId, pattern: indexPattern } = request.headers; const { from, to } = time || {}; // Init const printer = new ReportPrinter(); @@ -1940,7 +1936,7 @@ export class WazuhReportingCtrl { from, to, sanitizedFilters + ' AND rule.groups: "vulnerability-detector"', - indexPattern, + indexPatternTitle, agentID ); } diff --git a/server/routes/wazuh-reporting.ts b/server/routes/wazuh-reporting.ts index 21ac359814..d6addf9d1a 100644 --- a/server/routes/wazuh-reporting.ts +++ b/server/routes/wazuh-reporting.ts @@ -34,7 +34,9 @@ export function WazuhReportingRoutes(router: IRouter) { from: schema.string(), to: schema.string() }), schema.string()]), - title: schema.maybe(schema.string()) + title: schema.maybe(schema.string()), + indexPatternTitle: schema.string(), + apiId: schema.string() }), params: schema.object({ moduleID: schema.string() @@ -54,6 +56,7 @@ export function WazuhReportingRoutes(router: IRouter) { name: schema.string(), section: schema.maybe(schema.string()), tab: schema.string(), + apiId: schema.string() }), params: schema.object({ groupID: schema.string() @@ -73,6 +76,7 @@ export function WazuhReportingRoutes(router: IRouter) { name: schema.string(), section: schema.maybe(schema.string()), tab: schema.string(), + apiId: schema.string() }), params: schema.object({ agentID: schema.string() @@ -100,7 +104,9 @@ export function WazuhReportingRoutes(router: IRouter) { from: schema.string(), to: schema.string() }), schema.string()]), - title: schema.maybe(schema.string()) + title: schema.maybe(schema.string()), + indexPatternTitle: schema.string(), + apiId: schema.string() }), params: schema.object({ agentID: schema.string()