wazuh-kibana-app/server/api/wazuh-api.js

432 lines
14 KiB
JavaScript
Raw Normal View History

// Require some libraries
const fs = require('fs');
const path = require('path');
const needle = require('needle');
// External references
const fetchAgentsExternal = require('../monitoring');
const getConfig = require('./wazuh-elastic');
const getPath = require('../../util/get-path');
// Colors for console logging
const colors = require('ansicolors');
const blueWazuh = colors.blue('wazuh');
2017-10-20 18:53:56 +00:00
const pciRequirementsFile = '../integration_files/pci_requirements.json';
module.exports = (server, options) => {
2017-10-20 18:53:56 +00:00
// Variables
let packageInfo;
2017-09-29 05:26:18 +00:00
2017-10-20 18:53:56 +00:00
// Read Wazuh app package file
try {
packageInfo = require('../../package.json');
} catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh package file.');
}
2017-01-26 15:35:07 +00:00
const checkStoredAPI = (req, reply) => {
2017-10-20 18:53:56 +00:00
// Get config from elasticsearch
getConfig((wapi_config) => {
2017-10-20 18:53:56 +00:00
if (wapi_config.error_code > 1) {
// Can not connect to elasticsearch
reply({
'statusCode': 200,
'error': '1',
'data': 'no_elasticsearch'
});
2017-10-20 18:53:56 +00:00
return;
} else if (wapi_config.error_code > 0) {
// Credentials not found
reply({
'statusCode': 400,
'error': '2',
'data': 'no_credentials'
});
2017-10-20 18:53:56 +00:00
return;
}
needle('get', `${wapi_config.url}:${wapi_config.port}/version`, {}, {
username: wapi_config.user,
password: wapi_config.password,
rejectUnauthorized: !wapi_config.insecure
})
.then((response) => {
2017-10-20 18:53:56 +00:00
if (response.body.error == 0 && response.body.data) {
wapi_config.password = "You shall not pass";
reply({
'statusCode': 200,
'data': wapi_config
});
2017-10-20 18:53:56 +00:00
} else {
reply({
'statusCode': 500,
'error': 7,
'message': response.body
});
2017-10-20 18:53:56 +00:00
}
2017-01-23 17:58:50 +00:00
});
2017-10-20 18:53:56 +00:00
});
2017-01-23 17:58:50 +00:00
};
2017-06-01 15:08:10 +00:00
const checkAPI = (req, reply) => {
if (!('user' in req.payload)) {
return reply({
'statusCode': 400,
'error': 3,
'message': 'Missing param: API USER'
});
}
if (!('password' in req.payload)) {
return reply({
'statusCode': 400,
'error': 4,
'message': 'Missing param: API PASSWORD'
});
}
if (!('url' in req.payload)) {
return reply({
'statusCode': 400,
'error': 5,
'message': 'Missing param: API URL'
});
}
if (!('port' in req.payload)) {
return reply({
'statusCode': 400,
'error': 6,
'message': 'Missing param: API PORT'
});
}
if (!(req.payload.url.includes('https://')) && !(req.payload.url.includes('http://'))) {
return reply({
'statusCode': 200,
'error': '1',
'data': 'protocol_error'
});
}
req.payload.password = Buffer.from(req.payload.password, 'base64').toString("ascii");
needle('get', `${req.payload.url}:${req.payload.port}/version`, {}, {
username: req.payload.user,
password: req.payload.password,
rejectUnauthorized: !req.payload.insecure
})
.then((response) => {
if (response.body.error == 0 && response.body.data) {
needle('get', `${req.payload.url}:${req.payload.port}/agents/000`, {}, {
username: req.payload.user,
password: req.payload.password,
rejectUnauthorized: !req.payload.insecure
})
.then((response) => {
if (!response.body.error) {
let managerName = response.body.data.name;
needle('get', `${req.payload.url}:${req.payload.port}/cluster/node`, {}, {
username: req.payload.user,
password: req.payload.password,
rejectUnauthorized: !req.payload.insecure
})
.then((response) => {
if (!response.body.error) {
reply({
"manager": managerName,
"node": response.body.data.node,
"cluster": response.body.data.cluster
});
} else if (response.body.error){
reply({
'statusCode': 500,
'error': 7,
'message': response.body.message
}).code(500);
}
});
} else if (response.body.error){
reply({
'statusCode': 500,
'error': 5,
'message': response.body.message
}).code(500);
} else {
reply({
'statusCode': 500,
'error': 5,
'message': 'Error occurred'
}).code(500);
}
});
} else if (response.body.error){
reply({
'statusCode': 500,
'error': 5,
'message': response.body.message
}).code(500);
} else {
reply({
'statusCode': 500,
'error': 5,
'message': 'Error occurred'
}).code(500);
}
});
2016-10-25 19:54:05 +00:00
};
var getPciRequirement = function (req, reply) {
2017-10-20 18:53:56 +00:00
var pciRequirements = {};
var pci_description = "";
2017-10-20 18:53:56 +00:00
try {
pciRequirements = require(pciRequirementsFile);
2017-10-20 18:53:56 +00:00
} catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the mapping file.');
server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + pciRequirementsFile);
server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e);
};
2016-10-25 19:54:05 +00:00
if (req.params.requirement == "all") {
2017-10-20 18:53:56 +00:00
reply(pciRequirements);
return;
2016-10-25 19:54:05 +00:00
}
2017-06-01 15:08:10 +00:00
if (pciRequirements[req.params.requirement])
2017-10-20 18:53:56 +00:00
pci_description = pciRequirements[req.params.requirement];
reply({
pci: {
requirement: req.params.requirement,
description: pci_description
}
});
2016-10-25 19:54:05 +00:00
};
2016-10-25 19:54:05 +00:00
var errorControl = function (error, response) {
if (error) {
return ({
'isError': true,
'body': {
'statusCode': 500,
'error': 5,
'message': 'Request error',
'errorMessage': error.message
}
});
2016-10-25 19:54:05 +00:00
} else if (!error && response.body.error) {
return ({
'isError': true,
'body': {
'statusCode': 500,
'error': 6,
'message': 'Wazuh api error',
'errorData': response.body
}
});
2016-10-25 19:54:05 +00:00
}
return ({
'isError': false
});
2016-10-25 19:54:05 +00:00
};
var makeRequest = function (method, path, data, reply) {
getConfig(function (wapi_config) {
if (wapi_config.error_code > 1) {
//Can not connect to elasticsearch
reply({
'statusCode': 404,
'error': 2,
'message': 'Could not connect with elasticsearch'
}).code(404);
2016-10-25 19:54:05 +00:00
return;
} else if (wapi_config.error_code > 0) {
//Credentials not found
reply({
'statusCode': 404,
'error': 1,
'message': 'Credentials does not exists'
}).code(404);
2016-10-25 19:54:05 +00:00
return;
}
if (!data) {
data = {};
}
2016-10-25 19:54:05 +00:00
var options = {
headers: {
'wazuh-app-version': packageInfo.version
},
2016-10-25 19:54:05 +00:00
username: wapi_config.user,
password: wapi_config.password,
rejectUnauthorized: !wapi_config.insecure
};
var fullUrl = getPath(wapi_config) + path;
2016-10-25 19:54:05 +00:00
needle.request(method, fullUrl, data, options, function (error, response) {
var errorData = errorControl(error, response);
if (errorData.isError) {
reply(errorData.body).code(500);
} else {
reply(response.body);
}
});
});
};
var requestApi = function (req, reply) {
if (!req.payload.method) {
reply({
'statusCode': 400,
'error': 3,
'message': 'Missing param: Method'
}).code(400);
2016-10-25 19:54:05 +00:00
} else if (!req.payload.path) {
reply({
'statusCode': 400,
'error': 4,
'message': 'Missing param: Path'
}).code(400);
2016-10-25 19:54:05 +00:00
} else {
makeRequest(req.payload.method, req.payload.path, req.payload.body, reply);
}
};
var getApiSettings = function (req, reply) {
getConfig(function (wapi_config) {
if (wapi_config.error_code > 1) {
//Can not connect to elasticsearch
reply({
'statusCode': 200,
'error': '1',
'data': 'no_elasticsearch'
});
return;
} else if (wapi_config.error_code > 0) {
//Credentials not found
reply({
'statusCode': 200,
'error': '1',
'data': 'no_credentials'
});
return;
}
});
2016-10-25 19:54:05 +00:00
};
2017-10-20 18:53:56 +00:00
// Fetch agent status and insert it directly on demand
var fetchAgents = function (req, reply) {
fetchAgentsExternal();
reply({
'statusCode': 200,
'error': '0',
'data': ''
});
}
2017-06-01 15:08:10 +00:00
2016-10-25 19:54:05 +00:00
var postErrorLog = function (req, reply) {
if (!req.payload.message) {
server.log([blueWazuh, 'server', 'error'], 'Error logging failed:');
server.log([blueWazuh, 'server', 'error'], 'You must provide at least one error message to log');
reply({
'statusCode': 500,
'message': 'You must provide at least one error message to log'
});
2016-10-25 19:54:05 +00:00
} else {
server.log([blueWazuh, 'client', 'error'], req.payload.message);
if (req.payload.details) {
server.log([blueWazuh, 'client', 'error'], req.payload.details);
}
reply({
'statusCode': 200,
'message': 'Error logged succesfully'
});
2016-10-25 19:54:05 +00:00
}
};
//Server routes
/*
* GET /api/wazuh-api/test
* Returns if the wazuh-api configuration is working
*
**/
2016-10-25 19:54:05 +00:00
server.route({
method: 'GET',
2017-10-20 18:53:56 +00:00
path: '/api/wazuh-api/checkAPI',
handler: checkStoredAPI
2016-10-25 19:54:05 +00:00
});
/*
* POST /api/wazuh-api/test
* Check if credentials on POST connect to Wazuh API. Not storing them!
* Returns if the wazuh-api configuration received in the POST body will work
*
**/
2016-10-25 19:54:05 +00:00
server.route({
method: 'POST',
2017-10-20 18:53:56 +00:00
path: '/api/wazuh-api/checkAPI',
handler: checkAPI
2016-10-25 19:54:05 +00:00
});
2017-06-01 15:08:10 +00:00
2016-10-25 19:54:05 +00:00
/*
* POST /api/wazuh-api/request
* Returns the request result (With error control)
*
**/
2016-10-25 19:54:05 +00:00
server.route({
method: 'POST',
path: '/api/wazuh-api/request',
handler: requestApi
});
/*
* GET /api/wazuh-api/settings
* Get Wazuh-API settings from elasticsearch index
*
**/
2016-10-25 19:54:05 +00:00
server.route({
method: 'GET',
path: '/api/wazuh-api/settings',
handler: getApiSettings
});
2017-06-01 15:08:10 +00:00
/*
* GET /api/wazuh-api/pci/requirement
* Return a PCI requirement description
*
**/
2017-01-26 15:35:07 +00:00
server.route({
method: 'GET',
path: '/api/wazuh-api/pci/{requirement}',
handler: getPciRequirement
2017-06-01 15:08:10 +00:00
});
/*
* POST /api/wazuh/debug
* Write in debug log
*
**/
2016-10-25 19:54:05 +00:00
server.route({
method: 'POST',
path: '/api/wazuh/errlog',
handler: postErrorLog
});
2017-06-01 15:08:10 +00:00
/*
* GET /api/wazuh-api/pci/requirement
* Return a PCI requirement description
*
**/
server.route({
method: 'GET',
path: '/api/wazuh-api/fetchAgents',
handler: fetchAgents
2017-06-01 15:08:10 +00:00
});
};