Skip to content

Commit

Permalink
Clean registry hosts when deleting from config.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle committed Aug 2, 2019
1 parent 66d0d9e commit ab6ef8e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 29 deletions.
72 changes: 45 additions & 27 deletions public/controllers/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,33 @@ export class SettingsController {
*/
$onInit() {
// Loading data
this.getSettings().then(() => {
const restApiTableProps = {
currentDefault: this.currentDefault,
compressed: true,
setDefault: entry => this.setDefault(entry),
checkManager: (entry, isIndex = false, silent = false) => this.checkManager(entry, isIndex, silent),
removeManager: entry => this.removeManager(entry),
getApisInRegistry: () => this.getApisInRegistry(),
getSelectedApiIndex: item => this.getSelectedApiIndex(item),
digest: () => this.$scope.$applyAsync(),
handleError: error => this.errorHandler.handle(error),
setHostRegistryInfo: id => this.setHostRegistryInfo(id),
updateSettings: (entry, useItem = false) =>
this.updateSettings(entry, useItem),
switch: () => this.switch()
};
this.apiTableProps = Object.assign(this.apiTableProps, restApiTableProps);
this.addApiProps = {
saveSettings: entry => this.saveSettings(entry)
};
return this.getAppInfo();
});
this.cleanRegistryHosts().then(() => {
this.getSettings().then(() => {
const restApiTableProps = {
currentDefault: this.currentDefault,
compressed: true,
setDefault: entry => this.setDefault(entry),
checkManager: (entry, isIndex = false, silent = false) => this.checkManager(entry, isIndex, silent),
removeManager: entry => this.removeManager(entry),
getApisInRegistry: () => this.getApisInRegistry(),
getSelectedApiIndex: item => this.getSelectedApiIndex(item),
digest: () => this.$scope.$applyAsync(),
handleError: error => this.errorHandler.handle(error),
setHostRegistryInfo: id => this.setHostRegistryInfo(id),
updateSettings: (entry, useItem = false) =>
this.updateSettings(entry, useItem),
switch: () => this.switch()
};
this.apiTableProps = Object.assign(this.apiTableProps, restApiTableProps);
this.addApiProps = {
saveSettings: entry => this.saveSettings(entry)
};
return this.getAppInfo();
});
})
.catch(error => {
this.errorHandler.handle(error);
})
}

/**
Expand Down Expand Up @@ -270,11 +275,11 @@ export class SettingsController {
try {
const keys = apis.map(api => { return Object.keys(api)[0] });
const cleanKeys = [...new Set(keys)];
if (keys.length !== cleanKeys.length){
if (keys.length !== cleanKeys.length) {
this.duplicatedApisKeys = true;
this.errMsg = 'Please check the config.yml file, there were found duplicated APIs keys.'
}
return
return
} catch (error) {
console.error('Error checking duplicated APIs keys');
throw error;
Expand Down Expand Up @@ -844,10 +849,10 @@ export class SettingsController {
* Saves the cluster info of the API in the registry
* @param {Object} newEntry
*/
async saveApiInRegistry(newEntry){
async saveApiInRegistry(newEntry) {
try {
const id = newEntry._id || newEntry.id;
const host = Object.assign({id: id}, newEntry.cluster_info);
const host = Object.assign({ id: id }, newEntry.cluster_info);
return await this.genericReq.request(
'POST',
'/api/registry/add',
Expand All @@ -868,10 +873,23 @@ export class SettingsController {
if (!host.data || !host.data[id]) throw new Error(`Cannot update API ${id} registry`);
const info = await this.testAPI.check(host.data[id]);
const clusterInfo = info.data || {};
const api = Object.assign({id: id}, {cluster_info: clusterInfo});
const api = Object.assign({ id: id }, { cluster_info: clusterInfo });
return await this.saveApiInRegistry(api);
} catch (error) {
return Promise.reject(error);
}
}

/**
* Clean the wazuh-registry.json hosts in order to prevent information whithout it corresponding API entry.
*/
async cleanRegistryHosts() {
try {
const data = await this.genericReq.request('GET', '/apis/registry');
const registryHosts = data.data || [];
await this.genericReq.request('POST', '/apis/registry/clean', registryHosts);
} catch (error) {
return Promise.reject(error);
}
}
}
14 changes: 14 additions & 0 deletions server/controllers/wazuh-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1744,4 +1744,18 @@ export class WazuhApiCtrl {
return ErrorResponse('Missing parameters', 2015, 500, reply);
}
}

async cleanRegistryHosts(req, reply) {
try {
const registryKeys = req.payload.map(r => r.id.toString());
const hosts = await this.configurationFile.getHosts();
const hostsKeys = hosts.map(h => Object.keys(h)[0]);
const diff = registryKeys.filter(r => !hostsKeys.includes(r));
await this.wazuhRegistry.cleanRegistryHosts(diff);
return true;
} catch (error) {
log('wazuh-api:cleanRegistryHosts', error.message || error);
return ErrorResponse('Missing parameters', 2016, 500, reply);
}
}
}
37 changes: 35 additions & 2 deletions server/lib/update-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import fs from 'fs';
import path from 'path';
import { log } from '../logger';
import { ErrorResponse } from '../controllers/error-response';

export class UpdateRegistry {
constructor() {
Expand Down Expand Up @@ -147,7 +146,7 @@ export class UpdateRegistry {
}

/**
*
* Delete a host
* @param {Object} req
*/
async deleteHost(req) {
Expand All @@ -167,6 +166,24 @@ export class UpdateRegistry {
}
}

/**
* Deletes several hosts
* @param {Array} keys
*/
async deleteSeveralHosts(keys) {
try {
log('update-registry:deleteSeveralHosts', 'Deleting several registry hosts.', 'debug');
const content = await this.readContent();
const hosts = content.hosts || [];
const hostsUpdated = hosts.filter(h => !keys.includes(h.id.toString()));
content.hosts = hostsUpdated;
await this.writeContent(content);
} catch (error) {
log('update-registry:deleteSeveralHosts', error.message || error);
return Promise.reject(error);
}
}

/**
* Returns the cluster information associated to an API id
* @param {String} id
Expand All @@ -178,9 +195,25 @@ export class UpdateRegistry {
const info = hosts.filter(h => {
return h.id == id;
});
return info[0];
} catch (error) {
log('update-registry:getClusterInfoByAPI', error.message || error);
return Promise.reject(error);
}
}

/**
* Cleans the registry of the APIs ids given
* @param {Array} keys
*/
async cleanRegistryHosts(keys) {
try {
log('update-registry:cleanRegistryHosts', 'Cleaning registry hosts.', 'debug');
await this.deleteSeveralHosts(keys);
return 'Cleaning the registry hosts successfully';
} catch (error) {
log('update-registry:cleanRegistryHosts', error.message || error);
return Promise.reject(error);
}
}
}
8 changes: 8 additions & 0 deletions server/routes/wazuh-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,12 @@ export function WazuhApiRoutes(server) {
return ctrl.saveApiInRegistry(req, reply);
}
});

server.route({
method: 'POST',
path: '/apis/registry/clean',
handler(req, reply) {
return ctrl.cleanRegistryHosts(req, reply);
}
});
}

0 comments on commit ab6ef8e

Please sign in to comment.