wazuh-kibana-app/public/services/config-handler.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-01-28 10:02:39 +00:00
/*
* Wazuh app - Group handler service
* Copyright (C) 2015-2019 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
2019-02-07 16:25:14 +00:00
export class ConfigHandler {
constructor($rootScope, apiReq, errorHandler) {
2019-01-28 10:02:39 +00:00
this.apiReq = apiReq;
this.$rootScope = $rootScope;
this.errorHandler = errorHandler;
2019-01-28 10:02:39 +00:00
}
2019-02-04 09:40:07 +00:00
/**
* Send ossec.conf content for manager (single-node API call)
* @param {*} content XML raw content for ossec.conf file
*/
2019-01-28 10:02:39 +00:00
async saveManagerConfiguration(content) {
try {
const result = await this.apiReq.request(
'POST',
`/manager/files?path=etc/ossec.conf&overwrite=true`,
2019-01-28 10:02:39 +00:00
{ content, origin: 'xmleditor' }
);
return result;
} catch (error) {
return Promise.reject(error);
}
2019-02-04 09:40:07 +00:00
}
2019-01-28 10:02:39 +00:00
2019-02-04 09:40:07 +00:00
/**
* Send ossec.conf content for a cluster node
* @param {*} node Node name
* @param {*} content XML raw content for ossec.conf file
*/
2019-02-04 09:40:19 +00:00
async saveNodeConfiguration(node, content) {
2019-01-28 10:02:39 +00:00
try {
2019-02-06 09:56:02 +00:00
const result = await this.apiReq.request(
'POST',
`/cluster/${node}/files?path=etc/ossec.conf&overwrite=true`,
2019-02-06 09:56:02 +00:00
{ content, origin: 'xmleditor' }
);
2019-01-28 10:02:39 +00:00
return result;
} catch (error) {
return Promise.reject(error);
}
2019-02-04 09:40:19 +00:00
}
2019-02-07 12:46:20 +00:00
async performClusterRestart() {
try {
await this.apiReq.request('PUT', `/cluster/restart`, { delay: 15000 });
this.$rootScope.$broadcast('removeRestarting', {});
} catch (error) {
this.$rootScope.$broadcast('removeRestarting', {});
this.errorHandler.handle(error, 'Error restarting cluster');
}
}
2019-02-07 12:46:20 +00:00
/**
2019-02-11 09:13:13 +00:00
* Restart manager (single-node API call)
*/
2019-02-07 12:46:20 +00:00
async restartManager() {
try {
const validationError = await this.apiReq.request(
'GET',
`/manager/configuration/validation`,
{}
);
2019-02-11 09:13:13 +00:00
const data = ((validationError || {}).data || {}).data || {};
const isOk = data.status === 'OK';
if (!isOk && Array.isArray(data.details)) {
const str = data.details.join();
throw new Error(str);
}
2019-02-11 09:13:13 +00:00
const result = await this.apiReq.request('PUT', `/manager/restart`, {});
2019-02-07 12:46:20 +00:00
return result;
} catch (error) {
return Promise.reject(error);
}
}
2019-02-07 16:25:14 +00:00
/**
2019-02-11 09:13:13 +00:00
* Restart cluster
*/
2019-02-07 16:25:14 +00:00
async restartCluster() {
try {
const validationError = await this.apiReq.request(
'GET',
`/cluster/configuration/validation`,
{}
);
2019-02-11 09:13:13 +00:00
const data = ((validationError || {}).data || {}).data || {};
const isOk = data.status === 'OK';
if (!isOk && Array.isArray(data.details)) {
const str = data.details.join();
throw new Error(str);
}
this.performClusterRestart();
return { data: { data: 'Restarting cluster' } };
2019-02-07 16:25:14 +00:00
} catch (error) {
return Promise.reject(error);
}
}
2019-02-07 12:46:20 +00:00
/**
2019-02-11 09:13:13 +00:00
* Restart a cluster node
*/
2019-02-07 12:46:20 +00:00
async restartNode(node) {
try {
const validationError = await this.apiReq.request(
'GET',
`/cluster/${node}/configuration/validation`,
{}
);
2019-02-11 09:13:13 +00:00
const data = ((validationError || {}).data || {}).data || {};
const isOk = data.status === 'OK';
if (!isOk && Array.isArray(data.details)) {
const str = data.details.join();
throw new Error(str);
}
2019-02-07 12:46:20 +00:00
const result = await this.apiReq.request(
'PUT',
`/cluster/${node}/restart`,
{}
);
return result;
} catch (error) {
return Promise.reject(error);
}
}
2019-01-28 10:02:39 +00:00
}