wazuh-kibana-app/server/initialize.js

228 lines
7.2 KiB
JavaScript
Raw Normal View History

2017-10-27 09:07:38 +00:00
const needle = require('needle');
// Colors for console logging
const colors = require('ansicolors');
const blueWazuh = colors.blue('wazuh');
const OBJECTS_FILE = 'integration_files/objects_file.json';
module.exports = (server, options) => {
// Elastic JS Client
const elasticRequest = server.plugins.elasticsearch.getCluster('data');
2016-12-13 15:32:35 +00:00
2017-10-27 09:07:38 +00:00
//const uiSettings = server.uiSettings();
2016-12-13 15:32:35 +00:00
2016-12-05 16:59:53 +00:00
// Initialize variables
2017-10-27 09:07:38 +00:00
let index_pattern = "wazuh-alerts-*";
// External files template or objects
2017-10-27 09:07:38 +00:00
// Initialize objects
2017-10-27 09:07:38 +00:00
let objects = {};
let packageJSON = {};
2016-12-13 15:32:35 +00:00
// Read config from package JSON
2017-09-29 05:26:18 +00:00
try {
2017-10-27 09:07:38 +00:00
packageJSON = require('../package.json');
} catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the Wazuh package file.');
}
2017-09-29 05:26:18 +00:00
// Save Wazuh App first set up for future executions
2017-10-27 09:07:38 +00:00
const saveConfiguration = function (type) {
let configuration = {
"name": "Wazuh App",
"app-version": packageJSON.version,
"revision": packageJSON.revision,
"installationDate": new Date().toISOString()
};
if (type == "install") {
elasticRequest.callWithInternalUser('create', {
index: ".wazuh-version",
type: 'wazuh-version',
id: 1,
body: configuration
}).then(
function () {
2017-09-29 05:26:18 +00:00
server.log([blueWazuh, 'initialize', 'info'], 'Wazuh configuration inserted');
2017-10-27 09:07:38 +00:00
},
function () {
2017-09-29 05:26:18 +00:00
server.log([blueWazuh, 'initialize', 'error'], 'Could not insert Wazuh configuration');
}
);
}
2017-10-27 09:07:38 +00:00
};
// Importing Wazuh built-in visualizations and dashboards
2017-10-27 09:07:38 +00:00
const importObjects = function (id) {
server.log([blueWazuh, 'initialize', 'info'], 'Importing objects (Searches, visualizations and dashboards) into Elasticsearch...');
2017-09-29 05:26:18 +00:00
try {
2017-10-27 09:07:38 +00:00
objects = require(OBJECTS_FILE);
} catch (e) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not read the objects file.');
server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + OBJECTS_FILE);
server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e);
}
var body = '';
objects.forEach(function (element) {
2017-10-27 09:07:38 +00:00
body += '{ "index": { "_index": ".kibana", "_type": "doc", "_id": "' + element._type + ':' + element._id + '" } }\n';
var temp = {}
var aux = JSON.stringify(element._source);
aux = aux.replace("wazuh-alerts", id);
aux = JSON.parse(aux);
temp[element._type] = aux;
if (temp[element._type].kibanaSavedObjectMeta.searchSourceJSON.index) temp[element._type].kibanaSavedObjectMeta.searchSourceJSON.index = id;
temp["type"] = element._type;
body += JSON.stringify(temp) + "\n";
});
2017-10-27 09:07:38 +00:00
elasticRequest.callWithInternalUser('bulk', {
index: '.kibana',
body: body
}).then(function () {
2017-10-27 09:07:38 +00:00
elasticRequest.callWithInternalUser('indices.refresh', {
index: ['.kibana', index_pattern]
});
server.log([blueWazuh, 'initialize', 'info'], 'Templates, mappings, index patterns, visualizations, searches and dashboards were successfully installed. App ready to be used.');
}, function (err) {
server.log([blueWazuh, 'server', 'error'], 'Error importing objects into elasticsearch. Bulk request failed.');
});
2017-10-27 09:07:38 +00:00
};
2016-12-05 16:59:53 +00:00
2017-09-29 05:26:18 +00:00
// Setting default index pattern
2017-10-27 09:07:38 +00:00
const setDefaultKibanaSettings = function (id) {
server.log([blueWazuh, 'initialize', 'info'], 'Setting Kibana default values: Index pattern, time picker and metaFields...');
// Call the internal API and wait for the response
2017-10-27 09:07:38 +00:00
var options = {
headers: {
'kbn-version': packageJSON.kibana.version
},
json: true
}
2017-09-29 05:26:18 +00:00
2017-10-27 09:07:38 +00:00
var body = {
"value": id
}
2017-09-29 05:26:18 +00:00
2017-10-27 09:07:38 +00:00
needle('post', 'http://localhost:' + server.info.port + '/api/kibana/settings/defaultIndex', body, options).then(function (resp) {
server.log([blueWazuh, 'initialize', 'info'], 'Wazuh index-pattern successfully set to default.');
2017-10-27 09:07:38 +00:00
}).catch(function (err) {
2017-09-29 05:26:18 +00:00
server.log([blueWazuh, 'error'], 'Could not default Wazuh index-pattern.');
});
2017-10-27 09:07:38 +00:00
};
2017-09-29 05:26:18 +00:00
// Create index pattern
2017-10-27 09:07:38 +00:00
const createIndexPattern = function () {
server.log([blueWazuh, 'initialize', 'info'], 'Creating index pattern: ' + index_pattern);
2017-09-29 05:26:18 +00:00
// Call the internal API and wait for the response
2017-10-27 09:07:38 +00:00
var options = {
headers: {
'kbn-version': packageJSON.kibana.version
},
json: true
}
2017-09-29 05:26:18 +00:00
2017-10-27 09:07:38 +00:00
var body = {
attributes: {
title: index_pattern,
timeFieldName: '@timestamp'
}
};
2017-09-29 05:26:18 +00:00
2017-10-27 09:07:38 +00:00
needle('post', 'http://localhost:' + server.info.port + '/api/saved_objects/index-pattern', body, options).then(function (resp) {
server.log([blueWazuh, 'initialize', 'info'], 'Successfully created index-pattern.');
// Set the index-pattern as default in the Kibana configuration
2017-09-29 05:26:18 +00:00
setDefaultKibanaSettings(resp.body.id);
// Import objects (dashboards and visualizations) CAREFUL HERE, WE HAVE TO MANAGE SUCESIVE APP INITIATIONS!!!
importObjects(resp.body.id);
2017-10-27 09:07:38 +00:00
}).catch(function (err) {
server.log([blueWazuh, 'initialize', 'error'], 'Error creating index-pattern.');
2017-09-29 05:26:18 +00:00
});
2017-10-27 09:07:38 +00:00
};
2017-09-29 05:26:18 +00:00
// Configure Kibana status: Index pattern, default index pattern, default time, import dashboards.
2017-10-27 09:07:38 +00:00
const configureKibana = function (type) {
if (type == "install") {
2017-09-29 05:26:18 +00:00
// Create Index Pattern > Set it as default > Set default time
2017-10-27 09:07:38 +00:00
elasticRequest.callWithInternalUser('search', {
index: '.kibana',
type: 'index-pattern',
q: 'title:"wazuh-alerts-*"'
}).then(
2017-09-29 05:26:18 +00:00
function (data) {
if (data.hits.total >= 1) {
server.log([blueWazuh, 'initialize', 'info'], 'Skipping index-pattern creation. Already exists.');
2017-09-29 05:26:18 +00:00
} else {
createIndexPattern();
}
2017-10-27 09:07:38 +00:00
},
function (error) {
server.log([blueWazuh, 'initialize', 'error'], 'Could not reach elasticsearch.');
2017-10-27 09:07:38 +00:00
});
2017-09-29 05:26:18 +00:00
}
// Save Setup Info
saveConfiguration(type);
2017-10-27 09:07:38 +00:00
};
2017-09-29 05:26:18 +00:00
// Init function. Check for "wazuh-version" document existance.
2017-10-27 09:07:38 +00:00
const init = function () {
elasticRequest.callWithInternalUser('indices.exists', {
index: '.wazuh'
}).then(
function (result) {
if (!result) {
elasticRequest.callWithInternalUser('indices.create', {
index: '.wazuh'
}).then(
function () {
server.log([blueWazuh, 'initialize', 'info'], 'Index .wazuh created.');
},
function () {
server.log([blueWazuh, 'initialize', 'error'], 'Error creating index .wazuh.');
});
}
},
function () {
server.log([blueWazuh, 'initialize', 'error'], 'Could not check if the index .wazuh exists.');
});
elasticRequest.callWithInternalUser('get', {
index: ".wazuh-version",
type: "wazuh-version",
id: "1"
}).then(
function (data) {
2017-10-27 09:07:38 +00:00
server.log([blueWazuh, 'initialize', 'info'], 'Wazuh-configuration document already exists. Nothing to be done.');
},
function (data) {
server.log([blueWazuh, 'initialize', 'info'], 'Wazuh-configuration document does not exist. Initializating configuration...');
configureKibana("install");
}
);
2017-10-27 09:07:38 +00:00
};
// Wait until Kibana index is created / loaded and initialize Wazuh App
const checkKibanaIndex = () => {
elasticRequest
.callWithInternalUser('exists', {
index: ".kibana",
id: packageJSON.kibana.version,
type: "config"
})
.then((data) => server.plugins.elasticsearch.waitUntilReady())
.then(() => init())
.catch((error) => {
server.log([blueWazuh, 'initialize', 'info'],
'Waiting index ".kibana" to be created and prepared....');
setTimeout(() => checkKibanaIndex(), 3000);
});
};
// Check Kibana index and if it is prepared, start the initialization of Wazuh App.
checkKibanaIndex();
2017-10-27 09:07:38 +00:00
};