2018-04-22 20:16:38 +00:00
|
|
|
/*
|
|
|
|
* Wazuh app - Error handler service
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-04-21 11:31:47 +00:00
|
|
|
import * as modules from 'ui/modules'
|
|
|
|
|
|
|
|
const app = modules.get('app/wazuh', []);
|
2018-01-29 11:39:47 +00:00
|
|
|
|
|
|
|
app.service('errorHandler', function ( Notifier, appState, $location) {
|
|
|
|
const notify = new Notifier();
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-01-29 11:39:47 +00:00
|
|
|
const extractMessage = error => {
|
2018-01-29 12:50:26 +00:00
|
|
|
if(error.data && error.data.errorData && error.data.errorData.message) return error.data.errorData.message;
|
|
|
|
if(error.errorData && error.errorData.message) return error.errorData.message;
|
2018-01-29 11:39:47 +00:00
|
|
|
if(error.data && typeof error.data === 'string') return error.data;
|
2018-04-02 10:15:58 +00:00
|
|
|
if(error.data && error.data.message && typeof error.data.message === 'string') return error.data.message;
|
|
|
|
if(error.data && error.data.message && error.data.message.msg && typeof error.data.message.msg === 'string') return error.data.message.msg;
|
2018-01-29 16:04:36 +00:00
|
|
|
if(error.data && error.data.data && typeof error.data.data === 'string') return error.data.data;
|
2018-02-08 14:45:08 +00:00
|
|
|
if(typeof error.message === 'string') return error.message;
|
|
|
|
if(error.message && error.message.msg) return error.message.msg;
|
2018-01-29 11:39:47 +00:00
|
|
|
if(typeof error === 'string') return error;
|
|
|
|
if(typeof error === 'object') return JSON.stringify(error);
|
|
|
|
return error || 'Unexpected error';
|
|
|
|
}
|
|
|
|
|
|
|
|
const isUnauthorized = error => (error.status && error.status === 401);
|
|
|
|
const isNotFound = error => (error.status && error.status === 404);
|
2018-01-29 14:39:54 +00:00
|
|
|
const isHttps = error => (typeof error.https !== 'undefined' && error.https);
|
2018-01-29 11:39:47 +00:00
|
|
|
const isBadRequest = error => (error.status && error.status === 400);
|
2018-02-05 14:30:00 +00:00
|
|
|
const isAPIUnauthorized = error => (error && error.data && parseInt(error.data.statusCode) === 500 && parseInt(error.data.error) === 7 && error.data.message === '401 Unauthorized');
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-01-29 14:39:54 +00:00
|
|
|
const info = (message,location) => {
|
|
|
|
if(typeof message === 'string') {
|
|
|
|
message = location ? location + '. ' + message : message;
|
|
|
|
notify.info(message);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-08 14:45:08 +00:00
|
|
|
const handle = (error,location,isWarning,silent) => {
|
2018-02-05 14:30:00 +00:00
|
|
|
if(isAPIUnauthorized(error)){
|
|
|
|
$location.path('/settings');
|
|
|
|
return;
|
|
|
|
}
|
2018-01-29 11:39:47 +00:00
|
|
|
const message = extractMessage(error);
|
2018-02-05 09:55:52 +00:00
|
|
|
let goSettings = false;
|
2018-01-29 11:39:47 +00:00
|
|
|
if(isUnauthorized(error)){
|
|
|
|
appState.removeUserCode();
|
2018-02-12 09:14:58 +00:00
|
|
|
$location.path('/wlogin');
|
2018-01-29 11:39:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let text;
|
|
|
|
switch (message) {
|
2018-02-21 12:53:41 +00:00
|
|
|
case 'kibana_index_pattern_error':
|
2018-02-21 14:21:51 +00:00
|
|
|
text = `There seem to be a problem with Wazuh app visualizations in Kibana, please reinstall the Wazuh app.`;
|
2018-02-21 12:53:41 +00:00
|
|
|
break;
|
|
|
|
case 'elasticsearch_down':
|
2018-04-13 15:26:41 +00:00
|
|
|
text = `Could not find Kibana index on Elasticsearch or maybe Elasticsearch is down.<br>Please check it and try again.`;
|
2018-02-21 12:53:41 +00:00
|
|
|
break;
|
2018-01-29 11:39:47 +00:00
|
|
|
case 'no_elasticsearch':
|
2018-01-29 16:04:36 +00:00
|
|
|
text = `Could not connect with elasticsearch, maybe it's down.`;
|
2018-01-29 11:39:47 +00:00
|
|
|
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.';
|
2018-02-05 09:55:52 +00:00
|
|
|
goSettings = true;
|
2018-01-29 11:39:47 +00:00
|
|
|
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':
|
2018-01-29 14:39:54 +00:00
|
|
|
if(isHttps(error)){
|
2018-01-29 11:39:47 +00:00
|
|
|
text = 'Wrong Wazuh API protocol, please check it and try again with http instead https';
|
|
|
|
} else {
|
2018-03-22 10:39:01 +00:00
|
|
|
text = 'Could not connect with Wazuh API, please check url and port and try again.'
|
2018-01-29 11:39:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2018-01-29 14:10:54 +00:00
|
|
|
text = isWarning ? `Warning. ${message}` : `Error. ${message}`;
|
2018-01-29 11:39:47 +00:00
|
|
|
}
|
2018-03-22 10:39:01 +00:00
|
|
|
if(error.extraMessage) text = error.extraMessage;
|
2018-01-29 14:39:54 +00:00
|
|
|
text = location ? location + '. ' + text : text;
|
2018-02-08 14:45:08 +00:00
|
|
|
if(!silent){
|
|
|
|
if(isWarning) notify.warning(text);
|
|
|
|
else notify.error(text);
|
|
|
|
}
|
2018-02-05 09:55:52 +00:00
|
|
|
if(goSettings) $location.path('/settings');
|
2018-01-29 11:39:47 +00:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-01-29 16:31:58 +00:00
|
|
|
handle: handle,
|
|
|
|
info: info
|
2018-01-29 11:39:47 +00:00
|
|
|
}
|
2018-04-22 20:16:38 +00:00
|
|
|
});
|