2018-04-22 20:16:38 +00:00
|
|
|
/*
|
|
|
|
* Wazuh app - API test 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 chrome from 'ui/chrome';
|
|
|
|
import * as modules from 'ui/modules'
|
|
|
|
|
|
|
|
const app = modules.get('app/wazuh', []);
|
2018-01-29 11:39:33 +00:00
|
|
|
|
2018-06-04 15:16:19 +00:00
|
|
|
app.service('testAPI', function ($http, $location, $rootScope, appState, genericReq, wzMisc) {
|
2017-10-30 10:35:30 +00:00
|
|
|
return {
|
2018-04-02 11:01:10 +00:00
|
|
|
check_stored: async data => {
|
|
|
|
try {
|
|
|
|
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
/** Checks for outdated cookies */
|
|
|
|
const current = appState.getCreatedAt();
|
|
|
|
const lastRestart = $rootScope.lastRestart;
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
if(current && lastRestart && lastRestart > current){
|
|
|
|
appState.removeCurrentPattern();
|
|
|
|
appState.removeCurrentAPI();
|
|
|
|
appState.removeClusterInfo();
|
|
|
|
appState.removeCreatedAt();
|
|
|
|
delete $rootScope.lastRestart;
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
const configuration = await genericReq.request('GET', '/api/wazuh-api/configuration', {})
|
2018-03-12 15:36:20 +00:00
|
|
|
|
2018-03-13 14:18:49 +00:00
|
|
|
appState.setPatternSelector(typeof configuration.data.data['ip.selector'] !== 'undefined' ? configuration.data.data['ip.selector'] : true)
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-05-04 09:07:57 +00:00
|
|
|
return 'cookies_outdated'
|
2018-04-02 11:01:10 +00:00
|
|
|
/** End of checks for outdated cookies */
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
} else {
|
|
|
|
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
const result = await Promise.all([
|
|
|
|
genericReq.request('GET', '/api/wazuh-api/configuration', {}),
|
|
|
|
$http.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers)
|
|
|
|
]);
|
2018-03-11 11:17:33 +00:00
|
|
|
|
2018-04-02 11:01:10 +00:00
|
|
|
appState.setPatternSelector(typeof result[0].data.data['ip.selector'] !== 'undefined' ? result[0].data.data['ip.selector'] : true)
|
|
|
|
|
|
|
|
if(result[1].error) {
|
|
|
|
return Promise.reject(result[1])
|
2018-03-13 14:18:49 +00:00
|
|
|
}
|
2018-04-02 11:01:10 +00:00
|
|
|
return result[1];
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if(error.status && error.status === -1){
|
2018-06-04 15:16:19 +00:00
|
|
|
wzMisc.setApiIsDown(true)
|
2018-04-30 16:02:52 +00:00
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
2018-03-13 14:18:49 +00:00
|
|
|
}
|
2017-10-30 10:35:30 +00:00
|
|
|
},
|
2018-04-02 10:44:43 +00:00
|
|
|
check: async data => {
|
|
|
|
try {
|
|
|
|
const headers = {headers:{ "Content-Type": 'application/json' },timeout: $rootScope.userTimeout || 8000};
|
|
|
|
if(appState.getUserCode()) headers.headers.code = appState.getUserCode();
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-04-02 10:44:43 +00:00
|
|
|
const url = chrome.addBasePath("/api/wazuh-api/checkAPI");
|
|
|
|
const response = await $http.post(url, data, headers);
|
|
|
|
|
2017-10-30 10:35:30 +00:00
|
|
|
if (response.error) {
|
2018-04-02 10:44:43 +00:00
|
|
|
return Promise.reject(response);
|
2018-04-22 20:16:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:43 +00:00
|
|
|
return response;
|
|
|
|
|
|
|
|
} catch(error) {
|
2018-04-30 16:02:52 +00:00
|
|
|
return Promise.reject(error);
|
2018-04-02 10:44:43 +00:00
|
|
|
}
|
2017-10-30 10:35:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|