Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Managing errors in a better way plus some improvements #439

Merged
merged 20 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 6 additions & 60 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,66 +38,12 @@ import 'plugins/wazuh/../node_modules/angular-material/angular-material.js';
// Cookies
import 'plugins/wazuh/../node_modules/angular-cookies/angular-cookies.min.js';

////////////////////////////////////////////////////////////////////
// Require Kibana integrations
import 'ui/autoload/all';
import 'ui/chrome';
import 'plugins/wazuh/kibana-integrations/kibana-visualization.js';
import 'plugins/wazuh/kibana-integrations/kibana-filter-bar.js';
import 'plugins/wazuh/kibana-integrations/kibana-discover.js';
import 'plugins/wazuh/kibana-integrations/saved-visualizations.js';

// Require services
import 'plugins/wazuh/services/error-handler.js';
import 'plugins/wazuh/services/theming.js';
import 'plugins/wazuh/services/api-request.js';
import 'plugins/wazuh/services/generic-request.js';
import 'plugins/wazuh/services/data-handler.js';
import 'plugins/wazuh/services/app-state.js';
import 'plugins/wazuh/services/api-tester.js';
import 'plugins/wazuh/services/pattern-handler.js';
import 'plugins/wazuh/services/csv-request.js';

// Set up routes and views
import 'plugins/wazuh/services/routes.js';

// Require controllers

// Factories
import 'plugins/wazuh/factories/data-handler-composer.js';

// Wazuh Directives
import 'plugins/wazuh/directives/wz-dynamic/wz-dynamic.js';
import 'plugins/wazuh/directives/wz-enter/wz-enter.js';
import 'plugins/wazuh/directives/wz-menu/wz-menu.js';
import 'plugins/wazuh/directives/wz-menu/wz-menu.less';
import 'plugins/wazuh/directives/wz-search-bar/wz-search-bar.js';
import 'plugins/wazuh/directives/wz-table-header/wz-table-header.js';
import 'plugins/wazuh/directives/wz-table-header/wz-table-header.less';
import 'plugins/wazuh/directives/wz-table/wz-table.js';
import 'plugins/wazuh/directives/wz-table/wz-table.less';

// Blank Screen
import 'plugins/wazuh/controllers/blank-screen-controller.js';

// Login
import 'plugins/wazuh/controllers/login.js';

// Overview
import 'plugins/wazuh/controllers/overview.js';

// Manager
import 'plugins/wazuh/controllers/manager.js';
import 'plugins/wazuh/controllers/ruleset.js';
import 'plugins/wazuh/controllers/osseclog.js';
import 'plugins/wazuh/controllers/groups.js';

// Agents
import 'plugins/wazuh/controllers/agents.js';
import 'plugins/wazuh/controllers/agents-preview.js';

// Settings
import 'plugins/wazuh/controllers/settings.js';

// Health check
import 'plugins/wazuh/controllers/health-check.js';
// Wazuh
import 'plugins/wazuh/kibana-integrations'
import 'plugins/wazuh/services'
import 'plugins/wazuh/controllers'
import 'plugins/wazuh/factories'
import 'plugins/wazuh/directives'
22 changes: 22 additions & 0 deletions public/controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Wazuh app - File for app requirements and set up
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import 'plugins/wazuh/controllers/blank-screen-controller'
import 'plugins/wazuh/controllers/login'
import 'plugins/wazuh/controllers/overview'
import 'plugins/wazuh/controllers/manager'
import 'plugins/wazuh/controllers/ruleset'
import 'plugins/wazuh/controllers/osseclog'
import 'plugins/wazuh/controllers/groups'
import 'plugins/wazuh/controllers/agents'
import 'plugins/wazuh/controllers/agents-preview'
import 'plugins/wazuh/controllers/settings'
import 'plugins/wazuh/controllers/health-check'
30 changes: 0 additions & 30 deletions public/controllers/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ app.controller('rulesController', function ($scope, $rootScope, Rules, RulesRela
$scope.rulesRelated = RulesRelated;
$scope.rulesAutoComplete = RulesAutoComplete;
$scope.setRulesTab('rules');
$rootScope.tabVisualizations = { ruleset: 4 };
$scope.isArray = angular.isArray;

$scope.analizeRules = async search => {
Expand Down Expand Up @@ -127,13 +126,6 @@ app.controller('rulesController', function ($scope, $rootScope, Rules, RulesRela

const load = async () => {
try {
$rootScope.rawVisualizations = null;
const data = await genericReq.request('GET',`/api/wazuh-elastic/create-vis/manager-ruleset-rules/${appState.getCurrentPattern()}`)
$rootScope.rawVisualizations = data.data.raw;
// Render visualizations
$rootScope.$broadcast('updateVis');
if(!$rootScope.$$phase) $rootScope.$digest();

await Promise.all([
$scope.rules.nextPage(),
$scope.rulesAutoComplete.nextPage()
Expand Down Expand Up @@ -169,13 +161,6 @@ app.controller('rulesController', function ($scope, $rootScope, Rules, RulesRela
$scope.rules.reset();
$scope.rulesRelated.reset();
$scope.rulesAutoComplete.reset();
$rootScope.rawVisualizations = null;
if($rootScope.ownHandlers){
for(let h of $rootScope.ownHandlers){
h._scope.$destroy();
}
}
$rootScope.ownHandlers = [];
});
});

Expand All @@ -189,7 +174,6 @@ app.controller('decodersController', function ($scope, $rootScope, $sce, Decoder
$scope.decodersAutoComplete = DecodersAutoComplete;
$scope.typeFilter = "all";
$scope.setRulesTab('decoders');
$rootScope.tabVisualizations = { ruleset: 1 };
$scope.isArray = angular.isArray;

const colors = [
Expand Down Expand Up @@ -326,13 +310,6 @@ app.controller('decodersController', function ($scope, $rootScope, $sce, Decoder

const load = async () => {
try {
$rootScope.rawVisualizations = null;
const data = await genericReq.request('GET',`/api/wazuh-elastic/create-vis/manager-ruleset-decoders/${appState.getCurrentPattern()}`)
$rootScope.rawVisualizations = data.data.raw;
// Render visualizations
$rootScope.$broadcast('updateVis');
if(!$rootScope.$$phase) $rootScope.$digest();

await Promise.all([
$scope.decoders.nextPage(),
$scope.decodersAutoComplete.nextPage()
Expand All @@ -355,12 +332,5 @@ app.controller('decodersController', function ($scope, $rootScope, $sce, Decoder
$scope.decoders.reset();
$scope.decodersRelated.reset();
$scope.decodersAutoComplete.reset();
$rootScope.rawVisualizations = null;
if($rootScope.ownHandlers){
for(let h of $rootScope.ownHandlers){
h._scope.$destroy();
}
}
$rootScope.ownHandlers = [];
});
});
20 changes: 20 additions & 0 deletions public/directives/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Wazuh app - File for app requirements and set up
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import 'plugins/wazuh/directives/wz-dynamic/wz-dynamic';
import 'plugins/wazuh/directives/wz-enter/wz-enter';
import 'plugins/wazuh/directives/wz-menu/wz-menu';
import 'plugins/wazuh/directives/wz-menu/wz-menu.less';
import 'plugins/wazuh/directives/wz-search-bar/wz-search-bar';
import 'plugins/wazuh/directives/wz-table-header/wz-table-header';
import 'plugins/wazuh/directives/wz-table-header/wz-table-header.less';
import 'plugins/wazuh/directives/wz-table/wz-table';
import 'plugins/wazuh/directives/wz-table/wz-table.less';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Wazuh app - Module to export ruleset visualizations raw content
* Wazuh app - File for app requirements and set up
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -9,7 +9,4 @@
*
* Find more information about this on the LICENSE file.
*/
import decoders from './ruleset-decoders'
import rules from './ruleset-rules'

export { decoders, rules}
import 'plugins/wazuh/factories/data-handler-composer';
15 changes: 15 additions & 0 deletions public/kibana-integrations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Wazuh app - File for app requirements and set up
* Copyright (C) 2018 Wazuh, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Find more information about this on the LICENSE file.
*/
import 'plugins/wazuh/kibana-integrations/kibana-visualization.js';
import 'plugins/wazuh/kibana-integrations/kibana-filter-bar.js';
import 'plugins/wazuh/kibana-integrations/kibana-discover.js';
import 'plugins/wazuh/kibana-integrations/saved-visualizations.js';
10 changes: 6 additions & 4 deletions public/services/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const app = modules.get('app/wazuh', []);
app.service('apiReq', function ($q, $http, genericReq, appState, $location, $rootScope) {
return {
request: (method, path, body) => {
let defered = $q.defer();
const defered = $q.defer();

if (!method || !path || !body) {
defered.reject({
Expand All @@ -27,14 +27,16 @@ app.service('apiReq', function ($q, $http, genericReq, appState, $location, $roo
return defered.promise;
}

if (appState.getCurrentAPI() === undefined || appState.getCurrentAPI() === null)
if (!appState.getCurrentAPI()){
defered.reject({
error: -3,
message: 'No API selected.'
});
return defered.promise;
}

let id = JSON.parse(appState.getCurrentAPI()).id;
let requestData = { method, path, body, id };
const id = JSON.parse(appState.getCurrentAPI()).id;
const requestData = { method, path, body, id };

genericReq.request('POST', '/api/wazuh-api/request', requestData)
.then(data => {
Expand Down
23 changes: 5 additions & 18 deletions public/services/api-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ app.service('testAPI', function ($http, $location, $rootScope, appState, generic

appState.setPatternSelector(typeof configuration.data.data['ip.selector'] !== 'undefined' ? configuration.data.data['ip.selector'] : true)

return 'cookies_outdated';
return 'cookies_outdated'
/** End of checks for outdated cookies */

} else {
Expand All @@ -56,10 +56,9 @@ app.service('testAPI', function ($http, $location, $rootScope, appState, generic
} catch (error) {
if(error.status && error.status === -1){
$rootScope.apiIsDown = true;
return Promise.reject({data: 'request_timeout_checkstored'});
} else {
return Promise.reject(error);
}
}
return Promise.reject(error);

}
},
check: async data => {
Expand All @@ -77,19 +76,7 @@ app.service('testAPI', function ($http, $location, $rootScope, appState, generic
return response;

} catch(error) {
if(error.data && error.data.message && error.data.message.includes('ENOTFOUND')) {
return Promise.reject({data: 'invalid_url'});
} else if(error.data && error.data.message && error.data.message.includes('ECONNREFUSED')) {
return Promise.reject({data: 'invalid_port'});
} else if(error.status && error.status === -1){
return Promise.reject({data: 'request_timeout_checkapi'});
} else if (error.data && error.data.message && error.data.message === 'wrong_credentials') {
return Promise.reject({data: 'wrong_credentials'});
} else if(error.data && ((error.data.message && error.data.message === 'socket hang up') || (parseInt(error.data.error) === 5))) {
return Promise.reject({data:'socket_hang_up',extraMessage: `Wazuh API throws ${error.data.message}`, https: (data.url && data.url.includes('https'))});
} else {
return Promise.reject(error);
}
return Promise.reject(error);
}
}
};
Expand Down
64 changes: 1 addition & 63 deletions public/services/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ app.service('errorHandler', function ( Notifier, appState, $location) {
}

const isUnauthorized = error => (error.status && error.status === 401);
const isNotFound = error => (error.status && error.status === 404);
const isHttps = error => (typeof error.https !== 'undefined' && error.https);
const isBadRequest = error => (error.status && error.status === 400);
const isAPIUnauthorized = error => (error && error.data && parseInt(error.data.statusCode) === 500 && parseInt(error.data.error) === 7 && error.data.message === '401 Unauthorized');

const info = (message,location) => {
Expand All @@ -51,79 +48,20 @@ app.service('errorHandler', function ( Notifier, appState, $location) {
return;
}
const message = extractMessage(error);
let goSettings = false;
if(isUnauthorized(error)){
appState.removeUserCode();
$location.path('/wlogin');
return;
}

let text;
switch (message) {
case 'kibana_index_pattern_error':
text = `There seem to be a problem with Wazuh app visualizations in Kibana, please reinstall the Wazuh app.`;
break;
case 'elasticsearch_down':
text = `Could not find Kibana index on Elasticsearch or maybe Elasticsearch is down.<br>Please check it and try again.`;
break;
case 'no_elasticsearch':
text = `Could not connect with elasticsearch, maybe it's down.`;
break;
case 'no_credentials':
text = 'Valid credentials not found in elasticsearch. It seems the credentials ' +
'were not saved.';
break;
case 'protocol_error':
text = 'Invalid protocol in the API url. Please, specify <b>http://</b> or ' +
'<b>https://</b>.';
break;
case 'unauthorized':
text = 'Credentials were found, but they are not valid.';
break;
case 'bad_url':
text = 'The given URL does not contain a valid Wazuh RESTful API installation.';
break;
case 'self_signed':
text = 'The request to Wazuh RESTful API was blocked, because it is using a ' +
'selfsigned SSL certificate. Please, enable <b>"Accept selfsigned SSL"</b> ' +
'option if you want to connect anyway.';
break;
case 'not_running':
text = 'There are not services running in the given URL.';
break;
case 'request_timeout_checkstored':
text = 'The request to /api/wazuh-api/checkStoredAPI took too long and was aborted.';
goSettings = true;
break;
case 'request_timeout_checkapi':
text = 'The request to /api/wazuh-api/checkAPI took too long and was aborted.';
break;
case 'wrong_credentials':
text = 'Wrong Wazuh API credentials, please check them and try again';
break;
case 'invalid_url':
text = 'Wrong Wazuh API url, please check it and try again';
break;
case 'invalid_port':
text = 'Wrong Wazuh API port, please check it and try again';
break;
case 'socket_hang_up':
if(isHttps(error)){
text = 'Wrong Wazuh API protocol, please check it and try again with http instead https';
} else {
text = 'Could not connect with Wazuh API, please check url and port and try again.'
}
break;
default:
text = isWarning ? `Warning. ${message}` : `Error. ${message}`;
}
text = isWarning ? `Warning. ${message}` : `Error. ${message}`;
if(error.extraMessage) text = error.extraMessage;
text = location ? location + '. ' + text : text;
if(!silent){
if(isWarning || (text && typeof text === 'string' && text.toLowerCase().includes('no results'))) notify.warning(text);
else notify.error(text);
}
if(goSettings) $location.path('/settings');
return text;
}

Expand Down
Loading