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 ) => {
2017-02-01 21:06:05 +00:00
// Elastic JS Client
2017-03-31 17:34:08 +00:00
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-*" ;
2016-12-15 12:07:06 +00:00
2017-01-25 20:33:44 +00:00
// External files template or objects
2017-10-27 09:07:38 +00:00
2016-12-15 12:07:06 +00:00
2017-01-25 20:33:44 +00:00
// Initialize objects
2017-10-27 09:07:38 +00:00
let objects = { } ;
let packageJSON = { } ;
2016-12-13 15:32:35 +00:00
2017-01-25 20:33:44 +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 (
2017-02-14 12:37:51 +00:00
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-11 06:45:57 +00:00
}
) ;
2017-07-22 09:45:58 +00:00
}
2017-10-27 09:07:38 +00:00
} ;
2017-10-11 06:45:57 +00:00
// Importing Wazuh built-in visualizations and dashboards
2017-10-27 09:07:38 +00:00
const importObjects = function ( id ) {
2017-05-09 09:44:46 +00:00
server . log ( [ blueWazuh , 'initialize' , 'info' ] , 'Importing objects (Searches, visualizations and dashboards) into Elasticsearch...' ) ;
2017-09-29 05:26:18 +00:00
2017-05-09 09:44:46 +00:00
try {
2017-10-27 09:07:38 +00:00
objects = require ( OBJECTS _FILE ) ;
2017-05-09 09:44:46 +00:00
} 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 ) ;
}
2017-02-14 12:37:51 +00:00
2017-05-09 09:44:46 +00:00
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' ;
2017-10-26 03:29:43 +00:00
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-05-09 09:44:46 +00:00
} ) ;
2017-10-27 09:07:38 +00:00
elasticRequest . callWithInternalUser ( 'bulk' , {
2017-05-09 09:44:46 +00:00
index : '.kibana' ,
body : body
} ) . then ( function ( ) {
2017-10-27 09:07:38 +00:00
elasticRequest . callWithInternalUser ( 'indices.refresh' , {
index : [ '.kibana' , index _pattern ]
} ) ;
2017-05-09 09:44:46 +00:00
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...' ) ;
2017-10-11 06:45:57 +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 = {
"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 ) {
2017-10-03 06:37:18 +00:00
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
2017-10-11 06:45:57 +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 ) {
2017-10-03 06:37:18 +00:00
server . log ( [ blueWazuh , 'initialize' , 'info' ] , 'Successfully created index-pattern.' ) ;
2017-10-11 06:45:57 +00:00
// Set the index-pattern as default in the Kibana configuration
2017-09-29 05:26:18 +00:00
setDefaultKibanaSettings ( resp . body . id ) ;
2017-10-26 03:29:43 +00:00
// 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 ) {
2017-10-11 06:45:57 +00:00
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 ) {
2017-10-11 06:45:57 +00:00
if ( data . hits . total >= 1 ) {
2017-10-03 06:37:18 +00:00
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 ) {
2017-10-11 06:45:57 +00:00
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
2017-10-11 06:45:57 +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 (
2017-02-24 19:53:28 +00:00
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-02-24 19:53:28 +00:00
}
) ;
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 ) ;
} ) ;
} ;
2017-10-11 06:45:57 +00:00
// Check Kibana index and if it is prepared, start the initialization of Wazuh App.
2017-02-24 19:53:28 +00:00
checkKibanaIndex ( ) ;
2017-10-27 09:07:38 +00:00
} ;