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-09-10 08:32:49 +00:00
|
|
|
import chrome from 'ui/chrome';
|
2018-07-16 07:07:34 +00:00
|
|
|
import { uiModules } from 'ui/modules';
|
2018-04-21 11:31:47 +00:00
|
|
|
|
2018-06-15 10:42:57 +00:00
|
|
|
const app = uiModules.get('app/wazuh', []);
|
2018-01-29 11:39:33 +00:00
|
|
|
|
2018-08-20 07:00:50 +00:00
|
|
|
class ApiTester {
|
2018-09-10 08:32:49 +00:00
|
|
|
constructor($http, appState, wzMisc, wazuhConfig) {
|
|
|
|
this.$http = $http;
|
|
|
|
this.appState = appState;
|
|
|
|
this.wzMisc = wzMisc;
|
|
|
|
this.wazuhConfig = wazuhConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkStored(data) {
|
|
|
|
try {
|
|
|
|
const configuration = this.wazuhConfig.getConfig();
|
|
|
|
const timeout = configuration ? configuration.timeout : 8000;
|
|
|
|
const headers = {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
timeout: timeout || 8000
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Checks for outdated cookies */
|
|
|
|
const current = this.appState.getCreatedAt();
|
|
|
|
const lastRestart = this.wzMisc.getLastRestart();
|
|
|
|
|
|
|
|
if (current && lastRestart && lastRestart > current) {
|
|
|
|
this.appState.removeCurrentPattern();
|
|
|
|
this.appState.removeCurrentAPI();
|
|
|
|
this.appState.removeClusterInfo();
|
|
|
|
this.appState.removeCreatedAt();
|
|
|
|
this.wzMisc.setLastRestart(null);
|
|
|
|
|
|
|
|
this.appState.setPatternSelector(configuration['ip.selector']);
|
|
|
|
|
|
|
|
return 'cookies_outdated';
|
|
|
|
/** End of checks for outdated cookies */
|
|
|
|
} else {
|
|
|
|
const result = await this.$http.post(
|
|
|
|
chrome.addBasePath('/api/wazuh-api/checkStoredAPI'),
|
|
|
|
data,
|
|
|
|
headers
|
|
|
|
);
|
|
|
|
|
|
|
|
this.appState.setPatternSelector(configuration['ip.selector']);
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
return Promise.reject(result);
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.status && error.status === -1) {
|
|
|
|
this.wzMisc.setApiIsDown(true);
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
async check(data) {
|
|
|
|
try {
|
|
|
|
const { timeout } = this.wazuhConfig.getConfig();
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
const headers = {
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
timeout: timeout || 8000
|
|
|
|
};
|
2018-04-22 20:16:38 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
const url = chrome.addBasePath('/api/wazuh-api/checkAPI');
|
|
|
|
const response = await this.$http.post(url, data, headers);
|
2018-04-02 10:44:43 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
if (response.error) {
|
|
|
|
return Promise.reject(response);
|
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
2018-09-10 08:32:49 +00:00
|
|
|
}
|
2018-08-20 07:00:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:32:49 +00:00
|
|
|
app.service('testAPI', ApiTester);
|