Skip to content

Commit

Permalink
Fix when there are several APIs to migrate in the .wazuh index
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle committed Jul 29, 2019
1 parent b675bac commit f86b818
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
26 changes: 4 additions & 22 deletions server/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ export function Initialize(server) {

const defaultIndexPattern = pattern || 'wazuh-alerts-3.x-*';

// Decrypts the encrypted password
const decryptApiPassword = password => {
return Buffer.from(
password,
'base64'
).toString('ascii');
}

// Save Wazuh App setup
const saveConfiguration = async () => {
try {
Expand Down Expand Up @@ -206,21 +198,11 @@ export function Initialize(server) {
try {
const data = await wzWrapper.getWazuhAPIEntries();
const apiEntries = (((data || {}).hits || {}).hits || []);
apiEntries.forEach(async x => {
const host = x._source;
const added = await updateConfigurationFile.addHost({
url: host.url,
port: host.api_port,
user: host.api_user,
password: decryptApiPassword(host.api_password)
});
if (!added) {
throw new Error('Error adding Api host to config.yml.');
}
});
await updateConfigurationFile.migrateFromIndex(apiEntries)
log('initialize:checkWazuhIndex', 'Index .wazuh will be removed and its content will be migrated to config.yml', 'debug');
//await wzWrapper.deleteIndexByName('.wazuh');
log('initialize:checkWazuhIndex', 'Index .wazuh deleted.', 'debug');
// Delete the documents stored in the index .wazuh
await wzWrapper.deleteDocumentsInIndex('.wazuh');
log('initialize:checkWazuhIndex', 'Content of index .wazuh deleted.', 'debug');
} catch (error) {
throw new Error('Error deleting index .wazuh. ' + error);
}
Expand Down
77 changes: 73 additions & 4 deletions server/lib/update-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,81 @@ export class UpdateConfigurationFile {
return configuration['wazuh.hosts'] || [];
} catch (error) {
this.busy = false;
return Promise.reject(error);
}
}

// Decrypts the encrypted password
decryptApiPassword(password) {
return Buffer.from(
password,
'base64'
).toString('ascii');
}

/**
* Iterate the array with the API entries in given from the .wazuh index in order to create a valid array
* @param {Object} apiEntries
*/
transformIndexedApis(apiEntries) {
const entries = [];
try {
apiEntries.map(entry => {
const id = entry._id;
const host = entry._source;
const api = {
id: id,
url: host.url,
port: host.api_port,
user: host.api_user,
password: this.decryptApiPassword(host.api_password)
};
entries.push(api);
});
} catch (error) {
throw error;
}
return entries;
}


/**
* Calls transformIndexedApis() to get the entries to migrate and after that calls addSeveralHosts()
* @param {Object} apiEntries
*/
async migrateFromIndex(apiEntries) {
try {
const apis = this.transformIndexedApis(apiEntries);
await this.addSeveralHosts(apis);
} catch (error) {
return Promise.reject(error);
}
}

/**
* Recursive function used to add several APIs entries
* @param {Array} hosts
*/
async addSeveralHosts(hosts) {
try {
const entry = hosts.shift();
const response = await this.addHost(entry);
if (hosts && response) {
await this.addSeveralHosts(hosts);
} else {
return 'All APIs entries were migrated to the config.yml'
}
} catch (error) {
return Promise.reject(error);
}
}

/**
*
* @param {Obeject} host
*/
async addHost(host) {
const id = new Date().getTime();
const id = host.id || new Date().getTime();
const compose = this.composeHost(host, id);
const file = path.join(__dirname, '../../config.yml');
let data = await fs.readFileSync(file, { encoding: 'utf-8' });
Expand All @@ -139,7 +208,7 @@ export class UpdateConfigurationFile {
return id;
} catch (error) {
this.busy = false;
throw error;
return Promise.reject(error);
}
}

Expand Down Expand Up @@ -176,7 +245,7 @@ export class UpdateConfigurationFile {
return true;
} catch (error) {
this.busy = false;
throw error;
return Promise.reject(error);
}
}

Expand Down Expand Up @@ -207,7 +276,7 @@ export class UpdateConfigurationFile {
return true;
} catch (error) {
this.busy = false;
throw error;
return Promise.reject(error);
}
}
}

0 comments on commit f86b818

Please sign in to comment.