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

Services improvements #715

Merged
merged 15 commits into from
Jul 16, 2018
13 changes: 6 additions & 7 deletions public/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
*
* Find more information about this on the LICENSE file.
*/
import base64 from '../utils/base64.js'
import chrome from 'ui/chrome'
import { uiModules } from 'ui/modules'
import TabNames from '../utils/tab-names'
import base64 from '../utils/base64.js';
import { uiModules } from 'ui/modules';
import TabNames from '../utils/tab-names';

const app = uiModules.get('app/wazuh', []);

app.controller('settingsController',
function ($scope, $rootScope, $http, $routeParams, $route, $location, testAPI, appState, genericReq, errorHandler, wzMisc, wazuhConfig) {
function ($scope, $routeParams, $window, $route, $location, testAPI, appState, genericReq, errorHandler, wzMisc, wazuhConfig) {
if (wzMisc.getValue('comeFromWizard')) {
sessionStorage.removeItem('healthCheck');
wzMisc.setWizard(false)
$window.sessionStorage.removeItem('healthCheck');
wzMisc.setWizard(false);
}

$scope.apiIsDown = wzMisc.getValue('apiIsDown');
Expand Down
10 changes: 5 additions & 5 deletions public/directives/wazuh-table/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ app.directive('wazuhTable', function() {
$scope.wazuh_table_loading = false;
if(!$scope.$$phase) $scope.$digest()
} catch (error) {
errorHandler.handle(`Error sorting table by ${field ? field.value : 'undefined'} due to ${error.message || error}`,'Data factory');
errorHandler.handle(`Error sorting table by ${field ? field.value : 'undefined'}. ${error.message || error}`,'Data factory');
}
return;
};
Expand All @@ -171,7 +171,7 @@ app.directive('wazuhTable', function() {
$scope.wazuh_table_loading = false;
if(!$scope.$$phase) $scope.$digest();
} catch(error) {
errorHandler.handle(`Error searching data due to ${error.message || error}`,'Data factory');
errorHandler.handle(`Error searching. ${error.message || error}`,'Data factory');
}
return;
};
Expand All @@ -192,7 +192,7 @@ app.directive('wazuhTable', function() {
$scope.wazuh_table_loading = false;
if(!$scope.$$phase) $scope.$digest();
} catch(error) {
errorHandler.handle(`Error filtering by ${filter ? filter.value : 'undefined'} due to ${error.message || error}`,'Data factory');
errorHandler.handle(`Error filtering by ${filter ? filter.value : 'undefined'}. ${error.message || error}`,'Data factory');
}
return;
};
Expand Down Expand Up @@ -226,7 +226,7 @@ app.directive('wazuhTable', function() {
}
} catch(error) {
realTime = false;
errorHandler.handle(`Real time feature aborted due to ${error.message || error}`,'Data factory');
errorHandler.handle(`Real time feature aborted. ${error.message || error}`,'Data factory');
}
return;
};
Expand Down Expand Up @@ -256,7 +256,7 @@ app.directive('wazuhTable', function() {
$scope.wazuh_table_loading = false;
if(!$scope.$$phase) $scope.$digest();
} catch (error) {
errorHandler.handle(`Error while init table due to ${error.message || error}`,'Data factory');
errorHandler.handle(`Error while init table. ${error.message || error}`,'Data factory');
}
return;
};
Expand Down
55 changes: 24 additions & 31 deletions public/services/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,39 @@
*
* Find more information about this on the LICENSE file.
*/
import chrome from 'ui/chrome';
import { uiModules } from 'ui/modules'
import { uiModules } from 'ui/modules';

const app = uiModules.get('app/wazuh', []);

app.service('apiReq', function ($q, $http, genericReq, appState, $location, $rootScope) {
app.service('apiReq', function ($q, genericReq, appState) {
return {
request: (method, path, body) => {
const defered = $q.defer();
request: async (method, path, body) => {
try {

if (!method || !path || !body) {
defered.reject({
error: -1,
message: 'Missing parameters'
});
return defered.promise;
}

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

const id = JSON.parse(appState.getCurrentAPI()).id;
const requestData = { method, path, body, id };
if (!method || !path || !body) {
throw new Error('Missing parameters');
}

if (!appState.getCurrentAPI()){
throw new Error('No API selected.');
}

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

genericReq.request('POST', '/api/wazuh-api/request', requestData)
.then(data => {
const data = await genericReq.request('POST', '/api/wazuh-api/request', requestData);

if (data.error) {
defered.reject(data);
} else {
defered.resolve(data);
throw new Error(data.error);
}
})
.catch(error => defered.reject(error));
return $q.resolve(data);

return defered.promise;
} catch (error) {
return error && error.data && error.data.message ?
$q.reject(error.data.message) :
$q.reject(error.message || error);
}
}
};
});
17 changes: 9 additions & 8 deletions public/services/api-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
* Find more information about this on the LICENSE file.
*/
import chrome from 'ui/chrome';
import { uiModules } from 'ui/modules'
import { uiModules } from 'ui/modules';

const app = uiModules.get('app/wazuh', []);

app.service('testAPI', function ($http, $location, $rootScope, appState, wzMisc, wazuhConfig) {
app.service('testAPI', function ($http, $rootScope, appState, wzMisc, wazuhConfig) {
return {
check_stored: async data => {
try {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
const configuration = wazuhConfig.getConfig();
const timeout = configuration ? configuration.timeout : 8000;
const headers = {headers:{ "Content-Type": 'application/json' }, timeout: timeout || 8000};

/** Checks for outdated cookies */
const current = appState.getCreatedAt();
const lastRestart = $rootScope.lastRestart;
const configuration = wazuhConfig.getConfig();


if(current && lastRestart && lastRestart > current){
appState.removeCurrentPattern();
appState.removeCurrentAPI();
Expand All @@ -38,7 +39,6 @@ app.service('testAPI', function ($http, $location, $rootScope, appState, wzMisc,
/** End of checks for outdated cookies */

} else {
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();

const result = await $http.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers);

Expand All @@ -59,8 +59,9 @@ app.service('testAPI', function ($http, $location, $rootScope, appState, wzMisc,
},
check: async data => {
try {
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
const { timeout } = wazuhConfig.getConfig();

const headers = {headers:{ "Content-Type": 'application/json' },timeout: timeout || 8000};

const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
const response = await $http.post(url, data, headers);
Expand Down
12 changes: 0 additions & 12 deletions public/services/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,12 @@ uiModules.get('app/wazuh', [])
$cookies.putObject('API', API, { 'expires': exp});
}
},
setUserCode: code => {
$cookies.putObject('userCode', code);
},
getUserCode: () => {
return $cookies.getObject('userCode');
},
removeUserCode: () => {
return $cookies.remove('userCode');
},
getPatternSelector: () => {
return $cookies.getObject('patternSelector');
},
setPatternSelector: value => {
$cookies.putObject('patternSelector', value);
},
removePatternSelector: () => {
return $cookies.remove('patternSelector');
},
setCurrentDevTools: current => {
$window.localStorage.setItem('currentDevTools',current);
},
Expand Down
4 changes: 2 additions & 2 deletions public/services/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
*
* Find more information about this on the LICENSE file.
*/
import { uiModules } from 'ui/modules'
import { uiModules } from 'ui/modules';

const app = uiModules.get('app/wazuh', []);

app.service('errorHandler', function ( Notifier, appState, $location) {
app.service('errorHandler', function ( Notifier, $location) {
const notify = new Notifier();

const extractMessage = error => {
Expand Down
97 changes: 37 additions & 60 deletions public/services/generic-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,48 @@
*
* Find more information about this on the LICENSE file.
*/
import chrome from 'ui/chrome';
import chrome from 'ui/chrome';
import { uiModules } from 'ui/modules';

uiModules.get('app/wazuh', [])
.service('genericReq', function ($q, $http, $location, appState, wazuhConfig) {

const _request = (method, url, payload = null) => {
const defered = $q.defer();

if (!method || !url) {
defered.reject({
'error': -1,
'message': 'Missing parameters'
});
return defered.promise;
}

const config = wazuhConfig.getConfig();

const requestHeaders = { headers: { "Content-Type": 'application/json' }, timeout: config.timeout || 8000 };

const tmpUrl = chrome.addBasePath(url);
let tmp = null;
if(appState.getUserCode()) requestHeaders.headers.code = appState.getUserCode();
const id = appState.getCurrentAPI() ? JSON.parse(appState.getCurrentAPI()).id : false;
if(id) requestHeaders.headers.id = id;
if (method === "GET") tmp = $http.get(tmpUrl, requestHeaders);
if (method === "PUT") tmp = $http.put(tmpUrl, payload, requestHeaders);
if (method === "POST") tmp = $http.post(tmpUrl, payload, requestHeaders);
if (method === "DELETE") tmp = $http.delete(tmpUrl);

if(!tmp) {
defered.reject({
error: -2,
message: `Error doing a request to ${tmpUrl}, method: ${method}.`
});
return defered.promise;
}

tmp
.then(data => {
if (data.error && data.error !== '0') {
defered.reject(data);
} else {
defered.resolve(data);
}
})
.catch(defered.reject);

return defered.promise;
};
const app = uiModules.get('app/wazuh', []);

app.service('genericReq', function ($q, $http, appState, wazuhConfig) {
return {
request: (method, path, payload = null) => {
if (!method || !path) {
return Promise.reject(new Error('Missing parameters'));
request: async (method, path, payload = null) => {
try {
if (!method || !path) {
throw new Error('Missing parameters');
}

const { timeout } = wazuhConfig.getConfig();
const requestHeaders = { headers: { "Content-Type": 'application/json' }, timeout: timeout || 8000 };
const tmpUrl = chrome.addBasePath(path);

try {
requestHeaders.headers.id = JSON.parse(appState.getCurrentAPI()).id;
} catch (error) {
// Intended
}

const data = {};
if (method === "GET") Object.assign(data, await $http.get(tmpUrl, requestHeaders));
if (method === "PUT") Object.assign(data, await $http.put(tmpUrl, payload, requestHeaders));
if (method === "POST") Object.assign(data, await $http.post(tmpUrl, payload, requestHeaders));
if (method === "DELETE") Object.assign(data, await $http.delete(tmpUrl));

if (!data) {
throw new Error(`Error doing a request to ${tmpUrl}, method: ${method}.`);
}

if (data.error && data.error !== '0') {
throw new Error(data.error);
}

return $q.resolve(data);

} catch (error) {
return $q.reject(error);
}

const defered = $q.defer();

_request(method, path, payload)
.then(defered.resolve)
.catch(defered.reject);

return defered.promise;
}
};
});
20 changes: 10 additions & 10 deletions public/services/resolves/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Find more information about this on the LICENSE file.
*/

export default ($q, genericReq, errorHandler, wazuhConfig) => {
export default async ($q, genericReq, errorHandler, wazuhConfig) => {
// Remember to keep this values equal to default config.yml values
const defaultConfig = {
pattern : 'wazuh-alerts-3.x-*',
Expand All @@ -36,9 +36,10 @@ export default ($q, genericReq, errorHandler, wazuhConfig) => {
'wazuh.monitoring.frequency': 3600
};

const deferred = $q.defer();
genericReq.request('GET', '/api/wazuh-api/configuration', {})
.then(config => {
try {

const config = await genericReq.request('GET', '/api/wazuh-api/configuration', {});

if(!config || !config.data || !config.data.data) throw new Error('No config available');

const ymlContent = config.data.data;
Expand All @@ -52,12 +53,11 @@ export default ($q, genericReq, errorHandler, wazuhConfig) => {

wazuhConfig.setConfig(defaultConfig);

deferred.resolve();
})
.catch(() => {
} catch (error) {
wazuhConfig.setConfig(defaultConfig);
errorHandler.handle('Error parsing config.yml, using default values.', 'Config', true);
deferred.resolve();
});
return deferred.promise;
}

return $q.resolve();

};
4 changes: 2 additions & 2 deletions public/services/resolves/get-saved-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*
* Find more information about this on the LICENSE file.
*/
import healthCheck from './health-check'
import healthCheck from './health-check';

export default (courier, $q, $window, $rootScope, savedSearches, $route) => {
export default (courier, $location, $window, $rootScope, savedSearches, $route) => {
if (healthCheck($window, $rootScope)) {
$location.path('/health-check');
return Promise.reject();
Expand Down
Loading