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

84 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
* Wazuh app - API test service
2019-01-14 16:36:47 +00:00
* Copyright (C) 2015-2019 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-09-11 09:52:54 +00:00
export class ApiTester {
/**
* Class constructor
2018-12-13 10:02:53 +00:00
* @param {*} $http
* @param {*} appState
* @param {*} wzMisc
* @param {*} wazuhConfig
*/
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 : 20000;
2018-09-10 08:32:49 +00:00
const headers = {
headers: { 'Content-Type': 'application/json' },
timeout: timeout || 20000
2018-09-10 08:32:49 +00:00
};
2019-10-09 10:18:50 +00:00
const payload = { id: data };
const result = await this.$http.post(
chrome.addBasePath('/api/check-stored-api'),
Migrate API entries from .wazuh index to wazuh.yml (#1811) * Removed "Pattern" and "First use" sections from Settings * Simplified API entries table in Settings * Started "API is down" guide * Started "Add API" guide * Loading new components for Settings * Adapt Settings views for using new components * Migrate hosts and remove .wazuh index * Prevent duplicate hosts * Improve regex * Cleaning settings.js controller (in progress) * Prevent try to add again if .wazuh was not deleted * Add function to check if is busy * Migrate the cluster_info and extensions to the wazuh-registry * Replace wazuh-version by wazuh-registry * Add endpoint to get the host from wazuh-hosts.yml * Get the APIs from the wazuh-hosts.yml * Updates the cluster info in the registry when checking connection * Remove console.error * Check api connection from wazuh-host data * Try to connect to another API in case the default fails * Adapt settings-wizard and api-count * Adapt wazuh-api.js controller for using wazuh-hosts.yml * Prevent undefined results * Clean initialize * Remove wazuh-api-elastic routes and controllers * Remove addApiProps * Change config.yml by wazuh.yml * Replace config.yml in the kibana plugin helper * Delete wazuh-api-elastic tests * Check for orphan registry entries * Check manager connection before setting as default * Prevent error toaster when there is any api entry * Prevent errors when any api is reachable * Check for new apis and their connection * Prevent error when update registry of undefined * Prevent error when no cluster_info * Remove console.log * Change state by props * Fix settings-wizard * Fix when joinning hosts and registry * Check for new API entries * Close add api component * Remove await * Improve checks for new apis * Test if API is down in wazuh-api * Check down APIs * Api is down component finish * Transform hosts in the backend * Fix key * Adapt removeOrphanentries * Adapt settings-wizard * Improve api-is-down component * Change the way to display the helpers components * Check APIs status when get them * Remove console.log * Check manager sets the status to the API entry * Prevent create wazuh-registry.json without hosts * FIx extensions * Add panel * Change style for wazuh hosts * Show add api component from the table * Refresh API entries * Update wazuh-hosts * Api is down table loading effect * Show API is down when accessing to settings if any API is up * Update cluster info in the settings wizard * Change color * Remove wazuh-hosts.yml * Remove hosts from configuration * Fix \n in the migration * Fix api-count * Fix typo * Fix API wizard * Fix example in getting started guide * Fix typos * Refresh hosts in api-is-down component * Set default in props * Fix API count * Iterates the api entries to set one as default * Fix component unmount * Fix flick * Fix when refresh and all entries and the yml is corrupt * Improve handler error when getting error in the yml parser * Adapt monitoring * Allow unsigned certs * Fix when there is not stored api - health check * Fix when any api entry could be select * Warning when set an API as default * Send to settings when achieve connect with an API when trying set as default * Try to set others API entries * Preven duplicate hosts: in wazuh.yml * Improve when stored api is not reachable handler * Fix typo * Added ID and the capability to search in the table * Fix input style * Fix typo * Do pararell requests * Fix when checking entry * Add cheking spinner while checking api connection * Spinner while checking in api-is-down component * Use for instead of forEach * Change spinner by message while refreshing entries * Add a spinner by each entry while checking it * Show the reason why wazuh is down * Improve message from error 3099 * Add capability to copy to the clipboard the error
2019-10-09 10:13:14 +00:00
payload,
headers
);
2018-09-10 08:32:49 +00:00
this.appState.setPatternSelector(configuration['ip.selector']);
2018-09-10 08:32:49 +00:00
if (result.error) {
return Promise.reject(result);
2018-09-10 08:32:49 +00:00
}
return result;
2018-09-10 08:32:49 +00:00
} catch (error) {
if (((error || {}).data || {}).code === 3099) {
// Do nothing
return 3099;
}
2018-09-10 08:32:49 +00:00
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 || 20000
2018-09-10 08:32:49 +00:00
};
2018-10-01 07:56:50 +00:00
const url = chrome.addBasePath('/api/check-api');
2018-09-10 08:32:49 +00:00
const response = await this.$http.post(url, data, headers);
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
}