From 8b8a4dc19f385dcaabefbbb88ec0786ea5fde930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20David=20Guti=C3=A9rrez?= Date: Tue, 2 Aug 2022 15:05:36 +0200 Subject: [PATCH 1/4] fix: Fixed a routes loop when reinstalling Wazuh indexer --- .../check-index-pattern-object.service.ts | 26 ++++++++++++++-- .../services/check-pattern-support.service.ts | 7 +++++ public/react-services/saved-objects.js | 30 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/public/components/health-check/services/check-index-pattern/check-index-pattern-object.service.ts b/public/components/health-check/services/check-index-pattern/check-index-pattern-object.service.ts index 08d601b73d..2e822e4d2f 100644 --- a/public/components/health-check/services/check-index-pattern/check-index-pattern-object.service.ts +++ b/public/components/health-check/services/check-index-pattern/check-index-pattern-object.service.ts @@ -55,6 +55,10 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch listValidIndexPatterns = await SavedObject.getListOfWazuhValidIndexPatterns(defaultIndexPatterns, HEALTH_CHECK); checkLogger.info(`Valid index patterns found: ${listValidIndexPatterns.length || 0}`); if(!AppState.getCurrentPattern()){ + // Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint. + // Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 + await validateIntegritySavedObjects([defaultPatternId], checkLogger); + AppState.setCurrentPattern(defaultPatternId); checkLogger.info(`Index pattern set in cookie: [${defaultPatternId}]`); } @@ -69,8 +73,14 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch if (AppState.getCurrentPattern() && listValidIndexPatterns.length) { const indexPatternToSelect = listValidIndexPatterns.find(item => item.id === AppState.getCurrentPattern()); if (!indexPatternToSelect){ - AppState.setCurrentPattern(listValidIndexPatterns[0].id); - checkLogger.action(`Set index pattern id in cookie: [${listValidIndexPatterns[0].id}]`); + const indexPatternID = listValidIndexPatterns[0].id; + + // Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint. + // Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 + await validateIntegritySavedObjects([indexPatternID], checkLogger); + + AppState.setCurrentPattern(indexPatternID); + checkLogger.action(`Set index pattern id in cookie: [${indexPatternID}]`); } } @@ -91,6 +101,10 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch const indexPatternDefaultFound = listValidIndexPatterns.find((indexPattern) => indexPattern.title === defaultPatternId); checkLogger.info(`Index pattern id exists [${defaultPatternId}]: ${indexPatternDefaultFound ? 'yes': 'no'}`); if(indexPatternDefaultFound){ + // Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint. + // Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 + await validateIntegritySavedObjects([indexPatternDefaultFound.id], checkLogger); + AppState.setCurrentPattern(indexPatternDefaultFound.id); checkLogger.action(`Index pattern set in cookie: [${indexPatternDefaultFound.id}]`); } @@ -101,3 +115,11 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch } } }; + +// Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint. +// Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 +const validateIntegritySavedObjects = async (indexPatternSavedObjectIDs: string[], checkLogger: CheckLogger): Promise => { + checkLogger.info(`Checking the integrity of saved objects. Validating ${indexPatternSavedObjectIDs.join(',')} can be found...`); + await SavedObject.validateIndexPatternSavedObjectCanBeFound(indexPatternSavedObjectIDs); + checkLogger.info('Integrity of saved objects: [ok]'); +}; diff --git a/public/components/health-check/services/check-pattern-support.service.ts b/public/components/health-check/services/check-pattern-support.service.ts index e5e58f094c..d70dab0e69 100644 --- a/public/components/health-check/services/check-pattern-support.service.ts +++ b/public/components/health-check/services/check-pattern-support.service.ts @@ -46,4 +46,11 @@ export const checkPatternSupportService = (pattern: string, indexType : string) checkLogger.error(`Error creating index pattern id [${pattern}]: ${error.message || error}`); } }; + + const indexPatternSavedObjectIDs = [pattern]; + // Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint. + // Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 + checkLogger.info(`Checking the integrity of saved objects. Validating ${indexPatternSavedObjectIDs.join(',')} can be found...`); + await SavedObject.validateIndexPatternSavedObjectCanBeFound(indexPatternSavedObjectIDs); + checkLogger.info('Integrity of saved objects: [ok]'); } diff --git a/public/react-services/saved-objects.js b/public/react-services/saved-objects.js index c54dcc410f..0a94716339 100644 --- a/public/react-services/saved-objects.js +++ b/public/react-services/saved-objects.js @@ -16,10 +16,13 @@ import { FieldsStatistics } from '../utils/statistics-fields'; import { FieldsMonitoring } from '../utils/monitoring-fields'; import { HEALTH_CHECK, + PLUGIN_PLATFORM_NAME, WAZUH_INDEX_TYPE_ALERTS, WAZUH_INDEX_TYPE_MONITORING, WAZUH_INDEX_TYPE_STATISTICS, } from '../../common/constants'; +import { getSavedObjects } from '../kibana-services'; +import { webDocumentationLink } from '../../common/services/web_documentation'; export class SavedObject { /** @@ -279,4 +282,31 @@ export class SavedObject { } } }; + + /** + * Check if it exists the index pattern saved objects using the `GET /api/saved_objects/_find` endpoint. + * It is usefull to validate if the endpoint works as expected. Related issue: https://github.com/wazuh/wazuh-kibana-app/issues/4293 + * @param {string[]} indexPatternIDs + */ + static async validateIndexPatternSavedObjectCanBeFound(indexPatternIDs){ + const indexPatternsSavedObjects = await getSavedObjects().client.find({ + type: 'index-pattern', + fields: ['title'], + perPage: 10000 + }); + const indexPatternsSavedObjectsCanBeFound = indexPatternIDs + .every(indexPatternID => indexPatternsSavedObjects.savedObjects.some(savedObject => savedObject.id === indexPatternID)); + + if (!indexPatternsSavedObjectsCanBeFound) { + throw new Error(`Saved object for the index pattern can not be found. +The index that stores the saved objects could have wrong field mappings. +Remediations: +- Restart the ${PLUGIN_PLATFORM_NAME} service. This could generate another index with the valid field mappings. +- Remove the used index with wrong mapping and restart the ${PLUGIN_PLATFORM_NAME} service. +Troubleshooting +` +)}; + } } + + From 0c876bf55260c78286bb07805545c699ca5e73d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20David=20Guti=C3=A9rrez?= Date: Thu, 4 Aug 2022 11:59:45 +0200 Subject: [PATCH 2/4] fix: replaced the display error in the plugin loop --- public/react-services/saved-objects.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/public/react-services/saved-objects.js b/public/react-services/saved-objects.js index 0a94716339..772eaa36d9 100644 --- a/public/react-services/saved-objects.js +++ b/public/react-services/saved-objects.js @@ -298,15 +298,8 @@ export class SavedObject { .every(indexPatternID => indexPatternsSavedObjects.savedObjects.some(savedObject => savedObject.id === indexPatternID)); if (!indexPatternsSavedObjectsCanBeFound) { - throw new Error(`Saved object for the index pattern can not be found. -The index that stores the saved objects could have wrong field mappings. -Remediations: -- Restart the ${PLUGIN_PLATFORM_NAME} service. This could generate another index with the valid field mappings. -- Remove the used index with wrong mapping and restart the ${PLUGIN_PLATFORM_NAME} service. -Troubleshooting -` + throw new Error(`Saved object for index pattern not found. +Restart the ${PLUGIN_PLATFORM_NAME} service to initialize the index. More information in troubleshooting.` )}; } -} - - +} \ No newline at end of file From d2e254adddd45bf72489be4cc0674c5a5188454f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20David=20Guti=C3=A9rrez?= Date: Thu, 4 Aug 2022 12:19:28 +0200 Subject: [PATCH 3/4] changelog: add PR entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fa08f2588..9a91daba47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to the Wazuh app project will be documented in this file. ### Fixed - Fixed path in logo customization section [#4352](https://github.com/wazuh/wazuh-kibana-app/pull/4352) +- Fixed a routes loop when reinstalling Wazuh indexer [#4373](https://github.com/wazuh/wazuh-kibana-app/pull/4373) ## Wazuh v4.3.6 - OpenSearch Dashboards 1.2.0 - Revision 4307 From fc853db27b4c3cfa0c1d5b6369867b5d1c56c15a Mon Sep 17 00:00:00 2001 From: Matias Ezequiel Moreno Date: Fri, 12 Aug 2022 17:02:22 -0300 Subject: [PATCH 4/4] fix import variable --- server/start/initialize/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/start/initialize/index.ts b/server/start/initialize/index.ts index 64417111c2..b0cd0a3e37 100644 --- a/server/start/initialize/index.ts +++ b/server/start/initialize/index.ts @@ -15,7 +15,7 @@ import { pluginPlatformTemplate } from '../../integration-files/kibana-template' import { getConfiguration } from '../../lib/get-configuration'; import { totalmem } from 'os'; import fs from 'fs'; -import { WAZUH_ALERTS_PATTERN, WAZUH_DATA_CONFIG_REGISTRY_PATH, WAZUH_PLUGIN_PLATFORM_TEMPLATE_NAME, WAZUH_DATA_PLUGIN_PLATFORM_BASE_ABSOLUTE_PATH, PLUGIN_PLATFORM_NAME, PLUGIN_PLATFORM_INSTALLATION_USER_GROUP, PLUGIN_PLATFORM_INSTALLATION_USER, WAZUH_DEFAULT_APP_CONFIG } from '../../../common/constants'; +import { WAZUH_ALERTS_PATTERN, WAZUH_DATA_CONFIG_REGISTRY_PATH, WAZUH_PLUGIN_PLATFORM_TEMPLATE_NAME, WAZUH_DATA_PLUGIN_PLATFORM_BASE_ABSOLUTE_PATH, PLUGIN_PLATFORM_NAME, PLUGIN_PLATFORM_INSTALLATION_USER_GROUP, PLUGIN_PLATFORM_INSTALLATION_USER, WAZUH_DEFAULT_APP_CONFIG, PLUGIN_APP_NAME } from '../../../common/constants'; import { createDataDirectoryIfNotExists } from '../../lib/filesystem'; import _ from 'lodash';