Skip to content

Commit

Permalink
Remove .wazuh documents after migrate APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle committed Jul 29, 2019
1 parent e1dad8d commit a6f7929
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions server/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ export function Initialize(server) {
await updateConfigurationFile.migrateFromIndex(apiEntries)
log('initialize:checkWazuhIndex', 'Index .wazuh will be removed and its content will be migrated to config.yml', 'debug');
// Check if all APIs entries were migrated properly and delete it from the .wazuh index
if (await checkProperlyMigrate()){
await wzWrapper.deleteDocumentsInIndex('.wazuh');
const apisToDelete = await checkProperlyMigrate();
if (apisToDelete.length){
apisToDelete.map(async id => {
await wzWrapper.deleteWazuhAPIEntriesWithRequest({'params': {'id': id}});
});
} else {
throw new Error('Cannot migrate all APIs from .wazuh index to the config.yml file properly.');
}
Expand All @@ -229,10 +232,11 @@ export function Initialize(server) {
const apisConfigKeys = apisConfig.map(api => {
return Object.keys(api)[0];
});

apisIndexKeys.map(k => {
if (!apisConfigKeys.includes(k)) throw new Error('Cannot migrate all the APIs entries.');
});
return apisIndexKeys;
}

const checkWazuhVersionRegistry = async () => {
Expand Down
22 changes: 22 additions & 0 deletions server/lib/elastic-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,28 @@ export class ElasticWrapper {
}
}

/**
* Delete a Wazuh API entry using incoming request
* @param {*} req
*/
async deleteWazuhAPIEntriesWithRequest(req) {
try {
if (!req.params || !req.params.id)
return Promise.reject(new Error('No API id given'));

const data = await this.elasticRequest.callWithRequest(req, 'delete', {
index: '.wazuh',
type: '_doc',
id: req.params.id,
refresh: true
});

return data;
} catch (error) {
return Promise.reject(error);
}
}

/**
* Deletes wazuh monitoring index pattern
*/
Expand Down

0 comments on commit a6f7929

Please sign in to comment.