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] [HEALH CHECK] Fix listValidIndexPatterns.find is not a function error #3698

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Fixing double flyout clicking in a policy [#3676](https://github.com/wazuh/wazuh-kibana-app/pull/3676)
- Fixed error conflict setting kibana settings from the health check [#3678](https://github.com/wazuh/wazuh-kibana-app/pull/3678)
- Fixed compatibility to get the valid index patterns and refresh fields for Kibana 7.10.2-7.13.4 [3681](https://github.com/wazuh/wazuh-kibana-app/pull/3681)
- Fixed error getting the index pattern data when there is not `attributes.fields` in the saved object [3689](https://github.com/wazuh/wazuh-kibana-app/pull/3698)

## Wazuh v4.2.4 - Kibana 7.10.2, 7.11.2, 7.12.1 - Revision 4205

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const checkIndexPatternObjectService = async (appConfig, checkLogger: Ch
// show error
checkLogger.error(`Default index pattern not found`);
}
checkLogger.info(`Getting list of valid index patterns [${patternId}]...`);
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()){
Expand Down
8 changes: 3 additions & 5 deletions public/controllers/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,9 @@ export class SettingsController {
// Get settings function
async getSettings() {
try {
const patternList = await SavedObject.getListOfWazuhValidIndexPatterns();

this.indexPatterns = patternList;

if (!this.indexPatterns.length) {
try{
this.indexPatterns = await SavedObject.getListOfWazuhValidIndexPatterns();
}catch(error){
this.wzMisc.setBlankScr('Sorry but no valid index patterns were found');
this.$location.search('tab', null);
this.$location.path('/blank-screen');
Expand Down
96 changes: 38 additions & 58 deletions public/react-services/saved-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,30 @@ export class SavedObject {
* Returns the full list of index patterns
*/
static async getListOfIndexPatterns() {
try {
const savedObjects = await GenericRequest.request(
'GET',
`/api/saved_objects/_find?type=index-pattern&fields=title&fields=fields&per_page=9999`
const savedObjects = await GenericRequest.request(
'GET',
`/api/saved_objects/_find?type=index-pattern&fields=title&fields=fields&per_page=9999`
);
let indexPatterns = ((savedObjects || {}).data || {}).saved_objects || [];

let indexPatternsFields;
if(satisfyKibanaVersion('<7.11')){
indexPatternsFields = indexPatterns.map(indexPattern => JSON.parse(indexPattern.attributes.fields));
}else if(satisfyKibanaVersion('>=7.11')){
indexPatternsFields = await Promise.all(indexPatterns.map(async indexPattern => {
try{
const {data: {fields}} = await GenericRequest.request(
'GET',
`/api/index_patterns/_fields_for_wildcard?pattern=${indexPattern.attributes.title}`,
{}
);
return fields;
}catch(error){
return [];
}
}));
}
return indexPatterns.map((indexPattern, idx) => ({...indexPattern, _fields: indexPatternsFields[idx]}));
} catch (error) {
return ((error || {}).data || {}).message || false
? error.data.message
: error.message || error;
let indexPatternsFields;
if(satisfyKibanaVersion('<7.11')){
indexPatternsFields = indexPatterns.map(indexPattern => indexPattern?.attributes?.fields ? JSON.parse(indexPattern.attributes.fields) : []);
}else if(satisfyKibanaVersion('>=7.11')){
indexPatternsFields = await Promise.all(indexPatterns.map(async indexPattern => {
try{
const {data: {fields}} = await GenericRequest.request(
'GET',
`/api/index_patterns/_fields_for_wildcard?pattern=${indexPattern.attributes.title}`,
{}
);
return fields;
}catch(error){
return [];
}
}));
}
return indexPatterns.map((indexPattern, idx) => ({...indexPattern, _fields: indexPatternsFields[idx]}));
}

/**
Expand All @@ -66,30 +60,24 @@ export class SavedObject {
* An index is valid if its fields contain at least these 4 fields: 'timestamp', 'rule.groups', 'agent.id' and 'manager.name'
*/
static async getListOfWazuhValidIndexPatterns(defaultIndexPatterns, where) {
try {
let result = [];
if (where === HEALTH_CHECK) {
const list = await Promise.all(
defaultIndexPatterns.map(
async (pattern) => await SavedObject.getExistingIndexPattern(pattern)
)
);
result = this.validateIndexPatterns(list);
}

if (!result.length) {
const list = await this.getListOfIndexPatterns();
result = this.validateIndexPatterns(list);
}
let result = [];
if (where === HEALTH_CHECK) {
const list = await Promise.all(
defaultIndexPatterns.map(
async (pattern) => await SavedObject.getExistingIndexPattern(pattern)
)
);
result = this.validateIndexPatterns(list);
}

return result.map((item) => {
return { id: item.id, title: item.attributes.title };
});
} catch (error) {
return ((error || {}).data || {}).message || false
? error.data.message
: error.message || error;
if (!result.length) {
const list = await this.getListOfIndexPatterns();
result = this.validateIndexPatterns(list);
}

return result.map((item) => {
return { id: item.id, title: item.attributes.title };
});
}

static validateIndexPatterns(list) {
Expand All @@ -99,12 +87,7 @@ export class SavedObject {
'manager.name',
'agent.id',
];
return list.filter(item => {
if (item._fields) {
return requiredFields.every((reqField => item._fields.some(field => field.name === reqField)));
}
return false;
});
return list.filter(item => item && item._fields && requiredFields.every((reqField => item._fields.some(field => field.name === reqField))));
}

static async existsOrCreateIndexPattern(patternID) {
Expand Down Expand Up @@ -172,7 +155,7 @@ export class SavedObject {
);
let indexPatternFields;
if(satisfyKibanaVersion('<7.11')){
indexPatternFields = JSON.parse(indexPatternData.data.attributes.fields);
indexPatternFields = indexPatternData?.data?.attributes?.fields ? JSON.parse(indexPatternData.data.attributes.fields) : [];
}else if(satisfyKibanaVersion('>=7.11')){
try{
const {data: {fields}} = await GenericRequest.request(
Expand Down Expand Up @@ -227,7 +210,6 @@ export class SavedObject {
},
}
);
return;
} catch (error) {
return ((error || {}).data || {}).message || false
? error.data.message
Expand All @@ -249,8 +231,6 @@ export class SavedObject {
});

await this.refreshFieldsOfIndexPattern(pattern.id, pattern.title, fields);

return;
} catch (error) {
console.log(error)
return ((error || {}).data || {}).message || false
Expand Down