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

290 lines
8.1 KiB
JavaScript
Raw Normal View History

2018-09-26 09:10:43 +00:00
/*
* Wazuh app - Configuration handler class
2019-01-14 16:36:47 +00:00
* Copyright (C) 2015-2019 Wazuh, Inc.
2018-09-26 09:10:43 +00:00
*
* 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.
*/
import js2xmlparser from 'js2xmlparser';
import XMLBeautifier from './xml-beautifier';
import { queryConfig } from '../services/query-config';
2018-09-28 13:23:47 +00:00
import { objectWithoutProperties } from './remove-hash-key.js';
2018-09-26 09:10:43 +00:00
export class ConfigurationHandler {
constructor(apiReq, errorHandler) {
2018-09-28 13:23:47 +00:00
this.apiReq = apiReq;
this.errorHandler = errorHandler;
2018-09-26 09:10:43 +00:00
}
buildIntegrations(list, $scope) {
if (!list || !list.length) return;
for (const integration of list)
$scope.integrations[integration.name] = integration;
}
parseWodle(config, wodleKey) {
try {
const wmodulesArray =
((config || {})['wmodules-wmodules'] || {}).wmodules || [];
const result = wmodulesArray.filter(
item => typeof item[wodleKey] !== 'undefined'
);
return result.length ? result[0][wodleKey] : false;
} catch (error) {
return false;
}
}
2018-09-26 09:10:43 +00:00
/**
* Switchs between configuration tabs
* @param {string} configurationTab The configuration tab to open
* @param {Array<object>} sections Array that includes sections to be fetched
*/
async switchConfigTab(
configurationTab,
sections,
$scope,
agentId = false,
node = false
) {
2018-09-26 09:10:43 +00:00
try {
$scope.load = true;
$scope.currentConfig = null;
$scope.XMLContent = false;
$scope.JSONContent = false;
//$scope.configurationSubTab = false;
2018-09-26 09:10:43 +00:00
$scope.configurationTab = configurationTab;
$scope.currentConfig = await queryConfig(
agentId || '000',
sections,
this.apiReq,
this.errorHandler,
node
2018-09-26 09:10:43 +00:00
);
if ($scope.configurationSubTab === 'pm-sca') {
$scope.currentConfig.sca = this.parseWodle($scope.currentConfig, 'sca');
}
2018-09-26 09:10:43 +00:00
if (sections[0].component === 'integrator') {
this.buildIntegrations(
2018-11-28 16:21:52 +00:00
$scope.currentConfig['integrator-integration'].integration,
$scope
2018-09-26 09:10:43 +00:00
);
} else {
$scope.integrations = {};
}
if (($scope.currentConfig['logcollector-localfile'] || {}).localfile) {
const data = $scope.currentConfig['logcollector-localfile'].localfile;
$scope.currentConfig['logcollector-localfile'][
'localfile-logs'
] = data.filter(item => typeof item.file !== 'undefined');
$scope.currentConfig['logcollector-localfile'][
'localfile-commands'
] = data.filter(item => typeof item.file === 'undefined');
const sanitizeLocalfile = file => {
if (file.target) {
file.targetStr = '';
file.target.forEach(function(target, idx) {
file.targetStr = file.targetStr.concat(target);
if (idx != file.target.length - 1) {
file.targetStr = file.targetStr.concat(', ');
}
});
}
};
$scope.currentConfig['logcollector-localfile'][
'localfile-logs'
].forEach(sanitizeLocalfile);
$scope.currentConfig['logcollector-localfile'][
'localfile-commands'
].forEach(sanitizeLocalfile);
}
2018-09-26 09:10:43 +00:00
$scope.load = false;
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
} catch (error) {
2019-03-26 20:37:23 +00:00
this.errorHandler.handle(error.message || error);
2018-09-26 09:10:43 +00:00
$scope.load = false;
}
return;
}
/**
* Switchs to a wodle section
* @param {string} wodleName The wodle to open
*/
async switchWodle(wodleName, $scope, agentId = false, node = false) {
2018-09-26 09:10:43 +00:00
try {
$scope.load = true;
$scope.currentConfig = null;
$scope.XMLContent = false;
$scope.JSONContent = false;
//$scope.configurationSubTab = false;
2018-09-26 09:10:43 +00:00
$scope.configurationTab = wodleName;
$scope.currentConfig = await queryConfig(
agentId || '000',
[{ component: 'wmodules', configuration: 'wmodules' }],
this.apiReq,
this.errorHandler,
node
2018-09-26 09:10:43 +00:00
);
// Filter by provided wodleName
let result = [];
if (
wodleName &&
(($scope.currentConfig || {})['wmodules-wmodules'] || {}).wmodules
2018-09-26 09:10:43 +00:00
) {
result = $scope.currentConfig['wmodules-wmodules'].wmodules.filter(
item => typeof item[wodleName] !== 'undefined'
);
}
if (result.length) {
$scope.currentConfig =
wodleName === 'command'
? { commands: result.map(item => item.command) }
: result[0];
}
$scope.load = false;
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
} catch (error) {
2019-03-26 20:37:23 +00:00
this.errorHandler.handle(error.message || error);
2018-09-26 09:10:43 +00:00
$scope.load = false;
}
return;
}
/**
* Determines if a wodle is enabled or not
* @param {string} wodleName The wodle to check
* @param {string} agentId The agent ID
*/
async isWodleEnabled(wodleName, agentId = false) {
try {
// Get wodles configuration
2018-09-28 13:23:47 +00:00
const wodlesConfig = await queryConfig(
agentId || '000',
[{ component: 'wmodules', configuration: 'wmodules' }],
this.apiReq,
this.errorHandler
);
// Filter by provided wodleName
let result = [];
if (
wodleName &&
2018-09-28 13:23:47 +00:00
wodleName !== 'command' &&
((wodlesConfig || {})['wmodules-wmodules'] || {}).wmodules
) {
result = wodlesConfig['wmodules-wmodules'].wmodules.filter(
2018-09-28 13:23:47 +00:00
item =>
typeof item[wodleName] !== 'undefined' &&
item[wodleName].disabled === 'no'
);
}
2018-09-28 13:23:47 +00:00
return !!result.length;
} catch (error) {
return false;
}
}
2018-09-26 09:10:43 +00:00
/**
* Switchs between configuration tabs
* @param {*} configurationTab
*/
switchConfigurationTab(configurationTab, $scope) {
$scope.selectedItem = 0;
$scope.currentConfig = null;
$scope.XMLContent = false;
$scope.JSONContent = false;
//$scope.configurationSubTab = false;
2018-09-26 09:10:43 +00:00
$scope.configurationTab = configurationTab;
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
}
/**
* Switchs between configuration sub-tabs
* @param {*} configurationSubTab
*/
switchConfigurationSubTab(configurationSubTab, $scope) {
$scope.selectedItem = 0;
$scope.XMLContent = false;
$scope.JSONContent = false;
$scope.configurationSubTab = configurationSubTab;
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
}
/**
* Assigns XML raw content for specific configuration
* @param {object} config Raw content to show in XML
*/
getXML($scope) {
const config = {};
Object.assign(config, $scope.currentConfig);
$scope.JSONContent = false;
if ($scope.XMLContent) {
$scope.XMLContent = false;
} else {
try {
const cleaned = objectWithoutProperties(config);
$scope.XMLContent = XMLBeautifier(
js2xmlparser.parse('configuration', cleaned)
);
$scope.$broadcast('XMLContentReady', { data: $scope.XMLContent });
2018-09-26 09:10:43 +00:00
} catch (error) {
$scope.XMLContent = false;
}
}
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
}
2019-02-04 09:39:58 +00:00
json2xml(data) {
2019-02-04 09:40:19 +00:00
if (data) {
const result = XMLBeautifier(js2xmlparser.parse('configuration', data));
2019-01-23 12:54:26 +00:00
return result;
}
2019-02-04 09:39:58 +00:00
return false;
2019-01-23 12:54:26 +00:00
}
2018-09-26 09:10:43 +00:00
/**
* Assigns JSON raw content for specific configuration
* @param {object} config Raw content to show in JSON
*/
getJSON($scope) {
const config = {};
Object.assign(config, $scope.currentConfig);
$scope.XMLContent = false;
if ($scope.JSONContent) {
$scope.JSONContent = false;
} else {
try {
const cleaned = objectWithoutProperties(config);
2019-01-16 09:58:40 +00:00
$scope.JSONContent = JSON.stringify(cleaned, null, 2);
$scope.$broadcast('JSONContentReady', { data: $scope.JSONContent });
2018-09-26 09:10:43 +00:00
} catch (error) {
$scope.JSONContent = false;
}
}
$scope.$applyAsync();
2018-09-26 09:10:43 +00:00
}
reset($scope) {
$scope.currentConfig = null;
$scope.configurationTab = '';
$scope.configurationSubTab = '';
$scope.integrations = {};
$scope.selectedItem = 0;
}
2018-09-26 09:10:43 +00:00
}