2017-10-27 07:22:10 +00:00
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
// 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
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
const pciRequirementsFile = '../integration_files/pci_requirements.json';
|
|
|
|
|
2017-10-27 07:46:52 +00:00
|
|
|
module.exports = (server, options) => {
|
2017-10-20 18:53:56 +00:00
|
|
|
|
|
|
|
// Variables
|
2017-10-27 07:46:52 +00:00
|
|
|
let packageInfo;
|
2017-09-29 05:26:18 +00:00
|
|
|
|
2017-10-20 18:53:56 +00:00
|
|
|
// Read Wazuh app package file
|
2017-06-27 18:28:28 +00:00
|
|
|
try {
|
2017-10-27 07:34:38 +00:00
|
|
|
packageInfo = require('../../package.json');
|
2017-06-27 18:28:28 +00:00
|
|
|
} catch (e) {
|
|
|
|
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh package file.');
|
2017-10-27 07:34:38 +00:00
|
|
|
}
|
2017-01-26 15:35:07 +00:00
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
const checkStoredAPI = (req, reply) => {
|
2017-10-20 18:53:56 +00:00
|
|
|
// Get config from elasticsearch
|
2017-10-27 07:34:38 +00:00
|
|
|
getConfig((wapi_config) => {
|
2017-10-20 18:53:56 +00:00
|
|
|
if (wapi_config.error_code > 1) {
|
|
|
|
// Can not connect to elasticsearch
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 200,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': '1',
|
|
|
|
'data': 'no_elasticsearch'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-20 18:53:56 +00:00
|
|
|
return;
|
|
|
|
} else if (wapi_config.error_code > 0) {
|
|
|
|
// Credentials not found
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 400,
|
|
|
|
'error': '2',
|
|
|
|
'data': 'no_credentials'
|
|
|
|
});
|
2017-10-20 18:53:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-27 07:34:38 +00:00
|
|
|
|
|
|
|
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";
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 200,
|
|
|
|
'data': wapi_config
|
|
|
|
});
|
2017-10-20 18:53:56 +00:00
|
|
|
} else {
|
2017-10-27 07:34:38 +00:00
|
|
|
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
|
|
|
|
2017-10-27 07:46:52 +00:00
|
|
|
const checkAPI = (req, reply) => {
|
|
|
|
|
|
|
|
if (!('user' in req.payload)) {
|
|
|
|
return reply({
|
2017-10-27 07:34:38 +00:00
|
|
|
'statusCode': 400,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': 3,
|
|
|
|
'message': 'Missing param: API USER'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-27 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!('password' in req.payload)) {
|
|
|
|
return reply({
|
2017-10-27 07:34:38 +00:00
|
|
|
'statusCode': 400,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': 4,
|
|
|
|
'message': 'Missing param: API PASSWORD'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-27 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!('url' in req.payload)) {
|
|
|
|
return reply({
|
2017-10-27 07:34:38 +00:00
|
|
|
'statusCode': 400,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': 5,
|
|
|
|
'message': 'Missing param: API URL'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-27 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!('port' in req.payload)) {
|
|
|
|
return reply({
|
2017-10-27 07:34:38 +00:00
|
|
|
'statusCode': 400,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': 6,
|
|
|
|
'message': 'Missing param: API PORT'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-27 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(req.payload.url.includes('https://')) && !(req.payload.url.includes('http://'))) {
|
|
|
|
return reply({
|
2017-10-27 07:34:38 +00:00
|
|
|
'statusCode': 200,
|
2017-10-27 07:46:52 +00:00
|
|
|
'error': '1',
|
|
|
|
'data': 'protocol_error'
|
2017-10-27 07:34:38 +00:00
|
|
|
});
|
2017-10-27 07:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
var getPciRequirement = function (req, reply) {
|
2017-10-20 18:53:56 +00:00
|
|
|
var pciRequirements = {};
|
|
|
|
var pci_description = "";
|
2017-09-22 07:36:47 +00:00
|
|
|
|
2017-10-20 18:53:56 +00:00
|
|
|
try {
|
2017-10-27 07:46:52 +00:00
|
|
|
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
|
|
|
|
2017-10-27 07:34:38 +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
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
if (pciRequirements[req.params.requirement])
|
2017-10-20 18:53:56 +00:00
|
|
|
pci_description = pciRequirements[req.params.requirement];
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
pci: {
|
|
|
|
requirement: req.params.requirement,
|
|
|
|
description: pci_description
|
|
|
|
}
|
|
|
|
});
|
2016-10-25 19:54:05 +00:00
|
|
|
};
|
2017-08-13 19:54:56 +00:00
|
|
|
|
2016-10-25 19:54:05 +00:00
|
|
|
var errorControl = function (error, response) {
|
|
|
|
if (error) {
|
2017-10-27 07:34:38 +00:00
|
|
|
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) {
|
2017-10-27 07:34:38 +00:00
|
|
|
return ({
|
|
|
|
'isError': true,
|
|
|
|
'body': {
|
|
|
|
'statusCode': 500,
|
|
|
|
'error': 6,
|
|
|
|
'message': 'Wazuh api error',
|
|
|
|
'errorData': response.body
|
|
|
|
}
|
|
|
|
});
|
2016-10-25 19:54:05 +00:00
|
|
|
}
|
2017-10-27 07:34:38 +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
|
2017-10-27 07:34:38 +00:00
|
|
|
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
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 404,
|
|
|
|
'error': 1,
|
|
|
|
'message': 'Credentials does not exists'
|
|
|
|
}).code(404);
|
2016-10-25 19:54:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-27 07:34:38 +00:00
|
|
|
if (!data) {
|
|
|
|
data = {};
|
|
|
|
}
|
2016-10-25 19:54:05 +00:00
|
|
|
|
|
|
|
var options = {
|
2017-10-27 07:34:38 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2017-05-31 14:43:45 +00:00
|
|
|
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) {
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 400,
|
|
|
|
'error': 3,
|
|
|
|
'message': 'Missing param: Method'
|
|
|
|
}).code(400);
|
2016-10-25 19:54:05 +00:00
|
|
|
} else if (!req.payload.path) {
|
2017-10-27 07:34:38 +00:00
|
|
|
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) {
|
2017-10-27 07:34:38 +00:00
|
|
|
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
|
|
|
|
2017-10-27 07:34:38 +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');
|
2017-10-27 07:34:38 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-10-27 07:34:38 +00:00
|
|
|
reply({
|
|
|
|
'statusCode': 200,
|
|
|
|
'message': 'Error logged succesfully'
|
|
|
|
});
|
2016-10-25 19:54:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//Server routes
|
|
|
|
|
|
|
|
/*
|
2017-10-27 07:34:38 +00:00
|
|
|
* 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',
|
2017-02-13 19:58:44 +00:00
|
|
|
handler: checkStoredAPI
|
2016-10-25 19:54:05 +00:00
|
|
|
});
|
|
|
|
|
2017-10-27 07:34:38 +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',
|
2017-02-13 19:58:44 +00:00
|
|
|
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
|
|
|
/*
|
2017-10-27 07:34:38 +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
|
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
2017-10-27 07:34:38 +00:00
|
|
|
* 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
|
|
|
|
|
|
|
/*
|
2017-10-27 07:34:38 +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
|
|
|
});
|
2017-08-13 19:54:56 +00:00
|
|
|
|
2017-10-27 07:34:38 +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
|
|
|
|
|
|
|
/*
|
2017-10-27 07:34:38 +00:00
|
|
|
* GET /api/wazuh-api/pci/requirement
|
|
|
|
* Return a PCI requirement description
|
|
|
|
*
|
|
|
|
**/
|
2017-02-23 12:53:18 +00:00
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/api/wazuh-api/fetchAgents',
|
|
|
|
handler: fetchAgents
|
2017-06-01 15:08:10 +00:00
|
|
|
});
|
2017-10-27 07:34:38 +00:00
|
|
|
};
|