Patching API version within the App

This commit is contained in:
Pedro Sanchez 2017-06-23 20:26:24 +02:00 committed by GitHub
commit 30cb219a80
2 changed files with 89 additions and 22 deletions

View File

@ -3,23 +3,29 @@ module.exports = function (server, options) {
const pciRequirementsFile = ''; const pciRequirementsFile = '';
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const needle = require('needle');
var fetchAgentsExternal = require(path.resolve(__dirname, "../wazuh-monitoring.js")); var fetchAgentsExternal = require(path.resolve(__dirname, "../wazuh-monitoring.js"));
const wazuh_config_file = '../../configuration/config.json'; const wazuh_config_file = '../../configuration/config.json';
const wazuh_temp_file = '../../configuration/.patch_version';
var colors = require('ansicolors'); var colors = require('ansicolors');
var blueWazuh = colors.blue('wazuh'); var blueWazuh = colors.blue('wazuh');
var wazuh_config = {}; var wazuh_config = {};
var wazuh_api_version;
// Read Wazuh App configuration file // Read Wazuh App configuration file
try { try {
wazuh_config = JSON.parse(fs.readFileSync(path.resolve(__dirname, wazuh_config_file), 'utf8')); wazuh_config = JSON.parse(fs.readFileSync(path.resolve(__dirname, wazuh_config_file), 'utf8'));
} catch (e) { } catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh configuration file file.'); server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh configuration file file.');
server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + wazuh_config_file); server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + wazuh_config_file);
server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e); server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e);
}; };
const wazuh_api_version = wazuh_config.wazuhapi.version;
if(fs.existsSync(path.resolve(__dirname, wazuh_temp_file))){
wazuh_api_version = "v2.0.0";
} else{
wazuh_api_version = wazuh_config.wazuhapi.version;
}
// Elastic JS Client // Elastic JS Client
const serverConfig = server.config(); const serverConfig = server.config();
@ -313,6 +319,38 @@ module.exports = function (server, options) {
reply({ 'statusCode': 400, 'error': 7, 'message': 'Missing data' }).code(400); reply({ 'statusCode': 400, 'error': 7, 'message': 'Missing data' }).code(400);
return; return;
} }
var options = {
headers: { 'api-version': wazuh_api_version },
username: req.payload.user,
password: req.payload.password,
rejectUnauthorized: !req.payload.insecure
};
needle.request('get', req.payload.url + ':' + req.payload.port +'/version', {}, options, function (error, response) {
if (error || response.error || !response.body.data) {
options = {
headers: { 'api-version': 'v2.0.0' },
username: req.payload.user,
password: req.payload.password,
rejectUnauthorized: !req.payload.insecure
}
needle.request('get', req.payload.url + ':' + req.payload.port +'/version', {}, options, function (error, response) {
if (!error && !response.error && response.body.data) {
fs.writeFile(path.resolve(__dirname, wazuh_temp_file), "#Temporal file to avoid inconsistences when using App 2.0.1 with API 2.0.0",function(err){
if(err) server.log([blueWazuh, 'initialize', 'error'], err);
else{
server.log([blueWazuh, 'initialize', 'info'], 'Temporal file created to use the API 2.0.0');
}
});
} else {
server.log([blueWazuh, 'initialize', 'error'], 'Wazuh API credentials not found or are not correct. Open the app in your browser and configure it for start monitoring agents.');
}
});
}
});
var settings = { 'api_user': req.payload.user, 'api_password': req.payload.password, 'url': req.payload.url, 'api_port': req.payload.port , 'insecure': req.payload.insecure, 'component' : 'API', 'active' : req.payload.active, 'manager' : req.payload.manager, 'extensions' : req.payload.extensions}; var settings = { 'api_user': req.payload.user, 'api_password': req.payload.password, 'url': req.payload.url, 'api_port': req.payload.port , 'insecure': req.payload.insecure, 'component' : 'API', 'active' : req.payload.active, 'manager' : req.payload.manager, 'extensions' : req.payload.extensions};
elasticRequest.callWithRequest(req, 'index', { index: '.kibana', type: 'wazuh-configuration', body: settings, refresh: true }) elasticRequest.callWithRequest(req, 'index', { index: '.kibana', type: 'wazuh-configuration', body: settings, refresh: true })

View File

@ -28,7 +28,8 @@ module.exports = function (server, options) {
const TEMPLATE_FILE = 'startup/integration_files/template_file.json'; const TEMPLATE_FILE = 'startup/integration_files/template_file.json';
const MONITORING_SAMPLE_FILE = 'startup/integration_files/monitoring_sample.json'; const MONITORING_SAMPLE_FILE = 'startup/integration_files/monitoring_sample.json';
const wazuh_config_file = '../configuration/config.json'; const wazuh_config_file = '../configuration/config.json';
const wazuh_temp_file = '../configuration/.patch_version';
var wazuh_api_version;
var monitoring_sample = {}; var monitoring_sample = {};
var kibana_fields_data = {}; var kibana_fields_data = {};
var map_jsondata = {}; var map_jsondata = {};
@ -39,15 +40,19 @@ module.exports = function (server, options) {
var todayIndex = index_prefix + fDate; var todayIndex = index_prefix + fDate;
// Read Wazuh App configuration file // Read Wazuh App configuration file
try { try {
wazuh_config = JSON.parse(fs.readFileSync(path.resolve(__dirname, wazuh_config_file), 'utf8')); wazuh_config = JSON.parse(fs.readFileSync(path.resolve(__dirname, wazuh_config_file), 'utf8'));
} catch (e) { } catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh configuration file file.'); server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh configuration file file.');
server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + wazuh_config_file); server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + wazuh_config_file);
server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e); server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e);
}; };
const wazuh_api_version = wazuh_config.wazuhapi.version; if(fs.existsSync(path.resolve(__dirname, wazuh_temp_file))){
wazuh_api_version = "v2.0.0";
} else{
wazuh_api_version = wazuh_config.wazuhapi.version;
}
// Load Wazuh API credentials from Elasticsearch document // Load Wazuh API credentials from Elasticsearch document
var loadCredentials = function (apiEntries) { var loadCredentials = function (apiEntries) {
@ -92,10 +97,34 @@ module.exports = function (server, options) {
if (!error && !response.error && response.body.data && response.body.data.totalItems) { if (!error && !response.error && response.body.data && response.body.data.totalItems) {
checkStatus(apiEntry, response.body.data.totalItems); checkStatus(apiEntry, response.body.data.totalItems);
} else { } else {
server.log([blueWazuh, 'Wazuh agents monitoring', 'error'], 'Wazuh API credentials not found or are not correct. Open the app in your browser and configure it for start monitoring agents.'); needle.request('get', apiEntry.url + ':' + apiEntry.port +'/version', payload, options, function (error, response) {
return; if (!error && !response.error && response.body.data) {
server.log([blueWazuh, 'Wazuh agents monitoring', 'error'], 'Wazuh API credentials not found or are not correct. Open the app in your browser and configure it for start monitoring agents.');
return;
}
else{
options = {
headers: { 'api-version': 'v2.0.0' },
username: apiEntry.user,
password: apiEntry.password,
rejectUnauthorized: !apiEntry.insecure
}
needle.request('get', apiEntry.url + ':' + apiEntry.port +'/version', payload, options, function (error, response) {
if (!error && !response.error && response.body.data) {
fs.writeFile(path.resolve(__dirname, wazuh_temp_file), "#Temporal file to avoid inconsistences when using App 2.0.1 with API 2.0.0",function(err){
if(err) server.log([blueWazuh, 'initialize', 'error'], err);
else{
server.log([blueWazuh, 'initialize', 'info'], 'Temporal file created to use the API 2.0.0');
}
});
} else {
server.log([blueWazuh, 'initialize', 'error'], 'Wazuh API credentials not found or are not correct. Open the app in your browser and configure it for start monitoring agents.');
}
});
}
});
} }
}); });
}; };