Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix refreshing index pattern fields does not include the scripted fields #6237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ All notable changes to the Wazuh app project will be documented in this file.
- Develop logic of a new index for the fim module [#6227](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6227)
- Allow editing groups for an agent from Endpoints Summary [#6250](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6250)

# Fixed

- Fixed the scripted fields disappear when the fields of the events index pattern was refreshed [#6237](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6237)

## Wazuh v4.8.1 - OpenSearch Dashboards 2.10.0 - Revision 00

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,30 @@ import { AppState, SavedObject } from '../../../../react-services';
import { getDataPlugin } from '../../../../kibana-services';
import { CheckLogger } from '../../types/check_logger';

export const checkFieldsService = async (appConfig, checkLogger: CheckLogger) => {
export const checkFieldsService = async (
appConfig,
checkLogger: CheckLogger,
) => {
const patternId = AppState.getCurrentPattern();
checkLogger.info(`Index pattern id in cookie: [${patternId}]`);

checkLogger.info(`Getting index pattern data [${patternId}]...`);
const pattern = await getDataPlugin().indexPatterns.get(patternId);
checkLogger.info(`Index pattern data found: [${pattern ? 'yes' : 'no'}]`);

checkLogger.info(`Refreshing index pattern fields: title [${pattern.title}], id [${pattern.id}]...`);
await SavedObject.refreshIndexPattern(pattern, null);
checkLogger.action(`Refreshed index pattern fields: title [${pattern.title}], id [${pattern.id}]`);

checkLogger.info(
`Refreshing index pattern fields: title [${pattern.title}], id [${pattern.id}]...`,
);
try {
await SavedObject.refreshIndexPattern(pattern);
checkLogger.action(
`Refreshed index pattern fields: title [${pattern.title}], id [${pattern.id}]`,
);
} catch (error) {
if (error.message.includes('No indices match pattern')) {
checkLogger.warning(
`Index pattern fields for title [${pattern.title}], id [${pattern.id}] could not be refreshed due to: ${error.message}. This could be an indicator of some problem in the generation, not running server service or configuration to ingest of alerts data.`,
);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,91 @@ import { getDataPlugin } from '../../../../kibana-services';
import { HEALTH_CHECK } from '../../../../../common/constants';
import { CheckLogger } from '../../types/check_logger';

export const checkIndexPatternObjectService = async (appConfig, checkLogger: CheckLogger) => {
export const checkIndexPatternObjectService = async (
appConfig,
checkLogger: CheckLogger,
) => {
const patternId: string = AppState.getCurrentPattern();
const defaultPatternId: string = appConfig.data['pattern'];
const shouldCreateIndex: boolean = appConfig.data['checks.pattern'];
checkLogger.info(`Index pattern id in cookie: ${patternId ? `yes [${patternId}]` : 'no'}`);
checkLogger.info(
`Index pattern id in cookie: ${patternId ? `yes [${patternId}]` : 'no'}`,
);

const defaultIndexPatterns: string[] = [
defaultPatternId,
...(patternId && patternId !== defaultPatternId ? [patternId] : [])
...(patternId && patternId !== defaultPatternId ? [patternId] : []),
];
checkLogger.info(`Getting list of valid index patterns...`);
let listValidIndexPatterns = await SavedObject.getListOfWazuhValidIndexPatterns(defaultIndexPatterns, HEALTH_CHECK);
checkLogger.info(`Valid index patterns found: ${listValidIndexPatterns.length || 0}`);
let listValidIndexPatterns =
await SavedObject.getListOfWazuhValidIndexPatterns(
defaultIndexPatterns,
HEALTH_CHECK,
);
checkLogger.info(
`Valid index patterns found: ${listValidIndexPatterns.length || 0}`,
);

const indexPatternDefaultFound = listValidIndexPatterns.find((indexPattern) => indexPattern.title === defaultPatternId);
checkLogger.info(`Found default index pattern with title [${defaultPatternId}]: ${indexPatternDefaultFound ? 'yes' : 'no'}`);
const indexPatternDefaultFound = listValidIndexPatterns.find(
indexPattern => indexPattern.title === defaultPatternId,
);
checkLogger.info(
`Found default index pattern with title [${defaultPatternId}]: ${
indexPatternDefaultFound ? 'yes' : 'no'
}`,
);

if (!indexPatternDefaultFound && defaultPatternId) {
// if no valid index patterns are found we try to create the wazuh-alerts-*
try {
checkLogger.info(`Checking if index pattern [${defaultPatternId}] exists...`);
const existDefaultIndexPattern = await SavedObject.getExistingIndexPattern(defaultPatternId);
checkLogger.info(`Index pattern id [${defaultPatternId}] exists: ${existDefaultIndexPattern ? 'yes' : 'no'}`);
checkLogger.info(
`Checking if index pattern [${defaultPatternId}] exists...`,
);
const existDefaultIndexPattern =
await SavedObject.getExistingIndexPattern(defaultPatternId);
checkLogger.info(
`Index pattern id [${defaultPatternId}] exists: ${
existDefaultIndexPattern ? 'yes' : 'no'
}`,
);
if (existDefaultIndexPattern) {
checkLogger.info(`Refreshing index pattern fields [${defaultPatternId}]...`);
await SavedObject.refreshIndexPattern(defaultPatternId);
checkLogger.action(`Refreshed index pattern fields [${defaultPatternId}]`);
} else if(shouldCreateIndex) {
checkLogger.info(`Getting index pattern [${defaultPatternId}]...`);
const pattern = await getDataPlugin().indexPatterns.get(
defaultPatternId,
);
checkLogger.info(
`Refreshing index pattern fields: title [${pattern.title}], id [${pattern.id}]...`,
);
try {
await SavedObject.refreshIndexPattern(pattern);
checkLogger.action(
`Refreshed index pattern fields: title [${pattern.title}], id [${pattern.id}]`,
);
} catch (error) {
if (error.message.includes('No indices match pattern')) {
checkLogger.warning(
`Index pattern fields for title [${pattern.title}], id [${pattern.id}] could not be refreshed due to: ${error.message}. This could be an indicator of some problem in the generation, not running server service or configuration to ingest of alerts data.`,
);
}
}
} else if (shouldCreateIndex) {
checkLogger.info(`Creating index pattern [${defaultPatternId}]...`);
await SavedObject.createWazuhIndexPattern(defaultPatternId);
checkLogger.action(`Created index pattern [${defaultPatternId}]`);
}else{
} else {
// show error
checkLogger.error(`Default index pattern not found`);
}
checkLogger.info(`Getting list of valid index patterns...`);
listValidIndexPatterns = await SavedObject.getListOfWazuhValidIndexPatterns(defaultIndexPatterns, HEALTH_CHECK);
checkLogger.info(`Valid index patterns found: ${listValidIndexPatterns.length || 0}`);
if(!AppState.getCurrentPattern()){
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-dashboard-plugins/issues/4293
await validateIntegritySavedObjects([defaultPatternId], checkLogger);
Expand All @@ -71,8 +117,10 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch
}

if (AppState.getCurrentPattern() && listValidIndexPatterns.length) {
const indexPatternToSelect = listValidIndexPatterns.find(item => item.id === AppState.getCurrentPattern());
if (!indexPatternToSelect){
const indexPatternToSelect = listValidIndexPatterns.find(
item => item.id === AppState.getCurrentPattern(),
);
if (!indexPatternToSelect) {
const indexPatternID = listValidIndexPatterns[0].id;

// Check the index pattern saved objects can be found using `GET /api/saved_objects/_find` endpoint.
Expand All @@ -83,30 +131,59 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch
checkLogger.action(`Set index pattern id in cookie: [${indexPatternID}]`);
}
}

checkLogger.info(`Checking the app default pattern exists: id [${defaultPatternId}]...`);
const existsDefaultPattern = await SavedObject.existsIndexPattern(defaultPatternId);
checkLogger.info(`Default pattern with id [${defaultPatternId}] exists: ${existsDefaultPattern.status ? 'yes' : 'no'}`);

existsDefaultPattern.status
&& getDataPlugin().indexPatterns.setDefault(defaultPatternId, true)
&& checkLogger.action(`Default pattern id [${defaultPatternId}] set as default index pattern`);

patternId && checkLogger.info(`Checking the index pattern id [${patternId}] exists...`);
const patternData = patternId ? (await SavedObject.existsIndexPattern(patternId)) || {} : {} ;
patternId && checkLogger.info(`Index pattern id exists [${patternId}]: ${patternData.status ? 'yes': 'no'}`);
checkLogger.info(
`Checking the app default pattern exists: id [${defaultPatternId}]...`,
);
const existsDefaultPattern = await SavedObject.existsIndexPattern(
defaultPatternId,
);
checkLogger.info(
`Default pattern with id [${defaultPatternId}] exists: ${
existsDefaultPattern.status ? 'yes' : 'no'
}`,
);

existsDefaultPattern.status &&
getDataPlugin().indexPatterns.setDefault(defaultPatternId, true) &&
checkLogger.action(
`Default pattern id [${defaultPatternId}] set as default index pattern`,
);

patternId &&
checkLogger.info(`Checking the index pattern id [${patternId}] exists...`);
const patternData = patternId
? (await SavedObject.existsIndexPattern(patternId)) || {}
: {};
patternId &&
checkLogger.info(
`Index pattern id exists [${patternId}]: ${
patternData.status ? 'yes' : 'no'
}`,
);

if (!patternData.status) {
if (listValidIndexPatterns.length) {
const indexPatternDefaultFound = listValidIndexPatterns.find((indexPattern) => indexPattern.title === defaultPatternId);
checkLogger.info(`Index pattern id exists [${defaultPatternId}]: ${indexPatternDefaultFound ? 'yes': 'no'}`);
if(indexPatternDefaultFound){
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-dashboard-plugins/issues/4293
await validateIntegritySavedObjects([indexPatternDefaultFound.id], checkLogger);
await validateIntegritySavedObjects(
[indexPatternDefaultFound.id],
checkLogger,
);

AppState.setCurrentPattern(indexPatternDefaultFound.id);
checkLogger.action(`Index pattern set in cookie: [${indexPatternDefaultFound.id}]`);
checkLogger.action(
`Index pattern set in cookie: [${indexPatternDefaultFound.id}]`,
);
}
checkLogger.info('Retrying the check...');
return await checkIndexPatternObjectService(appConfig, checkLogger);
Expand All @@ -118,8 +195,17 @@ 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-dashboard-plugins/issues/4293
const validateIntegritySavedObjects = async (indexPatternSavedObjectIDs: string[], checkLogger: CheckLogger): Promise<void> => {
checkLogger.info(`Checking the integrity of saved objects. Validating ${indexPatternSavedObjectIDs.join(',')} can be found...`);
await SavedObject.validateIndexPatternSavedObjectCanBeFound(indexPatternSavedObjectIDs);
const validateIntegritySavedObjects = async (
indexPatternSavedObjectIDs: string[],
checkLogger: CheckLogger,
): Promise<void> => {
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]');
};
9 changes: 2 additions & 7 deletions plugins/main/public/components/visualize/wz-visualize.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export const WzVisualize = compose(
this.monitoringEnabled = !!(configuration || {})[
'wazuh.monitoring.enabled'
];
this.newFields = {};
}

async componentDidMount() {
Expand Down Expand Up @@ -123,19 +122,15 @@ export const WzVisualize = compose(
});
};

refreshKnownFields = async (newField = null) => {
if (newField && newField.name) {
this.newFields[newField.name] = newField;
}
refreshKnownFields = async () => {
if (!this.hasRefreshedKnownFields) {
// Known fields are refreshed only once per dashboard loading
try {
this.hasRefreshedKnownFields = true;
this.isRefreshing = true;
await PatternHandler.refreshIndexPattern(this.newFields);
await PatternHandler.refreshIndexPattern();
this.isRefreshing = false;
this.reloadToast();
this.newFields = {};
} catch (error) {
this.isRefreshing = false;
const options = {
Expand Down
Loading
Loading