diff --git a/public/directives/kibanaDashboardDirective.js b/public/directives/kibanaDashboardDirective.js index 1baf971e6b..5b3ad715c9 100644 --- a/public/directives/kibanaDashboardDirective.js +++ b/public/directives/kibanaDashboardDirective.js @@ -59,218 +59,216 @@ app.directive('kbnDash', function (Notifier, courier, AppState, timefilter, kbnU $route.requireDefaultIndex = true; - genericReq.request('PUT', '/api/wazuh-elastic/wazuh-pattern').then(function (data) { - savedDashboards.get($scope.dashId).then(function (_dash) { - $scope.dash = _dash; - const dash = _dash; + savedDashboards.get($scope.dashId).then(function (_dash) { + $scope.dash = _dash; + const dash = _dash; - const queryFilter = Private(FilterBarQueryFilterProvider); + const queryFilter = Private(FilterBarQueryFilterProvider); - const notify = new Notifier({ - location: '*' - }); - - if (dash.timeRestore && dash.timeTo && dash.timeFrom && !getAppState.previouslyStored()) { - timefilter.time.to = dash.timeTo; - timefilter.time.from = dash.timeFrom; - if (dash.refreshInterval) { - timefilter.refreshInterval = dash.refreshInterval; - } - } + const notify = new Notifier({ + location: '*' + }); - if ($scope.dashTimeTo && $scope.dashTimeFrom) { - timefilter.time.to = $scope.dashTimeTo; - timefilter.time.from = $scope.dashTimeFrom; + if (dash.timeRestore && dash.timeTo && dash.timeFrom && !getAppState.previouslyStored()) { + timefilter.time.to = dash.timeTo; + timefilter.time.from = dash.timeFrom; + if (dash.refreshInterval) { + timefilter.refreshInterval = dash.refreshInterval; } - - $scope.$on('$destroy', dash.destroy); - - const matchQueryFilter = function (filter) { - return filter.query && filter.query.query_string && !filter.meta; - }; - - const extractQueryFromFilters = function (filters) { - const filter = _.find(filters, matchQueryFilter); - if (filter) return filter.query; - }; - - - const stateDefaults = { - title: dash.title, - panels: dash.panelsJSON ? JSON.parse(dash.panelsJSON) : [], - options: dash.optionsJSON ? JSON.parse(dash.optionsJSON) : {}, - uiState: dash.uiStateJSON ? JSON.parse(dash.uiStateJSON) : {}, - query: extractQueryFromFilters(dash.searchSource.getOwn('filter')) || { query_string: { query: '*' } }, - filters: _.reject(dash.searchSource.getOwn('filter'), matchQueryFilter) - }; - - // Configure AppState. Get App State, if there is no App State create new one - let currentAppState = getAppState(); - - if(!currentAppState) - $scope.state = new AppState(stateDefaults); - else{ - $scope.state = currentAppState; - $scope.state.title = dash.title; - $scope.state.panels = dash.panelsJSON ? JSON.parse(dash.panelsJSON) : []; - $scope.state.options = dash.optionsJSON ? JSON.parse(dash.optionsJSON) : {}; - $scope.state.uiState = dash.uiStateJSON ? JSON.parse(dash.uiStateJSON) : {}; - $scope.state.query = extractQueryFromFilters(dash.searchSource.getOwn('filter')) || { query_string: { query: '*' } }; + } + + if ($scope.dashTimeTo && $scope.dashTimeFrom) { + timefilter.time.to = $scope.dashTimeTo; + timefilter.time.from = $scope.dashTimeFrom; + } + + $scope.$on('$destroy', dash.destroy); + + const matchQueryFilter = function (filter) { + return filter.query && filter.query.query_string && !filter.meta; + }; + + const extractQueryFromFilters = function (filters) { + const filter = _.find(filters, matchQueryFilter); + if (filter) return filter.query; + }; + + + const stateDefaults = { + title: dash.title, + panels: dash.panelsJSON ? JSON.parse(dash.panelsJSON) : [], + options: dash.optionsJSON ? JSON.parse(dash.optionsJSON) : {}, + uiState: dash.uiStateJSON ? JSON.parse(dash.uiStateJSON) : {}, + query: extractQueryFromFilters(dash.searchSource.getOwn('filter')) || { query_string: { query: '*' } }, + filters: _.reject(dash.searchSource.getOwn('filter'), matchQueryFilter) + }; + + // Configure AppState. Get App State, if there is no App State create new one + let currentAppState = getAppState(); + + if(!currentAppState) + $scope.state = new AppState(stateDefaults); + else{ + $scope.state = currentAppState; + $scope.state.title = dash.title; + $scope.state.panels = dash.panelsJSON ? JSON.parse(dash.panelsJSON) : []; + $scope.state.options = dash.optionsJSON ? JSON.parse(dash.optionsJSON) : {}; + $scope.state.uiState = dash.uiStateJSON ? JSON.parse(dash.uiStateJSON) : {}; + $scope.state.query = extractQueryFromFilters(dash.searchSource.getOwn('filter')) || { query_string: { query: '*' } }; + } + + const $state = $scope.state; + + const $uiState = $scope.uiState = $state.makeStateful('uiState'); + + $scope.$watchCollection('state.options', function (newVal, oldVal) { + if (!angular.equals(newVal, oldVal)) $state.save(); + }); + $scope.$watch('state.options.darkTheme', setDarkTheme); + + $scope.refresh = _.bindKey(courier, 'fetch'); + + timefilter.enabled = true; + $scope.timefilter = timefilter; + $scope.$listen(timefilter, 'fetch', $scope.refresh); + + courier.setRootSearchSource(dash.searchSource); + + function init() { + updateQueryOnRootSource(); + + const docTitle = Private(DocTitleProvider); + if (dash.id) { + docTitle.change(dash.title); } - const $state = $scope.state; - - const $uiState = $scope.uiState = $state.makeStateful('uiState'); - - $scope.$watchCollection('state.options', function (newVal, oldVal) { - if (!angular.equals(newVal, oldVal)) $state.save(); - }); - $scope.$watch('state.options.darkTheme', setDarkTheme); - - $scope.refresh = _.bindKey(courier, 'fetch'); - - timefilter.enabled = true; - $scope.timefilter = timefilter; - $scope.$listen(timefilter, 'fetch', $scope.refresh); - - courier.setRootSearchSource(dash.searchSource); - - function init() { - updateQueryOnRootSource(); - - const docTitle = Private(DocTitleProvider); - if (dash.id) { - docTitle.change(dash.title); + initPanelIndices(); + $scope.$emit('application.load'); + } + + function initPanelIndices() { + // find the largest panelIndex in all the panels + let maxIndex = getMaxPanelIndex(); + // ensure that all panels have a panelIndex + $scope.state.panels.forEach(function (panel) { + if (!panel.panelIndex) { + panel.panelIndex = maxIndex++; } - - initPanelIndices(); - $scope.$emit('application.load'); - } - - function initPanelIndices() { - // find the largest panelIndex in all the panels - let maxIndex = getMaxPanelIndex(); - // ensure that all panels have a panelIndex - $scope.state.panels.forEach(function (panel) { - if (!panel.panelIndex) { - panel.panelIndex = maxIndex++; - } - }); - } - - function getMaxPanelIndex() { - - let index = $scope.state.panels.reduce(function (idx, panel) { - // if panel is missing an index, add one and increment the index - return Math.max(idx, panel.panelIndex || idx); - }, 0); - return ++index; - } - - $scope.$watch("dashFilter", function () { - $state.query = { query_string: { query: $scope.dashFilter ? $scope.dashFilter : '*' } }; - $scope.filterResults(); }); + } - function updateQueryOnRootSource() { - const filters = queryFilter.getFilters(); - if ($state.query) { - dash.searchSource.set('filter', _.union(filters, [{ - query: $state.query - }])); - } else { - dash.searchSource.set('filter', filters); - } - } - - function setDarkTheme(enabled) { - const theme = Boolean(enabled) ? 'theme-dark' : 'theme-light'; - chrome.removeApplicationClass(['theme-dark', 'theme-light']); - chrome.addApplicationClass(theme); + function getMaxPanelIndex() { + + let index = $scope.state.panels.reduce(function (idx, panel) { + // if panel is missing an index, add one and increment the index + return Math.max(idx, panel.panelIndex || idx); + }, 0); + return ++index; + } + + $scope.$watch("dashFilter", function () { + $state.query = { query_string: { query: $scope.dashFilter ? $scope.dashFilter : '*' } }; + $scope.filterResults(); + }); + + function updateQueryOnRootSource() { + const filters = queryFilter.getFilters(); + if ($state.query) { + dash.searchSource.set('filter', _.union(filters, [{ + query: $state.query + }])); + } else { + dash.searchSource.set('filter', filters); } - - // update root source when filters update - $scope.$listen(queryFilter, 'update', function () { - updateQueryOnRootSource(); - $state.save(); - }); - - // update data when filters fire fetch event - $scope.$listen(queryFilter, 'fetch', $scope.refresh); - - $scope.newDashboard = function () { - kbnUrl.change('/dashboard', {}); - }; - - $scope.filterResults = function () { - updateQueryOnRootSource(); - $state.save(); - $scope.refresh(); - }; - - $scope.save = function () { - $state.title = dash.id = dash.title; - $state.save(); - - const timeRestoreObj = _.pick(timefilter.refreshInterval, ['display', 'pause', 'section', 'value']); - dash.panelsJSON = angular.toJson($state.panels); - dash.uiStateJSON = angular.toJson($uiState.getChanges()); - dash.timeFrom = dash.timeRestore ? timefilter.time.from : undefined; - dash.timeTo = dash.timeRestore ? timefilter.time.to : undefined; - dash.refreshInterval = dash.timeRestore ? timeRestoreObj : undefined; - dash.optionsJSON = angular.toJson($state.options); - - dash.save() - .then(function (id) { - $scope.kbnTopNav.close('save'); - if (id) { - if (dash.id !== $routeParams.id) { - kbnUrl.change('/dashboard/{{id}}', { id: dash.id }); - } + } + + function setDarkTheme(enabled) { + const theme = Boolean(enabled) ? 'theme-dark' : 'theme-light'; + chrome.removeApplicationClass(['theme-dark', 'theme-light']); + chrome.addApplicationClass(theme); + } + + // update root source when filters update + $scope.$listen(queryFilter, 'update', function () { + updateQueryOnRootSource(); + $state.save(); + }); + + // update data when filters fire fetch event + $scope.$listen(queryFilter, 'fetch', $scope.refresh); + + $scope.newDashboard = function () { + kbnUrl.change('/dashboard', {}); + }; + + $scope.filterResults = function () { + updateQueryOnRootSource(); + $state.save(); + $scope.refresh(); + }; + + $scope.save = function () { + $state.title = dash.id = dash.title; + $state.save(); + + const timeRestoreObj = _.pick(timefilter.refreshInterval, ['display', 'pause', 'section', 'value']); + dash.panelsJSON = angular.toJson($state.panels); + dash.uiStateJSON = angular.toJson($uiState.getChanges()); + dash.timeFrom = dash.timeRestore ? timefilter.time.from : undefined; + dash.timeTo = dash.timeRestore ? timefilter.time.to : undefined; + dash.refreshInterval = dash.timeRestore ? timeRestoreObj : undefined; + dash.optionsJSON = angular.toJson($state.options); + + dash.save() + .then(function (id) { + $scope.kbnTopNav.close('save'); + if (id) { + if (dash.id !== $routeParams.id) { + kbnUrl.change('/dashboard/{{id}}', { id: dash.id }); } - }) - .catch(); - }; - - let pendingVis = _.size($state.panels); - $scope.$on('ready:vis', function () { - if (pendingVis) pendingVis--; - if (pendingVis === 0) { - $state.save(); - $scope.refresh(); - } - }); - - // listen for notifications from the grid component that changes have - // been made, rather than watching the panels deeply - $scope.$on('change:vis', function () { + } + }) + .catch(); + }; + + let pendingVis = _.size($state.panels); + $scope.$on('ready:vis', function () { + if (pendingVis) pendingVis--; + if (pendingVis === 0) { $state.save(); - }); - - // called by the saved-object-finder when a user clicks a vis - $scope.addVis = function (hit) { - pendingVis++; - $state.panels.push({ id: hit.id, type: 'visualization', panelIndex: getMaxPanelIndex() }); - }; - - $scope.addSearch = function (hit) { - pendingVis++; - $state.panels.push({ id: hit.id, type: 'search', panelIndex: getMaxPanelIndex() }); - }; - - // Setup configurable values for config directive, after objects are initialized - $scope.opts = { - dashboard: dash, - ui: $state.options, - save: $scope.save, - addVis: $scope.addVis, - addSearch: $scope.addSearch, - timefilter: $scope.timefilter - }; - - init(); - }).catch(console.log('Dashboard not found!')); - }); + $scope.refresh(); + } + }); + + // listen for notifications from the grid component that changes have + // been made, rather than watching the panels deeply + $scope.$on('change:vis', function () { + $state.save(); + }); + + // called by the saved-object-finder when a user clicks a vis + $scope.addVis = function (hit) { + pendingVis++; + $state.panels.push({ id: hit.id, type: 'visualization', panelIndex: getMaxPanelIndex() }); + }; + + $scope.addSearch = function (hit) { + pendingVis++; + $state.panels.push({ id: hit.id, type: 'search', panelIndex: getMaxPanelIndex() }); + }; + + // Setup configurable values for config directive, after objects are initialized + $scope.opts = { + dashboard: dash, + ui: $state.options, + save: $scope.save, + addVis: $scope.addVis, + addSearch: $scope.addSearch, + timefilter: $scope.timefilter + }; + + init(); + }).catch(console.log('Dashboard not found!')); }, template: indexTemplate } diff --git a/public/directives/kibanaVisualizationDirective.js b/public/directives/kibanaVisualizationDirective.js index 41777874ce..67ba4c7f82 100644 --- a/public/directives/kibanaVisualizationDirective.js +++ b/public/directives/kibanaVisualizationDirective.js @@ -104,22 +104,7 @@ require('ui/modules').get('app/wazuh', []).controller('VisController', function // Bind visualization, index pattern and state $scope.vis = $scope.newVis.vis; $scope.indexPattern = $scope.vis.indexPattern; - angular.forEach($scope.indexPattern.fields, function(value, key) { - if(value.aggregatable) - agg_fields.push(value.displayName); - }); - $scope.state = $state; - - // Check if needed fields are agreggable - $scope.not_aggregable = false; - angular.forEach(visDecoded.vis.aggs, function(value, key) { - if(value.type == "terms" && (agg_fields.indexOf(value.params.field) === -1)){ - $scope.not_aggregable = true; - return; - } - }); - // Build visualization visState.aggs = visDecoded.vis.aggs; diff --git a/public/objects/routes.js b/public/objects/routes.js index bb2d152185..7762d6c7aa 100644 --- a/public/objects/routes.js +++ b/public/objects/routes.js @@ -2,13 +2,15 @@ var routes = require('ui/routes'); //Installation wizard -var settingsWizard = function ($location, testConnection, $mdToast, appState, $q) { +var settingsWizard = function ($location, testConnection, $mdToast, appState, $q, genericReq) { var deferred = $q.defer(); testConnection.test().then(function (data) { appState.setDefaultManager(data.manager); appState.setExtensions(data.extensions); - deferred.resolve(); + genericReq.request('PUT', '/api/wazuh-elastic/wazuh-pattern').then(function (data) { + deferred.resolve(); + }); }, function (data) { $mdToast.show({ template: 'Could not connect with Wazuh API. Please, configure it on settings tab.', diff --git a/public/templates/agents-audit.html b/public/templates/agents-audit.html index 9ef4508ceb..267ae17214 100644 --- a/public/templates/agents-audit.html +++ b/public/templates/agents-audit.html @@ -97,7 +97,7 @@ - + diff --git a/public/templates/directives/kibana-visualization-template.html b/public/templates/directives/kibana-visualization-template.html index 9808a5ff58..be1336ae10 100644 --- a/public/templates/directives/kibana-visualization-template.html +++ b/public/templates/directives/kibana-visualization-template.html @@ -1,14 +1,5 @@
-
-
-
- -

No results found

-
-
-
-
diff --git a/public/templates/directives/kibana-visualization-value-template.html b/public/templates/directives/kibana-visualization-value-template.html index e2d51aa8f0..4760c5b471 100644 --- a/public/templates/directives/kibana-visualization-value-template.html +++ b/public/templates/directives/kibana-visualization-value-template.html @@ -1,12 +1,4 @@
- -
-
-
-

No results found

-
-
-
diff --git a/public/templates/overview-audit.html b/public/templates/overview-audit.html index 55ca93e762..12421c29f1 100644 --- a/public/templates/overview-audit.html +++ b/public/templates/overview-audit.html @@ -109,7 +109,7 @@ - + diff --git a/server/routes/wazuh-elastic.js b/server/routes/wazuh-elastic.js index bf0365f61e..5d2b67f39d 100644 --- a/server/routes/wazuh-elastic.js +++ b/server/routes/wazuh-elastic.js @@ -180,28 +180,60 @@ module.exports = function (server, options) { }; var putWazuhPattern = function (req, reply) { - + function extend(target) { + var sources = [].slice.call(arguments, 1); + sources.forEach(function (source) { + for (var prop in source) { + target[prop] = source[prop]; + } + }); + return target; +} + try { - kibana_fields_data = JSON.parse(fs.readFileSync(path.resolve(__dirname, KIBANA_FIELDS_FILE), 'utf8')); + kibana_fields_data = JSON.parse(fs.readFileSync(path.resolve(__dirname, KIBANA_FIELDS_FILE), 'utf8')); + // Get fields index pattern template + var wazuhAlerts_indexPattern_template = JSON.parse(kibana_fields_data.wazuh_alerts) + var wazuhAlerts_indexPattern_current = {}; + var fields = []; + for (var i = 0, len = wazuhAlerts_indexPattern_template.length; i < len; i++) { + fields.push(wazuhAlerts_indexPattern_template[i].name); + } + + // Get current fields index pattern + client.get({ + index: '.kibana', + type: 'index-pattern', + id: index_pattern + }, function (error, response) { + wazuhAlerts_indexPattern_current = JSON.parse(response._source.fields); + // Compare and update fields properties + for (var i = 0, len = wazuhAlerts_indexPattern_current.length; i < len; i++) { + if (fields.indexOf(wazuhAlerts_indexPattern_current[i].name) >= 0) { + wazuhAlerts_indexPattern_current[i].searchable = true; + wazuhAlerts_indexPattern_current[i].aggregatable = true; + } + } + // Update index pattern + client.update({ + index: '.kibana', + type: 'index-pattern', + id: index_pattern, + body: { + doc: { + fields: JSON.stringify((wazuhAlerts_indexPattern_current)) + } + } + }, function (error, response) { + reply({ 'response': response, 'error': error }).code(200); + }) + }) + } catch (e) { server.log([blueWazuh, 'initialize', 'error'], 'Could not read the mapping file.'); server.log([blueWazuh, 'initialize', 'error'], 'Path: ' + KIBANA_FIELDS_FILE); server.log([blueWazuh, 'initialize', 'error'], 'Exception: ' + e); }; - - client.update({ - index: '.kibana', - type: 'index-pattern', - id: index_pattern, - body: { - doc: { - fields: kibana_fields_data.wazuh_alerts - } - } - }, function (error, response) { - reply({ 'response': response, 'error': error }).code(200); - }) - }; //Server routes