Skip to content

Commit

Permalink
Update cluster information in the wazuh-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle committed Jul 31, 2019
1 parent 2173008 commit a500cdc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
7 changes: 2 additions & 5 deletions server/controllers/wazuh-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,8 @@ export class WazuhApiCtrl {
status: clusterData.status
}

// TODO update manager/cluster name information
const r = await this.configurationFile.updateWazuhClusterInfo(apiId, api.cluster_info);
/*await this.wzWrapper.updateWazuhIndexDocument(null, req.payload, {
doc: { cluster_info: api.cluster_info }
});*/
await this.wazuhRegistry.updateWazuhClusterInfo(apiId, api.cluster_info);

} catch (error) {
log('wazuh-api:checkStoredAPI', error.message || error);
}
Expand Down
81 changes: 80 additions & 1 deletion server/lib/update-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,91 @@ export class UpdateRegistry {
}
}

/**
* Writes the wazuh-registry.json
* @param {Object} content
*/
async writeContent(content){
try {
if (this.busy) {
throw new Error('Another process is updating the registry file');
}
this.busy = true;
await fs.writeFileSync(this.file, JSON.stringify(content));
this.busy = false;
} catch (error) {
return Promise.reject(error)
}
}

/**
* Uptades the cluster information associated with the current selected API
* @param {String} id
* @param {Object} clusterInfo
*/
async updateWazuhClusterInfo(id, clusterInfo) {
return;
try {
const content = await this.readContent();
const hosts = content.hosts || [];
const data = Object.assign({'id': id}, clusterInfo);
if (!hosts.length || !this.checkHost(id, hosts)) {
content.hosts.push(data);
} else {
content.hosts = Object.assign(this.updateHostInfo(id, hosts, clusterInfo));
}
this.writeContent(content);
} catch (error) {
return Promise.reject(error);
}
}

/**
* Checks if the host exist in order to update the data, otherwise creates it
* @param {String} id
* @param {Object} hosts
*/
checkHost(id, hosts) {
try {
const exists = hosts.filter(host => {
return host.id == id;
});
return exists.length === 1; //Checks if the host exists and if is an unique
} catch (error) {
return Promise.reject(error);
}
}

/**
* Replace the existing host information with the new and return the hosts list updated
* @param {String} id
* @param {Object} hosts
* @param {Object} clusterInfo
*/
updateHostInfo(id, hosts, clusterInfo){
try {
const idx = this.findIndexHost(id, hosts)
hosts[idx] = Object.assign({'id': id}, clusterInfo);
return hosts;
} catch (error) {
return Promise.reject(error);
}
}

/**
* Returns the index of the array where the API is stored
* @param {String} id
* @param {Array} hosts
*/
findIndexHost(id, hosts){
try {
let i = 0;
while (i < hosts.length){
if (hosts[i].id == id) break;
i++;
}
return i;
} catch (error) {
return Promise.reject(error)
}
}
}

0 comments on commit a500cdc

Please sign in to comment.