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 {
|
2019-03-26 21:12:24 +00:00
|
|
|
constructor($rootScope, apiReq, errorHandler) {
|
2019-01-28 10:02:39 +00:00
|
|
|
this.apiReq = apiReq;
|
2019-02-21 14:42:12 +00:00
|
|
|
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',
|
2019-03-04 11:55:26 +00:00
|
|
|
`/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',
|
2019-03-04 11:55:26 +00:00
|
|
|
`/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
|
|
|
|
2019-02-21 14:42:12 +00:00
|
|
|
async performClusterRestart() {
|
|
|
|
try {
|
2019-03-11 08:34:32 +00:00
|
|
|
await this.apiReq.request('PUT', `/cluster/restart`, { delay: 15000 });
|
2019-04-01 08:04:24 +00:00
|
|
|
this.$rootScope.$broadcast('removeRestarting', {});
|
2019-02-21 14:42:12 +00:00
|
|
|
} catch (error) {
|
2019-04-01 08:04:24 +00:00
|
|
|
this.$rootScope.$broadcast('removeRestarting', {});
|
2019-02-21 14:42:12 +00:00
|
|
|
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 {
|
2019-02-08 15:47:57 +00:00
|
|
|
const validationError = await this.apiReq.request(
|
|
|
|
'GET',
|
|
|
|
`/manager/configuration/validation`,
|
|
|
|
{}
|
|
|
|
);
|
2019-02-11 09:13:13 +00:00
|
|
|
|
2019-02-13 16:29:00 +00:00
|
|
|
const data = ((validationError || {}).data || {}).data || {};
|
|
|
|
const isOk = data.status === 'OK';
|
|
|
|
if (!isOk && Array.isArray(data.details)) {
|
2019-02-17 11:18:33 +00:00
|
|
|
const str = data.details.join();
|
2019-02-13 16:29:00 +00:00
|
|
|
throw new Error(str);
|
2019-02-08 15:47:57 +00:00
|
|
|
}
|
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 {
|
2019-02-08 15:47:57 +00:00
|
|
|
const validationError = await this.apiReq.request(
|
|
|
|
'GET',
|
|
|
|
`/cluster/configuration/validation`,
|
|
|
|
{}
|
|
|
|
);
|
2019-02-11 09:13:13 +00:00
|
|
|
|
2019-02-13 16:29:00 +00:00
|
|
|
const data = ((validationError || {}).data || {}).data || {};
|
|
|
|
const isOk = data.status === 'OK';
|
|
|
|
if (!isOk && Array.isArray(data.details)) {
|
2019-02-17 11:18:33 +00:00
|
|
|
const str = data.details.join();
|
2019-02-13 16:29:00 +00:00
|
|
|
throw new Error(str);
|
2019-02-08 15:47:57 +00:00
|
|
|
}
|
2019-03-11 08:34:32 +00:00
|
|
|
this.performClusterRestart();
|
2019-02-21 14:42:12 +00:00
|
|
|
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 {
|
2019-02-08 15:47:57 +00:00
|
|
|
const validationError = await this.apiReq.request(
|
|
|
|
'GET',
|
|
|
|
`/cluster/${node}/configuration/validation`,
|
|
|
|
{}
|
|
|
|
);
|
2019-02-11 09:13:13 +00:00
|
|
|
|
2019-02-13 16:29:00 +00:00
|
|
|
const data = ((validationError || {}).data || {}).data || {};
|
|
|
|
const isOk = data.status === 'OK';
|
|
|
|
if (!isOk && Array.isArray(data.details)) {
|
2019-02-17 11:18:33 +00:00
|
|
|
const str = data.details.join();
|
2019-02-13 16:29:00 +00:00
|
|
|
throw new Error(str);
|
2019-02-08 15:47:57 +00:00
|
|
|
}
|
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
|
|
|
}
|