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

Add centralized request service #4831

Merged
merged 20 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Added validation to the plugin settings in the form of `Settings/Configuration` and the endpoint to update the plugin configuration [#4503](https:/wazuh/wazuh-kibana-app/pull/4503)[#4785](https:/wazuh/wazuh-kibana-app/pull/4785)
- Added new plugin settings to customize the header and footer on the PDF reports [#4505](https:/wazuh/wazuh-kibana-app/pull/4505)[#4798](https:/wazuh/wazuh-kibana-app/pull/4798)[#4805](https:/wazuh/wazuh-kibana-app/pull/4805)
- Add a new plugin setting to enable or disable the customization [#4507](https:/wazuh/wazuh-kibana-app/pull/4507)
- Added a centralized service to handle the requests [#4831](https:/wazuh/wazuh-kibana-app/pull/4831)

### Changed

Expand Down
2 changes: 2 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { getThemeAssetURL, getAssetURL } from './utils/assets';
import { WzRequest } from './react-services/wz-request';
import store from './redux/store';
import { updateAppConfig } from './redux/actions/appConfigActions';
import { initializeInterceptor } from './services/request-handler';

const SIDEBAR_LOGO = 'customization.logo.sidebar';
const innerAngularName = 'app/wazuh';
Expand Down Expand Up @@ -62,6 +63,7 @@ export class WazuhPlugin implements Plugin<WazuhSetup, WazuhStart, WazuhSetupPlu
getThemeAssetURL('icon.svg', UI_THEME)),
mount: async (params: AppMountParameters) => {
try {
initializeInterceptor(core);
if (!this.initializeInnerAngular) {
throw Error('Wazuh plugin method initializeInnerAngular is undefined');
}
Expand Down
4 changes: 2 additions & 2 deletions public/react-services/generic-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
* Find more information about this on the LICENSE file.
*/

import axios from 'axios';
import { AppState } from './app-state';
import { WazuhConfig } from './wazuh-config';
import { ApiCheck } from './wz-api-check';
import { WzMisc } from '../factories/misc';
import { OdfeUtils } from '../utils';
import { getHttp, getDataPlugin } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class GenericRequest {
static async request(method, path, payload = null, returnError = false) {
Expand Down Expand Up @@ -81,7 +81,7 @@ export class GenericRequest {
};
}

Object.assign(data, await axios(options));
Object.assign(data, await request(options));
if (!data) {
throw new Error(
`Error doing a request to ${tmpUrl}, method: ${method}.`
Expand Down
6 changes: 3 additions & 3 deletions public/react-services/wz-api-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* Find more information about this on the LICENSE file.
*/
import { WazuhConfig } from './wazuh-config';
import axios from 'axios';
import { AppState } from './app-state';
import { WzMisc } from '../factories/misc';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class ApiCheck {
static async checkStored(data, idChanged = false) {
Expand All @@ -40,7 +40,7 @@ export class ApiCheck {
AppState.setPatternSelector(configuration['ip.selector']);
}

const response = await axios(options);
const response = await request(options);

if (response.error) {
return Promise.reject(response);
Expand Down Expand Up @@ -79,7 +79,7 @@ export class ApiCheck {
timeout: timeout || 20000
};

const response = await axios(options);
const response = await request(options);

if (response.error) {
return Promise.reject(response);
Expand Down
5 changes: 3 additions & 2 deletions public/react-services/wz-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*
* Find more information about this on the LICENSE file.
*/
import axios from 'axios';
import { AppState } from './app-state';
import { ApiCheck } from './wz-api-check';
import { WzAuthentication } from './wz-authentication';
Expand All @@ -19,6 +18,8 @@ import { OdfeUtils } from '../utils';
import IApiResponse from './interfaces/api-response.interface';
import { getHttp } from '../kibana-services';
import { PLUGIN_PLATFORM_REQUEST_HEADERS } from '../../common/constants';
import { request } from '../services/request-handler';

export class WzRequest {
static wazuhConfig: any;

Expand Down Expand Up @@ -58,7 +59,7 @@ export class WzRequest {
timeout: timeout,
};

const data = await axios(options);
const data = await request(options);

if (data['error']) {
throw new Error(data['error']);
Expand Down
52 changes: 52 additions & 0 deletions public/services/request-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios from 'axios';
import { HTTP_STATUS_CODES } from '../../common/constants';

let allow = true;
const source = axios.CancelToken.source();

const disableRequests = () => {
allow = false;
source.cancel('Requests cancelled');
return;
}

export const initializeInterceptor = (core) => {
core.http.intercept({
responseError: (httpErrorResponse, controller) => {
if (
httpErrorResponse.response?.status === 401
) {
disableRequests();
}
},
});
}

export const request = async (options = '') => {
if (!allow) {
return Promise.reject('Requests are disabled');
}
if (!options.method | !options.url) {
return Promise.reject("Missing parameters")
}
options = {
...options, cancelToken: source.token, validateStatus: function (status) {
return (status >= 200 && status < 300) || status === 401;
},
}
if (allow) {
try {
const requestData = await axios(options);
if (requestData.status === HTTP_STATUS_CODES.UNAUTHORIZED) {
if (requestData.data.message === 'Unauthorized' || requestData.data.message === 'Authentication required') {
disableRequests();
}
throw new Error(requestData.data.message)
Desvelao marked this conversation as resolved.
Show resolved Hide resolved
}
return Promise.resolve(requestData);
}
catch (e) {
return Promise.reject(e);
}
}
}