Skip to content

Commit

Permalink
Fill cluster info when API was added in the config.yml directly
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle committed Aug 1, 2019
1 parent c525de6 commit 2a57fda
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 15 deletions.
70 changes: 59 additions & 11 deletions public/controllers/settings/components/api-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
};
23 changes: 21 additions & 2 deletions public/controllers/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
20 changes: 19 additions & 1 deletion server/controllers/wazuh-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions server/lib/update-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion server/lib/update-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions server/routes/wazuh-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 2a57fda

Please sign in to comment.