wazuh-kibana-app/public/services/api-tester.js

81 lines
2.8 KiB
JavaScript
Raw Normal View History

/*
* 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.
*/
import chrome from 'ui/chrome';
2018-07-16 07:07:34 +00:00
import { uiModules } from 'ui/modules';
2018-06-15 10:42:57 +00:00
const app = uiModules.get('app/wazuh', []);
2018-07-16 07:07:34 +00:00
app.service('testAPI', function ($http, $rootScope, appState, wzMisc, wazuhConfig) {
2017-10-30 10:35:30 +00:00
return {
check_stored: async data => {
try {
2018-07-16 07:07:34 +00:00
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;
2018-07-16 07:07:34 +00:00
if(current && lastRestart && lastRestart > current){
appState.removeCurrentPattern();
appState.removeCurrentAPI();
appState.removeClusterInfo();
appState.removeCreatedAt();
delete $rootScope.lastRestart;
appState.setPatternSelector(configuration['ip.selector']);
return 'cookies_outdated';
/** End of checks for outdated cookies */
} else {
const result = await $http.post(chrome.addBasePath('/api/wazuh-api/checkStoredAPI'), data,headers);
2018-03-11 11:17:33 +00:00
appState.setPatternSelector(configuration['ip.selector']);
if(result.error) {
return Promise.reject(result);
}
return result;
}
} catch (error) {
if(error.status && error.status === -1){
wzMisc.setApiIsDown(true);
2018-04-30 16:02:52 +00:00
}
return Promise.reject(error);
}
2017-10-30 10:35:30 +00:00
},
check: async data => {
try {
2018-07-16 07:07:34 +00:00
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);
2017-10-30 10:35:30 +00:00
if (response.error) {
return Promise.reject(response);
}
return response;
} catch(error) {
2018-04-30 16:02:52 +00:00
return Promise.reject(error);
}
2017-10-30 10:35:30 +00:00
}
};
});