From 2a57fdada5b4b3e497e98c38e1b92ae49eab17f6 Mon Sep 17 00:00:00 2001 From: Adri Valle Date: Thu, 1 Aug 2019 14:07:51 +0200 Subject: [PATCH] Fill cluster info when API was added in the config.yml directly --- .../settings/components/api-table.js | 70 ++++++++++++++++--- public/controllers/settings/settings.js | 23 +++++- server/controllers/wazuh-api.js | 20 +++++- server/lib/update-configuration.js | 16 +++++ server/lib/update-registry.js | 3 +- server/routes/wazuh-api.js | 8 +++ 6 files changed, 125 insertions(+), 15 deletions(-) diff --git a/public/controllers/settings/components/api-table.js b/public/controllers/settings/components/api-table.js index c57bca2de4..f6902b7a3d 100644 --- a/public/controllers/settings/components/api-table.js +++ b/public/controllers/settings/components/api-table.js @@ -34,19 +34,65 @@ export class ApiTable extends Component { this.editionEnabled = false; } + + async getHostsInconfig() { + try { + const result = await this.props.getApisInRegistry(); + return result.data || []; + } catch (error) { + return Promise.reject(error) + } + } + + /** + * Binds the cluster info to the API entries + */ async bindClusterInfo() { - if (this.editionEnabled) return; - const result = await this.props.getApisInRegistry(); - const hosts = result.data || []; - hosts.map(h => { - const idx = this.props.getSelectedApiIndex(h); - delete h.id; - this.props.apiEntries[idx].cluster_info = h; - }); - this.props.digest(); + try { + if (this.editionEnabled) return; + const hosts = await this.getHostsInconfig(); + await this.checkMissingApisInRegistry(angular.copy(hosts)); + hosts.map(h => { + this.setApiEntryInfo(h, h); + }); + this.props.digest(); + } catch (error) { + this.props.handleError('Cannot get cluster information about API entries'); + } + } + + /** + * Set in the apiEntries the cluster information for each API + * @param {String} id + * @param {Object} info + */ + setApiEntryInfo(id, info) { + const idx = this.props.getSelectedApiIndex(id); + delete info.id; + this.props.apiEntries[idx].cluster_info = info; + } + + /** + * Checks if in the wazuh-registry.json is missing any API entry of the config.yml in order to create it. + * @param {Object} registryHosts + */ + async checkMissingApisInRegistry(registryHosts) { + try { + const registryIds = registryHosts.map(h => {return h.id.toString()}); + const entriesIds = this.props.apiEntries.map(e => {return e._id}); + const diff = entriesIds.filter(api => !registryIds.includes(api)); + if (!diff.length) return; + diff.map(async id => { + const api = await this.props.setHostRegistryInfo(id); + const info = api.data || {}; + this.setApiEntryInfo(id, info); + }); + } catch (error) { + this.props.handleError('Cannot update registry'); + } } - shouldComponentUpdate() { + shouldComponentUpdate() { this.bindClusterInfo(); return true; } @@ -269,5 +315,7 @@ ApiTable.propTypes = { getSelectedApiIndex: PropTypes.func, digest: PropTypes.func, removeManager: PropTypes.func, - switch: PropTypes.func + switch: PropTypes.func, + handleError: PropTypes.func, + setHostRegistryInfo: PropTypes.func, }; diff --git a/public/controllers/settings/settings.js b/public/controllers/settings/settings.js index 674fef7971..ee959f8d7a 100644 --- a/public/controllers/settings/settings.js +++ b/public/controllers/settings/settings.js @@ -117,6 +117,8 @@ export class SettingsController { 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() @@ -605,7 +607,7 @@ export class SettingsController { * @param {Object} item */ getSelectedApiIndex(item) { - const id = item._id || item.id; + const id = item._id || item.id || item; return this.apiTableProps.apiEntries.map(item => (item._id).toString()).indexOf((id).toString()); } @@ -846,7 +848,7 @@ export class SettingsController { try { const id = newEntry._id || newEntry.id; const host = Object.assign({id: id}, newEntry.cluster_info); - await this.genericReq.request( + return await this.genericReq.request( 'POST', '/api/registry/add', host @@ -855,4 +857,21 @@ export class SettingsController { return Promise.reject(error); } } + + /** + * Save the missing APIs information in the registry + * @param {String} id + */ + async setHostRegistryInfo(id) { + try { + const host = await this.genericReq.request('GET', `/api/registry/${id}`); + 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}); + return await this.saveApiInRegistry(api); + } catch (error) { + return Promise.reject(error); + } + } } diff --git a/server/controllers/wazuh-api.js b/server/controllers/wazuh-api.js index a9435189ed..ee9b96fc17 100644 --- a/server/controllers/wazuh-api.js +++ b/server/controllers/wazuh-api.js @@ -1697,8 +1697,10 @@ export class WazuhApiCtrl { /** * Get the hosts in the registry + * @param {Object} req + * @param {Object} reply */ - async getApisInRegistry(req, reply){ + async getApisInRegistry(req, reply) { try { log('wazuh-api:getApisInRegistry', 'Getting APIs info fromt registry', 'debug'); return await this.wazuhRegistry.getHosts(); @@ -1708,6 +1710,22 @@ export class WazuhApiCtrl { } } + /** + * Get an API by id in the registry + * @param {Object} req + * @param {Object} reply + */ + async getApiById(req, reply) { + try { + log('wazuh-api:getApiById', 'Getting API info from registry', 'debug'); + if (!req || !req.params || !req.params.id) throw new Error('API id is missing'); + return await this.configurationFile.getHostById(req.params.id); + } catch (error) { + log('wazuh-api:getApiById', error.message || error); + return ErrorResponse('Missing parameters', 2016, 500, reply); + } + } + /** * Returns the cluster information associated to an API id * @param {Object} req diff --git a/server/lib/update-configuration.js b/server/lib/update-configuration.js index 1c80939ce6..81c426fff5 100644 --- a/server/lib/update-configuration.js +++ b/server/lib/update-configuration.js @@ -130,6 +130,22 @@ export class UpdateConfigurationFile { } } + /** + * Get host by id + * @param {String} id + */ + async getHostById(id) { + try { + log('update-configuration:getHostById', `Getting host ${id}`, 'debug'); + const hosts = await this.getHosts(); + const host = hosts.filter(h => { return Object.keys(h)[0] == id }); + return host[0]; + } catch (error) { + log('update-configuration:getHostById', error.message || error); + return Promise.reject(error); + } + } + // Decrypts the encrypted password decryptApiPassword(password) { return Buffer.from( diff --git a/server/lib/update-registry.js b/server/lib/update-registry.js index aa712c7db2..f10de2355b 100644 --- a/server/lib/update-registry.js +++ b/server/lib/update-registry.js @@ -85,7 +85,8 @@ export class UpdateRegistry { } this.writeContent(content); log('update-registry:updateWazuhClusterInfo', `API ${id} was properly updated`, 'debug'); - return `API ${id} was successful updated.`; + const updatedHost = content.hosts.filter(h => {return h.id == id}); + return updatedHost[0]; } catch (error) { log('update-registry:updateWazuhClusterInfo', error.message || error); return Promise.reject(error); diff --git a/server/routes/wazuh-api.js b/server/routes/wazuh-api.js index 3defe4aca5..050f637ea0 100644 --- a/server/routes/wazuh-api.js +++ b/server/routes/wazuh-api.js @@ -176,6 +176,14 @@ export function WazuhApiRoutes(server) { } }); + server.route({ + method: 'GET', + path: '/api/registry/{id}', + handler(req, reply) { + return ctrl.getApiById(req, reply); + } + }); + server.route({ method: 'GET', path: '/apis/registry',